PF_API 0.52

Android/jni/Libs/PF_LIB/PFManager.h

Go to the documentation of this file.
00001 
00002 //    Copyright (C) 2004-2011 Dylan Blair
00003 //
00004 //    email: dblair@alumni.cs.utexas.edu
00005 //
00006 //    This library is free software; you can redistribute it and/or
00007 //    modify it under the terms of the GNU Lesser General Public
00008 //    License as published by the Free Software Foundation; either
00009 //    version 2.1 of the License, or (at your option) any later version.
00010 //
00011 //    This library is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //    Lesser General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU Lesser General Public
00017 //    License along with this library; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #ifndef PFMANAGER_H
00022 #define PFMANAGER_H
00023 
00024 #include "../Math_LIB/MPath.h"
00025 #include "../Math_LIB/MInterpolator.h"
00026 #include "../Utils_LIB/UPrecisionTimer.h"
00027 
00028 #include "PFAStar.h"
00029 #include "PFQueue.h"
00030 #ifdef PF_USES_WPC
00031 #include "PFWaypointCollection.h"
00032 #endif
00033 
00034 namespace OpenSkyNet {
00035     namespace PF {
00129         class Manager {
00130         public:
00132             enum ALGORITHM { BFS, A_STAR };
00133         private:
00134             CD::Grid* _grid;
00135 
00136             const Math::Point<>* _driver;
00137             const CD::Volume* _drivBoundingVolume;
00138             Math::Point<int> _prevDrivBin;
00139 
00140             const Math::Point<>* _target;
00141             const CD::Volume* _targBoundingVolume;
00146             float _targetRadiusSqrd;
00147             Math::Point<int> _prevTargBin;
00148 
00149             bool _isDriverPathControlled;
00150             Math::Path** _drivPath;
00151 
00152             Math::Path _foundPath;
00153 
00154             void createRelativePath(const std::vector<CD::DIRECTION>& pathDirs_);
00155             void createRelativePath(const std::vector<DirOrPath>& dirsOrPaths_);
00156 
00157             inline void decrementDriverAndTargetBins() {
00158                 Utils::uint i;
00159                 for (i = 1; i < _drivBins.size(); i++) _grid->decrementBin(_drivBins[i]);
00160                 for (i = 1; i < _targBins.size(); i++) _grid->decrementBin(_targBins[i]);
00161             }
00162 
00163             inline void incrementDriverAndTargetBins() {
00164                 Utils::uint i;
00165                 for (i = 1; i < _drivBins.size(); i++) _grid->incrementBin(_drivBins[i]);
00166                 for (i = 1; i < _targBins.size(); i++) _grid->incrementBin(_targBins[i]);
00167             }
00168 
00169             double _timeStampIsStuckPrev;
00170             double _timeStampIsStuckCurr;
00171             double _numSecsWithSameDrivBin;
00172 
00173             bool _attemptedDirs[CD::POS_X_POS_Y_POS_Z+1];
00174             inline void clearAttemptedDirs() {
00175                 memset((void*)&_attemptedDirs,0,sizeof(bool)*(CD::POS_X_POS_Y_POS_Z+1));
00176                 _attemptedDirs[CD::NONE] = true;
00177             }
00178 
00179             Manager::ALGORITHM _algo;
00180 
00181             bool _continuing;
00182             double _timeStampBegin;
00183 
00184             std::vector<Math::Point<int> > _drivBins, _targBins;
00185             Math::Point<> _currDriver, _currTarget;
00186 
00187 #ifdef PF_USES_WPC
00188             const std::map<const CD::Volume*, WaypointCollection*>* _volWPCMap;
00189 #endif
00190 
00192             PriorityQueue _prioQ;
00193             Queue _q;
00194 
00195             RESULT findPathAStar(double secsAllotted_=-1.0);
00196             RESULT findPathBFS(double secsAllotted_=-1.0);
00198         public:
00199             Manager();
00200 
00203 
00205             inline void setDriver(const Math::Point<>* driver_, const CD::Volume* drivBoundingVolume_) {
00206                 _driver = driver_;
00207                 _drivBoundingVolume = drivBoundingVolume_;
00208             }
00209 
00215             inline void setTarget(const Math::Point<>* target_, const CD::Volume* targBoundingVolume_, 
00216                 float targetRadius_=-1.0f) {
00217                 _target = target_;
00218                 _targBoundingVolume = targBoundingVolume_;
00219                 if (targetRadius_ < 0.0f)
00220                     _targetRadiusSqrd = _targBoundingVolume->getBoundingRadius() * 
00221                                         _targBoundingVolume->getBoundingRadius();
00222                 else
00223                     _targetRadiusSqrd = targetRadius_ * targetRadius_;
00224             }
00225 
00226 #ifdef PF_USES_WPC
00227             inline void setWPCs(const std::map<const CD::Volume*, WaypointCollection*>* volWPCMap_) { _volWPCMap = volWPCMap_; }
00228 #endif
00229 
00230 
00231             virtual ~Manager();
00232 
00233             virtual RESULT calcDriverAndTargetBins(std::vector<Math::Point<int> >& drivBins_, std::vector<Math::Point<int> >& targBins_, bool calcPositions_=false, bool useInitialGrid_=false);
00234             inline RESULT calcDriverAndTargetBins(bool calcPositions_=false, bool useInitialGrid_=false) {
00235                 return calcDriverAndTargetBins(_drivBins,_targBins,calcPositions_,useInitialGrid_);
00236             }
00237 
00238             inline const std::vector<Math::Point<int> >& getDriverBins() const { return _drivBins; }
00239             inline const std::vector<Math::Point<int> >& getTargetBins() const { return _targBins; }
00240 
00242             inline const Math::Point<>* getDriver() const { return _driver; }
00243 
00245             inline const Math::Point<>* getTarget() const { return _target; }
00246 
00247             inline const CD::Grid* getGrid() const { return _grid; }
00248 
00255             inline void controlDriverPath(Math::Path*& drivPath_) {
00256                 _drivPath = &drivPath_;
00257                 _isDriverPathControlled = true;
00258             }
00259             inline bool controlDriverPath() {
00260                 if (!_drivPath) return false;
00261                 _isDriverPathControlled = true;
00262                 return true;
00263             }
00264             inline void releaseDriverPath() { _isDriverPathControlled = false; }
00265             inline bool isDriverPathControlled() const { return _isDriverPathControlled; }
00266 
00268             inline Math::Path& getFoundPath() { return _foundPath; }
00269 
00273             virtual bool isStuck();
00274 
00287             virtual RESULT findPath(Manager::ALGORITHM algo_=Manager::A_STAR, bool doUpdateMaxCorner_=true, double secsAllotted_=-1.0);
00288 
00294             void resetPathfindingVars();
00295 
00296             bool isDriverBinOutsideGrid() const;
00297 
00298             bool isTargetBinOutsideGrid() const;
00299         };
00300 
00301         extern Utils::PrecisionTimer g_timer;
00302     }
00303 }
00304 
00305 #endif //PFMANAGER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines