Skip to content

Commit

Permalink
Path viewport check, fix to blocked sight of monsters (needs review, …
Browse files Browse the repository at this point in the history
…it causes lag and memory increase)

Co-Authored-By: Nathan Heinz <NathanRHeinz@gmail.com>
  • Loading branch information
ralke23 and NRH-AA committed Apr 15, 2024
1 parent efdd636 commit d001657
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 59 deletions.
2 changes: 2 additions & 0 deletions src/configmanager.cpp
Expand Up @@ -324,6 +324,8 @@ bool ConfigManager::load()
integer[VIP_PREMIUM_LIMIT] = getGlobalNumber(L, "vipPremiumLimit", 100);
integer[DEPOT_FREE_LIMIT] = getGlobalNumber(L, "depotFreeLimit", 2000);
integer[DEPOT_PREMIUM_LIMIT] = getGlobalNumber(L, "depotPremiumLimit", 10000);
integer[PATHFINDING_INTERVAL] = getGlobalNumber(L, "pathfindingInterval", 100);
integer[PATHFINDING_DELAY] = getGlobalNumber(L, "pathfindingDelay", 200);

expStages = loadXMLStages();
if (expStages.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions src/configmanager.h
Expand Up @@ -134,6 +134,8 @@ class ConfigManager
VIP_PREMIUM_LIMIT,
DEPOT_FREE_LIMIT,
DEPOT_PREMIUM_LIMIT,
PATHFINDING_INTERVAL,
PATHFINDING_DELAY,

LAST_INTEGER_CONFIG /* this must be the last one */
};
Expand Down
65 changes: 13 additions & 52 deletions src/creature.cpp
Expand Up @@ -158,13 +158,16 @@ void Creature::onThink(uint32_t interval)

void Creature::forceUpdatePath()
{
if (attackedCreature || followCreature) {
const Position& position = attackedCreature ? attackedCreature->getPosition() : followCreature->getPosition();

if (g_game.isSightClear(getPosition(), position, true)) {
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
if (!attackedCreature && !followCreature) {
return;
}

if (lastPathUpdate - OTSYS_TIME() > 0) {
return;
}

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

void Creature::onAttacking(uint32_t interval)
Expand Down Expand Up @@ -224,23 +227,8 @@ void Creature::onWalk()
addEventWalk();
}

if (getTimeSinceLastMove() >= 500) {
const Position& position = getPosition();

if (attackedCreature) {
const Position& targetPosition = attackedCreature->getPosition();
if (Position::getDistanceX(position, targetPosition) <= Map::maxViewportX &&
Position::getDistanceY(position, targetPosition) <= Map::maxViewportY) {
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
}
}
else if (followCreature) {
const Position& targetPosition = followCreature->getPosition();
if (Position::getDistanceX(position, targetPosition) <= Map::maxViewportX &&
Position::getDistanceY(position, targetPosition) <= Map::maxViewportY) {
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
}
}
if (attackedCreature || followCreature) {
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
}
}

Expand Down Expand Up @@ -928,7 +916,7 @@ void Creature::getPathSearchParams(const Creature*, FindPathParams& fpp) const
{
fpp.fullPathSearch = !hasFollowPath;
fpp.clearSight = true;
fpp.maxSearchDist = Map::maxViewportX;
fpp.maxSearchDist = Map::maxViewportX + Map::maxViewportY;
fpp.minTargetDist = 1;
fpp.maxTargetDist = 1;
}
Expand Down Expand Up @@ -1031,43 +1019,16 @@ bool Creature::setFollowCreature(Creature* creature)
return true;
}

// Pathfinding Functions
void Creature::addFollowedByCreature(Creature* creature)
{
if (creature) {
followedByCreatures.push_back(creature);
}
}

// Pathfinding Events
void Creature::updateFollowingCreaturesPath()
{
if (followedByCreatures.empty()) {
return;
}

std::list<Creature*> newFollowedByList;
for (Creature* followedByCreature : followedByCreatures) {
if (followedByCreature == nullptr) {
continue;
}

if (!canSee(followedByCreature->getPosition())) {
continue;
}

newFollowedByList.push_back(followedByCreature);
g_dispatcher.addTask(createTask([id = followedByCreature->getID()]() { g_game.updateCreatureWalk(id); }));
}

followedByCreatures.clear();

for (Creature* newFollowCreature : newFollowedByList) {
if (newFollowCreature == nullptr) {
continue;
}

addFollowedByCreature(newFollowCreature);
for (Creature* followedByCreature : followedByCreatures) {
g_dispatcher.addTask(createTask([id = followedByCreature->getID()]() { g_game.updateCreatureWalk(id); }));
}
}

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

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

extern ConfigManager g_config;

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

Expand Down Expand Up @@ -69,9 +72,12 @@ class Tile;

static constexpr int32_t EVENT_CREATURECOUNT = 10;
static constexpr int32_t EVENT_CREATURE_THINK_INTERVAL = 1000;
static constexpr int32_t EVENT_CREATURE_PATH_INTERVAL = 200;
//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 Expand Up @@ -274,7 +280,7 @@ class Creature : virtual public Thing
virtual void onFollowCreatureComplete(const Creature*) {}

// Pathfinding functions
virtual void addFollowedByCreature(Creature * creature);
virtual void addFollowedByCreature(Creature* creature) { followedByCreatures.push_back(creature); };

// Pathfinding events
void updateFollowingCreaturesPath();
Expand Down Expand Up @@ -514,6 +520,7 @@ class Creature : virtual public Thing
std::list<Creature*> followedByCreatures;

uint64_t lastStep = 0;
int64_t lastPathUpdate = 0;
uint32_t referenceCounter = 0;
uint32_t id = 0;
uint32_t scriptEventsBitField = 0;
Expand Down
12 changes: 7 additions & 5 deletions src/map.cpp
Expand Up @@ -691,6 +691,12 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
}
}

// Don't update path. Target in not in the viewport.
if (Position::getDistanceX(startPos, targetPos) > Map::maxViewportX + 1 ||
Position::getDistanceY(startPos, targetPos) > Map::maxViewportY + 1) {
return false;
}

// Dont update path. We are on top of our target position. Let dance step decide.
if (startPos.x == targetPos.x && startPos.y == targetPos.y) {
return false;
Expand All @@ -708,7 +714,7 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
while (n) {
iterations++;

if (iterations >= 250) {
if (iterations >= 200) {
return false;
}

Expand Down Expand Up @@ -737,10 +743,6 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
continue;
}

if (Position::getDistanceX(pos, startPos) >= 20 || Position::getDistanceY(pos, startPos) >= 20) {
return false;
}

const Tile* tile;
AStarNode* neighborNode = nodes.getNodeByPosition(pos.x, pos.y);
if (neighborNode) {
Expand Down

0 comments on commit d001657

Please sign in to comment.