Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

astar2.h

Go to the documentation of this file.
00001 
00006 #ifndef astar2H
00007  #define astar2H
00008 
00009 
00010  #include <vector>
00011  #include <map>
00012  #include <set>
00013  #include "mapalgorithms.h"
00014  #include "gamemap.h"
00015 
00016 
00017 
00018 
00019  enum HexDirection { DirN, DirNE, DirSE, DirS, DirSW, DirNW, DirNone };
00020 
00021 
00023  class AStar {
00024     public:
00025        typedef vector<MapCoordinate> Path;
00026 
00027     protected:
00028        int MAXIMUM_PATH_LENGTH;
00029        GameMap* tempsMarked;
00030        Path *_path;
00031        Vehicle* _veh;
00032        GameMap* _actmap;
00033 
00034 
00036        virtual int getMoveCost ( int x1, int y1, int x2, int y2, const Vehicle* vehicle );
00037     public:
00038        AStar ( GameMap* actmap, Vehicle* veh );
00039 
00041        struct HexCoord{
00042            int m, n;
00043            HexCoord(): m(0), n(0) {}
00044            HexCoord( int m_, int n_ ): m(m_), n(n_) {}
00045            ~HexCoord() {}
00046        };
00047 
00048        struct Node {
00049            HexCoord h;        // location on the map, in hex coordinates
00050            int gval;        // g in A* represents how far we've already gone
00051            int hval;        // h in A* represents an estimate of how far is left
00052            Node(): h(0,0), gval(0), hval(0) {}
00053            bool operator< ( const Node& a );
00054        };
00055 
00056        int dist( HexCoord a, HexCoord b );
00057 
00058        typedef std::vector<Node> Container;
00059        greater<Node> comp;
00060 
00061        inline void get_first( Container& v, Node& n );
00062 
00063 
00064        Container visited;
00065 
00067        void findPath( HexCoord A, HexCoord B, Path& path );
00068 
00070        void findPath( Path& path, int x, int y );
00071 
00076        void findAllAccessibleFields ( int maxDist = maxint );  // all accessible fields will have a.temp set to 1
00077 
00079        int getDistance( );
00080 
00082        int getTravelTime( );
00083 
00085        bool fieldVisited ( int x, int y);
00086       
00087        virtual ~AStar ( );
00088  };
00089 
00090 
00092 extern void findPath( GameMap* actmap, AStar::Path& path, Vehicle* veh, int x, int y );
00093 
00094 
00095 
00097 class AStar3D {
00098     public:
00099        typedef float DistanceType;
00100        static const DistanceType longestPath;
00101        class OperationLimiter {
00102            public:
00103               virtual bool allowHeightChange() = 0;
00104               virtual bool allowMovement() = 0;
00105               virtual bool allowLeavingContainer() = 0;
00106               virtual bool allowDocking() = 0;
00107               virtual ~OperationLimiter() {};
00108        };
00109 
00110 
00111        class PathPoint: public MapCoordinate3D {
00112           public:
00113              PathPoint ( const MapCoordinate3D& mc, int dist_, int enterHeight_, bool hasAttacked_ ) : MapCoordinate3D(mc), dist(dist_), enterHeight(enterHeight_), hasAttacked(hasAttacked_) {};
00114              // PathPoint ( const MapCoordinate3D& mc ) : MapCoordinate3D(mc), dist(-1), enterHeight(-1), hasAttacked(false) {};
00115              int getRealHeight() { if ( getNumericalHeight() != -1 ) return getNumericalHeight(); else return enterHeight; };
00116              MapCoordinate3D getRealPos() { MapCoordinate3D p; p.setnum(x,y, getRealHeight()); return p; };
00117              int dist;
00118              int enterHeight;
00119              bool hasAttacked;
00120        };
00121 
00122        typedef vector<PathPoint> Path;
00123        struct Node {
00124            MapCoordinate3D h;        // location on the map, in hex coordinates
00125            AStar3D::DistanceType gval;        // g in A* represents how far we've already gone
00126            AStar3D::DistanceType hval;        // h in A* represents an estimate of how far is left
00127            bool canStop;
00128            bool hasAttacked;
00129            int enterHeight;
00130            Node(): gval(0), hval(0), canStop(false), enterHeight(-1), deleted(false) {}
00131            bool deleted;
00132            bool operator< ( const Node& b ) const;
00133        };
00134 
00135     protected:
00136        OperationLimiter* operationLimiter;
00137        int MAXIMUM_PATH_LENGTH;
00138        GameMap* tempsMarked;
00139        Path *_path;
00140        Vehicle* veh;
00141        GameMap* actmap;
00142        float vehicleSpeedFactor[8];
00143        bool markTemps;
00144        WindMovement* wind;
00145 
00146        virtual DistanceType getMoveCost ( const MapCoordinate3D& start, const MapCoordinate3D& dest, const Vehicle* vehicle, bool& canStop, bool& hasAttacked );
00147 
00148        HexDirection* posDirs;
00149        int*          posHHops;
00150        int*          fieldAccess;
00151 
00152        HexDirection& getPosDir ( const MapCoordinate3D& pos ) { return posDirs [(pos.y * actmap->xsize + pos.x) * 9 + 1+pos.getNumericalHeight()]; };
00153        int& getPosHHop ( const MapCoordinate3D& pos )         { return posHHops[(pos.y * actmap->xsize + pos.x) * 9 + 1+pos.getNumericalHeight()]; };
00154 
00155        DistanceType dist( const MapCoordinate3D& a, const MapCoordinate3D& b );
00156        DistanceType dist( const MapCoordinate3D& a, const vector<MapCoordinate3D>& b );
00157 
00158     public:
00159 
00160        class Container: protected multiset<Node, less<Node> > {
00161           public:
00162              typedef multiset<Node, less<Node> > Parent;
00163 
00164              // Container() {};
00165              void add ( const Node& node ) { insert ( node ); };
00166              bool update ( const Node& node );
00167              Node getFirst() { Node n = *Parent::begin(); Parent::erase ( Parent::begin() ); return n; };
00168              bool empty() { return Parent::empty(); };
00169 
00170 
00171              typedef Parent::iterator iterator;
00172              iterator find( const MapCoordinate3D& pos );
00173 
00174              iterator begin() { return Parent::begin(); };
00175              iterator end() { return Parent::end(); };
00176 
00177        };
00178 
00180        Container visited;
00181        // vector<Node> visited;
00182     protected:
00183 
00184        
00185        bool get_first( Container& v, Node& n );
00186 
00187        void nodeVisited ( const Node& n, HexDirection direc, Container& open, int prevHeight = -10, int heightChangeDist = 0 );
00188 
00189 
00190     public:
00191        AStar3D ( GameMap* actmap, Vehicle* veh, bool markTemps_ = true, int maxDistance = maxint );
00192 
00193 
00195        void registerOperationLimiter( OperationLimiter* ol ) { operationLimiter = ol; };
00196 
00198        void findPath( const MapCoordinate3D& A, const vector<MapCoordinate3D>& B, Path& path );
00199 
00201        void findPath( Path& path, const MapCoordinate3D& dest );
00202 
00204        void findPath( Path& path, const vector<MapCoordinate3D>& dest );
00205 
00210        void findAllAccessibleFields (  );
00211 
00213        int getDistance( );
00214 
00216        int getTravelTime( );
00217 
00219        const Node* fieldVisited ( const MapCoordinate3D& fld );
00220 
00221        int& getFieldAccess ( int x, int y );
00222        int& getFieldAccess ( const MapCoordinate& mc );
00223 
00224        virtual ~AStar3D ( );
00225  };
00226 
00227 #endif

Generated on Tue Jun 24 01:27:35 2008 for Advanced Strategic Command by  doxygen 1.4.2