FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
cellcache.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 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 
187  LayerChangeListener* getCellCacheChangeListener();
188 
192  Layer* getLayer();
193 
197  const Rect& getSize();
198 
203  void setSize(const Rect& rec);
204 
208  uint32_t getWidth();
209 
213  uint32_t getHeight();
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 
254  std::vector<Cell*> getCellsInLine(const ModelCoordinate& pt1, const ModelCoordinate& pt2, bool blocker = false);
255 
260  std::vector<Cell*> getCellsInRect(const Rect& rec);
261 
266  std::vector<Cell*> getBlockingCellsInRect(const Rect& rec);
267 
273  std::vector<Cell*> getCellsInCircle(const ModelCoordinate& center, uint16_t radius);
274 
282  std::vector<Cell*> getCellsInCircleSegment(const ModelCoordinate& center, uint16_t radius, int32_t sangle, int32_t eangle);
283 
288  void registerCost(const std::string& costId, double cost);
289 
293  void unregisterCost(const std::string& costId);
294 
299  double getCost(const std::string& costId);
300 
304  bool existsCost(const std::string& costId);
305 
309  std::list<std::string> getCosts();
310 
313  void unregisterAllCosts();
314 
319  void addCellToCost(const std::string& costId, Cell* cell);
320 
325  void addCellsToCost(const std::string& costId, const std::vector<Cell*>& cells);
326 
330  void removeCellFromCost(Cell* cell);
331 
336  void removeCellFromCost(const std::string& costId, Cell* cell);
337 
342  void removeCellsFromCost(const std::string& costId, const std::vector<Cell*>& cells);
343 
348  std::vector<Cell*> getCostCells(const std::string& costId);
349 
354  std::vector<std::string> getCellCosts(Cell* cell);
355 
361  bool existsCostForCell(const std::string& costId, Cell* cell);
362 
368  double getAdjacentCost(const ModelCoordinate& adjacent, const ModelCoordinate& next);
369 
376  double getAdjacentCost(const ModelCoordinate& adjacent, const ModelCoordinate& next, const std::string& costId);
377 
383  bool getCellSpeedMultiplier(const ModelCoordinate& cell, double& multiplier);
384 
388  void setDefaultCostMultiplier(double multi);
389 
393  double getDefaultCostMultiplier();
394 
398  void setDefaultSpeedMultiplier(double multi);
399 
403  double getDefaultSpeedMultiplier();
404 
409  bool isDefaultCost(Cell* cell);
410 
415  void setCostMultiplier(Cell* cell, double multi);
416 
421  double getCostMultiplier(Cell* cell);
422 
426  void resetCostMultiplier(Cell* cell);
427 
432  bool isDefaultSpeed(Cell* cell);
433 
438  void setSpeedMultiplier(Cell* cell, double multi);
439 
444  double getSpeedMultiplier(Cell* cell);
445 
449  void resetSpeedMultiplier(Cell* cell);
450 
454  void addTransition(Cell* cell);
455 
459  void removeTransition(Cell* cell);
460 
465  std::vector<Cell*> getTransitionCells(Layer* layer = NULL);
466 
470  const std::vector<Zone*>& getZones();
471 
476  Zone* getZone(uint32_t id);
477 
481  Zone* createZone();
482 
486  void removeZone(Zone* zone);
487 
491  void splitZone(Cell* cell);
492 
497  void mergeZones(Zone* zone1, Zone* zone2);
498 
503  void addNarrowCell(Cell* cell);
504 
508  const std::set<Cell*>& getNarrowCells();
509 
513  void removeNarrowCell(Cell* cell);
514 
517  void resetNarrowCells();
518 
523  bool isSearchNarrowCells();
524 
528  void setSearchNarrowCells(bool search);
529 
535  void addCellToArea(const std::string& id, Cell* cell);
536 
542  void addCellsToArea(const std::string& id, const std::vector<Cell*>& cells);
543 
547  void removeCellFromArea(Cell* cell);
548 
553  void removeCellFromArea(const std::string& id, Cell* cell);
554 
559  void removeCellsFromArea(const std::string& id, const std::vector<Cell*>& cells);
560 
564  void removeArea(const std::string& id);
565 
570  bool existsArea(const std::string& id);
571 
575  std::vector<std::string> getAreas();
576 
581  std::vector<std::string> getCellAreas(Cell* cell);
582 
587  std::vector<Cell*> getAreaCells(const std::string& id);
588 
594  bool isCellInArea(const std::string& id, Cell* cell);
595 
599  void setStaticSize(bool staticSize);
600 
604  bool isStaticSize();
605 
606  void setBlockingUpdate(bool update);
607  void setSizeUpdate(bool update);
608  void update();
609  private:
610  typedef std::multimap<std::string, Cell*> StringCellMultimap;
611  typedef StringCellMultimap::iterator StringCellIterator;
612  typedef std::pair<StringCellIterator, StringCellIterator> StringCellPair;
613 
617  Rect calculateCurrentSize();
618 
621 
624 
627 
630 
631  // cells on this cache
632  std::vector<std::vector<Cell*> > m_cells;
633 
637 
640 
643 
645  int32_t m_neighborZ;
646 
649 
652 
655 
658 
660  std::vector<Cell*> m_transitions;
661 
663  std::vector<Zone*> m_zones;
664 
666  std::set<Cell*> m_narrowCells;
667 
669  StringCellMultimap m_cellAreas;
670 
673 
675  std::map<std::string, double> m_costsTable;
676 
678  StringCellMultimap m_costsToCells;
679 
681  std::map<Cell*, double> m_costMultipliers;
682 
684  std::map<Cell*, double> m_speedMultipliers;
685  };
686 
687 } // FIFE
688 
689 #endif
StringCellMultimap::iterator StringCellIterator
Definition: cellcache.h:611
uint32_t m_width
cache width
Definition: cellcache.h:639
Layer * m_layer
walkable layer
Definition: cellcache.h:620
void addCell(Cell *cell)
Adds a cell to this zone.
Definition: cellcache.cpp:270
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:137
std::set< Cell * > m_cells
cells in the zone
Definition: cellcache.h:105
Listener interface for changes happening on a cell.
Definition: cell.h:97
std::set< Cell * > m_narrowCells
special cells which are monitored (zone split and merge)
Definition: cellcache.h:666
StringCellMultimap m_costsToCells
holds cells for each cost
Definition: cellcache.h:678
StringCellMultimap m_cellAreas
areas with assigned cells
Definition: cellcache.h:669
A CellCache is an abstract depiction of one or a few layers and contains additional information...
Definition: cellcache.h:111
bool m_blockingUpdate
indicates blocking update
Definition: cellcache.h:648
std::vector< std::vector< Cell * > > m_cells
Definition: cellcache.h:632
Base class for all fife classes Used e.g.
Definition: fifeclass.h:42
double m_defaultCostMulti
default cost
Definition: cellcache.h:623
void removeCell(Cell *cell)
Removes a cell from this zone.
Definition: cellcache.cpp:277
std::vector< Zone * > m_zones
zones
Definition: cellcache.h:663
int32_t m_neighborZ
max z value for neighbors
Definition: cellcache.h:645
std::map< Cell *, double > m_speedMultipliers
holds default speed multiplier, only if it is not default(1.0)
Definition: cellcache.h:684
void resetCells()
Remove all cells from zone but does not alter the cells.
Definition: cellcache.cpp:298
std::map< std::string, double > m_costsTable
holds cost table
Definition: cellcache.h:675
~Zone()
Destructor.
Definition: cellcache.cpp:264
A basic layer on a map.
Definition: layer.h:99
uint32_t m_id
identifier
Definition: cellcache.h:103
Listener interface for changes happening on a layer.
Definition: layer.h:71
bool m_searchNarrow
is automatic seach enabled
Definition: cellcache.h:654
CellChangeListener * m_cellZoneListener
listener for zones
Definition: cellcache.h:672
double m_defaultSpeedMulti
default speed
Definition: cellcache.h:626
A basic cell on a CellCache.
Definition: cell.h:123
unsigned short uint16_t
Definition: core.h:39
LayerChangeListener * m_cellListener
change listener
Definition: cellcache.h:629
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:636
const std::set< Cell * > & getCells() const
Returns all cells of this zone.
Definition: cellcache.cpp:294
std::map< Cell *, double > m_costMultipliers
holds default cost multiplier, only if it is not default(1.0)
Definition: cellcache.h:681
uint32_t m_height
cache height
Definition: cellcache.h:642
std::multimap< std::string, Cell * > StringCellMultimap
Definition: cellcache.h:610
void mergeZone(Zone *zone)
Merge two zones to one.
Definition: cellcache.cpp:285
A 3D Point.
Definition: point.h:205
std::pair< StringCellIterator, StringCellIterator > StringCellPair
Definition: cellcache.h:612
bool m_staticSize
is automatic size update enabled/disabled
Definition: cellcache.h:657
Zone(uint32_t id)
Constructor.
Definition: cellcache.cpp:260
uint32_t getCellCount() const
Returns the number of cells.
Definition: cellcache.cpp:306
bool m_sizeUpdate
indicates size update
Definition: cellcache.h:651
unsigned int uint32_t
Definition: core.h:40
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:660
uint32_t getId() const
Returns the zone identifier.
Definition: cellcache.cpp:302
A Zone is an abstract depiction of a CellCache or of a part of it.
Definition: cellcache.h:50