FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
cellcache.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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 
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 
622  Rect calculateCurrentSize();
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 
680  StringCellMultimap m_cellAreas;
681 
684 
686  std::map<std::string, double> m_costsTable;
687 
689  StringCellMultimap m_costsToCells;
690 
692  std::map<Cell*, double> m_costMultipliers;
693 
695  std::map<Cell*, double> m_speedMultipliers;
696  };
697 
698 } // FIFE
699 
700 #endif
StringCellMultimap::iterator StringCellIterator
Definition: cellcache.h:616
uint32_t m_width
cache width
Definition: cellcache.h:644
Layer * m_layer
walkable layer
Definition: cellcache.h:625
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:110
std::set< Cell * > m_narrowCells
special cells which are monitored (zone split and merge)
Definition: cellcache.h:677
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
const std::set< Cell * > & getCells() const
Returns all cells of this zone.
Definition: cellcache.cpp:294
std::vector< std::vector< Cell * > > m_cells
Definition: cellcache.h:637
Base class for all fife classes Used e.g.
Definition: fifeclass.h:42
double m_defaultCostMulti
default cost
Definition: cellcache.h:628
uint32_t getCellCount() const
Returns the number of cells.
Definition: cellcache.cpp:306
void removeCell(Cell *cell)
Removes a cell from this zone.
Definition: cellcache.cpp:277
uint32_t getId() const
Returns the zone identifier.
Definition: cellcache.cpp:302
std::vector< Zone * > m_zones
zones
Definition: cellcache.h:674
int32_t m_neighborZ
max z value for neighbors
Definition: cellcache.h:650
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
std::map< std::string, double > m_costsTable
holds cost table
Definition: cellcache.h:686
~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:665
CellChangeListener * m_cellZoneListener
listener for zones
Definition: cellcache.h:683
double m_defaultSpeedMulti
default speed
Definition: cellcache.h:631
A basic cell on a CellCache.
Definition: cell.h:136
unsigned short uint16_t
Definition: core.h:39
LayerChangeListener * m_cellListener
change listener
Definition: cellcache.h:634
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
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
bool m_fowUpdate
indicates fow update
Definition: cellcache.h:656
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:617
bool m_staticSize
is automatic size update enabled/disabled
Definition: cellcache.h:668
Zone(uint32_t id)
Constructor.
Definition: cellcache.cpp:260
bool m_sizeUpdate
indicates size update
Definition: cellcache.h:659
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:671
A Zone is an abstract depiction of a CellCache or of a part of it.
Definition: cellcache.h:50