Skip to content

Commit

Permalink
fix: crash in spawn_monster
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Nov 20, 2023
1 parent d794102 commit dbb33d2
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,22 @@ void SpawnMonster::startup() {
}
}
}
checkSpawnMonster();
for (auto &[spawnMonsterId, sb] : spawnMonsterMap) {
const auto &mType = sb.getMonsterType();
if (!mType) {
continue;
}
spawnMonster(spawnMonsterId, mType, sb.pos, sb.direction, true);
}
}

void SpawnMonster::checkSpawnMonster() {
checkSpawnMonsterEvent = 0;
if (checkSpawnMonsterEvent == 0) {
return;
}

checkSpawnMonsterEvent = 0;
cleanup();

uint32_t spawnMonsterCount = 0;

for (auto &it : spawnMonsterMap) {
Expand Down Expand Up @@ -286,10 +294,8 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
void SpawnMonster::cleanup() {
auto it = spawnedMonsterMap.begin();
while (it != spawnedMonsterMap.end()) {
uint32_t spawnMonsterId = it->first;
std::shared_ptr<Monster> monster = it->second;
if (!monster || monster->isRemoved()) {
spawnMonsterMap[spawnMonsterId].lastSpawn = OTSYS_TIME();
auto monster = it->second;
if (monster == nullptr || monster->isRemoved()) {
it = spawnedMonsterMap.erase(it);
} else {
++it;
Expand Down Expand Up @@ -381,6 +387,9 @@ std::shared_ptr<MonsterType> spawnBlock_t::getMonsterType() const {
}
uint32_t totalWeight = 0;
for (const auto &[mType, weight] : monsterTypes) {
if (!mType) {
continue;
}
if (mType->isBoss()) {
if (monsterTypes.size() > 1) {
g_logger().warn("[SpawnMonster] Boss monster {} has been added to spawn block with other monsters. This is not allowed.", mType->name);
Expand Down

0 comments on commit dbb33d2

Please sign in to comment.