From d2ab35f8fdddbce729b648c6190c6472937883c0 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Fri, 22 Sep 2023 17:50:59 -0300 Subject: [PATCH] SpawnNpc to smart pointer --- src/creatures/npcs/npc.hpp | 6 +++--- src/creatures/npcs/npcs.hpp | 2 +- src/creatures/npcs/spawns/spawn_npc.cpp | 18 ++++++++---------- src/creatures/npcs/spawns/spawn_npc.hpp | 6 +++--- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/creatures/npcs/npc.hpp b/src/creatures/npcs/npc.hpp index c55fff0911c..45941706f65 100644 --- a/src/creatures/npcs/npc.hpp +++ b/src/creatures/npcs/npc.hpp @@ -112,8 +112,8 @@ class Npc final : public Creature { RespawnType getRespawnType() const { return npcType->info.respawnType; } - void setSpawnNpc(SpawnNpc* newSpawn) { - this->spawnNpc = newSpawn; + void setSpawnNpc(const std::shared_ptr &newSpawn) { + spawnNpc = newSpawn; } void setPlayerInteraction(uint32_t playerId, uint16_t topicId = 0); @@ -178,7 +178,7 @@ class Npc final : public Creature { phmap::flat_hash_map> shopPlayerMap; std::shared_ptr npcType; - SpawnNpc* spawnNpc = nullptr; + std::shared_ptr spawnNpc; uint8_t speechBubble; diff --git a/src/creatures/npcs/npcs.hpp b/src/creatures/npcs/npcs.hpp index 472af4e89ad..d836e6e9252 100644 --- a/src/creatures/npcs/npcs.hpp +++ b/src/creatures/npcs/npcs.hpp @@ -23,7 +23,7 @@ class Shop { ShopBlock shopBlock; }; -class NpcType { +class NpcType : public SharedObject { struct NpcInfo { LuaScriptInterface* scriptInterface; diff --git a/src/creatures/npcs/spawns/spawn_npc.cpp b/src/creatures/npcs/spawns/spawn_npc.cpp index 36430ec025e..c7605f36c06 100644 --- a/src/creatures/npcs/spawns/spawn_npc.cpp +++ b/src/creatures/npcs/spawns/spawn_npc.cpp @@ -56,8 +56,7 @@ bool SpawnsNpc::loadFromXml(const std::string &fileNpcName) { continue; } - spawnNpcList.emplace_front(centerPos, radius); - SpawnNpc &spawnNpc = spawnNpcList.front(); + const auto &spawnNpc = spawnNpcList.emplace_front(std::make_shared(centerPos, radius)); for (auto childNode : spawnNode.children()) { if (strcasecmp(childNode.name(), "npc") == 0) { @@ -84,7 +83,7 @@ bool SpawnsNpc::loadFromXml(const std::string &fileNpcName) { ); int64_t interval = pugi::cast(childNode.attribute("spawntime").value()) * 1000; if (interval >= MINSPAWN_INTERVAL && interval <= MAXSPAWN_INTERVAL) { - spawnNpc.addNpc(nameAttribute.as_string(), pos, dir, static_cast(interval)); + spawnNpc->addNpc(nameAttribute.as_string(), pos, dir, static_cast(interval)); } else { if (interval <= MINSPAWN_INTERVAL) { g_logger().warn("[SpawnsNpc::loadFromXml] - {} {} spawntime can not be less than {} seconds", nameAttribute.as_string(), pos.toString(), MINSPAWN_INTERVAL / 1000); @@ -103,16 +102,16 @@ void SpawnsNpc::startup() { return; } - for (SpawnNpc &spawnNpc : spawnNpcList) { - spawnNpc.startup(); + for (const auto &spawnNpc : spawnNpcList) { + spawnNpc->startup(); } setStarted(true); } void SpawnsNpc::clear() { - for (SpawnNpc &spawnNpc : spawnNpcList) { - spawnNpc.stopEvent(); + for (const auto &spawnNpc : spawnNpcList) { + spawnNpc->stopEvent(); } spawnNpcList.clear(); @@ -137,8 +136,7 @@ void SpawnNpc::startSpawnNpcCheck() { SpawnNpc::~SpawnNpc() { for (const auto &it : spawnedNpcMap) { - auto npc = it.second; - npc->setSpawnNpc(nullptr); + it.second->setSpawnNpc(nullptr); } } @@ -171,7 +169,7 @@ bool SpawnNpc::spawnNpc(uint32_t spawnId, const std::shared_ptr &npcTyp } npc->setDirection(dir); - npc->setSpawnNpc(this); + npc->setSpawnNpc(static_self_cast()); npc->setMasterPos(pos); spawnedNpcMap.insert(spawned_pair(spawnId, npc)); diff --git a/src/creatures/npcs/spawns/spawn_npc.hpp b/src/creatures/npcs/spawns/spawn_npc.hpp index eeb5c2edcd2..dccc1a75e62 100644 --- a/src/creatures/npcs/spawns/spawn_npc.hpp +++ b/src/creatures/npcs/spawns/spawn_npc.hpp @@ -23,7 +23,7 @@ struct spawnBlockNpc_t { Direction direction; }; -class SpawnNpc { +class SpawnNpc : public SharedObject { public: SpawnNpc(Position initPos, int32_t initRadius) : centerPos(std::move(initPos)), radius(initRadius) { } @@ -94,12 +94,12 @@ class SpawnsNpc { return fileName = setName; } - std::forward_list &getSpawnNpcList() { + std::forward_list> &getSpawnNpcList() { return spawnNpcList; } private: - std::forward_list spawnNpcList; + std::forward_list> spawnNpcList; std::string fileName; bool loaded = false; bool started = false;