From 1bd29b48d4caaea2e1f810c7860caa03257c1a57 Mon Sep 17 00:00:00 2001 From: Ramon Bernardo Date: Thu, 14 Oct 2021 23:42:41 -0300 Subject: [PATCH 1/4] Improve Npc::getRandomStep cache - remove unnecessary heap allocation. --- src/npc.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/npc.cpp b/src/npc.cpp index 3d2900994c..3005136316 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -478,31 +478,31 @@ bool Npc::canWalkTo(const Position& fromPos, Direction dir) const bool Npc::getRandomStep(Direction& dir) const { - std::vector dirList; - const Position& creaturePos = getPosition(); + std::array dirList; + size_t directions = static_cast(-1); + const Position& creaturePos = getPosition(); if (canWalkTo(creaturePos, DIRECTION_NORTH)) { - dirList.push_back(DIRECTION_NORTH); + dirList[++directions] = DIRECTION_NORTH; } if (canWalkTo(creaturePos, DIRECTION_SOUTH)) { - dirList.push_back(DIRECTION_SOUTH); + dirList[++directions] = DIRECTION_SOUTH; } if (canWalkTo(creaturePos, DIRECTION_EAST)) { - dirList.push_back(DIRECTION_EAST); + dirList[++directions] = DIRECTION_EAST; } if (canWalkTo(creaturePos, DIRECTION_WEST)) { - dirList.push_back(DIRECTION_WEST); + dirList[++directions] = DIRECTION_WEST; } - if (dirList.empty()) { - return false; + if (directions <= 4) { + dir = dirList[uniform_random(0, directions)]; + return true; } - - dir = dirList[uniform_random(0, dirList.size() - 1)]; - return true; + return false; } bool Npc::doMoveTo(const Position& pos, int32_t minTargetDist/* = 1*/, int32_t maxTargetDist/* = 1*/, From 99249865d8f90ac49be61da684462871c00c3d09 Mon Sep 17 00:00:00 2001 From: Ramon Bernardo Date: Sun, 17 Oct 2021 04:08:45 -0300 Subject: [PATCH 2/4] dir comparison --- src/npc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/npc.cpp b/src/npc.cpp index 3005136316..79e0ff7869 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -498,7 +498,7 @@ bool Npc::getRandomStep(Direction& dir) const dirList[++directions] = DIRECTION_WEST; } - if (directions <= 4) { + if (directions != -1) { dir = dirList[uniform_random(0, directions)]; return true; } From 0a3171ec760cbb146814f842469771f15a7ce6b2 Mon Sep 17 00:00:00 2001 From: Ramon Bernardo Date: Thu, 28 Oct 2021 09:20:06 -0300 Subject: [PATCH 3/4] Revert previous changes --- src/npc.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/npc.cpp b/src/npc.cpp index 79e0ff7869..93ca3858d7 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -478,31 +478,32 @@ bool Npc::canWalkTo(const Position& fromPos, Direction dir) const bool Npc::getRandomStep(Direction& dir) const { - std::array dirList; - size_t directions = static_cast(-1); + std::vector dirList; + const Position& creaturePos = getPosition(); const Position& creaturePos = getPosition(); if (canWalkTo(creaturePos, DIRECTION_NORTH)) { - dirList[++directions] = DIRECTION_NORTH; + dirList.push_back(DIRECTION_NORTH); } if (canWalkTo(creaturePos, DIRECTION_SOUTH)) { - dirList[++directions] = DIRECTION_SOUTH; + dirList.push_back(DIRECTION_SOUTH); } if (canWalkTo(creaturePos, DIRECTION_EAST)) { - dirList[++directions] = DIRECTION_EAST; + dirList.push_back(DIRECTION_EAST); } if (canWalkTo(creaturePos, DIRECTION_WEST)) { - dirList[++directions] = DIRECTION_WEST; + dirList.push_back(DIRECTION_WEST); } - if (directions != -1) { - dir = dirList[uniform_random(0, directions)]; - return true; + if (dirList.empty()) { + return false; } - return false; + + dir = dirList[uniform_random(0, dirList.size() - 1)]; + return true; } bool Npc::doMoveTo(const Position& pos, int32_t minTargetDist/* = 1*/, int32_t maxTargetDist/* = 1*/, From 26619da36dcabef7c37e7c3b7169ab21a56af933 Mon Sep 17 00:00:00 2001 From: Ramon Bernardo Date: Thu, 28 Oct 2021 09:21:24 -0300 Subject: [PATCH 4/4] Use reserve in dirList vec --- src/npc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/npc.cpp b/src/npc.cpp index 93ca3858d7..0479821f93 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -479,7 +479,7 @@ bool Npc::canWalkTo(const Position& fromPos, Direction dir) const bool Npc::getRandomStep(Direction& dir) const { std::vector dirList; - const Position& creaturePos = getPosition(); + dirList.reserve(4); const Position& creaturePos = getPosition(); if (canWalkTo(creaturePos, DIRECTION_NORTH)) {