1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-03 23:45:33 +00:00

replaced multiple booleans with single state variable.

This commit is contained in:
dteviot 2015-07-19 18:02:13 +12:00
parent 1ed6e95c07
commit 22f49128cc
2 changed files with 29 additions and 43 deletions

View file

@ -64,10 +64,7 @@ namespace MWMechanics
const MWWorld::CellStore* mCell; // for detecting cell change const MWWorld::CellStore* mCell; // for detecting cell change
// AiWander states // AiWander states
bool mChooseAction; AiWander::WanderState mState;
bool mIdleNow;
bool mMoveNow;
bool mWalking;
unsigned short mPlayedIdle; unsigned short mPlayedIdle;
@ -80,10 +77,7 @@ namespace MWMechanics
mSaidGreeting(AiWander::Greet_None), mSaidGreeting(AiWander::Greet_None),
mGreetingTimer(0), mGreetingTimer(0),
mCell(NULL), mCell(NULL),
mChooseAction(true), mState(AiWander::Wander_ChooseAction),
mIdleNow(false),
mMoveNow(false),
mWalking(false),
mPlayedIdle(0) mPlayedIdle(0)
{}; {};
}; };
@ -200,38 +194,33 @@ namespace MWMechanics
ESM::Position pos = actor.getRefData().getPosition(); ESM::Position pos = actor.getRefData().getPosition();
bool& idleNow = storage.mIdleNow; WanderState& wanderState = storage.mState;
bool& moveNow = storage.mMoveNow;
bool& walking = storage.mWalking;
// Check if an idle actor is too close to a door - if so start walking // Check if an idle actor is too close to a door - if so start walking
mDoorCheckDuration += duration; mDoorCheckDuration += duration;
if(mDoorCheckDuration >= DOOR_CHECK_INTERVAL) if(mDoorCheckDuration >= DOOR_CHECK_INTERVAL)
{ {
mDoorCheckDuration = 0; // restart timer mDoorCheckDuration = 0; // restart timer
if(mDistance && // actor is not intended to be stationary if(mDistance && // actor is not intended to be stationary
idleNow && // but is in idle (wanderState == Wander_IdleNow) && // but is in idle
!walking && // FIXME: some actors are idle while walking
proximityToDoor(actor, MIN_DIST_TO_DOOR_SQUARED*1.6f*1.6f)) // NOTE: checks interior cells only proximityToDoor(actor, MIN_DIST_TO_DOOR_SQUARED*1.6f*1.6f)) // NOTE: checks interior cells only
{ {
idleNow = false; wanderState = Wander_MoveNow;
moveNow = true;
mTrimCurrentNode = false; // just in case mTrimCurrentNode = false; // just in case
} }
} }
// Are we there yet? // Are we there yet?
bool& chooseAction = storage.mChooseAction; if ((wanderState == Wander_Walking) &&
if(walking &&
storage.mPathFinder.checkPathCompleted(pos.pos[0], pos.pos[1], DESTINATION_TOLERANCE)) storage.mPathFinder.checkPathCompleted(pos.pos[0], pos.pos[1], DESTINATION_TOLERANCE))
{ {
stopWalking(actor, storage); stopWalking(actor, storage);
chooseAction = true; wanderState = Wander_ChooseAction;
mHasReturnPosition = false; mHasReturnPosition = false;
} }
if(walking) // have not yet reached the destination if (wanderState == Wander_Walking) // have not yet reached the destination
{ {
// turn towards the next point in mPath // turn towards the next point in mPath
zTurn(actor, storage.mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1])); zTurn(actor, storage.mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]));
@ -253,24 +242,23 @@ namespace MWMechanics
// Check if idle animation finished // Check if idle animation finished
short unsigned& playedIdle = storage.mPlayedIdle; short unsigned& playedIdle = storage.mPlayedIdle;
GreetingState& greetingState = storage.mSaidGreeting; GreetingState& greetingState = storage.mSaidGreeting;
if(idleNow && !checkIdle(actor, playedIdle) && (greetingState == Greet_Done || greetingState == Greet_None)) if ((wanderState == Wander_IdleNow) &&
!checkIdle(actor, playedIdle) && (greetingState == Greet_Done || greetingState == Greet_None))
{ {
playedIdle = 0; playedIdle = 0;
idleNow = false; wanderState = Wander_ChooseAction;
chooseAction = true;
} }
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
if(chooseAction) if (wanderState == Wander_ChooseAction)
{ {
playedIdle = 0; playedIdle = 0;
getRandomIdle(playedIdle); // NOTE: sets mPlayedIdle with a random selection getRandomIdle(playedIdle); // NOTE: sets mPlayedIdle with a random selection
if(!playedIdle && mDistance) if(!playedIdle && mDistance)
{ {
chooseAction = false; wanderState = Wander_MoveNow;
moveNow = true;
} }
else else
{ {
@ -278,8 +266,7 @@ namespace MWMechanics
MWWorld::TimeStamp currentTime = world->getTimeStamp(); MWWorld::TimeStamp currentTime = world->getTimeStamp();
mStartTime = currentTime; mStartTime = currentTime;
playIdle(actor, playedIdle); playIdle(actor, playedIdle);
chooseAction = false; wanderState = Wander_IdleNow;
idleNow = true;
} }
} }
@ -332,9 +319,6 @@ namespace MWMechanics
mHasReturnPosition = false; mHasReturnPosition = false;
if (mDistance == 0 && mHasReturnPosition && (pos.asVec3() - mReturnPosition).length2() > 20*20) if (mDistance == 0 && mHasReturnPosition && (pos.asVec3() - mReturnPosition).length2() > 20*20)
{ {
chooseAction = false;
idleNow = false;
if (!storage.mPathFinder.isPathConstructed()) if (!storage.mPathFinder.isPathConstructed())
{ {
ESM::Pathgrid::Point dest(PathFinder::MakePathgridPoint(mReturnPosition)); ESM::Pathgrid::Point dest(PathFinder::MakePathgridPoint(mReturnPosition));
@ -347,19 +331,18 @@ namespace MWMechanics
if(storage.mPathFinder.isPathConstructed()) if(storage.mPathFinder.isPathConstructed())
{ {
moveNow = false; wanderState = Wander_Walking;
walking = true;
} }
} }
} }
// Allow interrupting a walking actor to trigger a greeting // Allow interrupting a walking actor to trigger a greeting
if(idleNow || walking) if ((wanderState == Wander_IdleNow) || (wanderState == Wander_Walking))
{ {
playGreetingIfPlayerGetsTooClose(actor, storage); playGreetingIfPlayerGetsTooClose(actor, storage);
} }
if(moveNow && mDistance) if ((wanderState == Wander_MoveNow) && mDistance)
{ {
// Construct a new path if there isn't one // Construct a new path if there isn't one
if(!storage.mPathFinder.isPathConstructed()) if(!storage.mPathFinder.isPathConstructed())
@ -386,8 +369,7 @@ namespace MWMechanics
trimAllowedNodes(mAllowedNodes, storage.mPathFinder); trimAllowedNodes(mAllowedNodes, storage.mPathFinder);
mObstacleCheck.clear(); mObstacleCheck.clear();
storage.mPathFinder.clearPath(); storage.mPathFinder.clearPath();
storage.mWalking = false; storage.mState = Wander_MoveNow;
storage.mMoveNow = true;
} }
else // probably walking into another NPC else // probably walking into another NPC
{ {
@ -409,7 +391,7 @@ namespace MWMechanics
mObstacleCheck.clear(); mObstacleCheck.clear();
stopWalking(actor, storage); stopWalking(actor, storage);
storage.mChooseAction = true; storage.mState = Wander_ChooseAction;
mStuckCount = 0; mStuckCount = 0;
} }
//#endif //#endif
@ -481,11 +463,11 @@ namespace MWMechanics
{ {
greetingTimer++; greetingTimer++;
if (storage.mWalking) if (storage.mState == Wander_Walking)
{ {
stopWalking(actor, storage); stopWalking(actor, storage);
mObstacleCheck.clear(); mObstacleCheck.clear();
storage.mIdleNow = true; storage.mState = Wander_IdleNow;
getRandomIdle(storage.mPlayedIdle); getRandomIdle(storage.mPlayedIdle);
} }
@ -539,8 +521,7 @@ namespace MWMechanics
mAllowedNodes.push_back(mCurrentNode); mAllowedNodes.push_back(mCurrentNode);
mCurrentNode = temp; mCurrentNode = temp;
storage.mMoveNow = false; storage.mState = Wander_Walking;
storage.mWalking = true;
} }
// Choose a different node and delete this one from possible nodes because it is uncreachable: // Choose a different node and delete this one from possible nodes because it is uncreachable:
else else
@ -591,8 +572,6 @@ namespace MWMechanics
{ {
storage.mPathFinder.clearPath(); storage.mPathFinder.clearPath();
actor.getClass().getMovementSettings(actor).mPosition[1] = 0; actor.getClass().getMovementSettings(actor).mPosition[1] = 0;
storage.mMoveNow = false;
storage.mWalking = false;
} }
void AiWander::playIdle(const MWWorld::Ptr& actor, unsigned short idleSelect) void AiWander::playIdle(const MWWorld::Ptr& actor, unsigned short idleSelect)

View file

@ -63,6 +63,13 @@ namespace MWMechanics
Greet_InProgress, Greet_InProgress,
Greet_Done Greet_Done
}; };
enum WanderState {
Wander_ChooseAction,
Wander_IdleNow,
Wander_MoveNow,
Wander_Walking,
};
private: private:
// NOTE: mDistance and mDuration must be set already // NOTE: mDistance and mDuration must be set already
void init(); void init();