diff options
author | Shauren <shauren.trinity@gmail.com> | 2020-09-05 18:00:00 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2022-02-05 13:07:25 +0100 |
commit | 04021a30baa6ec37d9c8908534b1f46c84fb1190 (patch) | |
tree | 15249ca08a32800e122964572ed1f66a939c037e | |
parent | ae6f8bc4f546b0db9b39acad03687d968a348779 (diff) |
Core/Players: Directly store PlayerSpell in m_spells, not as pointer
Closes #25402
(cherry picked from commit 1e446b021b704818fb4ef9bd8622716d138295b0)
-rw-r--r-- | src/server/game/Achievements/CriteriaHandler.cpp | 4 | ||||
-rw-r--r-- | src/server/game/Entities/Player/Player.cpp | 151 | ||||
-rw-r--r-- | src/server/game/Entities/Player/Player.h | 2 | ||||
-rw-r--r-- | src/server/game/Entities/Unit/Unit.cpp | 2 | ||||
-rw-r--r-- | src/server/game/Spells/Auras/SpellAuraEffects.cpp | 2 | ||||
-rw-r--r-- | src/server/game/Spells/SpellHistory.cpp | 2 |
6 files changed, 77 insertions, 86 deletions
diff --git a/src/server/game/Achievements/CriteriaHandler.cpp b/src/server/game/Achievements/CriteriaHandler.cpp index 3104fe53936..166a2c9270c 100644 --- a/src/server/game/Achievements/CriteriaHandler.cpp +++ b/src/server/game/Achievements/CriteriaHandler.cpp @@ -688,9 +688,9 @@ void CriteriaHandler::UpdateCriteria(CriteriaType type, uint64 miscValue1 /*= 0* case CriteriaType::LearnTradeskillSkillLine: { uint32 spellCount = 0; - for (std::pair<uint32 const, PlayerSpell*>& spellIter : referencePlayer->GetSpellMap()) + for (auto& [spellId, _] : referencePlayer->GetSpellMap()) { - SkillLineAbilityMapBounds bounds = sSpellMgr->GetSkillLineAbilityMapBounds(spellIter.first); + SkillLineAbilityMapBounds bounds = sSpellMgr->GetSkillLineAbilityMapBounds(spellId); for (SkillLineAbilityMap::const_iterator skillIter = bounds.first; skillIter != bounds.second; ++skillIter) { if (skillIter->second->SkillLine == int32(criteria->Entry->Asset.SkillID)) diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index 3324a836751..fcf87e12376 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -361,9 +361,6 @@ Player::~Player() for (uint8 i = 0; i < PLAYER_SLOTS_COUNT; ++i) delete m_items[i]; - for (PlayerSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr) - delete itr->second; - //all mailed items should be deleted, also all mail should be deallocated for (PlayerMails::iterator itr = m_mail.begin(); itr != m_mail.end(); ++itr) delete *itr; @@ -2620,10 +2617,10 @@ void Player::SendKnownSpells() knownSpells.KnownSpells.reserve(m_spells.size()); for (PlayerSpellMap::value_type const& spell : m_spells) { - if (spell.second->state == PLAYERSPELL_REMOVED) + if (spell.second.state == PLAYERSPELL_REMOVED) continue; - if (!spell.second->active || spell.second->disabled) + if (!spell.second.active || spell.second.disabled) continue; knownSpells.KnownSpells.push_back(spell.first); @@ -2833,7 +2830,7 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent PlayerSpellMap::iterator itr = m_spells.find(spellId); // Remove temporary spell if found to prevent conflicts - if (itr != m_spells.end() && itr->second->state == PLAYERSPELL_TEMPORARY) + if (itr != m_spells.end() && itr->second.state == PLAYERSPELL_TEMPORARY) RemoveTemporarySpell(spellId); else if (itr != m_spells.end()) { @@ -2853,33 +2850,33 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent } // not do anything if already known in expected state - if (itr->second->state != PLAYERSPELL_REMOVED && itr->second->active == active && - itr->second->dependent == dependent && itr->second->disabled == disabled) + if (itr->second.state != PLAYERSPELL_REMOVED && itr->second.active == active && + itr->second.dependent == dependent && itr->second.disabled == disabled) { if (!IsInWorld() && !learning) // explicitly load from DB and then exist in it already and set correctly - itr->second->state = PLAYERSPELL_UNCHANGED; + itr->second.state = PLAYERSPELL_UNCHANGED; return false; } // dependent spell known as not dependent, overwrite state - if (itr->second->state != PLAYERSPELL_REMOVED && !itr->second->dependent && dependent) + if (itr->second.state != PLAYERSPELL_REMOVED && !itr->second.dependent && dependent) { - itr->second->dependent = dependent; - if (itr->second->state != PLAYERSPELL_NEW) - itr->second->state = PLAYERSPELL_CHANGED; + itr->second.dependent = dependent; + if (itr->second.state != PLAYERSPELL_NEW) + itr->second.state = PLAYERSPELL_CHANGED; dependent_set = true; } // update active state for known spell - if (itr->second->active != active && itr->second->state != PLAYERSPELL_REMOVED && !itr->second->disabled) + if (itr->second.active != active && itr->second.state != PLAYERSPELL_REMOVED && !itr->second.disabled) { - itr->second->active = active; + itr->second.active = active; if (!IsInWorld() && !learning && !dependent_set) // explicitly load from DB and then exist in it already and set correctly - itr->second->state = PLAYERSPELL_UNCHANGED; - else if (itr->second->state != PLAYERSPELL_NEW) - itr->second->state = PLAYERSPELL_CHANGED; + itr->second.state = PLAYERSPELL_UNCHANGED; + else if (itr->second.state != PLAYERSPELL_NEW) + itr->second.state = PLAYERSPELL_CHANGED; if (active) { @@ -2901,24 +2898,23 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent return active; // learn (show in spell book if active now) } - if (itr->second->disabled != disabled && itr->second->state != PLAYERSPELL_REMOVED) + if (itr->second.disabled != disabled && itr->second.state != PLAYERSPELL_REMOVED) { - if (itr->second->state != PLAYERSPELL_NEW) - itr->second->state = PLAYERSPELL_CHANGED; - itr->second->disabled = disabled; + if (itr->second.state != PLAYERSPELL_NEW) + itr->second.state = PLAYERSPELL_CHANGED; + itr->second.disabled = disabled; if (disabled) return false; disabled_case = true; } - else switch (itr->second->state) + else switch (itr->second.state) { case PLAYERSPELL_UNCHANGED: // known saved spell return false; case PLAYERSPELL_REMOVED: // re-learning removed not saved spell { - delete itr->second; m_spells.erase(itr); state = PLAYERSPELL_CHANGED; break; // need re-add @@ -2927,7 +2923,7 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent { // can be in case spell loading but learned at some previous spell loading if (!IsInWorld() && !learning && !dependent_set) - itr->second->state = PLAYERSPELL_UNCHANGED; + itr->second.state = PLAYERSPELL_UNCHANGED; return false; } @@ -2945,18 +2941,21 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent LearnSpell(prev_spell, true, fromSkill); } - PlayerSpell* newspell = new PlayerSpell; - newspell->state = state; - newspell->active = active; - newspell->dependent = dependent; - newspell->disabled = disabled; + std::pair<PlayerSpellMap::iterator, bool> inserted = m_spells.emplace(std::piecewise_construct, std::forward_as_tuple(spellId), std::forward_as_tuple()); + PlayerSpell& newspell = inserted.first->second; + // learning a previous rank might have given us this spell already from a skill autolearn, most likely with PLAYERSPELL_NEW state + // we dont want to do double insert if this happened during load from db so we force state to CHANGED, just in case + newspell.state = inserted.second ? state : PLAYERSPELL_CHANGED; + newspell.active = active; + newspell.dependent = dependent; + newspell.disabled = disabled; // replace spells in action bars and spellbook to bigger rank if only one spell rank must be accessible - if (newspell->active && !newspell->disabled && spellInfo->IsRanked()) + if (newspell.active && !newspell.disabled && spellInfo->IsRanked()) { for (PlayerSpellMap::iterator itr2 = m_spells.begin(); itr2 != m_spells.end(); ++itr2) { - if (itr2->second->state == PLAYERSPELL_REMOVED) + if (itr2->second.state == PLAYERSPELL_REMOVED) continue; SpellInfo const* i_spellInfo = sSpellMgr->GetSpellInfo(itr2->first, DIFFICULTY_NONE); @@ -2965,7 +2964,7 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent if (spellInfo->IsDifferentRankOf(i_spellInfo)) { - if (itr2->second->active) + if (itr2->second.active) { if (spellInfo->IsHighRankOf(i_spellInfo)) { @@ -2973,9 +2972,9 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent SendSupercededSpell(itr2->first, spellId); // mark old spell as disable (SMSG_SUPERCEDED_SPELL replace it in client by new) - itr2->second->active = false; - if (itr2->second->state != PLAYERSPELL_NEW) - itr2->second->state = PLAYERSPELL_CHANGED; + itr2->second.active = false; + if (itr2->second.state != PLAYERSPELL_NEW) + itr2->second.state = PLAYERSPELL_CHANGED; superceded_old = true; // new spell replace old in action bars and spell book. } else @@ -2984,19 +2983,17 @@ bool Player::AddSpell(uint32 spellId, bool active, bool learning, bool dependent SendSupercededSpell(spellId, itr2->first); // mark new spell as disable (not learned yet for client and will not learned) - newspell->active = false; - if (newspell->state != PLAYERSPELL_NEW) - newspell->state = PLAYERSPELL_CHANGED; + newspell.active = false; + if (newspell.state != PLAYERSPELL_NEW) + newspell.state = PLAYERSPELL_CHANGED; } } } } } - m_spells[spellId] = newspell; - // return false if spell disabled - if (newspell->disabled) + if (newspell.disabled) return false; } @@ -3111,12 +3108,11 @@ void Player::AddTemporarySpell(uint32 spellId) // spell already added - do not do anything if (itr != m_spells.end()) return; - PlayerSpell* newspell = new PlayerSpell; + PlayerSpell* newspell = &m_spells[spellId]; newspell->state = PLAYERSPELL_TEMPORARY; newspell->active = true; newspell->dependent = false; newspell->disabled = false; - m_spells[spellId] = newspell; } void Player::RemoveTemporarySpell(uint32 spellId) @@ -3126,9 +3122,8 @@ void Player::RemoveTemporarySpell(uint32 spellId) if (itr == m_spells.end()) return; // spell has other state than temporary - do not change it - if (itr->second->state != PLAYERSPELL_TEMPORARY) + if (itr->second.state != PLAYERSPELL_TEMPORARY) return; - delete itr->second; m_spells.erase(itr); } @@ -3163,8 +3158,8 @@ void Player::LearnSpell(uint32 spell_id, bool dependent, int32 fromSkill /*= 0*/ { PlayerSpellMap::iterator itr = m_spells.find(spell_id); - bool disabled = (itr != m_spells.end()) ? itr->second->disabled : false; - bool active = disabled ? itr->second->active : true; + bool disabled = (itr != m_spells.end()) ? itr->second.disabled : false; + bool active = disabled ? itr->second.active : true; bool learning = AddSpell(spell_id, active, true, dependent, false, false, fromSkill); @@ -3183,7 +3178,7 @@ void Player::LearnSpell(uint32 spell_id, bool dependent, int32 fromSkill /*= 0*/ if (uint32 nextSpell = sSpellMgr->GetNextSpellInChain(spell_id)) { PlayerSpellMap::iterator iter = m_spells.find(nextSpell); - if (iter != m_spells.end() && iter->second->disabled) + if (iter != m_spells.end() && iter->second.disabled) LearnSpell(nextSpell, false, fromSkill); } @@ -3191,7 +3186,7 @@ void Player::LearnSpell(uint32 spell_id, bool dependent, int32 fromSkill /*= 0*/ for (SpellsRequiringSpellMap::const_iterator itr2 = spellsRequiringSpell.first; itr2 != spellsRequiringSpell.second; ++itr2) { PlayerSpellMap::iterator iter2 = m_spells.find(itr2->second); - if (iter2 != m_spells.end() && iter2->second->disabled) + if (iter2 != m_spells.end() && iter2->second.disabled) LearnSpell(itr2->second, false, fromSkill); } } @@ -3205,7 +3200,7 @@ void Player::RemoveSpell(uint32 spell_id, bool disabled /*= false*/, bool learn_ if (itr == m_spells.end()) return; - if (itr->second->state == PLAYERSPELL_REMOVED || (disabled && itr->second->disabled) || itr->second->state == PLAYERSPELL_TEMPORARY) + if (itr->second.state == PLAYERSPELL_REMOVED || (disabled && itr->second.disabled) || itr->second.state == PLAYERSPELL_TEMPORARY) return; // unlearn non talent higher ranks (recursive) @@ -3225,24 +3220,21 @@ void Player::RemoveSpell(uint32 spell_id, bool disabled /*= false*/, bool learn_ if (itr == m_spells.end()) return; // already unleared - bool cur_active = itr->second->active; - bool cur_dependent = itr->second->dependent; + bool cur_active = itr->second.active; + bool cur_dependent = itr->second.dependent; if (disabled) { - itr->second->disabled = disabled; - if (itr->second->state != PLAYERSPELL_NEW) - itr->second->state = PLAYERSPELL_CHANGED; + itr->second.disabled = disabled; + if (itr->second.state != PLAYERSPELL_NEW) + itr->second.state = PLAYERSPELL_CHANGED; } else { - if (itr->second->state == PLAYERSPELL_NEW) - { - delete itr->second; + if (itr->second.state == PLAYERSPELL_NEW) m_spells.erase(itr); - } else - itr->second->state = PLAYERSPELL_REMOVED; + itr->second.state = PLAYERSPELL_REMOVED; } RemoveOwnedAura(spell_id, GetGUID()); @@ -3321,17 +3313,17 @@ void Player::RemoveSpell(uint32 spell_id, bool disabled /*= false*/, bool learn_ PlayerSpellMap::iterator prev_itr = m_spells.find(prev_id); if (prev_itr != m_spells.end()) { - if (prev_itr->second->dependent != cur_dependent) + if (prev_itr->second.dependent != cur_dependent) { - prev_itr->second->dependent = cur_dependent; - if (prev_itr->second->state != PLAYERSPELL_NEW) - prev_itr->second->state = PLAYERSPELL_CHANGED; + prev_itr->second.dependent = cur_dependent; + if (prev_itr->second.state != PLAYERSPELL_NEW) + prev_itr->second.state = PLAYERSPELL_CHANGED; } // now re-learn if need re-activate - if (!prev_itr->second->active && learn_low_rank) + if (!prev_itr->second.active && learn_low_rank) { - if (AddSpell(prev_id, true, false, prev_itr->second->dependent, prev_itr->second->disabled)) + if (AddSpell(prev_id, true, false, prev_itr->second.dependent, prev_itr->second.disabled)) { // downgrade spell ranks in spellbook and action bar SendSupercededSpell(spell_id, prev_id); @@ -3706,8 +3698,8 @@ void Player::ClearUpdateMask(bool remove) bool Player::HasSpell(uint32 spell) const { PlayerSpellMap::const_iterator itr = m_spells.find(spell); - return (itr != m_spells.end() && itr->second->state != PLAYERSPELL_REMOVED && - !itr->second->disabled); + return (itr != m_spells.end() && itr->second.state != PLAYERSPELL_REMOVED && + !itr->second.disabled); } bool Player::HasTalent(uint32 talentId, uint8 group) const @@ -3719,8 +3711,8 @@ bool Player::HasTalent(uint32 talentId, uint8 group) const bool Player::HasActiveSpell(uint32 spell) const { PlayerSpellMap::const_iterator itr = m_spells.find(spell); - return (itr != m_spells.end() && itr->second->state != PLAYERSPELL_REMOVED && - itr->second->active && !itr->second->disabled); + return (itr != m_spells.end() && itr->second.state != PLAYERSPELL_REMOVED && + itr->second.active && !itr->second.disabled); } /** @@ -7946,7 +7938,7 @@ void Player::ApplyItemDependentAuras(Item* item, bool apply) PlayerSpellMap const& spells = GetSpellMap(); for (auto itr = spells.begin(); itr != spells.end(); ++itr) { - if (itr->second->state == PLAYERSPELL_REMOVED || itr->second->disabled) + if (itr->second.state == PLAYERSPELL_REMOVED || itr->second.disabled) continue; SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(itr->first, DIFFICULTY_NONE); @@ -21447,7 +21439,7 @@ void Player::_SaveSpells(CharacterDatabaseTransaction& trans) for (PlayerSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end();) { - if (itr->second->state == PLAYERSPELL_REMOVED || itr->second->state == PLAYERSPELL_CHANGED) + if (itr->second.state == PLAYERSPELL_REMOVED || itr->second.state == PLAYERSPELL_CHANGED) { stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHAR_SPELL_BY_SPELL); stmt->setUInt32(0, itr->first); @@ -21456,25 +21448,24 @@ void Player::_SaveSpells(CharacterDatabaseTransaction& trans) } // add only changed/new not dependent spells - if (!itr->second->dependent && (itr->second->state == PLAYERSPELL_NEW || itr->second->state == PLAYERSPELL_CHANGED)) + if (!itr->second.dependent && (itr->second.state == PLAYERSPELL_NEW || itr->second.state == PLAYERSPELL_CHANGED)) { stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHAR_SPELL); stmt->setUInt64(0, GetGUID().GetCounter()); stmt->setUInt32(1, itr->first); - stmt->setBool(2, itr->second->active); - stmt->setBool(3, itr->second->disabled); + stmt->setBool(2, itr->second.active); + stmt->setBool(3, itr->second.disabled); trans->Append(stmt); } - if (itr->second->state == PLAYERSPELL_REMOVED) + if (itr->second.state == PLAYERSPELL_REMOVED) { - delete itr->second; itr = m_spells.erase(itr); continue; } - if (itr->second->state != PLAYERSPELL_TEMPORARY) - itr->second->state = PLAYERSPELL_UNCHANGED; + if (itr->second.state != PLAYERSPELL_TEMPORARY) + itr->second.state = PLAYERSPELL_UNCHANGED; ++itr; } diff --git a/src/server/game/Entities/Player/Player.h b/src/server/game/Entities/Player/Player.h index 1a69fb98b8d..94f196d9db9 100644 --- a/src/server/game/Entities/Player/Player.h +++ b/src/server/game/Entities/Player/Player.h @@ -299,7 +299,7 @@ struct PlayerCurrency typedef std::unordered_map<uint32, PlayerSpellState> PlayerTalentMap; typedef std::array<uint32, MAX_PVP_TALENT_SLOTS> PlayerPvpTalentMap; -typedef std::unordered_map<uint32, PlayerSpell*> PlayerSpellMap; +typedef std::unordered_map<uint32, PlayerSpell> PlayerSpellMap; typedef std::unordered_set<SpellModifier*> SpellModContainer; typedef std::unordered_map<uint32, PlayerCurrency> PlayerCurrenciesMap; diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 8e490b828c8..16f616e5379 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -5646,7 +5646,7 @@ void Unit::ModifyAuraState(AuraStateType flag, bool apply) PlayerSpellMap const& sp_list = ToPlayer()->GetSpellMap(); for (PlayerSpellMap::const_iterator itr = sp_list.begin(); itr != sp_list.end(); ++itr) { - if (itr->second->state == PLAYERSPELL_REMOVED || itr->second->disabled) + if (itr->second.state == PLAYERSPELL_REMOVED || itr->second.disabled) continue; SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(itr->first, DIFFICULTY_NONE); if (!spellInfo || !spellInfo->IsPassive()) diff --git a/src/server/game/Spells/Auras/SpellAuraEffects.cpp b/src/server/game/Spells/Auras/SpellAuraEffects.cpp index b0e88f9a51b..90682731ef0 100644 --- a/src/server/game/Spells/Auras/SpellAuraEffects.cpp +++ b/src/server/game/Spells/Auras/SpellAuraEffects.cpp @@ -1324,7 +1324,7 @@ void AuraEffect::HandleShapeshiftBoosts(Unit* target, bool apply) const PlayerSpellMap const& sp_list = plrTarget->GetSpellMap(); for (auto itr = sp_list.begin(); itr != sp_list.end(); ++itr) { - if (itr->second->state == PLAYERSPELL_REMOVED || itr->second->disabled) + if (itr->second.state == PLAYERSPELL_REMOVED || itr->second.disabled) continue; if (itr->first == spellId || itr->first == spellId2 || itr->first == spellId3 || itr->first == spellId4) diff --git a/src/server/game/Spells/SpellHistory.cpp b/src/server/game/Spells/SpellHistory.cpp index cdb088a2903..5070f932acc 100644 --- a/src/server/game/Spells/SpellHistory.cpp +++ b/src/server/game/Spells/SpellHistory.cpp @@ -738,7 +738,7 @@ void SpellHistory::LockSpellSchool(SpellSchoolMask schoolMask, Duration lockoutT if (Player* plrOwner = _owner->ToPlayer()) { for (auto const& p : plrOwner->GetSpellMap()) - if (p.second->state != PLAYERSPELL_REMOVED) + if (p.second.state != PLAYERSPELL_REMOVED) knownSpells.insert(p.first); } else if (Pet* petOwner = _owner->ToPet()) |