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* gamemap;
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;
00050 int gval;
00051 int hval;
00052 Node(): h(0,0), gval(0), hval(0) {}
00053 bool operator< ( const Node& a ) const;
00054 bool operator> ( const Node& a ) const;
00055 };
00056
00057 int dist( HexCoord a, HexCoord b );
00058
00059 typedef std::vector<Node> Container;
00060 std::greater<Node> comp;
00061
00062 inline void get_first( Container& v, Node& n );
00063
00064
00065 Container visited;
00066
00068 void findPath( HexCoord A, HexCoord B, Path& path );
00069
00071 void findPath( Path& path, int x, int y );
00072
00077 void findAllAccessibleFields ( int maxDist = maxint );
00078
00080 int getDistance( );
00081
00083 int getTravelTime( );
00084
00086 bool fieldVisited ( int x, int y);
00087
00088 virtual ~AStar ( );
00089 };
00090
00091
00093 extern void findPath( GameMap* actmap, AStar::Path& path, Vehicle* veh, int x, int y );
00094
00095
00096
00098 class AStar3D {
00099 public:
00100 typedef float DistanceType;
00101 static const DistanceType longestPath;
00102 class OperationLimiter {
00103 public:
00104 virtual bool allowHeightChange() = 0;
00105 virtual bool allowMovement() = 0;
00106 virtual bool allowLeavingContainer() = 0;
00107 virtual bool allowDocking() = 0;
00108 virtual ~OperationLimiter() {};
00109 };
00110
00111
00112 class PathPoint: public MapCoordinate3D {
00113 PathPoint() {};
00114 public:
00115 PathPoint ( const MapCoordinate3D& mc, int dist_, int enterHeight_, bool hasAttacked_ ) : MapCoordinate3D(mc), dist(dist_), enterHeight(enterHeight_), hasAttacked(hasAttacked_) {};
00116
00117 int getRealHeight() { if ( getNumericalHeight() != -1 ) return getNumericalHeight(); else return enterHeight; };
00118 MapCoordinate3D getRealPos() { MapCoordinate3D p; p.setnum(x,y, getRealHeight()); return p; };
00119 int dist;
00120 int enterHeight;
00121 bool hasAttacked;
00122 void write( tnstream& stream ) const;
00123 void read( tnstream& stream );
00124 static PathPoint newFromStream( tnstream& stream );
00125 };
00126
00127 typedef vector<PathPoint> Path;
00128 struct Node {
00129 MapCoordinate3D h;
00130 AStar3D::DistanceType gval;
00131 AStar3D::DistanceType hval;
00132 bool canStop;
00133 bool hasAttacked;
00134 int enterHeight;
00135 Node(): gval(0), hval(0), canStop(false), enterHeight(-1), deleted(false) {}
00136 bool deleted;
00137 bool operator< ( const Node& b ) const;
00138 };
00139
00140 protected:
00141 OperationLimiter* operationLimiter;
00142 int MAXIMUM_PATH_LENGTH;
00143 GameMap* tempsMarked;
00144 Path *_path;
00145 Vehicle* veh;
00146 GameMap* actmap;
00147 float vehicleSpeedFactor[8];
00148 float maxVehicleSpeedFactor;
00149 bool markTemps;
00150 WindMovement* wind;
00151
00152 virtual DistanceType getMoveCost ( const MapCoordinate3D& start, const MapCoordinate3D& dest, const Vehicle* vehicle, bool& canStop, bool& hasAttacked );
00153
00154 HexDirection* posDirs;
00155 int* posHHops;
00156 int* fieldAccess;
00157
00158 HexDirection& getPosDir ( const MapCoordinate3D& pos ) { return posDirs [(pos.y * actmap->xsize + pos.x) * 9 + 1+pos.getNumericalHeight()]; };
00159 int& getPosHHop ( const MapCoordinate3D& pos ) { return posHHops[(pos.y * actmap->xsize + pos.x) * 9 + 1+pos.getNumericalHeight()]; };
00160
00161 DistanceType dist( const MapCoordinate3D& a, const MapCoordinate3D& b );
00162 DistanceType dist( const MapCoordinate3D& a, const vector<MapCoordinate3D>& b );
00163
00164 public:
00165
00166 class Container: protected multiset<Node, less<Node> > {
00167 public:
00168 typedef multiset<Node, less<Node> > Parent;
00169
00170
00171 void add ( const Node& node ) { insert ( node ); };
00172 bool update ( const Node& node );
00173 Node getFirst() { Node n = *Parent::begin(); Parent::erase ( Parent::begin() ); return n; };
00174 bool empty() { return Parent::empty(); };
00175
00176
00177 typedef Parent::iterator iterator;
00178 iterator find( const MapCoordinate3D& pos );
00179
00180 iterator begin() { return Parent::begin(); };
00181 iterator end() { return Parent::end(); };
00182
00183 };
00184
00186 Container visited;
00187
00188 protected:
00189
00190
00191 bool get_first( Container& v, Node& n );
00192
00193 void nodeVisited ( const Node& n, HexDirection direc, Container& open, int prevHeight = -10, int heightChangeDist = 0 );
00194
00195
00196 public:
00197 AStar3D ( GameMap* actmap, Vehicle* veh, bool markTemps_ = true, int maxDistance = maxint );
00198
00199
00201 void registerOperationLimiter( OperationLimiter* ol ) { operationLimiter = ol; };
00202
00204 void findPath( const MapCoordinate3D& A, const vector<MapCoordinate3D>& B, Path& path );
00205
00207 void findPath( Path& path, const MapCoordinate3D& dest );
00208
00210 void findPath( Path& path, const vector<MapCoordinate3D>& dest );
00211
00217 void findAllAccessibleFields ( vector<MapCoordinate3D>* path = NULL );
00218
00220 int getDistance( );
00221
00223 int getTravelTime( );
00224
00226 const Node* fieldVisited ( const MapCoordinate3D& fld );
00227
00228 int& getFieldAccess ( int x, int y );
00229 int& getFieldAccess ( const MapCoordinate& mc );
00230
00231 virtual ~AStar3D ( );
00232 };
00233
00234 #endif