Tactics: Western Philosophers Vs. Musicians  0.12
A turn-based tactical game combining rules and gameplay elements inspired by Final Fantasy Tactics and the Mayfair Exponential Game System. Unlike most games of this type, motion is in full, grid-less 3D.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PFManager.h
Go to the documentation of this file.
1 // Copyright (C) 2004-2011 Dylan Blair
3 //
4 // email: dblair@alumni.cs.utexas.edu
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 #ifndef PFMANAGER_H
22 #define PFMANAGER_H
23 
24 #include "../Math_LIB/MPath.h"
25 #include "../Math_LIB/MInterpolator.h"
26 #include "../Utils_LIB/UPrecisionTimer.h"
27 
28 #include "PFAStar.h"
29 #include "PFQueue.h"
30 #ifdef PF_USES_WPC
31 #include "PFWaypointCollection.h"
32 #endif
33 
34 namespace OpenSkyNet {
35  namespace PF {
129  class Manager {
130  public:
132  enum ALGORITHM { BFS, A_STAR };
133  private:
134  CD::Grid* _grid;
135 
136  const Math::Point<>* _driver;
137  const CD::Volume* _drivBoundingVolume;
138  Math::Point<int> _prevDrivBin;
139 
140  const Math::Point<>* _target;
141  const CD::Volume* _targBoundingVolume;
146  float _targetRadiusSqrd;
147  Math::Point<int> _prevTargBin;
148 
149  bool _isDriverPathControlled;
150  Math::Path** _drivPath;
151 
152  Math::Path _foundPath;
153 
154  void createRelativePath(const std::vector<CD::DIRECTION>& pathDirs_);
155  void createRelativePath(const std::vector<DirOrPath>& dirsOrPaths_);
156 
157  inline void decrementDriverAndTargetBins() {
158  Utils::uint i;
159  for (i = 1; i < _drivBins.size(); i++) _grid->decrementBin(_drivBins[i]);
160  for (i = 1; i < _targBins.size(); i++) _grid->decrementBin(_targBins[i]);
161  }
162 
163  inline void incrementDriverAndTargetBins() {
164  Utils::uint i;
165  for (i = 1; i < _drivBins.size(); i++) _grid->incrementBin(_drivBins[i]);
166  for (i = 1; i < _targBins.size(); i++) _grid->incrementBin(_targBins[i]);
167  }
168 
169  double _timeStampIsStuckPrev;
170  double _timeStampIsStuckCurr;
171  double _numSecsWithSameDrivBin;
172 
173  bool _attemptedDirs[CD::POS_X_POS_Y_POS_Z+1];
174  inline void clearAttemptedDirs() {
175  memset((void*)&_attemptedDirs,0,sizeof(bool)*(CD::POS_X_POS_Y_POS_Z+1));
176  _attemptedDirs[CD::NONE] = true;
177  }
178 
179  Manager::ALGORITHM _algo;
180 
181  bool _continuing;
182  double _timeStampBegin;
183 
184  std::vector<Math::Point<int> > _drivBins, _targBins;
185  Math::Point<> _currDriver, _currTarget;
186 
187 #ifdef PF_USES_WPC
188  const std::map<const CD::Volume*, WaypointCollection*>* _volWPCMap;
189 #endif
190 
192  PriorityQueue _prioQ;
193  Queue _q;
194 
195  RESULT findPathAStar(double secsAllotted_=-1.0);
196  RESULT findPathBFS(double secsAllotted_=-1.0);
198  public:
199  Manager();
200 
203 
205  inline void setDriver(const Math::Point<>* driver_, const CD::Volume* drivBoundingVolume_) {
206  _driver = driver_;
207  _drivBoundingVolume = drivBoundingVolume_;
208  }
209 
215  inline void setTarget(const Math::Point<>* target_, const CD::Volume* targBoundingVolume_,
216  float targetRadius_=-1.0f) {
217  _target = target_;
218  _targBoundingVolume = targBoundingVolume_;
219  if (targetRadius_ < 0.0f)
220  _targetRadiusSqrd = _targBoundingVolume->getBoundingRadius() *
221  _targBoundingVolume->getBoundingRadius();
222  else
223  _targetRadiusSqrd = targetRadius_ * targetRadius_;
224  }
225 
226 #ifdef PF_USES_WPC
227  inline void setWPCs(const std::map<const CD::Volume*, WaypointCollection*>* volWPCMap_) { _volWPCMap = volWPCMap_; }
228 #endif
229 
230 
231  virtual ~Manager();
232 
233  virtual RESULT calcDriverAndTargetBins(std::vector<Math::Point<int> >& drivBins_, std::vector<Math::Point<int> >& targBins_, bool calcPositions_=false, bool useInitialGrid_=false);
234  inline RESULT calcDriverAndTargetBins(bool calcPositions_=false, bool useInitialGrid_=false) {
235  return calcDriverAndTargetBins(_drivBins,_targBins,calcPositions_,useInitialGrid_);
236  }
237 
238  inline const std::vector<Math::Point<int> >& getDriverBins() const { return _drivBins; }
239  inline const std::vector<Math::Point<int> >& getTargetBins() const { return _targBins; }
240 
242  inline const Math::Point<>* getDriver() const { return _driver; }
243 
245  inline const Math::Point<>* getTarget() const { return _target; }
246 
247  inline const CD::Grid* getGrid() const { return _grid; }
248 
255  inline void controlDriverPath(Math::Path*& drivPath_) {
256  _drivPath = &drivPath_;
257  _isDriverPathControlled = true;
258  }
259  inline bool controlDriverPath() {
260  if (!_drivPath) return false;
261  _isDriverPathControlled = true;
262  return true;
263  }
264  inline void releaseDriverPath() { _isDriverPathControlled = false; }
265  inline bool isDriverPathControlled() const { return _isDriverPathControlled; }
266 
268  inline Math::Path& getFoundPath() { return _foundPath; }
269 
273  virtual bool isStuck();
274 
287  virtual RESULT findPath(Manager::ALGORITHM algo_=Manager::A_STAR, bool doUpdateMaxCorner_=true, double secsAllotted_=-1.0);
288 
294  void resetPathfindingVars();
295 
296  bool isDriverBinOutsideGrid() const;
297 
298  bool isTargetBinOutsideGrid() const;
299  };
300 
302  }
303 }
304 
305 #endif //PFMANAGER_H
Definition: CDGrid.h:69
void resetPathfindingVars()
Definition: PFManager.cpp:373
RESULT calcDriverAndTargetBins(bool calcPositions_=false, bool useInitialGrid_=false)
Definition: PFManager.h:234
void controlDriverPath(Math::Path *&drivPath_)
Definition: PFManager.h:255
virtual RESULT findPath(Manager::ALGORITHM algo_=Manager::A_STAR, bool doUpdateMaxCorner_=true, double secsAllotted_=-1.0)
Definition: PFManager.cpp:73
const std::vector< Math::Point< int > > & getDriverBins() const
Definition: PFManager.h:238
const std::vector< Math::Point< int > > & getTargetBins() const
Definition: PFManager.h:239
Manager()
Definition: PFManager.cpp:24
virtual bool isStuck()
Definition: PFManager.cpp:48
bool isTargetBinOutsideGrid() const
Definition: PFManager.cpp:390
bool decrementBin(const Math::Point< int > &bin_)
Definition: CDGrid.h:261
void setTarget(const Math::Point<> *target_, const CD::Volume *targBoundingVolume_, float targetRadius_=-1.0f)
Definition: PFManager.h:215
const CD::Grid * getGrid() const
Definition: PFManager.h:247
Definition: CDVolume.h:225
Definition: PFManager.h:132
bool isDriverBinOutsideGrid() const
Definition: PFManager.cpp:380
RESULT
Definition: PFConsts.h:33
float getBoundingRadius() const
Definition: CDVolume.h:234
Definition: PFManager.h:132
virtual RESULT calcDriverAndTargetBins(std::vector< Math::Point< int > > &drivBins_, std::vector< Math::Point< int > > &targBins_, bool calcPositions_=false, bool useInitialGrid_=false)
ALGORITHM
Definition: PFManager.h:132
unsigned int uint
Definition: UTypes.h:39
virtual ~Manager()
Definition: PFManager.cpp:46
const Math::Point * getTarget() const
Definition: PFManager.h:245
Definition: MPoint.h:33
Definition: PFManager.h:129
Definition: CDGrid.h:61
Utils::PrecisionTimer g_timer
Definition: PFManager.cpp:12
Math::Path & getFoundPath()
Definition: PFManager.h:268
Definition: UPrecisionTimer.h:58
bool isDriverPathControlled() const
Definition: PFManager.h:265
void releaseDriverPath()
Definition: PFManager.h:264
void setDriver(const Math::Point<> *driver_, const CD::Volume *drivBoundingVolume_)
Definition: PFManager.h:205
bool controlDriverPath()
Definition: PFManager.h:259
Definition: CDGrid.h:92
void incrementBin(const Math::Point< int > &bin_)
Definition: CDGrid.h:272
const Math::Point * getDriver() const
Definition: PFManager.h:242
Definition: MPath.h:31