D:/simple_rts/include/Level.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "Team.h"
00004 #include "AStarSearchManager.h"
00005 
00006 #include "OgreVector3.h"
00007 #include "OgreMath.h"
00008 #include "OgreSingleton.h"
00009 #include "OgreColourValue.h"
00010 #include "OgreIteratorWrappers.h"
00011 #include "OgreAxisAlignedBox.h"
00012 
00013 #include <vector>
00014 using std::vector;
00015 
00016 using Ogre::ColourValue;
00017 using Ogre::AxisAlignedBox;
00018 using Ogre::VectorIterator;
00019 using Ogre::Singleton;
00020 using Ogre::Vector3;
00021 using Ogre::Math;
00022 
00023 
00024 
00025 namespace ASR
00026 {
00027         class Unit;
00028         class Building;
00053         class Level : public Singleton<Level>
00054         {
00056                 friend  class LevelStatusVisualizer;
00057 
00058 
00059         // Internal Classes
00060         // ----------------------------------------------------------------------------
00061         public:
00062                 class LevelNode
00063                 {
00064                         friend class Level;
00065 
00066                 // Data Storage
00067                 // ----------------------------------------------------------------------------
00068                 private:
00069                         Vector3                 mWorldPos;
00070                         float                   mTraversalCost;
00071                         unsigned int    mId;
00072 
00073                         // Useful for performing graph searches
00074                         // This will not be persistent. That is, it will only be valid in the
00075                         // current call in which you are modifying it. Other calls may use it
00076                         // so do not rely on it being the same value from call to call
00077                         bool                    mChecked;
00078 
00079                         int                             mNumNeighbours;
00080                         LevelNode*              mNeighbours[8];
00081 
00082                 // Construction
00083                 // ----------------------------------------------------------------------------
00084                 public:
00085                         LevelNode ( Vector3 worldPos, float traversalCost = 1.0f )
00086                                 : mWorldPos ( worldPos ), mTraversalCost ( traversalCost ),
00087                                   mNumNeighbours ( 0 )
00088                         {
00089                                 for ( int i = 0; i < 8; i++ )
00090                                 {
00091                                         mNeighbours[i] = NULL;
00092                                 }
00093                         }
00094 
00095                         void setTraversalCost ( float cost )
00096                         {
00097                                 mTraversalCost = cost;
00098                         }
00099 
00100 
00101                 // Implementation for use in AStarSearch as a UserState
00102                 // ----------------------------------------------------------------------------
00103                 public:
00104                         int getNumNeighbours () const
00105                         {
00106                                 return mNumNeighbours;
00107                         }
00108 
00109                         // ----------------------------------------------------------------------------
00110                         LevelNode* getNeighbour ( int index ) const
00111                         {
00112                                 assert ( index < mNumNeighbours );
00113                                 return mNeighbours[index];
00114                         }
00115                         
00116                         // ----------------------------------------------------------------------------
00117                         float getTraversalCost () const
00118                         {
00119                                 return mTraversalCost;
00120                         }
00121 
00122                         // ----------------------------------------------------------------------------
00123                         float getEstimatedCostToState ( const LevelNode& state ) const
00124                         {
00125                                 Vector3 distance;
00126                                 distance = state.mWorldPos - mWorldPos;
00127                                 return distance.length ();
00128                         }
00129 
00130 
00131                         // ----------------------------------------------------------------------------
00132                         Vector3 getWorldPosition ()
00133                         {
00134                                 return mWorldPos;
00135                         }
00136 
00137                         // ----------------------------------------------------------------------------
00138                         bool operator== ( const LevelNode& rhs ) const
00139                         {
00140                                 return mId == rhs.mId;
00141 
00142                                 Vector3 distance = mWorldPos - rhs.mWorldPos;
00143                                 return distance.isZeroLength ();
00144                         }
00145 
00146 
00147                         // ----------------------------------------------------------------------------
00148                         bool isNode ( const LevelNode& rhs ) const
00149                         {
00150                                 return operator== ( rhs );
00151                         }
00152                 };
00153 
00154         // Data Types
00155         // ----------------------------------------------------------------------------
00156         public:
00157                 typedef vector<Unit*>                           UnitList;
00158                 typedef vector<Team*>                           TeamList;
00159 
00160         // Data Storage
00161         // ----------------------------------------------------------------------------
00162         private:
00164                 int             mCellsWide;
00165                 int             mCellsHigh;
00166 
00168                 float   mCellWidth;
00169                 float   mCellHeight;
00170 
00172                 float   mWorldWidth;
00173                 float   mWorldHeight;
00174 
00175                 AStarSearchManager<LevelNode>*          mPathManager;
00176 
00178                 vector<LevelNode>       mOccupied;
00179 
00180                 TeamList                                                        mTeams;
00181 
00182 
00183         // Data Types
00184         // ----------------------------------------------------------------------------
00185         public:
00186                 typedef VectorIterator<TeamList>        TeamIterator;
00187 
00188 
00189 
00190         // Construction
00191         // ----------------------------------------------------------------------------
00192         public:
00193                 Level();
00194                 ~Level(void);
00195 
00211                 static Level& getSingleton(void);
00227                 static Level* getSingletonPtr(void);
00228 
00229         // Setup
00230         // ----------------------------------------------------------------------------
00231         public:
00243                 void setDimensions ( int cellsWide, int cellsHigh );
00244 
00245                 void setWorldDimensions ( float worldWidth, float worldHeight );
00246 
00247                 void setCellSize ( float width, float height );
00248 
00250                 void reset ();
00251 
00252                 void rebuild ();
00253 
00254 
00255         // Team Management
00256         // ----------------------------------------------------------------------------
00257         public:
00258                 Team* createTeam ( const String& teamName, const ColourValue& teamColor );
00259                 void removeAllTeams ();
00260                 void removeTeam ( const String& teamName );
00261                 void removeTeam ( Team* team );
00262                 size_t getNumTeams ();
00263                 Team* getTeam ( size_t teamIndex );
00264                 TeamList getEnemyTeams ( const Team* friendlyTeam );
00265 
00266                 TeamIterator getTeamIterator ();
00267 
00268         private:
00269                 Team* _findTeam ( const String& teamName ) const;
00270 
00271 
00272         // Query Functions
00273         // ----------------------------------------------------------------------------
00274         public:
00275                 LevelNode* getNode ( const Vector3& worldPos );
00276                 void getCellLocation ( const Vector3& worldPos, int* x, int* z );
00277                 Vector3 getNodeWorldPosition ( int cellX, int cellZ );
00278                 LevelNode* getClosestNode ( const Vector3& worldPos );
00279 
00284                 UnitList getAdjacentUnits ( const Vector3& position, float radius );
00285 
00290                 void occupy ( float worldX, float worldZ );
00291                 void unoccupy ( float worldX, float worldZ );
00292 
00293                 void occupyRegion ( const AxisAlignedBox& worldRegion );
00294                 void unoccupyRegion ( const AxisAlignedBox& worldRegion );
00295 
00300                 bool isOccupied ( float worldX, float worldZ );
00301 
00305                 int getWorldHeight () const;
00306 
00310                 int getWorldWidth () const;
00311 
00315                 int getNumNodesWide () const;
00316 
00320                 int getNumNodesHigh () const;
00321 
00330                 AStarSearchManager<LevelNode>* getPathManager () const;
00331 
00332 
00333                 
00334 
00335 
00336         // Helpers
00337         // ----------------------------------------------------------------------------
00338         protected:
00339                 void _markCell ( float worldX, float worldZ, bool status );
00340         };
00341 }

Generated on Sun Jun 25 19:23:43 2006 for Valors End by  doxygen 1.4.7