diff --git a/src/creatures/monsters/spawns/spawn_monster.cpp b/src/creatures/monsters/spawns/spawn_monster.cpp index db4cba34b..199ae3f97 100644 --- a/src/creatures/monsters/spawns/spawn_monster.cpp +++ b/src/creatures/monsters/spawns/spawn_monster.cpp @@ -129,10 +129,18 @@ bool SpawnsMonster::loadFromXML(const std::string &filemonstername) { } void SpawnsMonster::startup() { - if (!isLoaded() || isStarted()) { + if (!isLoaded()) { return; } + if (isStarted()) { + for (const auto &spawnMonster : spawnMonsterList) { + spawnMonster->stopEvent(); + spawnMonster->removeMonsters(); + } + started = false; + } + for (const auto &spawnMonster : spawnMonsterList) { spawnMonster->startup(); } @@ -143,6 +151,7 @@ void SpawnsMonster::startup() { void SpawnsMonster::clear() { for (const auto &spawnMonster : spawnMonsterList) { spawnMonster->stopEvent(); + spawnMonster->removeMonsters(); } spawnMonsterList.clear(); @@ -181,6 +190,7 @@ void SpawnMonster::startSpawnMonsterCheck() { SpawnMonster::~SpawnMonster() { stopEvent(); + removeMonsters(); } // moveable @@ -336,6 +346,9 @@ void SpawnMonster::cleanup() { for (auto it = spawnedMonsterMap.begin(); it != spawnedMonsterMap.end();) { const auto &monster = it->second; if (!monster || monster->isRemoved()) { + if (monster) { + monster->setSpawnMonster(nullptr); + } auto spawnIt = spawnMonsterMap.find(it->first); if (spawnIt != spawnMonsterMap.end()) { spawnIt->second.lastSpawn = OTSYS_TIME(); @@ -425,10 +438,20 @@ void SpawnMonster::removeMonster(const std::shared_ptr &monster) { break; } } - spawnedMonsterMap.erase(spawnMonsterId); + if (spawnMonsterId != 0) { + if (monster) { + monster->setSpawnMonster(nullptr); + } + spawnedMonsterMap.erase(spawnMonsterId); + } } void SpawnMonster::removeMonsters() { + for (auto &[id, monster] : spawnedMonsterMap) { + if (monster) { + monster->setSpawnMonster(nullptr); + } + } spawnMonsterMap.clear(); spawnedMonsterMap.clear(); }