From 22656b33e3af4c1d642b64983fcdecd2e3065089 Mon Sep 17 00:00:00 2001 From: zhenya Date: Sun, 15 Feb 2009 22:47:47 +0300 Subject: [PATCH] [7283] Use map for trainer spells for fast find data by spell id. Signed-off-by: VladimirMangos --- src/game/Creature.cpp | 13 +++-------- src/game/Creature.h | 13 +++++++---- src/game/NPCHandler.cpp | 4 ++-- src/game/ObjectMgr.cpp | 48 +++++++++++++++++++--------------------- src/shared/revision_nr.h | 2 +- 5 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp index cee13c874..d3deca6bd 100644 --- a/src/game/Creature.cpp +++ b/src/game/Creature.cpp @@ -46,18 +46,11 @@ // apply implementation of the singletons #include "Policies/SingletonImp.h" -void TrainerSpellData::Clear() -{ - for (TrainerSpellList::iterator itr = spellList.begin(); itr != spellList.end(); ++itr) - delete (*itr); - spellList.clear(); -} - TrainerSpell const* TrainerSpellData::Find(uint32 spell_id) const { - for(TrainerSpellList::const_iterator itr = spellList.begin(); itr != spellList.end(); ++itr) - if((*itr)->spell == spell_id) - return *itr; + TrainerSpellMap::const_iterator itr = spellList.find(spell_id); + if (itr != spellList.end()) + return &itr->second; return NULL; } diff --git a/src/game/Creature.h b/src/game/Creature.h index 2faef2381..01e670ce9 100644 --- a/src/game/Creature.h +++ b/src/game/Creature.h @@ -358,6 +358,12 @@ typedef std::list VendorItemCounts; struct TrainerSpell { + TrainerSpell() : spell(0), spellCost(0), reqSkill(0), reqSkillValue(0), reqLevel(0), learnedSpell(0) {} + + TrainerSpell(uint32 _spell, uint32 _spellCost, uint32 _reqSkill, uint32 _reqSkillValue, uint32 _reqLevel, uint32 _learnedspell) + : spell(_spell), spellCost(_spellCost), reqSkill(_reqSkill), reqSkillValue(_reqSkillValue), reqLevel(_reqLevel), learnedSpell(_learnedspell) + {} + uint32 spell; uint32 spellCost; uint32 reqSkill; @@ -369,18 +375,17 @@ struct TrainerSpell bool IsCastable() const { return learnedSpell != spell; } }; -typedef std::vector TrainerSpellList; +typedef UNORDERED_MAP TrainerSpellMap; struct TrainerSpellData { TrainerSpellData() : trainerType(0) {} - TrainerSpellList spellList; + TrainerSpellMap spellList; uint32 trainerType; // trainer type based at trainer spells, can be different from creature_template value. // req. for correct show non-prof. trainers like weaponmaster, allowed values 0 and 2. - - void Clear(); TrainerSpell const* Find(uint32 spell_id) const; + void Clear() { spellList.clear(); } }; typedef std::list GossipOptionList; diff --git a/src/game/NPCHandler.cpp b/src/game/NPCHandler.cpp index ccc922e9b..d762b9a77 100644 --- a/src/game/NPCHandler.cpp +++ b/src/game/NPCHandler.cpp @@ -160,9 +160,9 @@ void WorldSession::SendTrainerList( uint64 guid, const std::string& strTitle ) float fDiscountMod = _player->GetReputationPriceDiscount(unit); uint32 count = 0; - for(TrainerSpellList::const_iterator itr = trainer_spells->spellList.begin(); itr != trainer_spells->spellList.end(); ++itr) + for(TrainerSpellMap::const_iterator itr = trainer_spells->spellList.begin(); itr != trainer_spells->spellList.end(); ++itr) { - TrainerSpell const* tSpell = *itr; + TrainerSpell const* tSpell = &itr->second; if(!_player->IsSpellFitByClassAndRace(tSpell->learnedSpell)) continue; diff --git a/src/game/ObjectMgr.cpp b/src/game/ObjectMgr.cpp index ef01b772f..f4caa8f35 100644 --- a/src/game/ObjectMgr.cpp +++ b/src/game/ObjectMgr.cpp @@ -6870,36 +6870,34 @@ void ObjectMgr::LoadTrainerSpell() continue; } - TrainerSpell* pTrainerSpell = new TrainerSpell(); - pTrainerSpell->spell = spell; - pTrainerSpell->spellCost = fields[2].GetUInt32(); - pTrainerSpell->reqSkill = fields[3].GetUInt32(); - pTrainerSpell->reqSkillValue = fields[4].GetUInt32(); - pTrainerSpell->reqLevel = fields[5].GetUInt32(); - - if(!pTrainerSpell->reqLevel) - pTrainerSpell->reqLevel = spellinfo->spellLevel; - - // calculate learned spell for profession case when stored cast-spell - pTrainerSpell->learnedSpell = spell; - for(int i = 0; i <3; ++i) - { - if(spellinfo->Effect[i]!=SPELL_EFFECT_LEARN_SPELL) - continue; - - if(SpellMgr::IsProfessionOrRidingSpell(spellinfo->EffectTriggerSpell[i])) - { - pTrainerSpell->learnedSpell = spellinfo->EffectTriggerSpell[i]; - break; - } - } - TrainerSpellData& data = m_mCacheTrainerSpellMap[entry]; if(SpellMgr::IsProfessionSpell(spell)) data.trainerType = 2; - data.spellList.push_back(pTrainerSpell); + TrainerSpell& trainerSpell = data.spellList[spell]; + trainerSpell.spell = spell; + trainerSpell.spellCost = fields[2].GetUInt32(); + trainerSpell.reqSkill = fields[3].GetUInt32(); + trainerSpell.reqSkillValue = fields[4].GetUInt32(); + trainerSpell.reqLevel = fields[5].GetUInt32(); + + if(!trainerSpell.reqLevel) + trainerSpell.reqLevel = spellinfo->spellLevel; + + // calculate learned spell for profession case when stored cast-spell + trainerSpell.learnedSpell = spell; + for(int i = 0; i <3; ++i) + { + if(spellinfo->Effect[i] != SPELL_EFFECT_LEARN_SPELL) + continue; + if(SpellMgr::IsProfessionOrRidingSpell(spellinfo->EffectTriggerSpell[i])) + { + trainerSpell.learnedSpell = spellinfo->EffectTriggerSpell[i]; + break; + } + } + ++count; } while (result->NextRow()); diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index ffc05b9d6..e87df561a 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "7282" + #define REVISION_NR "7283" #endif // __REVISION_NR_H__