FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cellcache.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE 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 *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_CELLCACHE_H
23 #define FIFE_CELLCACHE_H
24 
25 // Standard C++ library includes
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29 #include <set>
30 #include <stack>
31 
32 // 3rd party library includes
33 
34 // FIFE includes
35 // These includes are split up in two parts, separated by one empty line
36 // First block: files included from the FIFE root src directory
37 // Second block: files included from the same folder
38 #include "util/base/fifeclass.h"
39 #include "util/structures/rect.h"
41 #include "model/metamodel/object.h"
42 
43 #include "layer.h"
44 #include "cell.h"
45 
46 namespace FIFE {
47 
50  class Zone {
51  public:
55  Zone(uint32_t id);
56 
59  ~Zone();
60 
64  void addCell(Cell* cell);
65 
69  void removeCell(Cell* cell);
70 
74  void mergeZone(Zone* zone);
75 
79  const std::set<Cell*>& getCells() const;
80 
83  void resetCells();
84 
88  uint32_t getId() const;
89 
93  uint32_t getCellCount() const;
94 
99  std::vector<Cell*> getTransitionCells(Layer* layer = NULL);
100 
101  private:
105  std::set<Cell*> m_cells;
106  };
107 
111  class CellCache : public FifeClass {
112  public:
116  CellCache(Layer* layer);
117 
120  ~CellCache();
121 
124  void reset();
125 
129  void resize();
130 
135  void resize(const Rect& rec);
136 
139  void createCells();
140 
143  void forceUpdate();
144 
148  void addCell(Cell* cell);
149 
154  Cell* createCell(const ModelCoordinate& mc);
155 
160  Cell* getCell(const ModelCoordinate& mc);
161 
165  const std::vector<std::vector<Cell*> >& getCells();
166 
172  void removeCell(Cell* cell);
173 
177  void addInteractOnRuntime(Layer* interact);
178 
182  void removeInteractOnRuntime(Layer* interact);
183 
188 
192  Layer* getLayer();
193 
197  const Rect& getSize();
198 
203  void setSize(const Rect& rec);
204 
208  uint32_t getWidth();
209 
214 
219  bool isInCellCache(const Location& location) const;
220 
225  int32_t convertCoordToInt(const ModelCoordinate& coord) const;
226 
231  ModelCoordinate convertIntToCoord(const int32_t cell) const;
232 
236  int32_t getMaxIndex() const;
237 
241  void setMaxNeighborZ(int32_t z);
242 
246  int32_t getMaxNeighborZ();
247 
251  void setUpdated(bool updated);
252 
256  bool isUpdated();
257 
264  std::vector<Cell*> getCellsInLine(const ModelCoordinate& pt1, const ModelCoordinate& pt2, bool blocker = false);
265 
270  std::vector<Cell*> getCellsInRect(const Rect& rec);
271 
277  std::vector<Cell*> getCellsInCircle(const ModelCoordinate& center, uint16_t radius);
278 
283  void registerCost(const std::string& costId, double cost);
284 
288  void unregisterCost(const std::string& costId);
289 
294  double getCost(const std::string& costId);
295 
299  bool existsCost(const std::string& costId);
300 
304  std::list<std::string> getCosts();
305 
308  void unregisterAllCosts();
309 
314  void addCellToCost(const std::string& costId, Cell* cell);
315 
320  void addCellsToCost(const std::string& costId, const std::vector<Cell*>& cells);
321 
325  void removeCellFromCost(Cell* cell);
326 
331  void removeCellFromCost(const std::string& costId, Cell* cell);
332 
337  void removeCellsFromCost(const std::string& costId, const std::vector<Cell*>& cells);
338 
343  std::vector<Cell*> getCostCells(const std::string& costId);
344 
349  std::vector<std::string> getCellCosts(Cell* cell);
350 
356  bool existsCostForCell(const std::string& costId, Cell* cell);
357 
363  double getAdjacentCost(const ModelCoordinate& adjacent, const ModelCoordinate& next);
364 
371  double getAdjacentCost(const ModelCoordinate& adjacent, const ModelCoordinate& next, const std::string& costId);
372 
378  bool getCellSpeedMultiplier(const ModelCoordinate& cell, double& multiplier);
379 
383  void setDefaultCostMultiplier(double multi);
384 
388  double getDefaultCostMultiplier();
389 
393  void setDefaultSpeedMultiplier(double multi);
394 
398  double getDefaultSpeedMultiplier();
399 
404  bool isDefaultCost(Cell* cell);
405 
410  void setCostMultiplier(Cell* cell, double multi);
411 
416  double getCostMultiplier(Cell* cell);
417 
421  void resetCostMultiplier(Cell* cell);
422 
427  bool isDefaultSpeed(Cell* cell);
428 
433  void setSpeedMultiplier(Cell* cell, double multi);
434 
439  double getSpeedMultiplier(Cell* cell);
440 
444  void resetSpeedMultiplier(Cell* cell);
445 
449  void addTransition(Cell* cell);
450 
454  void removeTransition(Cell* cell);
455 
460  std::vector<Cell*> getTransitionCells(Layer* layer = NULL);
461 
465  const std::vector<Zone*>& getZones();
466 
471  Zone* getZone(uint32_t id);
472 
476  Zone* createZone();
477 
481  void removeZone(Zone* zone);
482 
486  void splitZone(Cell* cell);
487 
492  void mergeZones(Zone* zone1, Zone* zone2);
493 
498  void addNarrowCell(Cell* cell);
499 
503  const std::set<Cell*>& getNarrowCells();
504 
508  void removeNarrowCell(Cell* cell);
509 
512  void resetNarrowCells();
513 
518  bool isSearchNarrowCells();
519 
523  void setSearchNarrowCells(bool search);
524 
530  void addCellToArea(const std::string& id, Cell* cell);
531 
537  void addCellsToArea(const std::string& id, const std::vector<Cell*>& cells);
538 
542  void removeCellFromArea(Cell* cell);
543 
548  void removeCellFromArea(const std::string& id, Cell* cell);
549 
554  void removeCellsFromArea(const std::string& id, const std::vector<Cell*>& cells);
555 
559  void removeArea(const std::string& id);
560 
565  bool existsArea(const std::string& id);
566 
570  std::vector<std::string> getAreas();
571 
576  std::vector<std::string> getCellAreas(Cell* cell);
577 
582  std::vector<Cell*> getAreaCells(const std::string& id);
583 
589  bool isCellInArea(const std::string& id, Cell* cell);
590 
594  void setStaticSize(bool staticSize);
595 
599  bool isStaticSize();
600 
601  void setBlockingUpdate(bool update);
602  void setFowUpdate(bool update);
603  void setSizeUpdate(bool update);
604  void update();
605  private:
606  typedef std::multimap<std::string, Cell*> StringCellMultimap;
607  typedef StringCellMultimap::iterator StringCellIterator;
608  typedef std::pair<StringCellIterator, StringCellIterator> StringCellPair;
609 
614 
617 
620 
623 
626 
627  // cells on this cache
628  std::vector<std::vector<Cell*> > m_cells;
629 
633 
636 
639 
641  int32_t m_neighborZ;
642 
645 
648 
651 
653  bool m_updated;
654 
657 
660 
662  std::vector<Cell*> m_transitions;
663 
665  std::vector<Zone*> m_zones;
666 
668  std::set<Cell*> m_narrowCells;
669 
672 
675 
677  std::map<std::string, double> m_costsTable;
678 
681 
683  std::map<Cell*, double> m_costMultipliers;
684 
686  std::map<Cell*, double> m_speedMultipliers;
687  };
688 
689 } // FIFE
690 
691 #endif
void removeCellsFromCost(const std::string &costId, const std::vector< Cell * > &cells)
Removes cells from a cost identifier.
Definition: cellcache.cpp:1069
void removeZone(Zone *zone)
Removes zone.
Definition: cellcache.cpp:1300
uint32_t getHeight()
Returns height of the CellCache.
Definition: cellcache.cpp:818
void setStaticSize(bool staticSize)
Sets the cache size to static so that automatic resize is disabled.
Definition: cellcache.cpp:1520
StringCellMultimap::iterator StringCellIterator
Definition: cellcache.h:607
double getSpeedMultiplier(Cell *cell)
Returns speed multiplier for the cell.
Definition: cellcache.cpp:1213
uint32_t m_width
cache width
Definition: cellcache.h:635
void removeInteractOnRuntime(Layer *interact)
Removes a interact layer from the CellCache on runtime and sets all needed layer properties.
Definition: cellcache.cpp:768
bool isUpdated()
Gets whether the CellCache need to be updated.
Definition: cellcache.cpp:862
void unregisterCost(const std::string &costId)
Removes a cost with the given id.
Definition: cellcache.cpp:989
Layer * m_layer
walkable layer
Definition: cellcache.h:616
double getCostMultiplier(Cell *cell)
Returns cost multiplier for the cell.
Definition: cellcache.cpp:1183
int32_t convertCoordToInt(const ModelCoordinate &coord) const
Convertes coordinate to unique identifier.
Definition: cellcache.cpp:835
void addCell(Cell *cell)
Adds a cell to this zone.
Definition: cellcache.cpp:270
void setSpeedMultiplier(Cell *cell, double multi)
Sets speed multiplier for the cell.
Definition: cellcache.cpp:1204
void resize()
Checks the layer size and if the size is different with current size then the cache size is adjusted...
Definition: cellcache.cpp:475
int32_t getMaxNeighborZ()
Gets maximal z range for neighbors.
Definition: cellcache.cpp:854
std::vector< std::string > getCellCosts(Cell *cell)
Returns cost identifiers for cell.
Definition: cellcache.cpp:1086
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:136
bool isDefaultSpeed(Cell *cell)
Gets if cell uses default speed multiplier.
Definition: cellcache.cpp:1196
void removeTransition(Cell *cell)
Removes a cell as transition.
Definition: cellcache.cpp:1230
bool isCellInArea(const std::string &id, Cell *cell)
Returns true if cell is part of the area, otherwise false.
Definition: cellcache.cpp:1488
std::set< Cell * > m_cells
cells in the zone
Definition: cellcache.h:105
Layer * getLayer()
Returns layer.
Definition: cellcache.cpp:802
Listener interface for changes happening on a cell.
Definition: cell.h:110
std::set< Cell * > m_narrowCells
special cells which are monitored (zone split and merge)
Definition: cellcache.h:668
void setSearchNarrowCells(bool search)
Sets if narrow cells should be searched automatic.
Definition: cellcache.cpp:1398
void removeCellFromCost(Cell *cell)
Removes a cell from costs.
Definition: cellcache.cpp:1047
std::vector< Cell * > getCellsInRect(const Rect &rec)
Returns all cells in the rect.
Definition: cellcache.cpp:911
StringCellMultimap m_costsToCells
holds cells for each cost
Definition: cellcache.h:680
StringCellMultimap m_cellAreas
areas with assigned cells
Definition: cellcache.h:671
A CellCache is an abstract depiction of one or a few layers and contains additional information...
Definition: cellcache.h:111
bool m_updated
need update
Definition: cellcache.h:653
bool m_blockingUpdate
indicates blocking update
Definition: cellcache.h:644
void resetNarrowCells()
Resets narrow cells.
Definition: cellcache.cpp:1386
void setDefaultCostMultiplier(double multi)
Sets default cost for this CellCache.
Definition: cellcache.cpp:1150
const std::set< Cell * > & getCells() const
Returns all cells of this zone.
Definition: cellcache.cpp:294
void addCellToCost(const std::string &costId, Cell *cell)
Assigns a cell to a cost identifier.
Definition: cellcache.cpp:1027
bool isDefaultCost(Cell *cell)
Gets if cell uses default cost multiplier.
Definition: cellcache.cpp:1166
void addCellsToArea(const std::string &id, const std::vector< Cell * > &cells)
Adds few cell to a specific area group.
Definition: cellcache.cpp:1406
void setBlockingUpdate(bool update)
Definition: cellcache.cpp:1528
std::vector< std::vector< Cell * > > m_cells
Definition: cellcache.h:628
Base class for all fife classes Used e.g.
Definition: fifeclass.h:42
void registerCost(const std::string &costId, double cost)
Adds a cost with the given id and value.
Definition: cellcache.cpp:980
std::vector< Cell * > getCellsInLine(const ModelCoordinate &pt1, const ModelCoordinate &pt2, bool blocker=false)
Returns all cells in the line.
Definition: cellcache.cpp:866
void resetSpeedMultiplier(Cell *cell)
Resets the speed multiplier for the cell.
Definition: cellcache.cpp:1222
bool existsCost(const std::string &costId)
Returns if the cost for the given id exists.
Definition: cellcache.cpp:1005
double m_defaultCostMulti
default cost
Definition: cellcache.h:619
uint32_t getCellCount() const
Returns the number of cells.
Definition: cellcache.cpp:306
std::vector< Cell * > getCostCells(const std::string &costId)
Returns cells for a cost identifier.
Definition: cellcache.cpp:1076
void removeCell(Cell *cell)
Removes a cell from this zone.
Definition: cellcache.cpp:277
void setSize(const Rect &rec)
Sets CellCache size.
Definition: cellcache.cpp:810
uint32_t getId() const
Returns the zone identifier.
Definition: cellcache.cpp:302
std::vector< Zone * > m_zones
zones
Definition: cellcache.h:665
void removeArea(const std::string &id)
Removes a area.
Definition: cellcache.cpp:1442
void removeCellFromArea(Cell *cell)
Removes the cell from all areas.
Definition: cellcache.cpp:1413
void setFowUpdate(bool update)
Definition: cellcache.cpp:1532
int32_t m_neighborZ
max z value for neighbors
Definition: cellcache.h:641
const std::vector< Zone * > & getZones()
Returns zones of this CellCache.
Definition: cellcache.cpp:1279
ModelCoordinate convertIntToCoord(const int32_t cell) const
Convertes unique identifier to coordinate.
Definition: cellcache.cpp:840
std::map< Cell *, double > m_speedMultipliers
holds default speed multiplier, only if it is not default(1.0)
Definition: cellcache.h:686
void resetCells()
Remove all cells from zone but does not alter the cells.
Definition: cellcache.cpp:298
void unregisterAllCosts()
Removes all costs.
Definition: cellcache.cpp:1022
double getAdjacentCost(const ModelCoordinate &adjacent, const ModelCoordinate &next)
Returns cost for movement between these two adjacent coordinates.
Definition: cellcache.cpp:1108
std::map< std::string, double > m_costsTable
holds cost table
Definition: cellcache.h:677
const Rect & getSize()
Returns CellCache size.
Definition: cellcache.cpp:806
~Zone()
Destructor.
Definition: cellcache.cpp:264
~CellCache()
Destructor.
Definition: cellcache.cpp:419
bool isInCellCache(const Location &location) const
Checks whether the location is in CellCache range.
Definition: cellcache.cpp:822
double getDefaultSpeedMultiplier()
Gets default speed for this CellCache.
Definition: cellcache.cpp:1162
void removeCellsFromArea(const std::string &id, const std::vector< Cell * > &cells)
Removes few cells from a area.
Definition: cellcache.cpp:1435
Cell * getCell(const ModelCoordinate &mc)
Returns cell on this coordinate.
Definition: cellcache.cpp:706
CellCache(Layer *layer)
Constructor.
Definition: cellcache.cpp:371
std::list< std::string > getCosts()
Returns all registered cost ids.
Definition: cellcache.cpp:1013
Zone * createZone()
Creates zone.
Definition: cellcache.cpp:1257
std::vector< Cell * > getTransitionCells(Layer *layer=NULL)
Returns transistion cells of this CellCache.
Definition: cellcache.cpp:1240
void addCellsToCost(const std::string &costId, const std::vector< Cell * > &cells)
Assigns cells to a cost identifier.
Definition: cellcache.cpp:1040
void setUpdated(bool updated)
Sets whether the CellCache need to be updated.
Definition: cellcache.cpp:858
A basic layer on a map.
Definition: layer.h:98
bool existsCostForCell(const std::string &costId, Cell *cell)
Gets if cell is assigned to cost identifier.
Definition: cellcache.cpp:1097
uint32_t m_id
identifier
Definition: cellcache.h:103
void splitZone(Cell *cell)
Splits zone on the cell.
Definition: cellcache.cpp:1310
Listener interface for changes happening on a layer.
Definition: layer.h:70
bool m_searchNarrow
is automatic seach enabled
Definition: cellcache.h:656
CellChangeListener * m_cellZoneListener
listener for zones
Definition: cellcache.h:674
double m_defaultSpeedMulti
default speed
Definition: cellcache.h:622
std::vector< std::string > getCellAreas(Cell *cell)
Returns all areas of a cell.
Definition: cellcache.cpp:1467
A basic cell on a CellCache.
Definition: cell.h:136
void forceUpdate()
Updates all cells.
Definition: cellcache.cpp:682
unsigned short uint16_t
Definition: core.h:39
void createCells()
Creates cells for this CellCache based on the size of the assigned layer.
Definition: cellcache.cpp:586
void addCell(Cell *cell)
Adds cell to this CellCache.
Definition: cellcache.cpp:692
LayerChangeListener * m_cellListener
change listener
Definition: cellcache.h:625
bool getCellSpeedMultiplier(const ModelCoordinate &cell, double &multiplier)
Returns speed value from cell.
Definition: cellcache.cpp:1138
Rect m_size
Rect holds the min and max size x = min.x, w = max.x, y = min.y, h = max.y.
Definition: cellcache.h:632
Zone * getZone(uint32_t id)
Gets zone by identifier.
Definition: cellcache.cpp:1283
std::vector< Cell * > getAreaCells(const std::string &id)
Returns all cells of an area.
Definition: cellcache.cpp:1478
void setDefaultSpeedMultiplier(double multi)
Sets default speed for this CellCache.
Definition: cellcache.cpp:1158
std::map< Cell *, double > m_costMultipliers
holds default cost multiplier, only if it is not default(1.0)
Definition: cellcache.h:683
uint32_t m_height
cache height
Definition: cellcache.h:638
std::multimap< std::string, Cell * > StringCellMultimap
Definition: cellcache.h:606
void mergeZones(Zone *zone1, Zone *zone2)
Merges two zones to one.
Definition: cellcache.cpp:1353
bool m_fowUpdate
indicates fow update
Definition: cellcache.h:647
bool isStaticSize()
Returns if the cache size is static.
Definition: cellcache.cpp:1524
void removeNarrowCell(Cell *cell)
Removes cell from narrow cells.
Definition: cellcache.cpp:1378
void addCellToArea(const std::string &id, Cell *cell)
Adds a cell to a specific area group.
Definition: cellcache.cpp:1402
Rect calculateCurrentSize()
Returns the current size.
Definition: cellcache.cpp:1499
std::vector< Cell * > getCellsInCircle(const ModelCoordinate &center, uint16_t radius)
Returns all cells in the circle.
Definition: cellcache.cpp:928
void mergeZone(Zone *zone)
Merge two zones to one.
Definition: cellcache.cpp:285
A 3D Point.
Definition: point.h:202
LayerChangeListener * getCellCacheChangeListener()
Returns change listener.
Definition: cellcache.cpp:798
void setSizeUpdate(bool update)
Definition: cellcache.cpp:1536
void removeCell(Cell *cell)
Removes cell from CellCache.
Definition: cellcache.cpp:721
const std::vector< std::vector< Cell * > > & getCells()
Returns all cells of this CellCache.
Definition: cellcache.cpp:717
void addTransition(Cell *cell)
Adds a cell as transition.
Definition: cellcache.cpp:1226
Cell * createCell(const ModelCoordinate &mc)
Creates cell on this CellCache.
Definition: cellcache.cpp:697
std::pair< StringCellIterator, StringCellIterator > StringCellPair
Definition: cellcache.h:608
bool m_staticSize
is automatic size update enabled/disabled
Definition: cellcache.h:659
Zone(uint32_t id)
Constructor.
Definition: cellcache.cpp:260
void addNarrowCell(Cell *cell)
Adds cell to narrow cells.
Definition: cellcache.cpp:1367
std::vector< std::string > getAreas()
Returns all area ids.
Definition: cellcache.cpp:1454
bool m_sizeUpdate
indicates size update
Definition: cellcache.h:650
double getCost(const std::string &costId)
Returns the cost value for the given id.
Definition: cellcache.cpp:997
const std::set< Cell * > & getNarrowCells()
Returns narrow cells.
Definition: cellcache.cpp:1374
unsigned int uint32_t
Definition: core.h:40
double getDefaultCostMultiplier()
Gets default cost for this CellCache.
Definition: cellcache.cpp:1154
void setMaxNeighborZ(int32_t z)
Sets maximal z range for neighbors.
Definition: cellcache.cpp:850
uint32_t getWidth()
Returns width of the CellCache.
Definition: cellcache.cpp:814
bool existsArea(const std::string &id)
Checks whether the area exists.
Definition: cellcache.cpp:1446
std::vector< Cell * > getTransitionCells(Layer *layer=NULL)
Returns transistion cells of this zone.
Definition: cellcache.cpp:310
std::vector< Cell * > m_transitions
cells with transitions
Definition: cellcache.h:662
int32_t getMaxIndex() const
Returns the number of cells on this CellCache.
Definition: cellcache.cpp:845
bool isSearchNarrowCells()
Gets if narrow cells should be searched automatic.
Definition: cellcache.cpp:1394
void resetCostMultiplier(Cell *cell)
Resets the cost multiplier for the cell.
Definition: cellcache.cpp:1192
void addInteractOnRuntime(Layer *interact)
Adds a interact layer to the CellCache on runtime and sets all needed layer properties.
Definition: cellcache.cpp:739
A Zone is an abstract depiction of a CellCache or of a part of it.
Definition: cellcache.h:50
void setCostMultiplier(Cell *cell, double multi)
Sets cost multiplier for the cell.
Definition: cellcache.cpp:1174
void reset()
Resets the CellCache.
Definition: cellcache.cpp:436