From d33fa74d7527b55025243cc28e6544eb15f04443 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 4 Nov 2023 20:01:31 +0800 Subject: [PATCH] 1 --- server/gameserver/ability.cc | 139 ++++++++++++----------------------- server/gameserver/types.h | 10 +-- 2 files changed, 53 insertions(+), 96 deletions(-) diff --git a/server/gameserver/ability.cc b/server/gameserver/ability.cc index ade7ee6e..23662ffd 100644 --- a/server/gameserver/ability.cc +++ b/server/gameserver/ability.cc @@ -7,115 +7,80 @@ #include "mt/Equip.h" #include "mt/Buff.h" -struct AttrAbsPtr -{ - struct AttrAbs* data; - AttrAbsPtr(AttrAbs* data) { this->data = data; }; -}; - struct AttrAbs { list_head entry; int attr_id; float value; - std::shared_ptr ptr; + std::shared_ptr holder; AttrAbs(int attr_id, float value) { this->attr_id = attr_id; this->value = value; INIT_LIST_HEAD(&entry); - ptr = std::make_shared(this); } }; -struct AttrRatePtr -{ - struct AttrRate* data; - AttrRatePtr(AttrRate* data) { this->data = data; }; -}; - struct AttrRate { list_head entry; int attr_id; float value; - std::shared_ptr ptr; + std::shared_ptr holder; AttrRate(int attr_id, float value) { this->attr_id = attr_id; this->value = value; INIT_LIST_HEAD(&entry); - ptr = std::make_shared(this); } }; -struct AttrAdditionPtr -{ - struct AttrAddition* data; - AttrAdditionPtr(AttrAddition* data) { this->data = data; }; -}; - struct AttrAddition { list_head entry; int attr_id; float value; - std::shared_ptr ptr; + std::shared_ptr holder; AttrAddition(int attr_id, float value) { this->attr_id = attr_id; this->value = value; INIT_LIST_HEAD(&entry); - ptr = std::make_shared(this); } }; -struct AttrRuducePtr -{ - struct AttrRuduce* data; - AttrRuducePtr(AttrRuduce* data) { this->data = data; }; -}; - struct AttrRuduce { list_head entry; int attr_id; float value; - std::shared_ptr ptr; + std::shared_ptr holder; AttrRuduce(int attr_id, float value) { this->attr_id = attr_id; this->value = value; INIT_LIST_HEAD(&entry); - ptr = std::make_shared(this); } }; -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; + std::shared_ptr holder; AttrDirect(int attr_id, float value) { this->attr_id = attr_id; this->value = value; INIT_LIST_HEAD(&entry); - ptr = std::make_shared(this); } }; @@ -164,9 +129,8 @@ void Ability::Clear() AttrAbs* e = list_first_entry(&std::get<1>(tuple), AttrAbs, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : attr_rate_) { @@ -175,9 +139,8 @@ void Ability::Clear() AttrRate* e = list_first_entry(&std::get<1>(tuple), AttrRate, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } @@ -187,9 +150,8 @@ void Ability::Clear() AttrAddition* e = list_first_entry(&std::get<1>(tuple), AttrAddition, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : attr_dec_) { @@ -198,9 +160,8 @@ void Ability::Clear() AttrRuduce* e = list_first_entry(&std::get<1>(tuple), AttrRuduce, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } @@ -210,9 +171,8 @@ void Ability::Clear() AttrAddition* e = list_first_entry(&std::get<1>(tuple), AttrAddition, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : vattr_dec_) { @@ -221,9 +181,8 @@ void Ability::Clear() AttrRuduce* e = list_first_entry(&std::get<1>(tuple), AttrRuduce, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : attr_direct_) { @@ -232,9 +191,8 @@ void Ability::Clear() AttrDirect* e = list_first_entry(&std::get<1>(tuple), AttrDirect, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } @@ -283,10 +241,11 @@ bool Ability::CanImmune(const std::set& tags) AttrAbsHandle Ability::AddAttrAbs(int attr_id, float value) { if (IsValidHumanAttr(attr_id)) { - auto p = new AttrAbs(attr_id, value); + auto p = std::make_shared(attr_id, value); + p->holder = p; list_add_tail(&p->entry, &std::get<1>(attr_abs_[attr_id])); RecalcAttrAbs(attr_id); - return p->ptr; + return p; } return AttrAbsHandle(); } @@ -295,19 +254,20 @@ void Ability::RemoveAttrAbs(AttrAbsHandle handle) { if (!handle.expired()) { auto p = handle.lock(); - list_del_init(&p->data->entry); - RecalcAttrAbs(p->data->attr_id); - delete p->data; + list_del_init(&p->entry); + RecalcAttrAbs(p->attr_id); + p->holder = nullptr; } } AttrRateHandle Ability::AddAttrRate(int attr_id, float value) { if (IsValidHumanAttr(attr_id)) { - auto p = new AttrRate(attr_id, value); + auto p = std::make_shared(attr_id, value); + p->holder = p; list_add_tail(&p->entry, &std::get<1>(attr_rate_[attr_id])); RecalcAttrRate(attr_id); - return p->ptr; + return p; } return AttrRateHandle(); } @@ -316,9 +276,9 @@ void Ability::RemoveAttrRate(AttrRateHandle handle) { if (!handle.expired()) { auto p = handle.lock(); - list_del_init(&p->data->entry); - RecalcAttrRate(p->data->attr_id); - delete p->data; + list_del_init(&p->entry); + RecalcAttrRate(p->attr_id); + p->holder = nullptr; } } @@ -374,14 +334,15 @@ AttrAdditionHandle Ability::AddAttrAddition(int attr_id, float rate) { if (IsValidHumanAttr(attr_id) || IsValidHumanVirtualAttr(attr_id)) { - auto p = new AttrAddition(attr_id, rate); + auto p = std::make_shared(attr_id, rate); + p->holder = p; if (IsValidHumanAttr(attr_id)) { list_add_tail(&p->entry, &std::get<1>(attr_add_[attr_id])); } else { list_add_tail(&p->entry, &std::get<1>(vattr_add_[attr_id - kHVAT_Begin])); } RecalcAttrAddition(attr_id); - return p->ptr; + return p; } return AttrAdditionHandle(); } @@ -390,9 +351,9 @@ 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; + list_del_init(&p->entry); + RecalcAttrAddition(p->attr_id); + p->holder = nullptr; } } @@ -400,14 +361,15 @@ AttrRuduceHandle Ability::AddAttrRuduce(int attr_id, float rate) { if (IsValidHumanAttr(attr_id) || IsValidHumanVirtualAttr(attr_id)) { - auto p = new AttrRuduce(attr_id, rate); + auto p = std::make_shared(attr_id, rate); + p->holder = p; if (IsValidHumanAttr(attr_id)) { list_add_tail(&p->entry, &std::get<1>(attr_dec_[attr_id])); } else { list_add_tail(&p->entry, &std::get<1>(vattr_dec_[attr_id - kHVAT_Begin])); } RecalcAttrRuduce(attr_id); - return p->ptr; + return p; } return AttrRuduceHandle(); } @@ -416,9 +378,9 @@ 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; + list_del_init(&p->entry); + RecalcAttrRuduce(p->attr_id); + p->holder = nullptr; } } @@ -571,9 +533,8 @@ void Ability::GMClearBaseAttr(int type) AttrAbs* e = list_first_entry(&std::get<1>(tuple), AttrAbs, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : attr_rate_) { @@ -582,9 +543,8 @@ void Ability::GMClearBaseAttr(int type) AttrRate* e = list_first_entry(&std::get<1>(tuple), AttrRate, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } } @@ -682,9 +642,8 @@ void Ability::GMClearGrowAttr(int type) AttrAddition* e = list_first_entry(&std::get<1>(tuple), AttrAddition, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : attr_dec_) { @@ -693,9 +652,8 @@ void Ability::GMClearGrowAttr(int type) AttrRuduce* e = list_first_entry(&std::get<1>(tuple), AttrRuduce, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } @@ -705,9 +663,8 @@ void Ability::GMClearGrowAttr(int type) AttrAddition* e = list_first_entry(&std::get<1>(tuple), AttrAddition, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } for (auto& tuple : vattr_dec_) { @@ -716,9 +673,8 @@ void Ability::GMClearGrowAttr(int type) AttrRuduce* e = list_first_entry(&std::get<1>(tuple), AttrRuduce, entry); - e->ptr->data = nullptr; list_del_init(&e->entry); - delete e; + e->holder = nullptr; } } } @@ -951,10 +907,11 @@ int Ability::GetSwitchTimes(int type) AttrDirectHandle Ability::AddAttrDirect(int attr_id, float value) { if (IsValidHumanAttr(attr_id)) { - auto p = new AttrDirect(attr_id, value); + auto p = std::make_shared(attr_id, value); + p->holder = p; list_add_tail(&p->entry, &std::get<1>(attr_direct_[attr_id])); RecalcAttrDirect(attr_id); - return p->ptr; + return p; } return AttrDirectHandle(); } @@ -963,9 +920,9 @@ 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; + list_del_init(&p->entry); + RecalcAttrDirect(p->attr_id); + p->holder = nullptr; } } diff --git a/server/gameserver/types.h b/server/gameserver/types.h index 134037dd..b98f2bd1 100644 --- a/server/gameserver/types.h +++ b/server/gameserver/types.h @@ -98,11 +98,11 @@ class IBullet virtual bool NoAdjustPos() = 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; +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; namespace a8 {