diff --git a/src/game/game.cpp b/src/game/game.cpp index 6f659e92916..b03256b33ec 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -10066,6 +10066,25 @@ void Game::removeMonster(const std::shared_ptr &monster) { } } +void Game::updateMonster(const std::shared_ptr &monster, const std::shared_ptr &monsterType) { + if (!monster) { + return; + } + + const auto &oldName = monster->getLowerName(); + const auto &newName = monsterType->name; + + 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 { auto it = guilds.find(id); if (it == guilds.end()) { diff --git a/src/game/game.hpp b/src/game/game.hpp index 9a4b59f8a55..cc184380dbe 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::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 3f5d0c25ea8..6485759b5f3 100644 --- a/src/lua/functions/creatures/monster/monster_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_functions.cpp @@ -138,6 +138,10 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) { g_logger().warn("[Warning - MonsterFunctions::luaMonsterSetType] Unknown event name: {}", scriptName); } } + + // Update created monster + g_game().updateMonster(monster, mType); + // Assign new MonsterType monster->mType = mType; monster->nameDescription = asLowerCaseString(mType->nameDescription);