From f7a66153792c77e65b0c92934d064c5124cbaf6f Mon Sep 17 00:00:00 2001 From: dongresource Date: Thu, 17 Dec 2020 01:59:55 +0100 Subject: [PATCH] Tweak mob roaming logic and a few other values This fixes the crash with mobs with a very small m_iIdleRange and avoids unnecessary looping. Co-authored-by: JadeShrineMaiden <69916714+JadeShrineMaiden@users.noreply.github.com> --- src/MobManager.cpp | 27 ++++++++++++++++----------- src/MobManager.hpp | 2 +- src/PlayerManager.cpp | 2 +- suppr.txt | 1 + tdata | 2 +- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/MobManager.cpp b/src/MobManager.cpp index 6a1ca23..0dafefb 100644 --- a/src/MobManager.cpp +++ b/src/MobManager.cpp @@ -661,19 +661,24 @@ void MobManager::roamingStep(Mob *mob, time_t currTime) { if (mob->idleRange == 0 || speed == 0) return; - int farX, farY; - int distance; // for short walk detection + int farX, farY, distance; + int minDistance = mob->idleRange / 2; - /* - * We don't want the mob to just take one step and stop, so we make sure - * it has walked a half-decent distance. - */ - do { - farX = xStart + rand() % mob->idleRange; - farY = yStart + rand() % mob->idleRange; + // pick a random destination + farX = xStart + rand() % mob->idleRange; + farY = yStart + rand() % mob->idleRange; - distance = std::hypot(mob->appearanceData.iX - farX, mob->appearanceData.iY - farY); - } while (distance < 500); + distance = std::abs(std::max(farX - mob->appearanceData.iX, farY - mob->appearanceData.iY)); + if (distance == 0) + distance += 1; // hack to avoid FPE + + // if it's too short a walk, go further in that direction + farX = mob->appearanceData.iX + (farX - mob->appearanceData.iX) * minDistance / distance; + farY = mob->appearanceData.iY + (farY - mob->appearanceData.iY) * minDistance / distance; + + // but don't got out of bounds + farX = std::clamp(farX, xStart, xStart + mob->idleRange); + farY = std::clamp(farY, yStart, yStart + mob->idleRange); // halve movement speed if snared if (mob->appearanceData.iConditionBitFlag & CSB_BIT_DN_MOVE_SPEED) diff --git a/src/MobManager.hpp b/src/MobManager.hpp index 32fca0c..7851274 100644 --- a/src/MobManager.hpp +++ b/src/MobManager.hpp @@ -70,7 +70,7 @@ struct Mob : public BaseNPC { data = d; regenTime = data["m_iRegenTime"]; - idleRange = (int)data["m_iIdleRange"] * 2; // TODO: tuning? + idleRange = (int)data["m_iIdleRange"]; dropType = data["m_iDropType"]; level = data["m_iNpcLevel"]; diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 55eeea7..1d3224d 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -908,7 +908,7 @@ std::string PlayerManager::getPlayerName(Player *plr, bool id) { return "NOT IN GAME"; std::string ret = ""; - if (id && plr->accountLevel <= 10) + if (id && plr->accountLevel <= 30) ret += "(GM) "; ret += U16toU8(plr->PCStyle.szFirstName) + " " + U16toU8(plr->PCStyle.szLastName); diff --git a/suppr.txt b/suppr.txt index ede1afc..5a0c332 100644 --- a/suppr.txt +++ b/suppr.txt @@ -1,4 +1,5 @@ leak:TableData::init +leak:ChunkManager::newChunk leak:NPCManager::updateNPCPosition leak:NPCManager::summonNPC leak:NanoManager::doBuff diff --git a/tdata b/tdata index daba8a8..a36aca3 160000 --- a/tdata +++ b/tdata @@ -1 +1 @@ -Subproject commit daba8a8a622ab9f2f63a72a66c243a49ea926088 +Subproject commit a36aca30c9ff69b39a1b128227a6e69c77e3c00e