From be32a8af4f5d64b3b4707b5fe0c7a75c211c5858 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 26 Mar 2023 19:40:50 +0800 Subject: [PATCH] 1 --- server/gameserver/ability.cc | 40 ++++++++++++++++++++++++++++++++++++ server/gameserver/ability.h | 2 ++ 2 files changed, 42 insertions(+) diff --git a/server/gameserver/ability.cc b/server/gameserver/ability.cc index 0ca7cfef..24f0789b 100644 --- a/server/gameserver/ability.cc +++ b/server/gameserver/ability.cc @@ -421,6 +421,7 @@ AttrAdditionHandle Ability::AddAttr(int attr_id, float rate) if (IsValidHumanAttr(attr_id)) { auto p = new AttrAddition(attr_id, rate); list_add_tail(&p->entry, &std::get<1>(attr_add_[attr_id])); + RecalcAttrAddition(attr_id); return p->ptr; } return AttrAdditionHandle(); @@ -431,6 +432,7 @@ void Ability::RemoveAttrAddition(AttrAdditionHandle handle) if (!handle.expired()) { auto p = handle.lock(); list_del_init(&p->data->entry); + RecalcAttrAddition(p->data->attr_id); delete p->data; } } @@ -440,6 +442,7 @@ AttrRuduceHandle Ability::DecAttr(int attr_id, float rate) if (IsValidHumanAttr(attr_id)) { auto p = new AttrRuduce(attr_id, rate); list_add_tail(&p->entry, &std::get<1>(attr_dec_[attr_id])); + RecalcAttrRuduce(attr_id); return p->ptr; } return AttrRuduceHandle(); @@ -450,6 +453,43 @@ void Ability::RemoveAttrRuduce(AttrRuduceHandle handle) if (!handle.expired()) { auto p = handle.lock(); list_del_init(&p->data->entry); + RecalcAttrRuduce(p->data->attr_id); delete p->data; } } + +void Ability::RecalcAttrAddition(int attr_id) +{ + if (!IsValidHumanAttr(attr_id)) { + return; + } + list_head* pos = nullptr; + list_head* next = nullptr; + list_head* head = &std::get<1>(attr_add_[attr_id]); + float new_val = 0.0f; + list_for_each_safe(pos, next, head) { + AttrAddition* e = list_entry(pos, + AttrAddition, + entry); + new_val += e->value; + } + std::get<0>(attr_add_[attr_id]) = new_val; +} + +void Ability::RecalcAttrRuduce(int attr_id) +{ + if (!IsValidHumanAttr(attr_id)) { + return; + } + list_head* pos = nullptr; + list_head* next = nullptr; + list_head* head = &std::get<1>(attr_dec_[attr_id]); + float new_val = 0.0f; + list_for_each_safe(pos, next, head) { + AttrRuduce* e = list_entry(pos, + AttrRuduce, + entry); + new_val += e->value; + } + std::get<0>(attr_dec_[attr_id]) = new_val; +} diff --git a/server/gameserver/ability.h b/server/gameserver/ability.h index a10bbab3..6b7965e7 100644 --- a/server/gameserver/ability.h +++ b/server/gameserver/ability.h @@ -71,6 +71,8 @@ class Ability void RemoveAttrAddition(AttrAdditionHandle handler); AttrRuduceHandle DecAttr(int attr_id, float rate); void RemoveAttrRuduce(AttrRuduceHandle handler); + void RecalcAttrAddition(int attr_id); + void RecalcAttrRuduce(int attr_id); private: CreatureWeakPtr owner_;