From 1604f38bfdd73085215381d6445ecba2c0e28bcc Mon Sep 17 00:00:00 2001 From: Beats Date: Sun, 13 Aug 2023 01:56:52 -0300 Subject: [PATCH 1/2] Spell, RuneSpell, InstantSpell, Combat, BaseSpell, Action --- src/creatures/combat/combat.cpp | 8 +- src/creatures/combat/combat.h | 2 +- src/creatures/combat/spells.cpp | 62 ++++----- src/creatures/combat/spells.h | 122 ++++++++-------- src/creatures/monsters/monsters.cpp | 12 +- src/creatures/monsters/monsters.h | 4 +- src/creatures/players/wheel/player_wheel.cpp | 4 +- src/creatures/players/wheel/player_wheel.hpp | 2 +- src/items/item.cpp | 2 +- src/lua/creature/actions.cpp | 36 +++-- src/lua/creature/actions.h | 28 ++-- .../creatures/combat/spell_functions.cpp | 130 ++++++++++-------- .../creatures/combat/spell_functions.hpp | 2 +- .../monster/monster_type_functions.cpp | 6 +- .../creatures/player/player_functions.cpp | 10 +- src/lua/functions/events/action_functions.cpp | 20 +-- src/lua/functions/events/action_functions.hpp | 2 +- src/server/network/protocol/protocolgame.cpp | 2 +- 18 files changed, 228 insertions(+), 226 deletions(-) diff --git a/src/creatures/combat/combat.cpp b/src/creatures/combat/combat.cpp index be8f17fd828..97a10cd8b7f 100644 --- a/src/creatures/combat/combat.cpp +++ b/src/creatures/combat/combat.cpp @@ -19,7 +19,7 @@ #include "creatures/monsters/monsters.h" #include "items/weapons/weapons.h" -int32_t Combat::getLevelFormula(const Player* player, const Spell* wheelSpell, const CombatDamage &damage) const { +int32_t Combat::getLevelFormula(const Player* player, const std::shared_ptr& wheelSpell, const CombatDamage &damage) const { if (!player) { return 0; } @@ -46,7 +46,7 @@ CombatDamage Combat::getCombatDamage(Creature* creature, Creature* target) const damage.instantSpellName = instantSpellName; damage.runeSpellName = runeSpellName; // Wheel of destiny - const Spell* wheelSpell = nullptr; + std::shared_ptr wheelSpell = nullptr; Player* attackerPlayer = creature ? creature->getPlayer() : nullptr; if (attackerPlayer) { wheelSpell = attackerPlayer->wheel()->getCombatDataSpell(damage); @@ -1438,9 +1438,9 @@ uint32_t ValueCallback::getMagicLevelSkill(const Player* player, const CombatDam uint32_t magicLevelSkill = player->getMagicLevel(); // Wheel of destiny if (player && player->wheel()->getInstant("Runic Mastery") && damage.instantSpellName.empty()) { - const Spell* spell = g_spells().getRuneSpellByName(damage.runeSpellName); + const std::shared_ptr& spell = g_spells().getRuneSpellByName(damage.runeSpellName); // Rune conjuring spell have the same name as the rune item spell. - const InstantSpell* conjuringSpell = g_spells().getInstantSpellByName(damage.runeSpellName); + const std::shared_ptr& conjuringSpell = g_spells().getInstantSpellByName(damage.runeSpellName); if (spell && conjuringSpell && conjuringSpell != spell && normal_random(0, 100) <= 25) { uint32_t castResult = conjuringSpell->canCast(player) ? 20 : 10; magicLevelSkill += magicLevelSkill * castResult / 100; diff --git a/src/creatures/combat/combat.h b/src/creatures/combat/combat.h index 2b2ac2b9a8b..abc6b077ce2 100644 --- a/src/creatures/combat/combat.h +++ b/src/creatures/combat/combat.h @@ -366,7 +366,7 @@ class Combat { * @param damage The combat damage. * @return The calculated level formula. */ - int32_t getLevelFormula(const Player* player, const Spell* wheelSpell, const CombatDamage &damage) const; + int32_t getLevelFormula(const Player* player, const std::shared_ptr& wheelSpell, const CombatDamage &damage) const; CombatDamage getCombatDamage(Creature* creature, Creature* target) const; bool doCombatChain(Creature* caster, Creature* target, bool aggressive) const; diff --git a/src/creatures/combat/spells.cpp b/src/creatures/combat/spells.cpp index bc87a1056ab..83147ee5225 100644 --- a/src/creatures/combat/spells.cpp +++ b/src/creatures/combat/spells.cpp @@ -30,7 +30,7 @@ TalkActionResult_t Spells::playerSaySpell(Player* player, std::string &words) { // strip trailing spaces trimString(str_words); - InstantSpell* instantSpell = getInstantSpell(str_words); + const std::shared_ptr &instantSpell = getInstantSpell(str_words); if (!instantSpell) { return TALKACTION_CONTINUE; } @@ -90,8 +90,7 @@ bool Spells::hasInstantSpell(const std::string &word) const { return false; } -bool Spells::registerInstantLuaEvent(InstantSpell* event) { - InstantSpell_ptr instant { event }; +bool Spells::registerInstantLuaEvent(const std::shared_ptr& instant) { if (instant) { // If the spell not have the "spell:words()" return a error message const std::string &instantName = instant->getName(); @@ -109,23 +108,22 @@ bool Spells::registerInstantLuaEvent(InstantSpell* event) { return false; } // Register spell word in the map - setInstantSpell(words, *instant); + setInstantSpell(words, instant); } return false; } -bool Spells::registerRuneLuaEvent(RuneSpell* event) { - RuneSpell_ptr rune { event }; +bool Spells::registerRuneLuaEvent(const std::shared_ptr& rune) { if (rune) { uint16_t id = rune->getRuneItemId(); - auto result = runes.emplace(rune->getRuneItemId(), std::move(*rune)); + auto result = runes.emplace(rune->getRuneItemId(), rune); if (!result.second) { g_logger().warn( "[{}] duplicate registered rune with id: {}, for script: {}", __FUNCTION__, id, - event->getScriptInterface()->getLoadingScriptName() + rune->getScriptInterface()->getLoadingScriptName() ); } return result.second; @@ -140,57 +138,57 @@ std::list Spells::getSpellsByVocation(uint16_t vocationId) { phmap::btree_map::const_iterator vocSpellsIt; for (const auto &it : instants) { - vocSpells = it.second.getVocMap(); + vocSpells = it.second->getVocMap(); vocSpellsIt = vocSpells.find(vocationId); if (vocSpellsIt != vocSpells.end() && vocSpellsIt->second) { - spellsList.push_back(it.second.getId()); + spellsList.push_back(it.second->getId()); } } return spellsList; } -Spell* Spells::getSpellByName(const std::string &name) { - Spell* spell = getRuneSpellByName(name); +std::shared_ptr Spells::getSpellByName(const std::string &name) { + std::shared_ptr spell = getRuneSpellByName(name); if (!spell) { spell = getInstantSpellByName(name); } return spell; } -RuneSpell* Spells::getRuneSpell(uint16_t id) { +std::shared_ptr Spells::getRuneSpell(uint16_t id) { auto it = runes.find(id); if (it == runes.end()) { for (auto &rune : runes) { - if (rune.second.getId() == id) { - return &rune.second; + if (rune.second->getId() == id) { + return rune.second; } } return nullptr; } - return &it->second; + return it->second; } -RuneSpell* Spells::getRuneSpellByName(const std::string &name) { +std::shared_ptr Spells::getRuneSpellByName(const std::string &name) { for (auto &it : runes) { - if (strcasecmp(it.second.getName().c_str(), name.c_str()) == 0) { - return &it.second; + if (strcasecmp(it.second->getName().c_str(), name.c_str()) == 0) { + return it.second; } } return nullptr; } -InstantSpell* Spells::getInstantSpell(const std::string &words) { - InstantSpell* result = nullptr; +std::shared_ptr Spells::getInstantSpell(const std::string &words) { + std::shared_ptr result = nullptr; for (auto &it : instants) { - const std::string &instantSpellWords = it.second.getWords(); + const std::string &instantSpellWords = it.second->getWords(); size_t spellLen = instantSpellWords.length(); if (strncasecmp(instantSpellWords.c_str(), words.c_str(), spellLen) == 0) { if (!result || spellLen > result->getWords().length()) { - result = &it.second; + result = it.second; if (words.length() == spellLen) { break; } @@ -216,19 +214,19 @@ InstantSpell* Spells::getInstantSpell(const std::string &words) { return nullptr; } -InstantSpell* Spells::getInstantSpellById(uint16_t spellId) { +std::shared_ptr Spells::getInstantSpellById(uint16_t spellId) { for (auto &it : instants) { - if (it.second.getId() == spellId) { - return &it.second; + if (it.second->getId() == spellId) { + return it.second; } } return nullptr; } -InstantSpell* Spells::getInstantSpellByName(const std::string &name) { +std::shared_ptr Spells::getInstantSpellByName(const std::string &name) { for (auto &it : instants) { - if (strcasecmp(it.second.getName().c_str(), name.c_str()) == 0) { - return &it.second; + if (strcasecmp(it.second->getName().c_str(), name.c_str()) == 0) { + return it.second; } } return nullptr; @@ -238,7 +236,7 @@ Position Spells::getCasterPosition(Creature* creature, Direction dir) { return getNextPosition(dir, creature->getPosition()); } -CombatSpell::CombatSpell(Combat* newCombat, bool newNeedTarget, bool newNeedDirection) : +CombatSpell::CombatSpell(const std::shared_ptr &newCombat, bool newNeedTarget, bool newNeedDirection) : Script(&g_spells().getScriptInterface()), combat(newCombat), needDirection(newNeedDirection), @@ -247,7 +245,7 @@ CombatSpell::CombatSpell(Combat* newCombat, bool newNeedTarget, bool newNeedDire } bool CombatSpell::loadScriptCombat() { - combat = g_luaEnvironment().getCombatObject(g_luaEnvironment().lastCombatId).get(); + combat = g_luaEnvironment().getCombatObject(g_luaEnvironment().lastCombatId); return combat != nullptr; } @@ -454,7 +452,7 @@ bool Spell::playerSpellCheck(Player* player) const { return true; } -bool Spell::playerInstantSpellCheck(Player* player, const Position &toPos) { +bool Spell::playerInstantSpellCheck(Player* player, const Position &toPos) const { if (toPos.x == 0xFFFF) { return true; } diff --git a/src/creatures/combat/spells.h b/src/creatures/combat/spells.h index de9b872c3c1..be6a9bf17f0 100644 --- a/src/creatures/combat/spells.h +++ b/src/creatures/combat/spells.h @@ -22,8 +22,6 @@ class RuneSpell; class Spell; using VocSpellMap = phmap::btree_map; -using InstantSpell_ptr = std::unique_ptr; -using RuneSpell_ptr = std::unique_ptr; class Spells final : public Scripts { public: @@ -38,14 +36,14 @@ class Spells final : public Scripts { return inject(); } - Spell* getSpellByName(const std::string &name); - RuneSpell* getRuneSpell(uint16_t id); - RuneSpell* getRuneSpellByName(const std::string &name); + std::shared_ptr getSpellByName(const std::string &name); + std::shared_ptr getRuneSpell(uint16_t id); + std::shared_ptr getRuneSpellByName(const std::string &name); - InstantSpell* getInstantSpell(const std::string &words); - InstantSpell* getInstantSpellByName(const std::string &name); + std::shared_ptr getInstantSpell(const std::string &words); + std::shared_ptr getInstantSpellByName(const std::string &name); - InstantSpell* getInstantSpellById(uint16_t spellId); + std::shared_ptr getInstantSpellById(uint16_t spellId); TalkActionResult_t playerSaySpell(Player* player, std::string &words); @@ -53,30 +51,30 @@ class Spells final : public Scripts { std::list getSpellsByVocation(uint16_t vocationId); - const std::map &getInstantSpells() const { + [[nodiscard]] const std::map> &getInstantSpells() const { return instants; }; - bool hasInstantSpell(const std::string &word) const; + [[nodiscard]] bool hasInstantSpell(const std::string &word) const; - void setInstantSpell(const std::string &word, InstantSpell &instant) { + void setInstantSpell(const std::string &word, const std::shared_ptr &instant) { instants.try_emplace(word, instant); } void clear(); - bool registerInstantLuaEvent(InstantSpell* event); - bool registerRuneLuaEvent(RuneSpell* event); + bool registerInstantLuaEvent(const std::shared_ptr& instant); + bool registerRuneLuaEvent(const std::shared_ptr& rune); private: - std::map runes; - std::map instants; + std::map> runes; + std::map> instants; friend class CombatSpell; }; constexpr auto g_spells = Spells::getInstance; -using RuneSpellFunction = std::function; +using RuneSpellFunction = std::function &spell, Player* player, const Position &posTo)>; class BaseSpell { public: @@ -90,10 +88,10 @@ class BaseSpell { SoundEffect_t soundCastEffect = SoundEffect_t::SPELL_OR_RUNE; }; -class CombatSpell final : public Script, public BaseSpell { +class CombatSpell final : public Script, public BaseSpell, public std::enable_shared_from_this { public: // Constructor - CombatSpell(Combat* newCombat, bool newNeedTarget, bool newNeedDirection); + CombatSpell(const std::shared_ptr& newCombat, bool newNeedTarget, bool newNeedDirection); // The copy constructor and the assignment operator have been deleted to prevent accidental copying. CombatSpell(const CombatSpell &) = delete; @@ -106,7 +104,7 @@ class CombatSpell final : public Script, public BaseSpell { bool executeCastSpell(Creature* creature, const LuaVariant &var) const; bool loadScriptCombat(); - Combat* getCombat() { + std::shared_ptr getCombat() { return combat; } @@ -115,7 +113,7 @@ class CombatSpell final : public Script, public BaseSpell { return "onCastSpell"; } - Combat* combat; + std::shared_ptr combat; bool needDirection; bool needTarget; @@ -125,13 +123,13 @@ class Spell : public BaseSpell { public: Spell() = default; - const std::string &getName() const { + [[nodiscard]] const std::string &getName() const { return name; } void setName(std::string n) { - name = n; + name = std::move(n); } - uint16_t getId() const { + [[nodiscard]] uint16_t getId() const { return spellId; } void setId(uint16_t id) { @@ -140,56 +138,56 @@ class Spell : public BaseSpell { void postCastSpell(Player* player, bool finishedCast = true, bool payCost = true) const; static void postCastSpell(Player* player, uint32_t manaCost, uint32_t soulCost); - virtual bool isInstant() const = 0; - bool isLearnable() const { + [[nodiscard]] virtual bool isInstant() const = 0; + [[nodiscard]] bool isLearnable() const { return learnable; } uint32_t getManaCost(const Player* player) const; - uint32_t getSoulCost() const { + [[nodiscard]] uint32_t getSoulCost() const { return soul; } void setSoulCost(uint32_t s) { soul = s; } - uint32_t getLevel() const { + [[nodiscard]] uint32_t getLevel() const { return level; } void setLevel(uint32_t lvl) { level = lvl; } - uint32_t getMagicLevel() const { + [[nodiscard]] uint32_t getMagicLevel() const { return magLevel; } void setMagicLevel(uint32_t lvl) { magLevel = lvl; } - uint32_t getMana() const { + [[nodiscard]] uint32_t getMana() const { return mana; } void setMana(uint32_t m) { mana = m; } - uint32_t getManaPercent() const { + [[nodiscard]] uint32_t getManaPercent() const { return manaPercent; } void setManaPercent(uint32_t m) { manaPercent = m; } - bool isPremium() const { + [[nodiscard]] bool isPremium() const { return premium; } void setPremium(bool p) { premium = p; } - bool isEnabled() const { + [[nodiscard]] bool isEnabled() const { return enabled; } void setEnabled(bool e) { enabled = e; } - const VocSpellMap &getVocMap() const { + [[nodiscard]] const VocSpellMap &getVocMap() const { return vocSpellMap; } void addVocMap(uint16_t n, bool b) { @@ -209,81 +207,81 @@ class Spell : public BaseSpell { secondaryGroup = g; } - uint32_t getCooldown() const { + [[nodiscard]] uint32_t getCooldown() const { return cooldown; } void setCooldown(uint32_t cd) { cooldown = cd; } - uint32_t getSecondaryCooldown() const { + [[nodiscard]] uint32_t getSecondaryCooldown() const { return secondaryGroupCooldown; } void setSecondaryCooldown(uint32_t cd) { secondaryGroupCooldown = cd; } - uint32_t getGroupCooldown() const { + [[nodiscard]] uint32_t getGroupCooldown() const { return groupCooldown; } void setGroupCooldown(uint32_t cd) { groupCooldown = cd; } - int32_t getRange() const { + [[nodiscard]] int32_t getRange() const { return range; } void setRange(int32_t r) { range = r; } - bool getNeedTarget() const { + [[nodiscard]] bool getNeedTarget() const { return needTarget; } void setNeedTarget(bool n) { needTarget = n; } - bool getNeedWeapon() const { + [[nodiscard]] bool getNeedWeapon() const { return needWeapon; } void setNeedWeapon(bool n) { needWeapon = n; } - bool getNeedLearn() const { + [[nodiscard]] bool getNeedLearn() const { return learnable; } void setNeedLearn(bool n) { learnable = n; } - bool getSelfTarget() const { + [[nodiscard]] bool getSelfTarget() const { return selfTarget; } void setSelfTarget(bool s) { selfTarget = s; } - bool getBlockingSolid() const { + [[nodiscard]] bool getBlockingSolid() const { return blockingSolid; } void setBlockingSolid(bool b) { blockingSolid = b; } - bool getBlockingCreature() const { + [[nodiscard]] bool getBlockingCreature() const { return blockingCreature; } void setBlockingCreature(bool b) { blockingCreature = b; } - bool getAggressive() const { + [[nodiscard]] bool getAggressive() const { return aggressive; } void setAggressive(bool a) { aggressive = a; } - bool getAllowOnSelf() const { + [[nodiscard]] bool getAllowOnSelf() const { return allowOnSelf; } void setAllowOnSelf(bool s) { allowOnSelf = s; } - bool getLockedPZ() const { + [[nodiscard]] bool getLockedPZ() const { return pzLocked; } void setLockedPZ(bool b) { @@ -295,7 +293,7 @@ class Spell : public BaseSpell { * * @return True if the wheel of destiny is upgraded, false otherwise. */ - bool getWheelOfDestinyUpgraded() const; + [[nodiscard]] bool getWheelOfDestinyUpgraded() const; /** * @brief Get the boost value for the wheel of destiny. @@ -304,7 +302,7 @@ class Spell : public BaseSpell { * @param grade The grade of the wheel of destiny. * @return The boost value for the specified boost and grade. */ - int32_t getWheelOfDestinyBoost(WheelSpellBoost_t boost, WheelSpellGrade_t grade) const; + [[nodiscard]] int32_t getWheelOfDestinyBoost(WheelSpellBoost_t boost, WheelSpellGrade_t grade) const; /** * @brief Set whether the wheel of destiny is upgraded. @@ -324,7 +322,7 @@ class Spell : public BaseSpell { SpellType_t spellType = SPELL_UNDEFINED; - const std::string &getWords() const { + [[nodiscard]] const std::string &getWords() const { return m_words; } @@ -332,7 +330,7 @@ class Spell : public BaseSpell { m_words = newWord.data(); } - const std::string &getSeparator() const { + [[nodiscard]] const std::string &getSeparator() const { return m_separator; } @@ -343,7 +341,7 @@ class Spell : public BaseSpell { protected: void applyCooldownConditions(Player* player) const; bool playerSpellCheck(Player* player) const; - bool playerInstantSpellCheck(Player* player, const Position &toPos); + bool playerInstantSpellCheck(Player* player, const Position &toPos) const; bool playerRuneSpellCheck(Player* player, const Position &toPos); VocSpellMap vocSpellMap; @@ -399,34 +397,34 @@ class InstantSpell final : public Script, public Spell { // Scripting spell bool executeCastSpell(Creature* creature, const LuaVariant &var) const; - bool isInstant() const override { + [[nodiscard]] bool isInstant() const override { return true; } - bool getHasParam() const { + [[nodiscard]] bool getHasParam() const { return hasParam; } void setHasParam(bool p) { hasParam = p; } - bool getHasPlayerNameParam() const { + [[nodiscard]] bool getHasPlayerNameParam() const { return hasPlayerNameParam; } void setHasPlayerNameParam(bool p) { hasPlayerNameParam = p; } - bool getNeedDirection() const { + [[nodiscard]] bool getNeedDirection() const { return needDirection; } void setNeedDirection(bool n) { needDirection = n; } - bool getNeedCasterTargetOrDirection() const { + [[nodiscard]] bool getNeedCasterTargetOrDirection() const { return casterTargetOrDirection; } void setNeedCasterTargetOrDirection(bool d) { casterTargetOrDirection = d; } - bool getBlockWalls() const { + [[nodiscard]] bool getBlockWalls() const { return checkLineOfSight; } void setBlockWalls(bool w) { @@ -436,7 +434,7 @@ class InstantSpell final : public Script, public Spell { bool canThrowSpell(const Creature* creature, const Creature* target) const; private: - std::string getScriptTypeName() const override { + [[nodiscard]] std::string getScriptTypeName() const override { return "onCastSpell"; } @@ -467,16 +465,16 @@ class RuneSpell final : public Action, public Spell { // Scripting spell bool executeCastSpell(Creature* creature, const LuaVariant &var, bool isHotkey) const; - bool isInstant() const override { + [[nodiscard]] bool isInstant() const override { return false; } - uint16_t getRuneItemId() const { + [[nodiscard]] uint16_t getRuneItemId() const { return runeId; } void setRuneItemId(uint16_t i) { runeId = i; } - uint32_t getCharges() const { + [[nodiscard]] uint32_t getCharges() const { return charges; } void setCharges(uint32_t c) { @@ -487,7 +485,7 @@ class RuneSpell final : public Action, public Spell { } private: - std::string getScriptTypeName() const override { + [[nodiscard]] std::string getScriptTypeName() const override { return "onCastSpell"; } diff --git a/src/creatures/monsters/monsters.cpp b/src/creatures/monsters/monsters.cpp index fba3f66ed6f..5187b6dbdc5 100644 --- a/src/creatures/monsters/monsters.cpp +++ b/src/creatures/monsters/monsters.cpp @@ -16,12 +16,6 @@ #include "game/game.h" #include "items/weapons/weapons.h" -spellBlock_t::~spellBlock_t() { - if (combatSpell) { - delete spell; - } -} - void MonsterType::loadLoot(MonsterType* monsterType, LootBlock lootBlock) { if (lootBlock.childLoot.empty()) { bool isContainer = Item::items[lootBlock.id].isContainer(); @@ -80,9 +74,9 @@ bool Monsters::deserializeSpell(MonsterSpell* spell, spellBlock_t &sb, const std return true; } - CombatSpell* combatSpell = nullptr; + std::shared_ptr combatSpell = nullptr; - auto combatPtr = std::make_unique(); + auto combatPtr = std::make_shared(); sb.combatSpell = true; @@ -258,7 +252,7 @@ bool Monsters::deserializeSpell(MonsterSpell* spell, spellBlock_t &sb, const std } combatPtr->setPlayerCombatValues(COMBAT_FORMULA_DAMAGE, sb.minCombatValue, 0, sb.maxCombatValue, 0); - combatSpell = new CombatSpell(combatPtr.release(), spell->needTarget, spell->needDirection); + combatSpell = std::make_shared(combatPtr, spell->needTarget, spell->needDirection); // Sanity check if (!combatSpell) { return false; diff --git a/src/creatures/monsters/monsters.h b/src/creatures/monsters/monsters.h index e94a3662c25..ee20527bebb 100644 --- a/src/creatures/monsters/monsters.h +++ b/src/creatures/monsters/monsters.h @@ -28,7 +28,7 @@ class Loot { class BaseSpell; struct spellBlock_t { constexpr spellBlock_t() = default; - ~spellBlock_t(); + ~spellBlock_t() = default; spellBlock_t(const spellBlock_t &other) = delete; spellBlock_t &operator=(const spellBlock_t &other) = delete; spellBlock_t(spellBlock_t &&other) : @@ -43,7 +43,7 @@ struct spellBlock_t { other.spell = nullptr; } - BaseSpell* spell = nullptr; + std::shared_ptr spell = nullptr; uint32_t chance = 100; uint32_t speed = 2000; uint32_t range = 0; diff --git a/src/creatures/players/wheel/player_wheel.cpp b/src/creatures/players/wheel/player_wheel.cpp index 374f4a57af9..cc515a5e350 100644 --- a/src/creatures/players/wheel/player_wheel.cpp +++ b/src/creatures/players/wheel/player_wheel.cpp @@ -2075,8 +2075,8 @@ void PlayerWheel::downgradeSpell(const std::string &name) { } } -Spell* PlayerWheel::getCombatDataSpell(CombatDamage &damage) { - Spell* spell = nullptr; +std::shared_ptr PlayerWheel::getCombatDataSpell(CombatDamage &damage) { + std::shared_ptr spell = nullptr; damage.damageMultiplier += getMajorStatConditional("Divine Empowerment", WheelMajor_t::DAMAGE); WheelSpellGrade_t spellGrade = WheelSpellGrade_t::NONE; if (!(damage.instantSpellName).empty()) { diff --git a/src/creatures/players/wheel/player_wheel.hpp b/src/creatures/players/wheel/player_wheel.hpp index bce195d9677..88513f32d41 100644 --- a/src/creatures/players/wheel/player_wheel.hpp +++ b/src/creatures/players/wheel/player_wheel.hpp @@ -274,7 +274,7 @@ class PlayerWheel { void setPointsBySlotType(uint8_t slotType, uint16_t points); - Spell* getCombatDataSpell(CombatDamage &damage); + std::shared_ptr getCombatDataSpell(CombatDamage &damage); const PlayerWheelMethodsBonusData &getBonusData() const; diff --git a/src/items/item.cpp b/src/items/item.cpp index 8344c2d8fee..f29897ae0f2 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -2217,7 +2217,7 @@ std::string Item::getDescription(const ItemType &it, int32_t lookDistance, const if (it.isRune()) { if (it.runeLevel > 0 || it.runeMagLevel > 0) { - if (const RuneSpell* rune = g_spells().getRuneSpell(it.id)) { + if (const auto& rune = g_spells().getRuneSpell(it.id)) { int32_t tmpSubType = subType; if (item) { tmpSubType = item->getSubType(); diff --git a/src/lua/creature/actions.cpp b/src/lua/creature/actions.cpp index 7f010c0fb47..bc493c82ec4 100644 --- a/src/lua/creature/actions.cpp +++ b/src/lua/creature/actions.cpp @@ -26,7 +26,7 @@ void Actions::clear() { actionPositionMap.clear(); } -bool Actions::registerLuaItemEvent(Action* action) { +bool Actions::registerLuaItemEvent(const std::shared_ptr &action) { auto itemIdVector = action->getItemIdsVector(); if (itemIdVector.empty()) { return false; @@ -51,7 +51,7 @@ bool Actions::registerLuaItemEvent(Action* action) { } // Register item in the action item map - setItemId(itemId, std::move(*action)); + setItemId(itemId, action); tmpVector.emplace_back(itemId); } @@ -59,7 +59,7 @@ bool Actions::registerLuaItemEvent(Action* action) { return !itemIdVector.empty(); } -bool Actions::registerLuaUniqueEvent(Action* action) { +bool Actions::registerLuaUniqueEvent(const std::shared_ptr &action) { auto uniqueIdVector = action->getUniqueIdsVector(); if (uniqueIdVector.empty()) { return false; @@ -72,7 +72,7 @@ bool Actions::registerLuaUniqueEvent(Action* action) { // Check if the unique is already registered and prevent it from being registered again if (!hasUniqueId(uniqueId)) { // Register unique id the unique item map - setUniqueId(uniqueId, std::move(*action)); + setUniqueId(uniqueId, action); tmpVector.emplace_back(uniqueId); } else { g_logger().warn( @@ -90,7 +90,7 @@ bool Actions::registerLuaUniqueEvent(Action* action) { return !uniqueIdVector.empty(); } -bool Actions::registerLuaActionEvent(Action* action) { +bool Actions::registerLuaActionEvent(const std::shared_ptr &action) { auto actionIdVector = action->getActionIdsVector(); if (actionIdVector.empty()) { return false; @@ -103,7 +103,7 @@ bool Actions::registerLuaActionEvent(Action* action) { // Check if the unique is already registered and prevent it from being registered again if (!hasActionId(actionId)) { // Register action in the action item map - setActionId(actionId, std::move(*action)); + setActionId(actionId, action); tmpVector.emplace_back(actionId); } else { g_logger().warn( @@ -121,7 +121,7 @@ bool Actions::registerLuaActionEvent(Action* action) { return !actionIdVector.empty(); } -bool Actions::registerLuaPositionEvent(Action* action) { +bool Actions::registerLuaPositionEvent(const std::shared_ptr &action) { auto positionVector = action->getPositionsVector(); if (positionVector.empty()) { return false; @@ -134,7 +134,7 @@ bool Actions::registerLuaPositionEvent(Action* action) { // Check if the position is already registered and prevent it from being registered again if (!hasPosition(position)) { // Register position in the action position map - setPosition(position, std::move(*action)); + setPosition(position, action); tmpVector.emplace_back(position); } else { g_logger().warn( @@ -150,9 +150,7 @@ bool Actions::registerLuaPositionEvent(Action* action) { return !positionVector.empty(); } -bool Actions::registerLuaEvent(Action* action) { - Action_ptr actionPtr { action }; - +bool Actions::registerLuaEvent(const std::shared_ptr &action) { // Call all register lua events if (registerLuaItemEvent(action) || registerLuaUniqueEvent(action) || registerLuaActionEvent(action) || registerLuaPositionEvent(action)) { return true; @@ -183,7 +181,7 @@ ReturnValue Actions::canUse(const Player* player, const Position &pos) { } ReturnValue Actions::canUse(const Player* player, const Position &pos, const Item* item) { - Action* action = getAction(item); + const std::shared_ptr& action = getAction(item); if (action != nullptr) { return action->canExecuteAction(player, pos); } @@ -211,24 +209,24 @@ ReturnValue Actions::canUseFar(const Creature* creature, const Position &toPos, return RETURNVALUE_NOERROR; } -Action* Actions::getAction(const Item* item) { +std::shared_ptr Actions::getAction(const Item* item) { if (item->hasAttribute(ItemAttribute_t::UNIQUEID)) { auto it = uniqueItemMap.find(item->getAttribute(ItemAttribute_t::UNIQUEID)); if (it != uniqueItemMap.end()) { - return &it->second; + return it->second; } } if (item->hasAttribute(ItemAttribute_t::ACTIONID)) { auto it = actionItemMap.find(item->getAttribute(ItemAttribute_t::ACTIONID)); if (it != actionItemMap.end()) { - return &it->second; + return it->second; } } auto it = useItemMap.find(item->getID()); if (it != useItemMap.end()) { - return &it->second; + return it->second; } if (auto iteratePositions = actionPositionMap.find(item->getPosition()); @@ -241,7 +239,7 @@ Action* Actions::getAction(const Item* item) { return nullptr; } - return &iteratePositions->second; + return iteratePositions->second; } } @@ -259,7 +257,7 @@ ReturnValue Actions::internalUseItem(Player* player, const Position &pos, uint8_ auto itemId = item->getID(); const ItemType &itemType = Item::items[itemId]; auto transformTo = itemType.m_transformOnUse; - Action* action = getAction(item); + const std::shared_ptr &action = getAction(item); if (!action && transformTo > 0 && itemId != transformTo) { if (g_game().transformItem(item, transformTo) == nullptr) { g_logger().warn("[{}] item with id {} failed to transform to item {}", __FUNCTION__, itemId, transformTo); @@ -428,7 +426,7 @@ bool Actions::useItemEx(Player* player, const Position &fromPos, const Position player->setNextAction(OTSYS_TIME() + g_configManager().getNumber(EX_ACTIONS_DELAY_INTERVAL)); } - Action* action = getAction(item); + const std::shared_ptr &action = getAction(item); if (action == nullptr) { player->sendCancelMessage(RETURNVALUE_CANNOTUSETHISOBJECT); return false; diff --git a/src/lua/creature/actions.h b/src/lua/creature/actions.h index b41697e6dea..acfa07434cf 100644 --- a/src/lua/creature/actions.h +++ b/src/lua/creature/actions.h @@ -17,8 +17,6 @@ class Action; class Position; -using Action_ptr = std::unique_ptr; - class Action : public Script { public: explicit Action(LuaScriptInterface* interface); @@ -153,11 +151,11 @@ class Actions final : public Scripts { ReturnValue canUse(const Player* player, const Position &pos, const Item* item); ReturnValue canUseFar(const Creature* creature, const Position &toPos, bool checkLineOfSight, bool checkFloor); - bool registerLuaItemEvent(Action* action); - bool registerLuaUniqueEvent(Action* action); - bool registerLuaActionEvent(Action* action); - bool registerLuaPositionEvent(Action* action); - bool registerLuaEvent(Action* event); + bool registerLuaItemEvent(const std::shared_ptr &action); + bool registerLuaUniqueEvent(const std::shared_ptr& action); + bool registerLuaActionEvent(const std::shared_ptr& action); + bool registerLuaPositionEvent(const std::shared_ptr& action); + bool registerLuaEvent(const std::shared_ptr &action); // Clear maps for reloading void clear(); @@ -170,11 +168,11 @@ class Actions final : public Scripts { return false; } - phmap::btree_map getPositionsMap() const { + [[nodiscard]] phmap::btree_map> getPositionsMap() const { return actionPositionMap; } - void setPosition(Position position, Action action) { + void setPosition(Position position, std::shared_ptr action) { actionPositionMap.try_emplace(position, action); } @@ -186,7 +184,7 @@ class Actions final : public Scripts { return false; } - void setItemId(uint16_t itemId, Action action) { + void setItemId(uint16_t itemId, const std::shared_ptr& action) { useItemMap.try_emplace(itemId, action); } @@ -198,7 +196,7 @@ class Actions final : public Scripts { return false; } - void setUniqueId(uint16_t uniqueId, Action action) { + void setUniqueId(uint16_t uniqueId, const std::shared_ptr& action) { uniqueItemMap.try_emplace(uniqueId, action); } @@ -210,20 +208,20 @@ class Actions final : public Scripts { return false; } - void setActionId(uint16_t actionId, Action action) { + void setActionId(uint16_t actionId, const std::shared_ptr& action) { actionItemMap.try_emplace(actionId, action); } ReturnValue internalUseItem(Player* player, const Position &pos, uint8_t index, Item* item, bool isHotkey); static void showUseHotkeyMessage(Player* player, const Item* item, uint32_t count); - using ActionUseMap = phmap::btree_map; + using ActionUseMap = std::map>; ActionUseMap useItemMap; ActionUseMap uniqueItemMap; ActionUseMap actionItemMap; - phmap::btree_map actionPositionMap; + phmap::btree_map> actionPositionMap; - Action* getAction(const Item* item); + std::shared_ptr getAction(const Item* item); }; constexpr auto g_actions = Actions::getInstance; diff --git a/src/lua/functions/creatures/combat/spell_functions.cpp b/src/lua/functions/creatures/combat/spell_functions.cpp index 895f9ed59e5..078ce83f597 100644 --- a/src/lua/functions/creatures/combat/spell_functions.cpp +++ b/src/lua/functions/creatures/combat/spell_functions.cpp @@ -27,7 +27,7 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { if (isNumber(L, 2)) { uint16_t id = getNumber(L, 2); - RuneSpell* rune = g_spells().getRuneSpell(id); + std::shared_ptr rune = g_spells().getRuneSpell(id); if (rune) { pushUserdata(L, rune); @@ -38,7 +38,7 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { spellType = static_cast(id); } else if (isString(L, 2)) { std::string arg = getString(L, 2); - InstantSpell* instant = g_spells().getInstantSpellByName(arg); + std::shared_ptr instant = g_spells().getInstantSpellByName(arg); if (instant) { pushUserdata(L, instant); setMetatable(L, -1, "Spell"); @@ -50,7 +50,7 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { setMetatable(L, -1, "Spell"); return 1; } - RuneSpell* rune = g_spells().getRuneSpellByName(arg); + std::shared_ptr rune = g_spells().getRuneSpellByName(arg); if (rune) { pushUserdata(L, rune); setMetatable(L, -1, "Spell"); @@ -66,7 +66,7 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { } if (spellType == SPELL_INSTANT) { - InstantSpell* spell = new InstantSpell(getScriptEnv()->getScriptInterface()); + auto spell = std::make_shared(getScriptEnv()->getScriptInterface()); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -78,7 +78,7 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { spell->spellType = SPELL_INSTANT; return 1; } else if (spellType == SPELL_RUNE) { - auto runeSpell = new RuneSpell(getScriptEnv()->getScriptInterface()); + auto runeSpell = std::make_shared(getScriptEnv()->getScriptInterface()); if (!runeSpell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -97,10 +97,11 @@ int SpellFunctions::luaSpellCreate(lua_State* L) { int SpellFunctions::luaSpellOnCastSpell(lua_State* L) { // spell:onCastSpell(callback) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (spell->spellType == SPELL_INSTANT) { - InstantSpell* instant = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &instant = std::static_pointer_cast(spellBase); if (!instant->loadCallback()) { pushBoolean(L, false); return 1; @@ -108,7 +109,8 @@ int SpellFunctions::luaSpellOnCastSpell(lua_State* L) { instant->setLoadedCallback(true); pushBoolean(L, true); } else if (spell->spellType == SPELL_RUNE) { - RuneSpell* rune = dynamic_cast(getUserdata(L, 1)); + std::shared_ptr spellBase = getUserdataShared(L, 1); + std::shared_ptr rune = std::static_pointer_cast(spellBase); if (!rune->loadCallback()) { pushBoolean(L, false); return 1; @@ -124,8 +126,7 @@ int SpellFunctions::luaSpellOnCastSpell(lua_State* L) { int SpellFunctions::luaSpellRegister(lua_State* L) { // spell:register() - Spell* spell = getUserdata(L, 1); - + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -133,14 +134,16 @@ int SpellFunctions::luaSpellRegister(lua_State* L) { } if (spell->spellType == SPELL_INSTANT) { - InstantSpell* instant = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &instant = std::static_pointer_cast(spellBase); if (!instant->isLoadedCallback()) { pushBoolean(L, false); return 1; } pushBoolean(L, g_spells().registerInstantLuaEvent(instant)); } else if (spell->spellType == SPELL_RUNE) { - RuneSpell* rune = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &rune = std::static_pointer_cast(spellBase); if (rune->getMagicLevel() != 0 || rune->getLevel() != 0) { // Change information in the ItemType to get accurate description ItemType &iType = Item::items.getItemType(rune->getRuneItemId()); @@ -164,7 +167,7 @@ int SpellFunctions::luaSpellRegister(lua_State* L) { int SpellFunctions::luaSpellName(lua_State* L) { // spell:name(name) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushString(L, spell->getName()); @@ -180,7 +183,7 @@ int SpellFunctions::luaSpellName(lua_State* L) { int SpellFunctions::luaSpellId(lua_State* L) { // spell:id(id) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (spell->spellType != SPELL_INSTANT && spell->spellType != SPELL_RUNE) { reportErrorFunc("The method: 'spell:id(id)' is only for use of instant spells and rune spells"); @@ -201,7 +204,7 @@ int SpellFunctions::luaSpellId(lua_State* L) { int SpellFunctions::luaSpellGroup(lua_State* L) { // spell:group(primaryGroup[, secondaryGroup]) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getGroup()); @@ -276,7 +279,7 @@ int SpellFunctions::luaSpellGroup(lua_State* L) { int SpellFunctions::luaSpellCastSound(lua_State* L) { // get: spell:castSound() set: spell:castSound(effect) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(spell->soundCastEffect)); @@ -292,7 +295,7 @@ int SpellFunctions::luaSpellCastSound(lua_State* L) { int SpellFunctions::luaSpellImpactSound(lua_State* L) { // get: spell:impactSound() set: spell:impactSound(effect) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, static_cast(spell->soundImpactEffect)); @@ -308,7 +311,7 @@ int SpellFunctions::luaSpellImpactSound(lua_State* L) { int SpellFunctions::luaSpellCooldown(lua_State* L) { // spell:cooldown(cooldown) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getCooldown()); @@ -324,7 +327,7 @@ int SpellFunctions::luaSpellCooldown(lua_State* L) { int SpellFunctions::luaSpellGroupCooldown(lua_State* L) { // spell:groupCooldown(primaryGroupCd[, secondaryGroupCd]) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getGroupCooldown()); @@ -346,7 +349,7 @@ int SpellFunctions::luaSpellGroupCooldown(lua_State* L) { int SpellFunctions::luaSpellLevel(lua_State* L) { // spell:level(lvl) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getLevel()); @@ -362,7 +365,7 @@ int SpellFunctions::luaSpellLevel(lua_State* L) { int SpellFunctions::luaSpellMagicLevel(lua_State* L) { // spell:magicLevel(lvl) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getMagicLevel()); @@ -378,7 +381,7 @@ int SpellFunctions::luaSpellMagicLevel(lua_State* L) { int SpellFunctions::luaSpellMana(lua_State* L) { // spell:mana(mana) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getMana()); @@ -394,7 +397,7 @@ int SpellFunctions::luaSpellMana(lua_State* L) { int SpellFunctions::luaSpellManaPercent(lua_State* L) { // spell:manaPercent(percent) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getManaPercent()); @@ -410,7 +413,7 @@ int SpellFunctions::luaSpellManaPercent(lua_State* L) { int SpellFunctions::luaSpellSoul(lua_State* L) { // spell:soul(soul) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getSoulCost()); @@ -426,7 +429,7 @@ int SpellFunctions::luaSpellSoul(lua_State* L) { int SpellFunctions::luaSpellRange(lua_State* L) { // spell:range(range) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_pushnumber(L, spell->getRange()); @@ -442,7 +445,7 @@ int SpellFunctions::luaSpellRange(lua_State* L) { int SpellFunctions::luaSpellPremium(lua_State* L) { // spell:isPremium(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->isPremium()); @@ -458,7 +461,7 @@ int SpellFunctions::luaSpellPremium(lua_State* L) { int SpellFunctions::luaSpellEnabled(lua_State* L) { // spell:isEnabled(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->isEnabled()); @@ -474,7 +477,7 @@ int SpellFunctions::luaSpellEnabled(lua_State* L) { int SpellFunctions::luaSpellNeedTarget(lua_State* L) { // spell:needTarget(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getNeedTarget()); @@ -490,7 +493,7 @@ int SpellFunctions::luaSpellNeedTarget(lua_State* L) { int SpellFunctions::luaSpellNeedWeapon(lua_State* L) { // spell:needWeapon(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getNeedWeapon()); @@ -506,7 +509,7 @@ int SpellFunctions::luaSpellNeedWeapon(lua_State* L) { int SpellFunctions::luaSpellNeedLearn(lua_State* L) { // spell:needLearn(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getNeedLearn()); @@ -522,7 +525,7 @@ int SpellFunctions::luaSpellNeedLearn(lua_State* L) { int SpellFunctions::luaSpellSelfTarget(lua_State* L) { // spell:isSelfTarget(bool) - if (Spell* spell = getUserdata(L, 1)) { + if (const auto &spell = getUserdataShared(L, 1)) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getSelfTarget()); } else { @@ -537,7 +540,7 @@ int SpellFunctions::luaSpellSelfTarget(lua_State* L) { int SpellFunctions::luaSpellBlocking(lua_State* L) { // spell:isBlocking(blockingSolid, blockingCreature) - if (Spell* spell = getUserdata(L, 1)) { + if (const auto &spell = getUserdataShared(L, 1)) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getBlockingSolid()); pushBoolean(L, spell->getBlockingCreature()); @@ -555,7 +558,7 @@ int SpellFunctions::luaSpellBlocking(lua_State* L) { int SpellFunctions::luaSpellAggressive(lua_State* L) { // spell:isAggressive(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getAggressive()); @@ -571,7 +574,7 @@ int SpellFunctions::luaSpellAggressive(lua_State* L) { int SpellFunctions::luaSpellAllowOnSelf(lua_State* L) { // spell:allowOnSelf(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getAllowOnSelf()); @@ -587,7 +590,7 @@ int SpellFunctions::luaSpellAllowOnSelf(lua_State* L) { int SpellFunctions::luaSpellPzLocked(lua_State* L) { // spell:isPzLocked(bool) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { pushBoolean(L, spell->getLockedPZ()); @@ -603,7 +606,7 @@ int SpellFunctions::luaSpellPzLocked(lua_State* L) { int SpellFunctions::luaSpellVocation(lua_State* L) { // spell:vocation(vocation) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (spell) { if (lua_gettop(L) == 1) { lua_createtable(L, 0, 0); @@ -645,7 +648,8 @@ int SpellFunctions::luaSpellVocation(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellWords(lua_State* L) { // spell:words(words[, separator = ""]) - InstantSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil if (spell->spellType != SPELL_INSTANT) { @@ -675,7 +679,8 @@ int SpellFunctions::luaSpellWords(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellNeedDirection(lua_State* L) { // spell:needDirection(bool) - InstantSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil if (spell->spellType != SPELL_INSTANT) { @@ -698,7 +703,8 @@ int SpellFunctions::luaSpellNeedDirection(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellHasParams(lua_State* L) { // spell:hasParams(bool) - InstantSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil if (spell->spellType != SPELL_INSTANT) { @@ -721,7 +727,8 @@ int SpellFunctions::luaSpellHasParams(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellHasPlayerNameParam(lua_State* L) { // spell:hasPlayerNameParam(bool) - InstantSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil if (spell->spellType != SPELL_INSTANT) { @@ -744,7 +751,8 @@ int SpellFunctions::luaSpellHasPlayerNameParam(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellNeedCasterTargetOrDirection(lua_State* L) { // spell:needCasterTargetOrDirection(bool) - InstantSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil if (spell->spellType != SPELL_INSTANT) { @@ -767,7 +775,8 @@ int SpellFunctions::luaSpellNeedCasterTargetOrDirection(lua_State* L) { // only for InstantSpells int SpellFunctions::luaSpellIsBlockingWalls(lua_State* L) { // spell:blockWalls(bool) - InstantSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_INSTANT, it means that this actually is no InstantSpell, so we return nil if (spell->spellType != SPELL_INSTANT) { @@ -790,7 +799,8 @@ int SpellFunctions::luaSpellIsBlockingWalls(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellRuneId(lua_State* L) { // spell:runeId(id) - RuneSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil if (spell->spellType != SPELL_RUNE) { @@ -813,7 +823,8 @@ int SpellFunctions::luaSpellRuneId(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellCharges(lua_State* L) { // spell:charges(charges) - RuneSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil if (spell->spellType != SPELL_RUNE) { @@ -836,7 +847,8 @@ int SpellFunctions::luaSpellCharges(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellAllowFarUse(lua_State* L) { // spell:allowFarUse(bool) - RuneSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil if (spell->spellType != SPELL_RUNE) { @@ -859,7 +871,8 @@ int SpellFunctions::luaSpellAllowFarUse(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellBlockWalls(lua_State* L) { // spell:blockWalls(bool) - RuneSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil if (spell->spellType != SPELL_RUNE) { @@ -882,7 +895,8 @@ int SpellFunctions::luaSpellBlockWalls(lua_State* L) { // only for RuneSpells int SpellFunctions::luaSpellCheckFloor(lua_State* L) { // spell:checkFloor(bool) - RuneSpell* spell = dynamic_cast(getUserdata(L, 1)); + const auto &spellBase = getUserdataShared(L, 1); + const auto &spell = std::static_pointer_cast(spellBase); if (spell) { // if spell != SPELL_RUNE, it means that this actually is no RuneSpell, so we return nil if (spell->spellType != SPELL_RUNE) { @@ -905,7 +919,7 @@ int SpellFunctions::luaSpellCheckFloor(lua_State* L) { // Wheel of destiny int SpellFunctions::luaSpellManaWOD(lua_State* L) { // spell:manaWOD(grade, mana) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); WheelSpellGrade_t grade = getNumber(L, 2); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); @@ -925,7 +939,7 @@ int SpellFunctions::luaSpellManaWOD(lua_State* L) { int SpellFunctions::luaSpellCooldownWOD(lua_State* L) { // spell:cooldownWOD(grade, time) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -945,7 +959,7 @@ int SpellFunctions::luaSpellCooldownWOD(lua_State* L) { int SpellFunctions::luaSpellGroupCooldownWOD(lua_State* L) { // spell:groupCooldownWOD(grade, time) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -965,7 +979,7 @@ int SpellFunctions::luaSpellGroupCooldownWOD(lua_State* L) { int SpellFunctions::luaSpellSecondaryGroupCooldownWOD(lua_State* L) { // spell:secondaryGroupCooldownWOD(grade, time) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -985,7 +999,7 @@ int SpellFunctions::luaSpellSecondaryGroupCooldownWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaseManaLeechWOD(lua_State* L) { // spell:increaseManaLeechWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -1011,7 +1025,7 @@ int SpellFunctions::luaSpellIncreaseManaLeechWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaselifeLeechWOD(lua_State* L) { // spell:increaselifeLeechWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -1037,7 +1051,7 @@ int SpellFunctions::luaSpellIncreaselifeLeechWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaseDamageWOD(lua_State* L) { // spell:increaseDamageWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -1057,7 +1071,7 @@ int SpellFunctions::luaSpellIncreaseDamageWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaseDamageReductionWOD(lua_State* L) { // spell:increaseDamageReductionWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -1077,7 +1091,7 @@ int SpellFunctions::luaSpellIncreaseDamageReductionWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaseHealWOD(lua_State* L) { // spell:increaseHealWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -1097,7 +1111,7 @@ int SpellFunctions::luaSpellIncreaseHealWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaseCriticalDamageWOD(lua_State* L) { // spell:increaseCriticalDamageWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); @@ -1117,7 +1131,7 @@ int SpellFunctions::luaSpellIncreaseCriticalDamageWOD(lua_State* L) { int SpellFunctions::luaSpellIncreaseCriticalChanceWOD(lua_State* L) { // spell:increaseCriticalChanceWOD(grade, value) - Spell* spell = getUserdata(L, 1); + const auto &spell = getUserdataShared(L, 1); if (!spell) { reportErrorFunc(getErrorDesc(LUA_ERROR_SPELL_NOT_FOUND)); pushBoolean(L, false); diff --git a/src/lua/functions/creatures/combat/spell_functions.hpp b/src/lua/functions/creatures/combat/spell_functions.hpp index 95744cc19a1..a125ea72e98 100644 --- a/src/lua/functions/creatures/combat/spell_functions.hpp +++ b/src/lua/functions/creatures/combat/spell_functions.hpp @@ -15,7 +15,7 @@ class SpellFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "Spell", "", SpellFunctions::luaSpellCreate); + registerSharedClass(L, "Spell", "", SpellFunctions::luaSpellCreate); registerMetaMethod(L, "Spell", "__eq", SpellFunctions::luaUserdataCompare); registerMethod(L, "Spell", "onCastSpell", SpellFunctions::luaSpellOnCastSpell); diff --git a/src/lua/functions/creatures/monster/monster_type_functions.cpp b/src/lua/functions/creatures/monster/monster_type_functions.cpp index fe6e73f97d4..6630afe898e 100644 --- a/src/lua/functions/creatures/monster/monster_type_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_type_functions.cpp @@ -753,7 +753,8 @@ int MonsterTypeFunctions::luaMonsterTypeGetAttackList(lua_State* L) { setField(L, "maxCombatValue", spellBlock.maxCombatValue); setField(L, "range", spellBlock.range); setField(L, "speed", spellBlock.speed); - pushUserdata(L, static_cast(spellBlock.spell)); + const auto &combatSpell = std::static_pointer_cast(spellBlock.spell); + pushUserdata(L, combatSpell); lua_setfield(L, -2, "spell"); lua_rawseti(L, -2, ++index); @@ -803,7 +804,8 @@ int MonsterTypeFunctions::luaMonsterTypeGetDefenseList(lua_State* L) { setField(L, "maxCombatValue", spellBlock.maxCombatValue); setField(L, "range", spellBlock.range); setField(L, "speed", spellBlock.speed); - pushUserdata(L, static_cast(spellBlock.spell)); + const auto &combatSpell = std::static_pointer_cast(spellBlock.spell); + pushUserdata(L, combatSpell); lua_setfield(L, -2, "spell"); lua_rawseti(L, -2, ++index); diff --git a/src/lua/functions/creatures/player/player_functions.cpp b/src/lua/functions/creatures/player/player_functions.cpp index 64dfc721d25..f3df122b808 100644 --- a/src/lua/functions/creatures/player/player_functions.cpp +++ b/src/lua/functions/creatures/player/player_functions.cpp @@ -2579,7 +2579,7 @@ int PlayerFunctions::luaPlayerCanLearnSpell(lua_State* L) { } const std::string &spellName = getString(L, 2); - const InstantSpell* spell = g_spells().getInstantSpellByName(spellName); + const auto& spell = g_spells().getInstantSpellByName(spellName); if (!spell) { reportErrorFunc("Spell \"" + spellName + "\" not found"); pushBoolean(L, false); @@ -2922,10 +2922,10 @@ int PlayerFunctions::luaPlayerGetInstantSpells(lua_State* L) { return 1; } - std::vector spells; + std::vector> spells; for (auto &[key, spell] : g_spells().getInstantSpells()) { - if (spell.canCast(player)) { - spells.push_back(&spell); + if (spell->canCast(player)) { + spells.push_back(spell); } } @@ -2942,7 +2942,7 @@ int PlayerFunctions::luaPlayerGetInstantSpells(lua_State* L) { int PlayerFunctions::luaPlayerCanCast(lua_State* L) { // player:canCast(spell) Player* player = getUserdata(L, 1); - InstantSpell* spell = getUserdata(L, 2); + const auto& spell = getUserdataShared(L, 2); if (player && spell) { pushBoolean(L, spell->canCast(player)); } else { diff --git a/src/lua/functions/events/action_functions.cpp b/src/lua/functions/events/action_functions.cpp index d5a716638ba..85c43230e17 100644 --- a/src/lua/functions/events/action_functions.cpp +++ b/src/lua/functions/events/action_functions.cpp @@ -16,7 +16,7 @@ int ActionFunctions::luaCreateAction(lua_State* L) { // Action() - Action* action = new Action(getScriptEnv()->getScriptInterface()); + std::shared_ptr action = std::make_shared(getScriptEnv()->getScriptInterface()); if (action) { pushUserdata(L, action); setMetatable(L, -1, "Action"); @@ -29,7 +29,7 @@ int ActionFunctions::luaCreateAction(lua_State* L) { int ActionFunctions::luaActionOnUse(lua_State* L) { // action:onUse(callback) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { if (!action->loadCallback()) { pushBoolean(L, false); @@ -46,7 +46,7 @@ int ActionFunctions::luaActionOnUse(lua_State* L) { int ActionFunctions::luaActionRegister(lua_State* L) { // action:register() - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { if (!action->isLoadedCallback()) { pushBoolean(L, false); @@ -63,7 +63,7 @@ int ActionFunctions::luaActionRegister(lua_State* L) { int ActionFunctions::luaActionItemId(lua_State* L) { // action:id(ids) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { @@ -83,7 +83,7 @@ int ActionFunctions::luaActionItemId(lua_State* L) { int ActionFunctions::luaActionActionId(lua_State* L) { // action:aid(aids) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { @@ -103,7 +103,7 @@ int ActionFunctions::luaActionActionId(lua_State* L) { int ActionFunctions::luaActionUniqueId(lua_State* L) { // action:uid(uids) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { int parameters = lua_gettop(L) - 1; // - 1 because self is a parameter aswell, which we want to skip ofc if (parameters > 1) { @@ -127,7 +127,7 @@ int ActionFunctions::luaActionPosition(lua_State* L) { * @param itemId or @param itemName = if item id or string name is set, the item is created on position (if not exists), this variable is nil by default * action:position(positions, itemId or name) */ - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (!action) { reportErrorFunc(getErrorDesc(LUA_ERROR_ACTION_NOT_FOUND)); pushBoolean(L, false); @@ -184,7 +184,7 @@ int ActionFunctions::luaActionPosition(lua_State* L) { int ActionFunctions::luaActionAllowFarUse(lua_State* L) { // action:allowFarUse(bool) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { action->setAllowFarUse(getBoolean(L, 2)); pushBoolean(L, true); @@ -197,7 +197,7 @@ int ActionFunctions::luaActionAllowFarUse(lua_State* L) { int ActionFunctions::luaActionBlockWalls(lua_State* L) { // action:blockWalls(bool) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { action->setCheckLineOfSight(getBoolean(L, 2)); pushBoolean(L, true); @@ -210,7 +210,7 @@ int ActionFunctions::luaActionBlockWalls(lua_State* L) { int ActionFunctions::luaActionCheckFloor(lua_State* L) { // action:checkFloor(bool) - Action* action = getUserdata(L, 1); + const auto &action = getUserdataShared(L, 1); if (action) { action->setCheckFloor(getBoolean(L, 2)); pushBoolean(L, true); diff --git a/src/lua/functions/events/action_functions.hpp b/src/lua/functions/events/action_functions.hpp index 08d7fa1d9a5..17f5c2e1e76 100644 --- a/src/lua/functions/events/action_functions.hpp +++ b/src/lua/functions/events/action_functions.hpp @@ -15,7 +15,7 @@ class ActionFunctions final : LuaScriptInterface { public: static void init(lua_State* L) { - registerClass(L, "Action", "", ActionFunctions::luaCreateAction); + registerSharedClass(L, "Action", "", ActionFunctions::luaCreateAction); registerMethod(L, "Action", "onUse", ActionFunctions::luaActionOnUse); registerMethod(L, "Action", "register", ActionFunctions::luaActionRegister); registerMethod(L, "Action", "id", ActionFunctions::luaActionItemId); diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index de8b8121975..00ea57d03bc 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -3846,7 +3846,7 @@ void ProtocolGame::sendBasicData() { // Filter only valid ids std::list spellsList = g_spells().getSpellsByVocation(player->getVocationId()); - std::vector validSpells; + std::vector> validSpells; for (uint16_t sid : spellsList) { auto spell = g_spells().getInstantSpellById(sid); if (spell && spell->getId() > 0) { From de78e1496ea5f5359fda1d31c2d70c7d43048906 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Wed, 16 Aug 2023 16:49:37 +0000 Subject: [PATCH 2/2] Code format - (Clang-format) --- src/creatures/combat/combat.cpp | 6 +++--- src/creatures/combat/combat.h | 2 +- src/creatures/combat/spells.cpp | 4 ++-- src/creatures/combat/spells.h | 6 +++--- src/items/item.cpp | 2 +- src/lua/creature/actions.cpp | 2 +- src/lua/creature/actions.h | 12 ++++++------ .../functions/creatures/player/player_functions.cpp | 4 ++-- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/creatures/combat/combat.cpp b/src/creatures/combat/combat.cpp index 97a10cd8b7f..bfc6656229b 100644 --- a/src/creatures/combat/combat.cpp +++ b/src/creatures/combat/combat.cpp @@ -19,7 +19,7 @@ #include "creatures/monsters/monsters.h" #include "items/weapons/weapons.h" -int32_t Combat::getLevelFormula(const Player* player, const std::shared_ptr& wheelSpell, const CombatDamage &damage) const { +int32_t Combat::getLevelFormula(const Player* player, const std::shared_ptr &wheelSpell, const CombatDamage &damage) const { if (!player) { return 0; } @@ -1438,9 +1438,9 @@ uint32_t ValueCallback::getMagicLevelSkill(const Player* player, const CombatDam uint32_t magicLevelSkill = player->getMagicLevel(); // Wheel of destiny if (player && player->wheel()->getInstant("Runic Mastery") && damage.instantSpellName.empty()) { - const std::shared_ptr& spell = g_spells().getRuneSpellByName(damage.runeSpellName); + const std::shared_ptr &spell = g_spells().getRuneSpellByName(damage.runeSpellName); // Rune conjuring spell have the same name as the rune item spell. - const std::shared_ptr& conjuringSpell = g_spells().getInstantSpellByName(damage.runeSpellName); + const std::shared_ptr &conjuringSpell = g_spells().getInstantSpellByName(damage.runeSpellName); if (spell && conjuringSpell && conjuringSpell != spell && normal_random(0, 100) <= 25) { uint32_t castResult = conjuringSpell->canCast(player) ? 20 : 10; magicLevelSkill += magicLevelSkill * castResult / 100; diff --git a/src/creatures/combat/combat.h b/src/creatures/combat/combat.h index abc6b077ce2..e1ebcb92db2 100644 --- a/src/creatures/combat/combat.h +++ b/src/creatures/combat/combat.h @@ -366,7 +366,7 @@ class Combat { * @param damage The combat damage. * @return The calculated level formula. */ - int32_t getLevelFormula(const Player* player, const std::shared_ptr& wheelSpell, const CombatDamage &damage) const; + int32_t getLevelFormula(const Player* player, const std::shared_ptr &wheelSpell, const CombatDamage &damage) const; CombatDamage getCombatDamage(Creature* creature, Creature* target) const; bool doCombatChain(Creature* caster, Creature* target, bool aggressive) const; diff --git a/src/creatures/combat/spells.cpp b/src/creatures/combat/spells.cpp index 83147ee5225..a353fc69d37 100644 --- a/src/creatures/combat/spells.cpp +++ b/src/creatures/combat/spells.cpp @@ -90,7 +90,7 @@ bool Spells::hasInstantSpell(const std::string &word) const { return false; } -bool Spells::registerInstantLuaEvent(const std::shared_ptr& instant) { +bool Spells::registerInstantLuaEvent(const std::shared_ptr &instant) { if (instant) { // If the spell not have the "spell:words()" return a error message const std::string &instantName = instant->getName(); @@ -114,7 +114,7 @@ bool Spells::registerInstantLuaEvent(const std::shared_ptr& instan return false; } -bool Spells::registerRuneLuaEvent(const std::shared_ptr& rune) { +bool Spells::registerRuneLuaEvent(const std::shared_ptr &rune) { if (rune) { uint16_t id = rune->getRuneItemId(); auto result = runes.emplace(rune->getRuneItemId(), rune); diff --git a/src/creatures/combat/spells.h b/src/creatures/combat/spells.h index be6a9bf17f0..81a92171799 100644 --- a/src/creatures/combat/spells.h +++ b/src/creatures/combat/spells.h @@ -62,8 +62,8 @@ class Spells final : public Scripts { } void clear(); - bool registerInstantLuaEvent(const std::shared_ptr& instant); - bool registerRuneLuaEvent(const std::shared_ptr& rune); + bool registerInstantLuaEvent(const std::shared_ptr &instant); + bool registerRuneLuaEvent(const std::shared_ptr &rune); private: std::map> runes; @@ -91,7 +91,7 @@ class BaseSpell { class CombatSpell final : public Script, public BaseSpell, public std::enable_shared_from_this { public: // Constructor - CombatSpell(const std::shared_ptr& newCombat, bool newNeedTarget, bool newNeedDirection); + CombatSpell(const std::shared_ptr &newCombat, bool newNeedTarget, bool newNeedDirection); // The copy constructor and the assignment operator have been deleted to prevent accidental copying. CombatSpell(const CombatSpell &) = delete; diff --git a/src/items/item.cpp b/src/items/item.cpp index f29897ae0f2..9c8d7f65682 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -2217,7 +2217,7 @@ std::string Item::getDescription(const ItemType &it, int32_t lookDistance, const if (it.isRune()) { if (it.runeLevel > 0 || it.runeMagLevel > 0) { - if (const auto& rune = g_spells().getRuneSpell(it.id)) { + if (const auto &rune = g_spells().getRuneSpell(it.id)) { int32_t tmpSubType = subType; if (item) { tmpSubType = item->getSubType(); diff --git a/src/lua/creature/actions.cpp b/src/lua/creature/actions.cpp index bc493c82ec4..a19ad221bcd 100644 --- a/src/lua/creature/actions.cpp +++ b/src/lua/creature/actions.cpp @@ -181,7 +181,7 @@ ReturnValue Actions::canUse(const Player* player, const Position &pos) { } ReturnValue Actions::canUse(const Player* player, const Position &pos, const Item* item) { - const std::shared_ptr& action = getAction(item); + const std::shared_ptr &action = getAction(item); if (action != nullptr) { return action->canExecuteAction(player, pos); } diff --git a/src/lua/creature/actions.h b/src/lua/creature/actions.h index acfa07434cf..dcb9db3dcdf 100644 --- a/src/lua/creature/actions.h +++ b/src/lua/creature/actions.h @@ -152,9 +152,9 @@ class Actions final : public Scripts { ReturnValue canUseFar(const Creature* creature, const Position &toPos, bool checkLineOfSight, bool checkFloor); bool registerLuaItemEvent(const std::shared_ptr &action); - bool registerLuaUniqueEvent(const std::shared_ptr& action); - bool registerLuaActionEvent(const std::shared_ptr& action); - bool registerLuaPositionEvent(const std::shared_ptr& action); + bool registerLuaUniqueEvent(const std::shared_ptr &action); + bool registerLuaActionEvent(const std::shared_ptr &action); + bool registerLuaPositionEvent(const std::shared_ptr &action); bool registerLuaEvent(const std::shared_ptr &action); // Clear maps for reloading void clear(); @@ -184,7 +184,7 @@ class Actions final : public Scripts { return false; } - void setItemId(uint16_t itemId, const std::shared_ptr& action) { + void setItemId(uint16_t itemId, const std::shared_ptr &action) { useItemMap.try_emplace(itemId, action); } @@ -196,7 +196,7 @@ class Actions final : public Scripts { return false; } - void setUniqueId(uint16_t uniqueId, const std::shared_ptr& action) { + void setUniqueId(uint16_t uniqueId, const std::shared_ptr &action) { uniqueItemMap.try_emplace(uniqueId, action); } @@ -208,7 +208,7 @@ class Actions final : public Scripts { return false; } - void setActionId(uint16_t actionId, const std::shared_ptr& action) { + void setActionId(uint16_t actionId, const std::shared_ptr &action) { actionItemMap.try_emplace(actionId, action); } diff --git a/src/lua/functions/creatures/player/player_functions.cpp b/src/lua/functions/creatures/player/player_functions.cpp index f3df122b808..02e0b89f258 100644 --- a/src/lua/functions/creatures/player/player_functions.cpp +++ b/src/lua/functions/creatures/player/player_functions.cpp @@ -2579,7 +2579,7 @@ int PlayerFunctions::luaPlayerCanLearnSpell(lua_State* L) { } const std::string &spellName = getString(L, 2); - const auto& spell = g_spells().getInstantSpellByName(spellName); + const auto &spell = g_spells().getInstantSpellByName(spellName); if (!spell) { reportErrorFunc("Spell \"" + spellName + "\" not found"); pushBoolean(L, false); @@ -2942,7 +2942,7 @@ int PlayerFunctions::luaPlayerGetInstantSpells(lua_State* L) { int PlayerFunctions::luaPlayerCanCast(lua_State* L) { // player:canCast(spell) Player* player = getUserdata(L, 1); - const auto& spell = getUserdataShared(L, 2); + const auto &spell = getUserdataShared(L, 2); if (player && spell) { pushBoolean(L, spell->canCast(player)); } else {