$treeview $search $mathjax
Stratagus
2.2.7
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 // _________ __ __ 00002 // / _____// |_____________ _/ |______ ____ __ __ ______ 00003 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00004 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00005 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00006 // \/ \/ \//_____/ \/ 00007 // ______________________ ______________________ 00008 // T H E W A R B E G I N S 00009 // Stratagus - A free fantasy real time strategy game engine 00010 // 00012 // 00013 // (c) Copyright 1998-2005 by Lutz Sammer, Russell Smith 00014 // 00015 // This program is free software; you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation; only version 2 of the License. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 // GNU General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00029 00030 #ifndef __PATH_FINDER_H__ 00031 #define __PATH_FINDER_H__ 00032 00034 00035 #ifndef STDCALL 00036 #ifdef _MSC_VER 00037 #define STDCALL __stdcall 00038 #else 00039 #define STDCALL 00040 #endif 00041 #endif 00042 00043 #if defined(DEBUG_ASTAR) 00044 #define AstarDebugPrint(x) DebugPrint(x) 00045 #else 00046 #define AstarDebugPrint(x) 00047 #endif 00048 00049 /*---------------------------------------------------------------------------- 00050 -- Declarations 00051 ----------------------------------------------------------------------------*/ 00052 00053 #include <queue> 00054 #include "vec2i.h" 00055 00056 class CUnit; 00057 class CFile; 00058 struct lua_State; 00059 00067 enum _move_return_ { 00068 PF_FAILED = -3, 00069 PF_UNREACHABLE = -2, 00070 PF_REACHED = -1, 00071 PF_WAIT = 0, 00072 PF_MOVE = 1 00073 }; 00074 00075 class PathFinderInput 00076 { 00077 public: 00078 PathFinderInput(); 00079 CUnit *GetUnit() const { return unit; } 00080 const Vec2i &GetUnitPos() const; 00081 Vec2i GetUnitSize() const; 00082 const Vec2i &GetGoalPos() const { return goalPos; } 00083 const Vec2i &GetGoalSize() const { return goalSize; } 00084 int GetMinRange() const { return minRange; } 00085 int GetMaxRange() const { return maxRange; } 00086 bool IsRecalculateNeeded() const { return isRecalculatePathNeeded; } 00087 00088 void SetUnit(CUnit &_unit); 00089 void SetGoal(const Vec2i &pos, const Vec2i &size); 00090 void SetMinRange(int range); 00091 void SetMaxRange(int range); 00092 00093 void PathRacalculated(); 00094 00095 void Save(CFile &file) const; 00096 void Load(lua_State *l); 00097 00098 private: 00099 CUnit *unit; 00100 Vec2i unitSize; 00101 Vec2i goalPos; 00102 Vec2i goalSize; 00103 int minRange; 00104 int maxRange; 00105 bool isRecalculatePathNeeded; 00106 }; 00107 00108 class PathFinderOutput 00109 { 00110 public: 00111 enum {MAX_PATH_LENGTH = 28}; 00112 public: 00113 PathFinderOutput(); 00114 void Save(CFile &file) const; 00115 void Load(lua_State *l); 00116 public: 00117 unsigned short int Cycles; 00118 char Fast; 00119 char Length; 00120 char Path[MAX_PATH_LENGTH]; 00121 }; 00122 00123 class PathFinderData 00124 { 00125 public: 00126 PathFinderInput input; 00127 PathFinderOutput output; 00128 }; 00129 00130 00131 // 00132 // Terrain traversal stuff. 00133 // 00134 00135 enum VisitResult { 00136 VisitResult_Finished, 00137 VisitResult_DeadEnd, 00138 VisitResult_Ok, 00139 VisitResult_Cancel 00140 }; 00141 00142 class TerrainTraversal 00143 { 00144 public: 00145 typedef short unsigned int dataType; 00146 public: 00147 void SetSize(unsigned int width, unsigned int height); 00148 void Init(); 00149 00150 void PushPos(const Vec2i &pos); 00151 void PushNeighboor(const Vec2i &pos); 00152 void PushUnitPosAndNeighboor(const CUnit &unit); 00153 00154 template <typename T> 00155 bool Run(T &context); 00156 00157 bool IsVisited(const Vec2i &pos) const; 00158 bool IsReached(const Vec2i &pos) const; 00159 bool IsInvalid(const Vec2i &pos) const; 00160 00161 // Accept pos to be at one inside the real map 00162 dataType Get(const Vec2i &pos) const; 00163 00164 private: 00165 void Set(const Vec2i &pos, dataType value); 00166 00167 struct PosNode { 00168 PosNode(const Vec2i &pos, const Vec2i &from) : pos(pos), from(from) {} 00169 Vec2i pos; 00170 Vec2i from; 00171 }; 00172 00173 private: 00174 std::vector<dataType> m_values; 00175 std::queue<PosNode> m_queue; 00176 unsigned int m_extented_width; 00177 unsigned int m_height; 00178 }; 00179 00180 template <typename T> 00181 bool TerrainTraversal::Run(T &context) 00182 { 00183 for (; m_queue.empty() == false; m_queue.pop()) { 00184 const PosNode &posNode = m_queue.front(); 00185 00186 switch (context.Visit(*this, posNode.pos, posNode.from)) { 00187 case VisitResult_Finished: return true; 00188 case VisitResult_DeadEnd: Set(posNode.pos, -1); break; 00189 case VisitResult_Ok: PushNeighboor(posNode.pos); break; 00190 case VisitResult_Cancel: return false; 00191 } 00192 Assert(IsVisited(posNode.pos)); 00193 } 00194 return false; 00195 } 00196 00197 00198 /*---------------------------------------------------------------------------- 00199 -- Variables 00200 ----------------------------------------------------------------------------*/ 00201 00203 extern int AStarFixedUnitCrossingCost; 00205 extern int AStarMovingUnitCrossingCost; 00207 extern bool AStarKnowUnseenTerrain; 00209 extern int AStarUnknownTerrainCost; 00210 00211 // 00212 // Convert heading into direction. 00213 // N NE E SE S SW W NW 00214 extern const int Heading2X[9]; 00215 extern const int Heading2Y[9]; 00216 extern const int XY2Heading[3][3]; 00217 00218 /*---------------------------------------------------------------------------- 00219 -- Functions 00220 ----------------------------------------------------------------------------*/ 00221 00223 extern void InitPathfinder(); 00225 extern void FreePathfinder(); 00226 00228 extern int NextPathElement(CUnit &unit, short int *xdp, short int *ydp); 00230 extern int UnitReachable(const CUnit &unit, const CUnit &dst, int range); 00232 extern int PlaceReachable(const CUnit &src, const Vec2i &pos, int w, int h, 00233 int minrange, int maxrange); 00234 00235 // 00236 // in astar.cpp 00237 // 00238 00239 extern void SetAStarFixedUnitCrossingCost(int cost); 00240 extern int GetAStarFixedUnitCrossingCost(); 00241 00242 extern void SetAStarMovingUnitCrossingCost(int cost); 00243 extern int GetAStarMovingUnitCrossingCost(); 00244 00245 extern void SetAStarUnknownTerrainCost(int cost); 00246 extern int GetAStarUnknownTerrainCost(); 00247 00248 extern void PathfinderCclRegister(); 00249 00251 00252 #endif // !__PATH_FINDER_H__