Tactics: Western Philosophers Vs. Musicians  0.12
A turn-based tactical game combining rules and gameplay elements inspired by Final Fantasy Tactics and the Mayfair Exponential Game System. Unlike most games of this type, motion is in full, grid-less 3D.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CDGrid.h
Go to the documentation of this file.
1 // Copyright (C) 2004-2012 Dylan Blair
3 //
4 // email: dblair@alumni.cs.utexas.edu
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 #ifndef CDGRID_H
22 #define CDGRID_H
23 
24 #include "../Utils_LIB/UHash.h"
25 
26 #ifdef USE_EASTL
27 #include <EASTL/fixed_list.h>
28 #include <EASTL/fixed_map.h>
29 #endif //USE_EASTL
30 
31 #include <vector>
32 #include <list>
33 #include <set>
34 #include <map>
35 
36 #include "CDVolume.h"
37 
38 namespace OpenSkyNet {
39  namespace Utils {
42  return getCompositeKey(bin_.x(), bin_.y(), bin_.z());
43  }
44 
47  assert(bin_.x() >= 0);
48  assert(bin_.y() >= 0);
49  assert(bin_.z() >= 0);
50  return getCompositeKey(static_cast<Utils::uint>(bin_.x()),
51  static_cast<Utils::uint>(bin_.y()), static_cast<Utils::uint>(bin_.z()));
52  }
53  }
54 
55  namespace PF {
56  class Manager;
57  }
58 
59  namespace CD {
62  //12 edges
67  //8 corners
70 
73 
76 
79 
85 
92  class Grid {
93  #ifdef _WIN32
94  #pragma warning(push)
95  #pragma warning(disable:4100) //unreferenced formal parameter
96  #endif
97  Grid& operator=(const Grid& rhs_) { return *this; }
98  #ifdef _WIN32
99  #pragma warning(pop)
100  #endif
101  public:
104 
107 
110 
113 
116 
119 
122 
128  struct BinDir {
131  BinDir(const Math::Point<int>& bin_, DIRECTION dir_) : _bin(&bin_), _dir(dir_) {}
132  };
133 
135 #ifdef USE_EASTL
136  typedef eastl::fixed_list<CD::Volume*, 8> BinOccupiersList;
137  typedef eastl::fixed_list<const Math::Point<int>*, 64> OccupiedBinsList;
138  typedef eastl::fixed_map<const Math::Point<int>*, BinOccupiersList, 64> BinOccupiersMap;
139  typedef eastl::fixed_map<CD::Volume*, OccupiedBinsList, 8> OccupiedBinsMap;
140 #else
141  typedef std::list<CD::Volume*> BinOccupiersList;
142  typedef std::list<const Math::Point<int>*> OccupiedBinsList;
143  typedef std::map<const Math::Point<int>*, BinOccupiersList> BinOccupiersMap;
144  typedef std::map<CD::Volume*, OccupiedBinsList> OccupiedBinsMap;
145 #endif
146  protected:
147  friend class PF::Manager;
148 
150 
152 
155 
157 
161 
162  std::set<CD::Volume*>** _nodes;
163 
164  bool _isSub;
168 
169  void makeSubGrid();
170 
171  void cullObjectOutside(int& minBinX_, int& maxBinX_, int& minBinY_, int& maxBinY_, int& minBinZ_, int& maxBinZ_) const;
172  void cullObjectOutside(Math::Point<int>& bin_) const;
173 
175  const Math::Point<Utils::uint>& maxBin_, const Math::Point<>& maxCenter_);
176 
178 
181  public:
182  Grid(Utils::uint initXDivs_, Utils::uint initYDivs_, Utils::uint initZDivs_, Utils::uint maxSubdivs_);
183  Grid(const Grid& grid_);
184  virtual ~Grid();
185 
188 
189  static void init(Utils::uint initGlobalXDivs_, Utils::uint initGlobalYDivs_, Utils::uint initGlobalZDivs_, Utils::uint maxGlobalSubdivs_);
190 
199  void setCorners(const Math::Point<>* corners_);
200 
202  inline void setNodes(std::set<CD::Volume*>& nodes_) { *_nodes = &nodes_; }
203 
204  inline std::set<CD::Volume*>* getNodes() const { return *_nodes; }
205 
206  void resetGrid();
207 
210  static void resetAllGlobalGrids();
211 
213 
219  virtual bool calcOccupiedBins(CD::Volume* vol_, bool calcPlanes_=true, bool useOctalPartitioning_=false, bool decrementPrevOccupiedBins_=false);
220 
225  virtual bool calcOccupiedBins(bool calcPlanes_=true, bool useOctalPartitioning_=false);
226 
228  inline const Math::Point<>& getMaxCorner() const { return *_maxCorner; }
229 
232  inline void translateMaxCorner() { *_maxCorner = (*_corners)[2]; }
233 
235  inline const Math::Point<>& getSize() const { return *_size; }
236 
238  inline const Math::Point<Utils::uint>& getAxisDivs() const { return _axisDivisions; }
239 
242  inline const Math::Point<>& getBinSize() const { return _subBinSize; }
243 
244  inline bool isSubdivided() const { return _isSub; }
245 
247  inline const Utils::uint& getSubNum() const { return _subNum; }
248 
250  inline void getBin(const Math::Point<>& p_, Math::Point<int>& bin_) const {
251  bin_.x() = static_cast<int>(floor((_maxCorner->x()-p_.x())/_subBinSize.x()));
252  bin_.y() = static_cast<int>(floor((_maxCorner->y()-p_.y())/_subBinSize.y()));
253  bin_.z() = static_cast<int>(floor((_maxCorner->z()-p_.z())/_subBinSize.z()));
254  }
255 
256  inline unsigned short getNumOccupants(const Math::Point<int>& bin_) {
258  }
259 
261  inline bool decrementBin(const Math::Point<int>& bin_) {
262  unsigned short& currVal = _isBinOccupied.add(Utils::getCompositeKey(bin_));
263  //currVal == 0 iff this bin is not occupied
264  if (currVal) {
265  --currVal;
266  return true;
267  }
268  return false;
269  }
270 
272  inline void incrementBin(const Math::Point<int>& bin_) {
274  }
275 
277  inline void clearBin(const Math::Point<int>& bin_) {
279  }
280 
282  bool isObjectOutside(const int& minBinX_, const int& maxBinX_, const int& minBinY_,
283  const int& maxBinY_, const int& minBinZ_, const int& maxBinZ_) const;
284 
285  bool isObjectOutside(const Math::Point<int>& bin_) const;
286 
288  void getOccupiedBins(std::vector<const Math::Point<int>* >& bins_) const;
289 
291  return _occupiedBins[vol_];
292  }
293 
295  return _binOccupiers[bin_];
296  }
297 
298  void getNeighbors(const Math::Point<int>& bin_,
299  std::vector<BinDir>& neighbors_, Utils::uint numOccupants_=0,
300  bool allowDiagonals_=false, bool allowCorners_=false) const;
301 
302  Math::Point<> translatePointByBin(const Math::Point<>& p_, const Math::Point<int>& bin_) const;
303 
305 
308  static void shutDown();
310  };
311 
312  extern CD::Grid* g_initialGrid;
313  #define MAX_GLOBAL_GRID_SUBDIVISIONS 2
314  #if MAX_GLOBAL_GRID_SUBDIVISIONS > 0
316  #else
317  //can't have zero-sized array
318  extern CD::Grid* g_subdividedGrids[1];
319  #endif
320  }
321 }
322 
323 #endif //CDGRID_H
Definition: CDGrid.h:69
const Utils::uint MAX_X_DIVISIONS_FOR_ALL_GRIDS
Definition: CDGrid.h:72
Definition: CDGrid.h:61
const Math::Point< Utils::uint > & getAxisDivs() const
Definition: CDGrid.h:238
Definition: CDGrid.h:64
Definition: CDGrid.h:63
const Utils::uint MAX_SUBDIVISIONS
Definition: CDGrid.h:112
Definition: CDGrid.h:63
void setCorners(const Math::Point<> *corners_)
Definition: CDGrid.cpp:95
Definition: CDGrid.h:61
void translateMaxCorner()
Definition: CDGrid.h:232
std::map< const Math::Point< int > *, BinOccupiersList > BinOccupiersMap
Definition: CDGrid.h:143
const Utils::uint MAX_X_DIVISIONS
Definition: CDGrid.h:115
Grid * g_initialGrid
Definition: CDGrid.cpp:18
BinOccupiersList & getOccupiers(const Math::Point< int > *bin_)
Definition: CDGrid.h:294
Utils::uint _subNum
Definition: CDGrid.h:165
void resetGrid()
Definition: CDGrid.cpp:124
void calcOccupiedBinsUsingOctalPartitioning(Volume *vol_, const Math::Point< Utils::uint > &minBin_, const Math::Point< Utils::uint > &maxBin_, const Math::Point<> &maxCenter_)
Definition: CDGrid.cpp:180
Definition: CDGrid.h:66
Definition: CDGrid.h:64
BinOccupiersMap _binOccupiers
Definition: CDGrid.h:179
T & y()
Definition: MPoint.h:65
const Math::Point ** _corners
Definition: CDGrid.h:151
static void resetAllGlobalGrids()
Definition: CDGrid.cpp:115
Math::Point * _binSize
Definition: CDGrid.h:160
const Utils::uint INITIAL_Y_DIVISIONS
Definition: CDGrid.h:106
void getBin(const Math::Point<> &p_, Math::Point< int > &bin_) const
Definition: CDGrid.h:250
std::map< CD::Volume *, OccupiedBinsList > OccupiedBinsMap
Definition: CDGrid.h:144
Math::Point * _maxCorner
Definition: CDGrid.h:154
Definition: CDGrid.h:68
void cullObjectOutside(int &minBinX_, int &maxBinX_, int &minBinY_, int &maxBinY_, int &minBinZ_, int &maxBinZ_) const
Definition: CDGrid.h:61
bool decrementBin(const Math::Point< int > &bin_)
Definition: CDGrid.h:261
T & x()
Definition: MPoint.h:64
Utils::uint getCompositeKey(const Math::Point< Utils::uint > &bin_)
Definition: CDGrid.h:41
std::list< const Math::Point< int > * > OccupiedBinsList
Definition: CDGrid.h:142
Math::Point _subBinSize
Definition: CDGrid.h:167
const Math::Point & getMaxCorner() const
Definition: CDGrid.h:228
Utils::HashTableUIntKeys< unsigned short > _isBinOccupied
Definition: CDGrid.h:177
const Math::Point & getSize() const
Definition: CDGrid.h:235
DIRECTION
Definition: CDGrid.h:61
Definition: CDVolume.h:225
std::list< CD::Volume * > BinOccupiersList
Definition: CDGrid.h:141
bool _didDynamicallyAllocateMem
Definition: CDGrid.h:149
Definition: CDGrid.h:63
const Utils::uint INITIAL_Z_DIVISIONS
Definition: CDGrid.h:109
Definition: CDGrid.h:68
const Math::Point & getBinSize() const
Definition: CDGrid.h:242
#define MAX_GLOBAL_GRID_SUBDIVISIONS
Definition: CDGrid.h:313
Definition: CDGrid.h:64
void getOccupiedBins(std::vector< const Math::Point< int > * > &bins_) const
bool _isSub
Definition: CDGrid.h:164
void clearBin(const Math::Point< int > &bin_)
Definition: CDGrid.h:277
Grid(Utils::uint initXDivs_, Utils::uint initYDivs_, Utils::uint initZDivs_, Utils::uint maxSubdivs_)
Definition: CDGrid.cpp:28
Definition: CDGrid.h:64
static void shutDown()
Definition: CDGrid.cpp:85
Point< int > g_binLookupTable[MAX_X_DIVISIONS_FOR_ALL_GRIDS][MAX_Y_DIVISIONS_FOR_ALL_GRIDS][MAX_Z_DIVISIONS_FOR_ALL_GRIDS]
Definition: CDGrid.cpp:16
bool isSubdivided() const
Definition: CDGrid.h:244
Definition: CDGrid.h:68
static void init(Utils::uint initGlobalXDivs_, Utils::uint initGlobalYDivs_, Utils::uint initGlobalZDivs_, Utils::uint maxGlobalSubdivs_)
Definition: CDGrid.cpp:70
void makeSubGrid()
Definition: CDGrid.cpp:133
const Math::Point< int > * _bin
Definition: CDGrid.h:129
Definition: CDGrid.h:69
Definition: CDGrid.h:69
unsigned int uint
Definition: UTypes.h:39
std::set< CD::Volume * > * getNodes() const
Definition: CDGrid.h:204
Definition: CDGrid.h:68
Definition: CDGrid.h:69
T & z()
Definition: MPoint.h:66
Definition: CDGrid.h:63
DIRECTION _dir
Definition: CDGrid.h:130
const Utils::uint & getSubNum() const
Definition: CDGrid.h:247
Definition: PFManager.h:129
void add(const uint &key_, const T &value_)
Definition: UHash.h:185
Definition: CDGrid.h:61
std::set< CD::Volume * > ** _nodes
Definition: CDGrid.h:162
OccupiedBinsList & getOccupiedBins(CD::Volume *vol_)
Definition: CDGrid.h:290
OccupiedBinsMap _occupiedBins
Definition: CDGrid.h:180
virtual bool calcOccupiedBins(CD::Volume *vol_, bool calcPlanes_=true, bool useOctalPartitioning_=false, bool decrementPrevOccupiedBins_=false)
Grid * g_subdividedGrids[MAX_GLOBAL_GRID_SUBDIVISIONS]
Definition: CDGrid.cpp:20
const Utils::uint MAX_Z_DIVISIONS_FOR_ALL_GRIDS
Definition: CDGrid.h:78
const Utils::uint INITIAL_X_DIVISIONS
Definition: CDGrid.h:103
Math::Point getNearestBinCenter(const Math::Point<> &p_) const
Definition: CDGrid.cpp:609
Definition: CDGrid.h:66
Math::Point translatePointByBin(const Math::Point<> &p_, const Math::Point< int > &bin_) const
Definition: CDGrid.cpp:598
Definition: CDGrid.h:61
void setNodes(std::set< CD::Volume * > &nodes_)
Definition: CDGrid.h:202
const Utils::uint MAX_Z_DIVISIONS
Definition: CDGrid.h:121
bool isObjectOutside(const int &minBinX_, const int &maxBinX_, const int &minBinY_, const int &maxBinY_, const int &minBinZ_, const int &maxBinZ_) const
Definition: CDGrid.h:65
Definition: CDGrid.h:92
virtual ~Grid()
Definition: CDGrid.cpp:60
void getNeighbors(const Math::Point< int > &bin_, std::vector< BinDir > &neighbors_, Utils::uint numOccupants_=0, bool allowDiagonals_=false, bool allowCorners_=false) const
Definition: CDGrid.cpp:460
const Utils::uint MAX_Y_DIVISIONS_FOR_ALL_GRIDS
Definition: CDGrid.h:75
Definition: CDGrid.h:61
void incrementBin(const Math::Point< int > &bin_)
Definition: CDGrid.h:272
Math::Point * _size
Definition: CDGrid.h:156
const Utils::uint MAX_Y_DIVISIONS
Definition: CDGrid.h:118
Definition: CDGrid.h:61
Math::Point< Utils::uint > _axisDivisions
Definition: CDGrid.h:166
BinDir(const Math::Point< int > &bin_, DIRECTION dir_)
Definition: CDGrid.h:131
unsigned short getNumOccupants(const Math::Point< int > &bin_)
Definition: CDGrid.h:256
Definition: CDGrid.h:65
Definition: CDGrid.h:128