Skip to content

Commit

Permalink
NRA-AA fixing memory leaks
Browse files Browse the repository at this point in the history
Fixed memory leaks for NRH-AA pathfinding, specifically for new behaviour where monsters follow clear sight path from far.
  • Loading branch information
ralke23 committed Apr 15, 2024
1 parent d001657 commit 4fc0dfa
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/creature.cpp
Expand Up @@ -166,7 +166,7 @@ void Creature::forceUpdatePath()
return;
}

lastPathUpdate = OTSYS_TIME() + EVENT_CREATURE_PATH_DELAY;
lastPathUpdate = OTSYS_TIME() + g_config.getNumber(ConfigManager::PATHFINDING_DELAY);
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
}

Expand Down
6 changes: 0 additions & 6 deletions src/creature.h
Expand Up @@ -20,7 +20,6 @@
#ifndef FS_CREATURE_H_5363C04015254E298F84E6D59A139508
#define FS_CREATURE_H_5363C04015254E298F84E6D59A139508

#include "configmanager.h"
#include "map.h"
#include "position.h"
#include "condition.h"
Expand All @@ -29,8 +28,6 @@
#include "enums.h"
#include "creatureevent.h"

extern ConfigManager g_config;

using ConditionList = std::list<Condition*>;
using CreatureEventList = std::list<CreatureEvent*>;

Expand Down Expand Up @@ -75,9 +72,6 @@ static constexpr int32_t EVENT_CREATURE_THINK_INTERVAL = 1000;
//static constexpr int32_t EVENT_CREATURE_PATH_INTERVAL = 100;
static constexpr int32_t EVENT_CHECK_CREATURE_INTERVAL = (EVENT_CREATURE_THINK_INTERVAL / EVENT_CREATURECOUNT);

static int32_t EVENT_CREATURE_PATH_INTERVAL = g_config.getNumber(ConfigManager::PATHFINDING_INTERVAL);
static int32_t EVENT_CREATURE_PATH_DELAY = g_config.getNumber(ConfigManager::PATHFINDING_DELAY);

class FrozenPathingConditionCall
{
public:
Expand Down
5 changes: 3 additions & 2 deletions src/game.cpp
Expand Up @@ -73,7 +73,8 @@ void Game::start(ServiceManager* manager)
g_scheduler.addEvent(createSchedulerTask(EVENT_LIGHTINTERVAL, std::bind(&Game::checkLight, this)));
}
g_scheduler.addEvent(createSchedulerTask(EVENT_CREATURE_THINK_INTERVAL, std::bind(&Game::checkCreatures, this, 0)));
g_scheduler.addEvent(createSchedulerTask(EVENT_CREATURE_PATH_INTERVAL, [this]() { updateCreaturesPath(0); }));
g_scheduler.addEvent(createSchedulerTask(g_config.getNumber(ConfigManager::PATHFINDING_INTERVAL),
[this]() { updateCreaturesPath(0); }));
g_scheduler.addEvent(createSchedulerTask(EVENT_DECAYINTERVAL, std::bind(&Game::checkDecay, this)));
}

Expand Down Expand Up @@ -3600,7 +3601,7 @@ void Game::checkCreatures(size_t index)

void Game::updateCreaturesPath(size_t index)
{
g_scheduler.addEvent(createSchedulerTask(EVENT_CREATURE_PATH_INTERVAL, [=]() { updateCreaturesPath((index + 1) % EVENT_CREATURECOUNT); }));
g_scheduler.addEvent(createSchedulerTask(g_config.getNumber(ConfigManager::PATHFINDING_INTERVAL), [=]() { updateCreaturesPath((index + 1) % EVENT_CREATURECOUNT); }));

auto& checkCreatureList = checkCreatureLists[index];
auto it = checkCreatureList.begin(), end = checkCreatureList.end();
Expand Down
7 changes: 5 additions & 2 deletions src/map.cpp
Expand Up @@ -722,6 +722,7 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
const int_fast32_t y = n->y;
pos.x = x;
pos.y = y;
// Will be removed on future
if (pathCondition(startPos, pos, fpp, bestMatch)) {
found = n;
endPos = pos;
Expand All @@ -735,8 +736,8 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
pos.y = y + allNeighbors[i][1];

if (fpp.maxSearchDist != 0 &&
(Position::getDistanceX(startPos, pos) > fpp.maxSearchDist || Position::getDistanceY(startPos, pos) > fpp.maxSearchDist)) {
continue;
(Position::getDistanceX(startPos, pos) > fpp.maxSearchDist + 1 || Position::getDistanceY(startPos, pos) > fpp.maxSearchDist + 1)) {
break;
}

if (fpp.keepDistance && !pathCondition.isInRange(startPos, pos, fpp)) {
Expand Down Expand Up @@ -779,6 +780,8 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
n = nodes.getBestNode();
}

nodes.clear();

if (!found) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/map.h
Expand Up @@ -60,6 +60,7 @@ class AStarNodes

AStarNode* getBestNode();
AStarNode* getNodeByPosition(uint16_t x, uint16_t y) { return nodeMap[x][y]; };
void clear() { nodes.clear(); nodeMap.clear(); };

static double calculateHeuristic(const Position& p1, const Position& p2);
static double getMapWalkCost(AStarNode* node, const Position& neighborPos);
Expand Down

0 comments on commit 4fc0dfa

Please sign in to comment.