Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check creatures crash #3168

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

#include <appearances.pb.h>

std::vector<std::shared_ptr<Creature>> checkCreatureLists[EVENT_CREATURECOUNT];
std::vector<std::weak_ptr<Creature>> checkCreatureLists[EVENT_CREATURECOUNT];

namespace InternalGame {
void sendBlockEffect(BlockType_t blockType, CombatType_t combatType, const Position &targetPos, const std::shared_ptr<Creature> &source) {
Expand Down Expand Up @@ -6461,16 +6461,15 @@ void Game::addCreatureCheck(const std::shared_ptr<Creature> &creature) {

creature->creatureCheck.store(true);

if (creature->inCheckCreaturesVector.load()) {
if (creature->inCheckCreaturesVector.exchange(true)) {
// already in a vector
return;
}

creature->inCheckCreaturesVector.store(true);

creature->safeCall([this, creature] {
checkCreatureLists[uniform_random(0, EVENT_CREATURECOUNT - 1)].emplace_back(creature);
});
g_dispatcher().addEvent([this, index = uniform_random(0, EVENT_CREATURECOUNT - 1), creature] {
checkCreatureLists[index].emplace_back(creature);
},
"Game::addCreatureCheck");
}

void Game::removeCreatureCheck(const std::shared_ptr<Creature> &creature) {
Expand All @@ -6484,15 +6483,18 @@ void Game::checkCreatures() {
metrics::method_latency measure(__METRICS_METHOD_NAME__);
static size_t index = 0;

std::erase_if(checkCreatureLists[index], [this](const std::shared_ptr<Creature> creature) {
if (creature->creatureCheck && creature->isAlive()) {
creature->onThink(EVENT_CREATURE_THINK_INTERVAL);
creature->onAttacking(EVENT_CREATURE_THINK_INTERVAL);
creature->executeConditions(EVENT_CREATURE_THINK_INTERVAL);
return false;
std::erase_if(checkCreatureLists[index], [this](const std::weak_ptr<Creature> &weak) {
if (const auto creature = weak.lock()) {
if (creature->creatureCheck && creature->isAlive()) {
creature->onThink(EVENT_CREATURE_THINK_INTERVAL);
creature->onAttacking(EVENT_CREATURE_THINK_INTERVAL);
creature->executeConditions(EVENT_CREATURE_THINK_INTERVAL);
return false;
}

creature->inCheckCreaturesVector = false;
}

creature->inCheckCreaturesVector = false;
return true;
});

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 @@ -75,6 +75,7 @@ class Task {
"Game::createInfluencedMonsters",
"Game::updateCreatureWalk",
"Game::updateForgeableMonsters",
"Game::addCreatureCheck",
"GlobalEvents::think",
"LuaEnvironment::executeTimerEvent",
"Modules::executeOnRecvbyte",
Expand Down
Loading