From 120f57ac3b62eeadce9e060f7a37b5ec649e1150 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Tue, 14 Jan 2025 16:25:42 -0300 Subject: [PATCH 1/2] fix: update monster name on "Monster:setType" --- src/game/game.cpp | 13 +++++++++++++ src/game/game.hpp | 1 + .../creatures/monster/monster_functions.cpp | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/src/game/game.cpp b/src/game/game.cpp index 6f659e92916..97d0b066b9b 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -10066,6 +10066,19 @@ void Game::removeMonster(const std::shared_ptr &monster) { } } +void Game::updateMonster(const std::shared_ptr &monster, const std::string &oldName, uint32_t oldId) { + if (!monster) { + return; + } + + const auto &newName = monster->getLowerName(); + monstersNameIndex.erase(oldName); + monstersNameIndex[newName] = monsters.size() - 1; + + monstersIdIndex.erase(oldId); + monstersIdIndex[monster->getID()] = monsters.size() - 1; +} + std::shared_ptr Game::getGuild(uint32_t id, bool allowOffline /* = flase */) const { auto it = guilds.find(id); if (it == guilds.end()) { diff --git a/src/game/game.hpp b/src/game/game.hpp index 9a4b59f8a55..f8e6400630d 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -541,6 +541,7 @@ class Game { void addMonster(const std::shared_ptr &monster); void removeMonster(const std::shared_ptr &monster); + void updateMonster(const std::shared_ptr &monster, const std::string &oldName, uint32_t oldId); std::shared_ptr getGuild(uint32_t id, bool allowOffline = false) const; std::shared_ptr getGuildByName(const std::string &name, bool allowOffline = false) const; diff --git a/src/lua/functions/creatures/monster/monster_functions.cpp b/src/lua/functions/creatures/monster/monster_functions.cpp index 3f5d0c25ea8..e174edf031b 100644 --- a/src/lua/functions/creatures/monster/monster_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_functions.cpp @@ -138,6 +138,12 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) { g_logger().warn("[Warning - MonsterFunctions::luaMonsterSetType] Unknown event name: {}", scriptName); } } + + // Update created monster + const std::string oldName = monster->getLowerName(); + uint32_t oldId = monster->getID(); + g_game().updateMonster(monster, oldName, oldId); + // Assign new MonsterType monster->mType = mType; monster->nameDescription = asLowerCaseString(mType->nameDescription); From 8bc43a949e384b8303549fb6dcc4296caa909a7d Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Sun, 19 Jan 2025 14:34:08 -0300 Subject: [PATCH 2/2] fix: monster update --- src/game/game.cpp | 18 ++++++++++++------ src/game/game.hpp | 2 +- .../creatures/monster/monster_functions.cpp | 4 +--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/game/game.cpp b/src/game/game.cpp index 97d0b066b9b..b03256b33ec 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -10066,17 +10066,23 @@ void Game::removeMonster(const std::shared_ptr &monster) { } } -void Game::updateMonster(const std::shared_ptr &monster, const std::string &oldName, uint32_t oldId) { +void Game::updateMonster(const std::shared_ptr &monster, const std::shared_ptr &monsterType) { if (!monster) { return; } - const auto &newName = monster->getLowerName(); - monstersNameIndex.erase(oldName); - monstersNameIndex[newName] = monsters.size() - 1; + const auto &oldName = monster->getLowerName(); + const auto &newName = monsterType->name; - monstersIdIndex.erase(oldId); - monstersIdIndex[monster->getID()] = monsters.size() - 1; + auto it = monstersNameIndex.find(oldName); + if (it == monstersNameIndex.end()) { + return; + } + + size_t index = it->second; + + monstersNameIndex.erase(it); + monstersNameIndex[newName] = index; } std::shared_ptr Game::getGuild(uint32_t id, bool allowOffline /* = flase */) const { diff --git a/src/game/game.hpp b/src/game/game.hpp index f8e6400630d..cc184380dbe 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -541,7 +541,7 @@ class Game { void addMonster(const std::shared_ptr &monster); void removeMonster(const std::shared_ptr &monster); - void updateMonster(const std::shared_ptr &monster, const std::string &oldName, uint32_t oldId); + void updateMonster(const std::shared_ptr &monster, const std::shared_ptr &monsterType); std::shared_ptr getGuild(uint32_t id, bool allowOffline = false) const; std::shared_ptr getGuildByName(const std::string &name, bool allowOffline = false) const; diff --git a/src/lua/functions/creatures/monster/monster_functions.cpp b/src/lua/functions/creatures/monster/monster_functions.cpp index e174edf031b..6485759b5f3 100644 --- a/src/lua/functions/creatures/monster/monster_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_functions.cpp @@ -140,9 +140,7 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) { } // Update created monster - const std::string oldName = monster->getLowerName(); - uint32_t oldId = monster->getID(); - g_game().updateMonster(monster, oldName, oldId); + g_game().updateMonster(monster, mType); // Assign new MonsterType monster->mType = mType;