From d28646dcd5c4513ffca60a029084b6b9cdd312ef Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Fri, 22 Sep 2023 17:41:46 -0300 Subject: [PATCH] SpawnNpc to smart pointer --- src/creatures/npcs/npc.cpp | 9 +-- src/creatures/npcs/npc.hpp | 4 +- src/creatures/npcs/npcs.cpp | 12 +--- src/creatures/npcs/npcs.hpp | 6 +- src/creatures/npcs/spawns/spawn_npc.cpp | 4 +- src/creatures/npcs/spawns/spawn_npc.hpp | 4 +- .../creatures/npc/npc_type_functions.cpp | 64 +++++++++---------- 7 files changed, 47 insertions(+), 56 deletions(-) diff --git a/src/creatures/npcs/npc.cpp b/src/creatures/npcs/npc.cpp index b8775ee148a..61896f2f4db 100644 --- a/src/creatures/npcs/npc.cpp +++ b/src/creatures/npcs/npc.cpp @@ -23,14 +23,11 @@ int32_t Npc::despawnRadius; uint32_t Npc::npcAutoID = 0x80000000; std::shared_ptr Npc::createNpc(const std::string &name) { - NpcType* npcType = g_npcs().getNpcType(name); - if (!npcType) { - return nullptr; - } - return std::make_shared(npcType); + const auto &npcType = g_npcs().getNpcType(name); + return npcType ? std::make_shared(npcType) : nullptr; } -Npc::Npc(NpcType* npcType) : +Npc::Npc(const std::shared_ptr &npcType) : Creature(), strDescription(npcType->nameDescription), npcType(npcType) { diff --git a/src/creatures/npcs/npc.hpp b/src/creatures/npcs/npc.hpp index 3af93863fc3..c55fff0911c 100644 --- a/src/creatures/npcs/npc.hpp +++ b/src/creatures/npcs/npc.hpp @@ -25,7 +25,7 @@ class Npc final : public Creature { static int32_t despawnRange; static int32_t despawnRadius; - explicit Npc(NpcType* npcType); + explicit Npc(const std::shared_ptr &npcType); Npc() = default; ~Npc(); @@ -177,7 +177,7 @@ class Npc final : public Creature { phmap::flat_hash_map> shopPlayerMap; - NpcType* npcType; + std::shared_ptr npcType; SpawnNpc* spawnNpc = nullptr; uint8_t speechBubble; diff --git a/src/creatures/npcs/npcs.cpp b/src/creatures/npcs/npcs.cpp index d3c1fff7adc..432b0adbe15 100644 --- a/src/creatures/npcs/npcs.cpp +++ b/src/creatures/npcs/npcs.cpp @@ -73,7 +73,7 @@ bool NpcType::loadCallback(LuaScriptInterface* scriptInterface) { return true; } -void NpcType::loadShop(NpcType* npcType, ShopBlock shopBlock) { +void NpcType::loadShop(const std::shared_ptr &npcType, ShopBlock shopBlock) { ItemType &iType = Item::items.getItemType(shopBlock.itemId); // Registering item prices globaly. @@ -134,7 +134,7 @@ bool Npcs::reload() { return false; } -NpcType* Npcs::getNpcType(const std::string &name, bool create /* = false*/) { +std::shared_ptr Npcs::getNpcType(const std::string &name, bool create /* = false*/) { std::string key = asLowerCaseString(name); auto it = npcs.find(key); @@ -142,11 +142,5 @@ NpcType* Npcs::getNpcType(const std::string &name, bool create /* = false*/) { return it->second; } - if (!create) { - return nullptr; - } - - npcs[key] = new NpcType(name); - - return npcs[key]; + return create ? npcs[key] = std::make_shared(name) : nullptr; } diff --git a/src/creatures/npcs/npcs.hpp b/src/creatures/npcs/npcs.hpp index 4b8f59186a2..472af4e89ad 100644 --- a/src/creatures/npcs/npcs.hpp +++ b/src/creatures/npcs/npcs.hpp @@ -86,7 +86,7 @@ class NpcType { std::string nameDescription; NpcInfo info; - void loadShop(NpcType* npcType, ShopBlock shopBlock); + void loadShop(const std::shared_ptr &npcType, ShopBlock shopBlock); bool loadCallback(LuaScriptInterface* scriptInterface); bool canSpawn(const Position &pos); @@ -103,7 +103,7 @@ class Npcs { return inject(); } - NpcType* getNpcType(const std::string &name, bool create = false); + std::shared_ptr getNpcType(const std::string &name, bool create = false); // Reset npcs informations on reload bool load(bool loadLibs = true, bool loadNpcs = true, bool reloading = false) const; @@ -111,7 +111,7 @@ class Npcs { private: std::unique_ptr scriptInterface; - std::map npcs; + std::map> npcs; }; constexpr auto g_npcs = Npcs::getInstance; diff --git a/src/creatures/npcs/spawns/spawn_npc.cpp b/src/creatures/npcs/spawns/spawn_npc.cpp index 9d29598792f..36430ec025e 100644 --- a/src/creatures/npcs/spawns/spawn_npc.cpp +++ b/src/creatures/npcs/spawns/spawn_npc.cpp @@ -157,7 +157,7 @@ bool SpawnNpc::isInSpawnNpcZone(const Position &pos) { return SpawnsNpc::isInZone(centerPos, radius, pos); } -bool SpawnNpc::spawnNpc(uint32_t spawnId, NpcType* npcType, const Position &pos, Direction dir, bool startup /*= false*/) { +bool SpawnNpc::spawnNpc(uint32_t spawnId, const std::shared_ptr &npcType, const Position &pos, Direction dir, bool startup /*= false*/) { auto npc = std::make_shared(npcType); if (startup) { // No need to send out events to the surrounding since there is no one out there to listen! @@ -246,7 +246,7 @@ void SpawnNpc::cleanup() { } bool SpawnNpc::addNpc(const std::string &name, const Position &pos, Direction dir, uint32_t scheduleInterval) { - NpcType* npcType = g_npcs().getNpcType(name); + const auto &npcType = g_npcs().getNpcType(name); if (!npcType) { g_logger().error("Can not find {}", name); return false; diff --git a/src/creatures/npcs/spawns/spawn_npc.hpp b/src/creatures/npcs/spawns/spawn_npc.hpp index 4926d2c8d09..eeb5c2edcd2 100644 --- a/src/creatures/npcs/spawns/spawn_npc.hpp +++ b/src/creatures/npcs/spawns/spawn_npc.hpp @@ -17,7 +17,7 @@ class NpcType; struct spawnBlockNpc_t { Position pos; - NpcType* npcType; + std::shared_ptr npcType; int64_t lastSpawnNpc; uint32_t interval; Direction direction; @@ -63,7 +63,7 @@ class SpawnNpc { uint32_t checkSpawnNpcEvent = 0; static bool findPlayer(const Position &pos); - bool spawnNpc(uint32_t spawnId, NpcType* npcType, const Position &pos, Direction dir, bool startup = false); + bool spawnNpc(uint32_t spawnId, const std::shared_ptr &npcType, const Position &pos, Direction dir, bool startup = false); void checkSpawnNpc(); void scheduleSpawnNpc(uint32_t spawnId, spawnBlockNpc_t &sb, uint16_t interval); }; diff --git a/src/lua/functions/creatures/npc/npc_type_functions.cpp b/src/lua/functions/creatures/npc/npc_type_functions.cpp index 6f1d553ea34..8c520a4ae4d 100644 --- a/src/lua/functions/creatures/npc/npc_type_functions.cpp +++ b/src/lua/functions/creatures/npc/npc_type_functions.cpp @@ -37,7 +37,7 @@ void NpcTypeFunctions::createNpcTypeShopLuaTable(lua_State* L, const std::vector int NpcTypeFunctions::luaNpcTypeCreate(lua_State* L) { // NpcType(name) - NpcType* npcType = g_npcs().getNpcType(getString(L, 1), true); + const auto &npcType = g_npcs().getNpcType(getString(L, 1), true); pushUserdata(L, npcType); setMetatable(L, -1, "NpcType"); return 1; @@ -45,7 +45,7 @@ int NpcTypeFunctions::luaNpcTypeCreate(lua_State* L) { int NpcTypeFunctions::luaNpcTypeIsPushable(lua_State* L) { // get: npcType:isPushable() set: npcType:isPushable(bool) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushBoolean(L, npcType->info.pushable); @@ -61,7 +61,7 @@ int NpcTypeFunctions::luaNpcTypeIsPushable(lua_State* L) { int NpcTypeFunctions::luaNpcTypeFloorChange(lua_State* L) { // get: npcType:floorChange() set: npcType:floorChange(bool) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushBoolean(L, npcType->info.floorChange); @@ -77,7 +77,7 @@ int NpcTypeFunctions::luaNpcTypeFloorChange(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCanSpawn(lua_State* L) { // monsterType:canSpawn(pos) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); const Position &position = getPosition(L, 2); if (npcType) { pushBoolean(L, npcType->canSpawn(position)); @@ -89,7 +89,7 @@ int NpcTypeFunctions::luaNpcTypeCanSpawn(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCanPushItems(lua_State* L) { // get: npcType:canPushItems() set: npcType:canPushItems(bool) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushBoolean(L, npcType->info.canPushItems); @@ -105,7 +105,7 @@ int NpcTypeFunctions::luaNpcTypeCanPushItems(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCanPushCreatures(lua_State* L) { // get: npcType:canPushCreatures() set: npcType:canPushCreatures(bool) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushBoolean(L, npcType->info.canPushCreatures); @@ -121,7 +121,7 @@ int NpcTypeFunctions::luaNpcTypeCanPushCreatures(lua_State* L) { int32_t NpcTypeFunctions::luaNpcTypeName(lua_State* L) { // get: npcType:name() set: npcType:name(name) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushString(L, npcType->name); @@ -137,7 +137,7 @@ int32_t NpcTypeFunctions::luaNpcTypeName(lua_State* L) { int NpcTypeFunctions::luaNpcTypeNameDescription(lua_State* L) { // get: npcType:nameDescription() set: npcType:nameDescription(desc) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushString(L, npcType->nameDescription); @@ -153,7 +153,7 @@ int NpcTypeFunctions::luaNpcTypeNameDescription(lua_State* L) { int NpcTypeFunctions::luaNpcTypeHealth(lua_State* L) { // get: npcType:health() set: npcType:health(health) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.health); @@ -169,7 +169,7 @@ int NpcTypeFunctions::luaNpcTypeHealth(lua_State* L) { int NpcTypeFunctions::luaNpcTypeMaxHealth(lua_State* L) { // get: npcType:maxHealth() set: npcType:maxHealth(health) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.healthMax); @@ -185,7 +185,7 @@ int NpcTypeFunctions::luaNpcTypeMaxHealth(lua_State* L) { int NpcTypeFunctions::luaNpcTypeAddShopItem(lua_State* L) { // npcType:addShopItem(shop) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -203,7 +203,7 @@ int NpcTypeFunctions::luaNpcTypeAddShopItem(lua_State* L) { int NpcTypeFunctions::luaNpcTypeAddVoice(lua_State* L) { // npcType:addVoice(sentence, interval, chance, yell) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { voiceBlock_t voice; voice.text = getString(L, 2); @@ -220,7 +220,7 @@ int NpcTypeFunctions::luaNpcTypeAddVoice(lua_State* L) { int NpcTypeFunctions::luaNpcTypeGetVoices(lua_State* L) { // npcType:getVoices() - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -239,7 +239,7 @@ int NpcTypeFunctions::luaNpcTypeGetVoices(lua_State* L) { int NpcTypeFunctions::luaNpcTypeGetCreatureEvents(lua_State* L) { // npcType:getCreatureEvents() - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -256,7 +256,7 @@ int NpcTypeFunctions::luaNpcTypeGetCreatureEvents(lua_State* L) { int NpcTypeFunctions::luaNpcTypeRegisterEvent(lua_State* L) { // npcType:registerEvent(name) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { npcType->info.scripts.push_back(getString(L, 2)); pushBoolean(L, true); @@ -275,7 +275,7 @@ int NpcTypeFunctions::luaNpcTypeEventOnCallback(lua_State* L) { // npcType:onBuyItem(callback) // npcType:onSellItem(callback) // npcType:onCheckItem(callback) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (npcType->loadCallback(&g_scripts().getScriptInterface())) { pushBoolean(L, true); @@ -290,7 +290,7 @@ int NpcTypeFunctions::luaNpcTypeEventOnCallback(lua_State* L) { int NpcTypeFunctions::luaNpcTypeEventType(lua_State* L) { // npcType:eventType(event) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { npcType->info.eventType = getNumber(L, 2); pushBoolean(L, true); @@ -302,7 +302,7 @@ int NpcTypeFunctions::luaNpcTypeEventType(lua_State* L) { int NpcTypeFunctions::luaNpcTypeOutfit(lua_State* L) { // get: npcType:outfit() set: npcType:outfit(outfit) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { pushOutfit(L, npcType->info.outfit); @@ -324,7 +324,7 @@ int NpcTypeFunctions::luaNpcTypeOutfit(lua_State* L) { int NpcTypeFunctions::luaNpcTypeBaseSpeed(lua_State* L) { // npcType:getBaseSpeed() - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.baseSpeed); @@ -340,7 +340,7 @@ int NpcTypeFunctions::luaNpcTypeBaseSpeed(lua_State* L) { int NpcTypeFunctions::luaNpcTypeWalkInterval(lua_State* L) { // get: npcType:walkInterval() set: npcType:walkInterval(interval) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.walkInterval); @@ -356,7 +356,7 @@ int NpcTypeFunctions::luaNpcTypeWalkInterval(lua_State* L) { int NpcTypeFunctions::luaNpcTypeWalkRadius(lua_State* L) { // get: npcType:walkRadius() set: npcType:walkRadius(id) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.walkRadius); @@ -372,7 +372,7 @@ int NpcTypeFunctions::luaNpcTypeWalkRadius(lua_State* L) { int NpcTypeFunctions::luaNpcTypeLight(lua_State* L) { // get: npcType:light() set: npcType:light(color, level) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1; @@ -392,7 +392,7 @@ int NpcTypeFunctions::luaNpcTypeLight(lua_State* L) { int NpcTypeFunctions::luaNpcTypeYellChance(lua_State* L) { // get: npcType:yellChance() set: npcType:yellChance(chance) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { if (lua_gettop(L) == 1) { @@ -413,7 +413,7 @@ int NpcTypeFunctions::luaNpcTypeYellChance(lua_State* L) { int NpcTypeFunctions::luaNpcTypeYellSpeedTicks(lua_State* L) { // get: npcType:yellSpeedTicks() set: npcType:yellSpeedTicks(rate) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.yellSpeedTicks); @@ -433,7 +433,7 @@ int NpcTypeFunctions::luaNpcTypeYellSpeedTicks(lua_State* L) { int NpcTypeFunctions::luaNpcTypeRespawnTypePeriod(lua_State* L) { // npcType:respawnTypePeriod() - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.respawnType.period); @@ -449,7 +449,7 @@ int NpcTypeFunctions::luaNpcTypeRespawnTypePeriod(lua_State* L) { int NpcTypeFunctions::luaNpcTypeRespawnTypeIsUnderground(lua_State* L) { // npcType:respawnTypeIsUnderground() - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (npcType) { if (lua_gettop(L) == 1) { lua_pushnumber(L, npcType->info.respawnType.underground); @@ -466,7 +466,7 @@ int NpcTypeFunctions::luaNpcTypeRespawnTypeIsUnderground(lua_State* L) { int NpcTypeFunctions::luaNpcTypeSpeechBubble(lua_State* L) { // get = npcType:speechBubble() // set = npcType:speechBubble(newSpeechBubble) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); pushBoolean(L, false); @@ -485,7 +485,7 @@ int NpcTypeFunctions::luaNpcTypeSpeechBubble(lua_State* L) { int NpcTypeFunctions::luaNpcTypeCurrency(lua_State* L) { // get = npcType:currency() // set = npcType:currency(newCurrency) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); pushBoolean(L, false); @@ -503,7 +503,7 @@ int NpcTypeFunctions::luaNpcTypeCurrency(lua_State* L) { int NpcTypeFunctions::luaNpcTypeSoundChance(lua_State* L) { // get: npcType:soundChance() set: npcType:soundChance(chance) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); pushBoolean(L, false); @@ -521,7 +521,7 @@ int NpcTypeFunctions::luaNpcTypeSoundChance(lua_State* L) { int NpcTypeFunctions::luaNpcTypeSoundSpeedTicks(lua_State* L) { // get: npcType:soundSpeedTicks() set: npcType:soundSpeedTicks(ticks) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); pushBoolean(L, false); @@ -539,7 +539,7 @@ int NpcTypeFunctions::luaNpcTypeSoundSpeedTicks(lua_State* L) { int NpcTypeFunctions::luaNpcTypeAddSound(lua_State* L) { // npcType:addSound(soundId) - NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { reportErrorFunc(getErrorDesc(LUA_ERROR_NPC_TYPE_NOT_FOUND)); pushBoolean(L, false); @@ -553,7 +553,7 @@ int NpcTypeFunctions::luaNpcTypeAddSound(lua_State* L) { int NpcTypeFunctions::luaNpcTypeGetSounds(lua_State* L) { // npcType:getSounds() - const NpcType* npcType = getUserdata(L, 1); + const auto &npcType = getUserdataShared(L, 1); if (!npcType) { lua_pushnil(L); return 1;