diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-03-24 13:20:54 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-03-24 13:20:54 +0300 |
commit | f1f5595df167c69cc1c04157e477cd9dce290f66 (patch) | |
tree | 9ea15c536f1524606b1d3689949f99feb892686d /src | |
parent | 890349c3d1ad29ff668a3ef7fd7c535ed9a876ef (diff) | |
download | manaverse-f1f5595df167c69cc1c04157e477cd9dce290f66.tar.gz manaverse-f1f5595df167c69cc1c04157e477cd9dce290f66.tar.bz2 manaverse-f1f5595df167c69cc1c04157e477cd9dce290f66.tar.xz manaverse-f1f5595df167c69cc1c04157e477cd9dce290f66.zip |
improve spellmanager class.
Diffstat (limited to 'src')
-rw-r--r-- | src/spellmanager.cpp | 27 | ||||
-rw-r--r-- | src/spellmanager.h | 13 |
2 files changed, 22 insertions, 18 deletions
diff --git a/src/spellmanager.cpp b/src/spellmanager.cpp index 9c50ca65f..0059f6d5d 100644 --- a/src/spellmanager.cpp +++ b/src/spellmanager.cpp @@ -47,15 +47,18 @@ SpellManager::~SpellManager() mSpellsVector.clear(); } -TextCommand* SpellManager::getSpell(const int spellId) +TextCommand* SpellManager::getSpell(const int spellId) const { if (spellId < 0 || static_cast<unsigned int>(spellId) >= mSpells.size()) return nullptr; - return mSpells[spellId]; + std::map<unsigned int, TextCommand*>::const_iterator + it = mSpells.find(spellId); + + return it != mSpells.end() ? (*it).second : nullptr; } -TextCommand* SpellManager::getSpellByItem(const int itemId) +TextCommand* SpellManager::getSpellByItem(const int itemId) const { return getSpell(itemId - SPELL_MIN_ID); } @@ -116,12 +119,12 @@ std::vector<TextCommand*> SpellManager::getAll() const return mSpellsVector; } -void SpellManager::useItem(const int itemId) +void SpellManager::useItem(const int itemId) const { invoke(itemId - SPELL_MIN_ID); } -void SpellManager::invoke(const int spellId) +void SpellManager::invoke(const int spellId) const { if (!player_node) return; @@ -181,8 +184,8 @@ std::string SpellManager::parseCommand(std::string command, if (!player_node) return command; - std::string name(""); - std::string id(""); + std::string name; + std::string id; std::string name2; if (target) @@ -289,7 +292,7 @@ void SpellManager::load(const bool oldConfig) } } -void SpellManager::save() +void SpellManager::save() const { for (unsigned i = 0; i < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; i++) { @@ -350,7 +353,7 @@ void SpellManager::save() } } -std::string SpellManager::autoComplete(std::string partName) +std::string SpellManager::autoComplete(const std::string &partName) const { std::vector<TextCommand*>::const_iterator i = mSpellsVector.begin(); const std::vector<TextCommand*>::const_iterator @@ -363,12 +366,12 @@ std::string SpellManager::autoComplete(std::string partName) const TextCommand *const cmd = *i; const std::string line = cmd->getCommand(); - if (line != "") + if (!line.empty()) { const size_t pos = line.find(partName, 0); if (pos == 0) { - if (newName != "") + if (!newName.empty()) { newName = findSameSubstring(line, newName); newCommand = nullptr; @@ -382,7 +385,7 @@ std::string SpellManager::autoComplete(std::string partName) } ++i; } - if (newName != "" && newCommand + if (!newName.empty() && newCommand && newCommand->getTargetType() == NEEDTARGET) { return newName.append(" "); diff --git a/src/spellmanager.h b/src/spellmanager.h index c374aae19..1b25fd83d 100644 --- a/src/spellmanager.h +++ b/src/spellmanager.h @@ -42,9 +42,9 @@ class SpellManager final ~SpellManager(); - TextCommand *getSpell(const int spellId) A_WARN_UNUSED; + TextCommand *getSpell(const int spellId) const A_WARN_UNUSED; - TextCommand* getSpellByItem(const int itemId) A_WARN_UNUSED; + TextCommand* getSpellByItem(const int itemId) const A_WARN_UNUSED; bool addSpell(TextCommand *const spell); @@ -52,15 +52,16 @@ class SpellManager final std::vector<TextCommand*> getAll() const A_WARN_UNUSED; - void useItem(const int itemId); + void useItem(const int itemId) const; - void invoke(const int spellId); + void invoke(const int spellId) const; void load(const bool oldConfig = false); - void save(); + void save() const; - std::string autoComplete(std::string partName) A_WARN_UNUSED; + std::string autoComplete(const std::string &partName) + const A_WARN_UNUSED; private: void fillSpells(); |