OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
aiwander.hpp
Go to the documentation of this file.
1 #ifndef GAME_MWMECHANICS_AIWANDER_H
2 #define GAME_MWMECHANICS_AIWANDER_H
3 
4 #include "aipackage.hpp"
5 
6 #include <vector>
7 
8 #include "../mwworld/timestamp.hpp"
9 
10 #include "pathfinding.hpp"
11 #include "obstacle.hpp"
12 #include "aistate.hpp"
13 
14 namespace ESM
15 {
16  struct Cell;
17  namespace AiSequence
18  {
19  struct AiWander;
20  }
21 }
22 
23 namespace MWMechanics
24 {
25  struct AiWanderStorage;
26 
28  class AiWander : public AiPackage
29  {
30  public:
32 
37  AiWander(int distance, int duration, int timeOfDay, const std::vector<unsigned char>& idle, bool repeat);
38 
39  AiWander (const ESM::AiSequence::AiWander* wander);
40 
41  virtual AiPackage *clone() const;
42 
43  virtual bool execute (const MWWorld::Ptr& actor, CharacterController& characterController, AiState& state, float duration);
44 
45  virtual int getTypeId() const;
46 
48 
49  void setReturnPosition (const osg::Vec3f& position);
50 
51  virtual void writeState(ESM::AiSequence::AiSequence &sequence) const;
52 
53  virtual void fastForward(const MWWorld::Ptr& actor, AiState& state);
54 
55  bool getRepeat() const;
56 
61  };
62 
63  enum WanderState {
68  };
69 
70  private:
71  // NOTE: mDistance and mDuration must be set already
72  void init();
73  void stopWalking(const MWWorld::Ptr& actor, AiWanderStorage& storage);
74 
77  bool playIdle(const MWWorld::Ptr& actor, unsigned short idleSelect);
78  bool checkIdle(const MWWorld::Ptr& actor, unsigned short idleSelect);
79  short unsigned getRandomIdle();
80  void setPathToAnAllowedNode(const MWWorld::Ptr& actor, AiWanderStorage& storage, const ESM::Position& actorPos);
82  void evadeObstacles(const MWWorld::Ptr& actor, AiWanderStorage& storage, float duration, ESM::Position& pos);
83  void playIdleDialogueRandomly(const MWWorld::Ptr& actor);
84  void turnActorToFacePlayer(const osg::Vec3f& actorPosition, const osg::Vec3f& playerPosition, AiWanderStorage& storage);
85  void doPerFrameActionsForState(const MWWorld::Ptr& actor, float duration, AiWanderStorage& storage, ESM::Position& pos);
86  void onIdleStatePerFrameActions(const MWWorld::Ptr& actor, float duration, AiWanderStorage& storage);
87  void onWalkingStatePerFrameActions(const MWWorld::Ptr& actor, float duration, AiWanderStorage& storage, ESM::Position& pos);
89  bool reactionTimeActions(const MWWorld::Ptr& actor, AiWanderStorage& storage,
90  const MWWorld::CellStore*& currentCell, bool cellChange, ESM::Position& pos, float duration);
91  bool isPackageCompleted(const MWWorld::Ptr& actor, AiWanderStorage& storage);
92  void returnToStartLocation(const MWWorld::Ptr& actor, AiWanderStorage& storage, ESM::Position& pos);
93  void wanderNearStart(const MWWorld::Ptr &actor, AiWanderStorage &storage, int wanderDistance);
94  bool destinationIsAtWater(const MWWorld::Ptr &actor, const osg::Vec3f& destination);
95  bool destinationThroughGround(const osg::Vec3f& startPoint, const osg::Vec3f& destination);
96  void completeManualWalking(const MWWorld::Ptr &actor, AiWanderStorage &storage);
97 
98  int mDistance; // how far the actor can wander from the spawn point
99  int mDuration;
102  std::vector<unsigned char> mIdle;
103  bool mRepeat;
104 
105  bool mHasReturnPosition; // NOTE: Could be removed if mReturnPosition was initialized to actor position,
106  // if we had the actor in the AiWander constructor...
107  osg::Vec3f mReturnPosition;
110 
111  void getAllowedNodes(const MWWorld::Ptr& actor, const ESM::Cell* cell, AiWanderStorage& storage);
112 
113  void trimAllowedNodes(std::vector<ESM::Pathgrid::Point>& nodes, const PathFinder& pathfinder);
114 
115  // constants for converting idleSelect values into groupNames
117  {
120  };
121 
123  void ToWorldCoordinates(ESM::Pathgrid::Point& point, const ESM::Cell * cell);
124 
125  void SetCurrentNodeToClosestAllowedNode(osg::Vec3f npcPos, AiWanderStorage& storage);
126 
127  void AddNonPathGridAllowedPoints(osg::Vec3f npcPos, const ESM::Pathgrid * pathGrid, int pointIndex, AiWanderStorage& storage);
128 
130 
133 
134  static int OffsetToPreventOvercrowding();
135  };
136 }
137 
138 #endif
void SetCurrentNodeToClosestAllowedNode(osg::Vec3f npcPos, AiWanderStorage &storage)
Definition: aiwander.cpp:896
Definition: aiwander.hpp:60
bool mRepeat
Definition: aiwander.hpp:103
virtual void fastForward(const MWWorld::Ptr &actor, AiState &state)
Simulates the passing of time.
Definition: aiwander.cpp:776
std::vector< unsigned char > mIdle
Definition: aiwander.hpp:102
void trimAllowedNodes(std::vector< ESM::Pathgrid::Point > &nodes, const PathFinder &pathfinder)
Definition: aiwander.cpp:682
bool isPackageCompleted(const MWWorld::Ptr &actor, AiWanderStorage &storage)
Definition: aiwander.cpp:318
void AddNonPathGridAllowedPoints(osg::Vec3f npcPos, const ESM::Pathgrid *pathGrid, int pointIndex, AiWanderStorage &storage)
Definition: aiwander.cpp:869
void playIdleDialogueRandomly(const MWWorld::Ptr &actor)
Definition: aiwander.cpp:547
osg::Vec3f mInitialActorPosition
Definition: aiwander.hpp:108
Definition: aiwander.hpp:66
bool checkIdle(const MWWorld::Ptr &actor, unsigned short idleSelect)
Definition: aiwander.cpp:733
void ToWorldCoordinates(ESM::Pathgrid::Point &point, const ESM::Cell *cell)
convert point from local (i.e. cell) to world coordinates
Definition: aiwander.cpp:677
Definition: pathfinding.hpp:33
Definition: aiwander.hpp:58
void init()
Definition: aiwander.cpp:122
void playGreetingIfPlayerGetsTooClose(const MWWorld::Ptr &actor, AiWanderStorage &storage)
Definition: aiwander.cpp:577
This class holds the variables AiWander needs which are deleted if the package becomes inactive...
Definition: aiwander.cpp:54
Base class for AI packages.
Definition: aipackage.hpp:32
void onWalkingStatePerFrameActions(const MWWorld::Ptr &actor, float duration, AiWanderStorage &storage, ESM::Position &pos)
Definition: aiwander.cpp:473
short unsigned getRandomIdle()
Definition: aiwander.cpp:755
void getAllowedNodes(const MWWorld::Ptr &actor, const ESM::Cell *cell, AiWanderStorage &storage)
Definition: aiwander.cpp:808
bool mHasReturnPosition
Definition: aiwander.hpp:105
void stopWalking(const MWWorld::Ptr &actor, AiWanderStorage &storage)
Definition: aiwander.cpp:713
void wanderNearStart(const MWWorld::Ptr &actor, AiWanderStorage &storage, int wanderDistance)
Definition: aiwander.cpp:355
int mDuration
Definition: aiwander.hpp:99
Definition: character.hpp:145
virtual bool execute(const MWWorld::Ptr &actor, CharacterController &characterController, AiState &state, float duration)
Definition: aiwander.cpp:190
Definition: aiwander.hpp:118
void returnToStartLocation(const MWWorld::Ptr &actor, AiWanderStorage &storage, ESM::Position &pos)
Definition: aiwander.cpp:333
void setReturnPosition(const osg::Vec3f &position)
Set the position to return to for a stationary (non-wandering) actor.
Definition: aiwander.cpp:746
Definition: aisequence.hpp:66
Definition: aiwander.hpp:119
void setPathToAnAllowedNode(const MWWorld::Ptr &actor, AiWanderStorage &storage, const ESM::Position &actorPos)
Definition: aiwander.cpp:646
bool reactionTimeActions(const MWWorld::Ptr &actor, AiWanderStorage &storage, const MWWorld::CellStore *&currentCell, bool cellChange, ESM::Position &pos, float duration)
Definition: aiwander.cpp:230
Definition: aiwander.hpp:67
void turnActorToFacePlayer(const osg::Vec3f &actorPosition, const osg::Vec3f &playerPosition, AiWanderStorage &storage)
Definition: aiwander.cpp:637
void completeManualWalking(const MWWorld::Ptr &actor, AiWanderStorage &storage)
Definition: aiwander.cpp:407
Mutable state of a cell.
Definition: cellstore.hpp:53
int mDistance
Definition: aiwander.hpp:98
bool destinationIsAtWater(const MWWorld::Ptr &actor, const osg::Vec3f &destination)
Definition: aiwander.cpp:392
GroupIndex
Definition: aiwander.hpp:116
virtual AiPackage * clone() const
Clones the package.
Definition: aiwander.cpp:135
int mTimeOfDay
Definition: aiwander.hpp:101
bool mStoredInitialActorPosition
Definition: aiwander.hpp:109
void onIdleStatePerFrameActions(const MWWorld::Ptr &actor, float duration, AiWanderStorage &storage)
Definition: aiwander.cpp:439
void evadeObstacles(const MWWorld::Ptr &actor, AiWanderStorage &storage, float duration, ESM::Position &pos)
Definition: aiwander.cpp:517
float distance(const ESM::Pathgrid::Point &point, float x, float y, float z)
Definition: pathfinding.cpp:69
WanderState
Definition: aiwander.hpp:63
Definition: loadpgrd.hpp:16
Definition: loadcell.hpp:64
Definition: aiwander.hpp:65
void AddPointBetweenPathGridPoints(const ESM::Pathgrid::Point &start, const ESM::Pathgrid::Point &end, AiWanderStorage &storage)
Definition: aiwander.cpp:881
void onChooseActionStatePerFrameActions(const MWWorld::Ptr &actor, AiWanderStorage &storage)
Definition: aiwander.cpp:490
GreetingState
Definition: aiwander.hpp:57
Definition: defs.hpp:38
Definition: aisequence.hpp:148
static const std::string sIdleSelectToGroupName[GroupIndex_MaxIdle-GroupIndex_MinIdle+1]
lookup table for converting idleSelect value to groupName
Definition: aiwander.hpp:132
osg::Vec3f mReturnPosition
Definition: aiwander.hpp:107
static int OffsetToPreventOvercrowding()
Definition: aiwander.cpp:803
virtual int getTypeId() const
Definition: aiwander.cpp:708
Definition: aiwander.hpp:59
Causes the Actor to wander within a specified range.
Definition: aiwander.hpp:28
bool getRepeat() const
Return true if this package should repeat. Currently only used for Wander packages.
Definition: aiwander.cpp:312
AiWander(int distance, int duration, int timeOfDay, const std::vector< unsigned char > &idle, bool repeat)
Constructor.
Definition: aiwander.cpp:114
void doPerFrameActionsForState(const MWWorld::Ptr &actor, float duration, AiWanderStorage &storage, ESM::Position &pos)
Definition: aiwander.cpp:413
bool destinationThroughGround(const osg::Vec3f &startPoint, const osg::Vec3f &destination)
Definition: aiwander.cpp:402
Definition: loadpgrd.hpp:30
Pointer to a LiveCellRef.
Definition: ptr.hpp:19
virtual void writeState(ESM::AiSequence::AiSequence &sequence) const
Definition: aiwander.cpp:914
bool playIdle(const MWWorld::Ptr &actor, unsigned short idleSelect)
Definition: aiwander.cpp:719
float mRemainingDuration
Definition: aiwander.hpp:100