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 
286  std::vector<Cell*> getCellsInCircleSegment(const ModelCoordinate& center, uint16_t radius, int32_t sangle, int32_t eangle);
287 
292  void registerCost(const std::string& costId, double cost);
293 
297  void unregisterCost(const std::string& costId);
298 
303  double getCost(const std::string& costId);
304 
308  bool existsCost(const std::string& costId);
309 
313  std::list<std::string> getCosts();
314 
317  void unregisterAllCosts();
318 
323  void addCellToCost(const std::string& costId, Cell* cell);
324 
329  void addCellsToCost(const std::string& costId, const std::vector<Cell*>& cells);
330 
334  void removeCellFromCost(Cell* cell);
335 
340  void removeCellFromCost(const std::string& costId, Cell* cell);
341 
346  void removeCellsFromCost(const std::string& costId, const std::vector<Cell*>& cells);
347 
352  std::vector<Cell*> getCostCells(const std::string& costId);
353 
358  std::vector<std::string> getCellCosts(Cell* cell);
359 
365  bool existsCostForCell(const std::string& costId, Cell* cell);
366 
372  double getAdjacentCost(const ModelCoordinate& adjacent, const ModelCoordinate& next);
373 
380  double getAdjacentCost(const ModelCoordinate& adjacent, const ModelCoordinate& next, const std::string& costId);
381 
387  bool getCellSpeedMultiplier(const ModelCoordinate& cell, double& multiplier);
388 
392  void setDefaultCostMultiplier(double multi);
393 
397  double getDefaultCostMultiplier();
398 
402  void setDefaultSpeedMultiplier(double multi);
403 
407  double getDefaultSpeedMultiplier();
408 
413  bool isDefaultCost(Cell* cell);
414 
419  void setCostMultiplier(Cell* cell, double multi);
420 
425  double getCostMultiplier(Cell* cell);
426 
430  void resetCostMultiplier(Cell* cell);
431 
436  bool isDefaultSpeed(Cell* cell);
437 
442  void setSpeedMultiplier(Cell* cell, double multi);
443 
448  double getSpeedMultiplier(Cell* cell);
449 
453  void resetSpeedMultiplier(Cell* cell);
454 
458  void addTransition(Cell* cell);
459 
463  void removeTransition(Cell* cell);
464 
469  std::vector<Cell*> getTransitionCells(Layer* layer = NULL);
470 
474  const std::vector<Zone*>& getZones();
475 
480  Zone* getZone(uint32_t id);
481 
485  Zone* createZone();
486 
490  void removeZone(Zone* zone);
491 
495  void splitZone(Cell* cell);
496 
501  void mergeZones(Zone* zone1, Zone* zone2);
502 
507  void addNarrowCell(Cell* cell);
508 
512  const std::set<Cell*>& getNarrowCells();
513 
517  void removeNarrowCell(Cell* cell);
518 
521  void resetNarrowCells();
522 
527  bool isSearchNarrowCells();
528 
532  void setSearchNarrowCells(bool search);
533 
539  void addCellToArea(const std::string& id, Cell* cell);
540 
546  void addCellsToArea(const std::string& id, const std::vector<Cell*>& cells);
547 
551  void removeCellFromArea(Cell* cell);
552 
557  void removeCellFromArea(const std::string& id, Cell* cell);
558 
563  void removeCellsFromArea(const std::string& id, const std::vector<Cell*>& cells);
564 
568  void removeArea(const std::string& id);
569 
574  bool existsArea(const std::string& id);
575 
579  std::vector<std::string> getAreas();
580 
585  std::vector<std::string> getCellAreas(Cell* cell);
586 
591  std::vector<Cell*> getAreaCells(const std::string& id);
592 
598  bool isCellInArea(const std::string& id, Cell* cell);
599 
603  void setStaticSize(bool staticSize);
604 
608  bool isStaticSize();
609 
610  void setBlockingUpdate(bool update);
611  void setFowUpdate(bool update);
612  void setSizeUpdate(bool update);
613  void update();
614  private:
615  typedef std::multimap<std::string, Cell*> StringCellMultimap;
616  typedef StringCellMultimap::iterator StringCellIterator;
617  typedef std::pair<StringCellIterator, StringCellIterator> StringCellPair;
618 
623 
626 
629 
632 
635 
636  // cells on this cache
637  std::vector<std::vector<Cell*> > m_cells;
638 
642 
645 
648 
650  int32_t m_neighborZ;
651 
654 
657 
660 
662  bool m_updated;
663 
666 
669 
671  std::vector<Cell*> m_transitions;
672 
674  std::vector<Zone*> m_zones;
675 
677  std::set<Cell*> m_narrowCells;
678 
681 
684 
686  std::map<std::string, double> m_costsTable;
687 
690 
692  std::map<Cell*, double> m_costMultipliers;
693 
695  std::map<Cell*, double> m_speedMultipliers;
696  };
697 
698 } // FIFE
699 
700 #endif
void removeCellsFromCost(const std::string &costId, const std::vector< Cell * > &cells)
Removes cells from a cost identifier.
Definition: cellcache.cpp:1063
void removeZone(Zone *zone)
Removes zone.
Definition: cellcache.cpp:1294
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:1514
StringCellMultimap::iterator StringCellIterator
Definition: cellcache.h:616
double getSpeedMultiplier(Cell *cell)
Returns speed multiplier for the cell.
Definition: cellcache.cpp:1207
uint32_t m_width
cache width
Definition: cellcache.h:644
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:983
Layer * m_layer
walkable layer
Definition: cellcache.h:625
double getCostMultiplier(Cell *cell)
Returns cost multiplier for the cell.
Definition: cellcache.cpp:1177
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:1198
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:1080
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:1190
void removeTransition(Cell *cell)
Removes a cell as transition.
Definition: cellcache.cpp:1224
bool isCellInArea(const std::string &id, Cell *cell)
Returns true if cell is part of the area, otherwise false.
Definition: cellcache.cpp:1482
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:677
std::vector< Cell * > getCellsInCircleSegment(const ModelCoordinate &center, uint16_t radius, int32_t sangle, int32_t eangle)
Returns all cells in the circle segment.
Definition: cellcache.cpp:952
void setSearchNarrowCells(bool search)
Sets if narrow cells should be searched automatic.
Definition: cellcache.cpp:1392
void removeCellFromCost(Cell *cell)
Removes a cell from costs.
Definition: cellcache.cpp:1041
std::vector< Cell * > getCellsInRect(const Rect &rec)
Returns all cells in the rect.
Definition: cellcache.cpp:883
StringCellMultimap m_costsToCells
holds cells for each cost
Definition: cellcache.h:689
StringCellMultimap m_cellAreas
areas with assigned cells
Definition: cellcache.h:680
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:662
bool m_blockingUpdate
indicates blocking update
Definition: cellcache.h:653
void resetNarrowCells()
Resets narrow cells.
Definition: cellcache.cpp:1380
void setDefaultCostMultiplier(double multi)
Sets default cost for this CellCache.
Definition: cellcache.cpp:1144
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:1021
bool isDefaultCost(Cell *cell)
Gets if cell uses default cost multiplier.
Definition: cellcache.cpp:1160
void addCellsToArea(const std::string &id, const std::vector< Cell * > &cells)
Adds few cell to a specific area group.
Definition: cellcache.cpp:1400
void setBlockingUpdate(bool update)
Definition: cellcache.cpp:1522
std::vector< std::vector< Cell * > > m_cells
Definition: cellcache.h:637
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:974
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:1216
bool existsCost(const std::string &costId)
Returns if the cost for the given id exists.
Definition: cellcache.cpp:999
double m_defaultCostMulti
default cost
Definition: cellcache.h:628
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:1070
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:674
void removeArea(const std::string &id)
Removes a area.
Definition: cellcache.cpp:1436
void removeCellFromArea(Cell *cell)
Removes the cell from all areas.
Definition: cellcache.cpp:1407
void setFowUpdate(bool update)
Definition: cellcache.cpp:1526
int32_t m_neighborZ
max z value for neighbors
Definition: cellcache.h:650
const std::vector< Zone * > & getZones()
Returns zones of this CellCache.
Definition: cellcache.cpp:1273
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:695
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:1016
double getAdjacentCost(const ModelCoordinate &adjacent, const ModelCoordinate &next)
Returns cost for movement between these two adjacent coordinates.
Definition: cellcache.cpp:1102
std::map< std::string, double > m_costsTable
holds cost table
Definition: cellcache.h:686
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:1156
void removeCellsFromArea(const std::string &id, const std::vector< Cell * > &cells)
Removes few cells from a area.
Definition: cellcache.cpp:1429
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:1007
Zone * createZone()
Creates zone.
Definition: cellcache.cpp:1251
std::vector< Cell * > getTransitionCells(Layer *layer=NULL)
Returns transistion cells of this CellCache.
Definition: cellcache.cpp:1234
void addCellsToCost(const std::string &costId, const std::vector< Cell * > &cells)
Assigns cells to a cost identifier.
Definition: cellcache.cpp:1034
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:99
bool existsCostForCell(const std::string &costId, Cell *cell)
Gets if cell is assigned to cost identifier.
Definition: cellcache.cpp:1091
uint32_t m_id
identifier
Definition: cellcache.h:103
void splitZone(Cell *cell)
Splits zone on the cell.
Definition: cellcache.cpp:1304
Listener interface for changes happening on a layer.
Definition: layer.h:71
bool m_searchNarrow
is automatic seach enabled
Definition: cellcache.h:665
CellChangeListener * m_cellZoneListener
listener for zones
Definition: cellcache.h:683
double m_defaultSpeedMulti
default speed
Definition: cellcache.h:631
std::vector< std::string > getCellAreas(Cell *cell)
Returns all areas of a cell.
Definition: cellcache.cpp:1461
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:634
bool getCellSpeedMultiplier(const ModelCoordinate &cell, double &multiplier)
Returns speed value from cell.
Definition: cellcache.cpp:1132
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:641
Zone * getZone(uint32_t id)
Gets zone by identifier.
Definition: cellcache.cpp:1277
std::vector< Cell * > getAreaCells(const std::string &id)
Returns all cells of an area.
Definition: cellcache.cpp:1472
void setDefaultSpeedMultiplier(double multi)
Sets default speed for this CellCache.
Definition: cellcache.cpp:1152
std::map< Cell *, double > m_costMultipliers
holds default cost multiplier, only if it is not default(1.0)
Definition: cellcache.h:692
uint32_t m_height
cache height
Definition: cellcache.h:647
std::multimap< std::string, Cell * > StringCellMultimap
Definition: cellcache.h:615
void mergeZones(Zone *zone1, Zone *zone2)
Merges two zones to one.
Definition: cellcache.cpp:1347
bool m_fowUpdate
indicates fow update
Definition: cellcache.h:656
bool isStaticSize()
Returns if the cache size is static.
Definition: cellcache.cpp:1518
void removeNarrowCell(Cell *cell)
Removes cell from narrow cells.
Definition: cellcache.cpp:1372
void addCellToArea(const std::string &id, Cell *cell)
Adds a cell to a specific area group.
Definition: cellcache.cpp:1396
Rect calculateCurrentSize()
Returns the current size.
Definition: cellcache.cpp:1493
std::vector< Cell * > getCellsInCircle(const ModelCoordinate &center, uint16_t radius)
Returns all cells in the circle.
Definition: cellcache.cpp:900
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:1530
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:1220
Cell * createCell(const ModelCoordinate &mc)
Creates cell on this CellCache.
Definition: cellcache.cpp:697
std::pair< StringCellIterator, StringCellIterator > StringCellPair
Definition: cellcache.h:617
bool m_staticSize
is automatic size update enabled/disabled
Definition: cellcache.h:668
Zone(uint32_t id)
Constructor.
Definition: cellcache.cpp:260
void addNarrowCell(Cell *cell)
Adds cell to narrow cells.
Definition: cellcache.cpp:1361
std::vector< std::string > getAreas()
Returns all area ids.
Definition: cellcache.cpp:1448
bool m_sizeUpdate
indicates size update
Definition: cellcache.h:659
double getCost(const std::string &costId)
Returns the cost value for the given id.
Definition: cellcache.cpp:991
const std::set< Cell * > & getNarrowCells()
Returns narrow cells.
Definition: cellcache.cpp:1368
unsigned int uint32_t
Definition: core.h:40
double getDefaultCostMultiplier()
Gets default cost for this CellCache.
Definition: cellcache.cpp:1148
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:1440
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:671
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:1388
void resetCostMultiplier(Cell *cell)
Resets the cost multiplier for the cell.
Definition: cellcache.cpp:1186
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:1168
void reset()
Resets the CellCache.
Definition: cellcache.cpp:436