From 20bd1491a784b19dba1b2f22cb473555a2ebbfea Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 12 Apr 2025 17:38:51 +0200 Subject: [PATCH] Make sure distance and duration are not negative --- apps/openmw/mwmechanics/aiwander.cpp | 19 ++++++++----------- apps/openmw/mwmechanics/aiwander.hpp | 4 ++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/apps/openmw/mwmechanics/aiwander.cpp b/apps/openmw/mwmechanics/aiwander.cpp index 447140d8e3..3dacbd167e 100644 --- a/apps/openmw/mwmechanics/aiwander.cpp +++ b/apps/openmw/mwmechanics/aiwander.cpp @@ -46,10 +46,10 @@ namespace MWMechanics constexpr float idlePositionCheckInterval = 1.5f; // to prevent overcrowding - constexpr int destinationTolerance = 64; + constexpr unsigned destinationTolerance = 64; // distance must be long enough that NPC will need to move to get there. - constexpr int minimumWanderDistance = destinationTolerance * 2; + constexpr unsigned minimumWanderDistance = destinationTolerance * 2; constexpr std::size_t maxIdleSize = 8; @@ -128,8 +128,8 @@ namespace MWMechanics AiWander::AiWander(int distance, int duration, int timeOfDay, const std::vector& idle, bool repeat) : TypedAiPackage(repeat) - , mDistance(std::max(0, distance)) - , mDuration(std::max(0, duration)) + , mDistance(static_cast(std::max(0, distance))) + , mDuration(static_cast(std::max(0, duration))) , mRemainingDuration(duration) , mTimeOfDay(timeOfDay) , mIdle(getInitialIdle(idle)) @@ -259,9 +259,6 @@ namespace MWMechanics bool AiWander::reactionTimeActions(const MWWorld::Ptr& actor, AiWanderStorage& storage, ESM::Position& pos) { - if (mDistance <= 0) - storage.mCanWanderAlongPathGrid = false; - if (isPackageCompleted()) { stopWalking(actor); @@ -915,10 +912,10 @@ namespace MWMechanics float length = delta.length(); delta.normalize(); - int distance = std::max(mDistance / 2, minimumWanderDistance); + unsigned distance = std::max(mDistance / 2, minimumWanderDistance); // must not travel longer than distance between waypoints or NPC goes past waypoint - distance = std::min(distance, static_cast(length)); + distance = std::min(distance, static_cast(length)); delta *= distance; storage.mAllowedNodes.push_back(PathFinder::makePathgridPoint(vectorStart + delta)); } @@ -970,8 +967,8 @@ namespace MWMechanics AiWander::AiWander(const ESM::AiSequence::AiWander* wander) : TypedAiPackage(makeDefaultOptions().withRepeat(wander->mData.mShouldRepeat != 0)) - , mDistance(std::max(static_cast(0), wander->mData.mDistance)) - , mDuration(std::max(static_cast(0), wander->mData.mDuration)) + , mDistance(static_cast(std::max(static_cast(0), wander->mData.mDistance))) + , mDuration(static_cast(std::max(static_cast(0), wander->mData.mDuration))) , mRemainingDuration(wander->mDurationData.mRemainingDuration) , mTimeOfDay(wander->mData.mTimeOfDay) , mIdle(getInitialIdle(wander->mData.mIdle)) diff --git a/apps/openmw/mwmechanics/aiwander.hpp b/apps/openmw/mwmechanics/aiwander.hpp index f08980ad29..a1474bce5f 100644 --- a/apps/openmw/mwmechanics/aiwander.hpp +++ b/apps/openmw/mwmechanics/aiwander.hpp @@ -147,8 +147,8 @@ namespace MWMechanics void completeManualWalking(const MWWorld::Ptr& actor, AiWanderStorage& storage); bool isNearAllowedNode(const MWWorld::Ptr& actor, const AiWanderStorage& storage, float distance) const; - const int mDistance; // how far the actor can wander from the spawn point - const int mDuration; + const unsigned mDistance; // how far the actor can wander from the spawn point + const unsigned mDuration; float mRemainingDuration; const int mTimeOfDay; const std::vector mIdle;