Skip to content

Commit

Permalink
fix: map safety on spawn_monster
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Nov 20, 2023
1 parent dbb33d2 commit b324903
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
42 changes: 24 additions & 18 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ void SpawnMonster::startSpawnMonsterCheck() {
}

SpawnMonster::~SpawnMonster() {
for (const auto &it : spawnedMonsterMap) {
std::shared_ptr<Monster> monster = it.second;
for (const auto &[_, monster] : spawnedMonsterMap) {

Check warning on line 159 in src/creatures/monsters/spawns/spawn_monster.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/creatures/monsters/spawns/spawn_monster.cpp#L159

Unused variable: _
Raw output
src/creatures/monsters/spawns/spawn_monster.cpp:159:Unused variable: _
monster->setSpawnMonster(nullptr);
}
}
Expand All @@ -177,6 +176,9 @@ bool SpawnMonster::isInSpawnMonsterZone(const Position &pos) {
}

bool SpawnMonster::spawnMonster(uint32_t spawnMonsterId, const std::shared_ptr<MonsterType> monsterType, const Position &pos, Direction dir, bool startup /*= false*/) {
if (spawnedMonsterMap.contains(spawnMonsterId)) {
return false;
}
auto monster = std::make_shared<Monster>(monsterType);
if (startup) {
// No need to send out events to the surrounding since there is no one out there to listen!
Expand All @@ -193,14 +195,14 @@ bool SpawnMonster::spawnMonster(uint32_t spawnMonsterId, const std::shared_ptr<M
monster->setSpawnMonster(this);
monster->setMasterPos(pos);

spawnedMonsterMap.insert(spawned_pair(spawnMonsterId, monster));
spawnedMonsterMap[spawnMonsterId] = monster;
spawnMonsterMap[spawnMonsterId].lastSpawn = OTSYS_TIME();
g_events().eventMonsterOnSpawn(monster, pos);
g_callbacks().executeCallback(EventCallback_t::monsterOnSpawn, &EventCallback::monsterOnSpawn, monster, pos);
return true;
}

void SpawnMonster::startup() {
void SpawnMonster::startup(bool delayed) {
if (g_configManager().getBoolean(RANDOM_MONSTER_SPAWN)) {
for (auto it = spawnMonsterMap.begin(); it != spawnMonsterMap.end(); ++it) {
auto &[spawnMonsterId, sb] = *it;

Check warning on line 208 in src/creatures/monsters/spawns/spawn_monster.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/creatures/monsters/spawns/spawn_monster.cpp#L208

Variable 'spawnMonsterId' is not assigned a value.
Raw output
src/creatures/monsters/spawns/spawn_monster.cpp:208:Variable 'spawnMonsterId' is not assigned a value.
Expand Down Expand Up @@ -230,7 +232,11 @@ void SpawnMonster::startup() {
if (!mType) {
continue;
}
spawnMonster(spawnMonsterId, mType, sb.pos, sb.direction, true);
if (delayed) {
g_dispatcher().addEvent(std::bind(&SpawnMonster::scheduleSpawn, this, spawnMonsterId, sb, mType, 0), "SpawnMonster::startup");
} else {
scheduleSpawn(spawnMonsterId, sb, mType, 0);
}
}
}

Expand All @@ -243,13 +249,11 @@ void SpawnMonster::checkSpawnMonster() {
cleanup();
uint32_t spawnMonsterCount = 0;

for (auto &it : spawnMonsterMap) {
uint32_t spawnMonsterId = it.first;
if (spawnedMonsterMap.find(spawnMonsterId) != spawnedMonsterMap.end()) {
for (auto &[spawnMonsterId, sb] : spawnMonsterMap) {
if (spawnedMonsterMap.contains(spawnMonsterId)) {
continue;
}

spawnBlock_t &sb = it.second;
const auto &mType = sb.getMonsterType();
if (!mType) {
continue;
Expand Down Expand Up @@ -292,15 +296,15 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
}

void SpawnMonster::cleanup() {
auto it = spawnedMonsterMap.begin();
while (it != spawnedMonsterMap.end()) {
auto monster = it->second;
std::vector<uint32_t> removeList;
for (const auto &[spawnMonsterId, monster] : spawnedMonsterMap) {
if (monster == nullptr || monster->isRemoved()) {
it = spawnedMonsterMap.erase(it);
} else {
++it;
removeList.push_back(spawnMonsterId);
}
}
for (const auto &spawnMonsterId : removeList) {
spawnedMonsterMap.erase(spawnMonsterId);
}
}

bool SpawnMonster::addMonster(const std::string &name, const Position &pos, Direction dir, uint32_t scheduleInterval, uint32_t weight /*= 1*/) {
Expand Down Expand Up @@ -354,12 +358,14 @@ bool SpawnMonster::addMonster(const std::string &name, const Position &pos, Dire
}

void SpawnMonster::removeMonster(std::shared_ptr<Monster> monster) {
for (auto it = spawnedMonsterMap.begin(), end = spawnedMonsterMap.end(); it != end; ++it) {
if (it->second == monster) {
spawnedMonsterMap.erase(it);
uint32_t spawnMonsterId = 0;
for (const auto &[id, m] : spawnedMonsterMap) {

Check warning on line 362 in src/creatures/monsters/spawns/spawn_monster.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/creatures/monsters/spawns/spawn_monster.cpp#L362

Variable 'id' is not assigned a value.
Raw output
src/creatures/monsters/spawns/spawn_monster.cpp:362:Variable 'id' is not assigned a value.
if (m == monster) {
spawnMonsterId = id;
break;
}
}
spawnedMonsterMap.erase(spawnMonsterId);
}

void SpawnMonster::setMonsterVariant(const std::string &variant) {
Expand Down
8 changes: 3 additions & 5 deletions src/creatures/monsters/spawns/spawn_monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SpawnMonster {
uint32_t getInterval() const {
return interval;
}
void startup();
void startup(bool delayed = false);

void startSpawnMonsterCheck();
void stopEvent();
Expand All @@ -58,12 +58,10 @@ class SpawnMonster {

private:
// map of the spawned creatures
using SpawnedMap = std::multimap<uint32_t, std::shared_ptr<Monster>>;
using spawned_pair = SpawnedMap::value_type;
SpawnedMap spawnedMonsterMap;
phmap::parallel_flat_hash_map_m<uint32_t, std::shared_ptr<Monster>> spawnedMonsterMap;

// map of creatures in the spawn
std::map<uint32_t, spawnBlock_t> spawnMonsterMap;
phmap::parallel_flat_hash_map_m<uint32_t, spawnBlock_t> spawnMonsterMap;

Position centerPos;
int32_t radius;
Expand Down
1 change: 1 addition & 0 deletions src/game/scheduling/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Task {
"Raids::checkRaids",
"SpawnMonster::checkSpawnMonster",
"SpawnMonster::scheduleSpawn",
"SpawnMonster::startup",
"SpawnNpc::checkSpawnNpc",
"Webhook::run",
"Protocol::sendRecvMessageCallback",
Expand Down

0 comments on commit b324903

Please sign in to comment.