Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: change some raw pointers to use std::shared_ptr #1418

Merged
merged 4 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Spell> &wheelSpell, const CombatDamage &damage) const {
if (!player) {
return 0;
}
Expand All @@ -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<Spell> wheelSpell = nullptr;
Player* attackerPlayer = creature ? creature->getPlayer() : nullptr;
if (attackerPlayer) {
wheelSpell = attackerPlayer->wheel()->getCombatDataSpell(damage);
Expand Down Expand Up @@ -1433,9 +1433,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> &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<InstantSpell> &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;
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/combat/combat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Spell> &wheelSpell, const CombatDamage &damage) const;
CombatDamage getCombatDamage(Creature* creature, Creature* target) const;

bool doCombatChain(Creature* caster, Creature* target, bool aggressive) const;
Expand Down
62 changes: 30 additions & 32 deletions src/creatures/combat/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> &instantSpell = getInstantSpell(str_words);
if (!instantSpell) {
return TALKACTION_CONTINUE;
}
Expand Down Expand Up @@ -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<InstantSpell> &instant) {
if (instant) {
// If the spell not have the "spell:words()" return a error message
const std::string &instantName = instant->getName();
Expand All @@ -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<RuneSpell> &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;
Expand All @@ -140,57 +138,57 @@ std::list<uint16_t> Spells::getSpellsByVocation(uint16_t vocationId) {
phmap::btree_map<uint16_t, bool>::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<Spell> Spells::getSpellByName(const std::string &name) {
std::shared_ptr<Spell> spell = getRuneSpellByName(name);
if (!spell) {
spell = getInstantSpellByName(name);
}
return spell;
}

RuneSpell* Spells::getRuneSpell(uint16_t id) {
std::shared_ptr<RuneSpell> 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<RuneSpell> 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<InstantSpell> Spells::getInstantSpell(const std::string &words) {
std::shared_ptr<InstantSpell> 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;
}
Expand All @@ -216,19 +214,19 @@ InstantSpell* Spells::getInstantSpell(const std::string &words) {
return nullptr;
}

InstantSpell* Spells::getInstantSpellById(uint16_t spellId) {
std::shared_ptr<InstantSpell> 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<InstantSpell> 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;
Expand All @@ -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<Combat> &newCombat, bool newNeedTarget, bool newNeedDirection) :
Script(&g_spells().getScriptInterface()),
combat(newCombat),
needDirection(newNeedDirection),
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
Loading