| 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_CELL_H |
| 23 |
#define FIFE_CELL_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <vector> |
| 27 |
#include <set> |
| 28 |
#include <list> |
| 29 |
#include <map> |
| 30 |
|
| 31 |
// 3rd party library includes |
| 32 |
|
| 33 |
// FIFE includes |
| 34 |
// These includes are split up in two parts, separated by one empty line |
| 35 |
// First block: files included from the FIFE root src directory |
| 36 |
// Second block: files included from the same folder |
| 37 |
#include "util/base/fifeclass.h" |
| 38 |
#include "model/metamodel/modelcoords.h" |
| 39 |
|
| 40 |
|
| 41 |
namespace FIFE { |
| 42 |
|
| 43 |
class Instance; |
| 44 |
class Layer; |
| 45 |
class Cell; |
| 46 |
class Zone; |
| 47 |
|
| 48 |
static const double MIN_CELL_Z = -9999999; |
| 49 |
|
| 50 |
/** Defines different blocker types. |
| 51 |
* |
| 52 |
* CTYPE_NO_BLOCKER means there is no blocker. |
| 53 |
* CTYPE_STATIC_BLOCKER means there is at least one blocking instance which is static. |
| 54 |
* CTYPE_DYNAMIC_BLOCKER means there is at least one blocking instance which is not static. |
| 55 |
* CTYPE_CELL_NO_BLOCKER means there will never be a blocker. Regardless of the instances on this cell. |
| 56 |
* CTYPE_CELL_BLOCKER means there will always be a blocker. Regardless of the instances on this cell. |
| 57 |
*/ |
| 58 |
enum CellType { |
| 59 |
CTYPE_NO_BLOCKER = 0, |
| 60 |
CTYPE_CELL_NO_BLOCKER = 1, |
| 61 |
CTYPE_DYNAMIC_BLOCKER = 2, |
| 62 |
CTYPE_STATIC_BLOCKER = 3, |
| 63 |
CTYPE_CELL_BLOCKER = 4 |
| 64 |
}; |
| 65 |
typedef uint8_t CellTypeInfo; |
| 66 |
|
| 67 |
/** Simple class to hold the data for transistions. |
| 68 |
*/ |
| 69 |
class TransitionInfo { |
| 70 |
public: |
| 71 |
TransitionInfo(Layer* layer) { m_layer = layer; m_difflayer = false; m_immediate = true;} |
| 72 |
~TransitionInfo() {}; |
| 73 |
//! target layer |
| 74 |
Layer* m_layer; |
| 75 |
//! target coordinates |
| 76 |
ModelCoordinate m_mc; |
| 77 |
//! is target on another layer |
| 78 |
bool m_difflayer; |
| 79 |
//! use immediate |
| 80 |
bool m_immediate; |
| 81 |
}; |
| 82 |
|
| 83 |
/** Listener interface for deletions happening on a cell, used for transistions. |
| 84 |
*/ |
| 85 |
class CellDeleteListener { |
| 86 |
public: |
| 87 |
virtual ~CellDeleteListener() {}; |
| 88 |
|
| 89 |
/** Called when a cell gets deleted. |
| 90 |
* @param cell which will be deleted. |
| 91 |
*/ |
| 92 |
virtual void onCellDeleted(Cell* cell) = 0; |
| 93 |
}; |
| 94 |
|
| 95 |
/** Listener interface for changes happening on a cell. |
| 96 |
*/ |
| 97 |
class CellChangeListener { |
| 98 |
public: |
| 99 |
virtual ~CellChangeListener() {}; |
| 100 |
|
| 101 |
/** Called when some instance entered the cell. |
| 102 |
* @param cell where enter occurred. |
| 103 |
* @param instance which enter the cell. |
| 104 |
*/ |
| 105 |
virtual void onInstanceEnteredCell(Cell* cell, Instance* instance) = 0; |
| 106 |
|
| 107 |
/** Called when some instance exited the cell. |
| 108 |
* @param cell where exit occurred. |
| 109 |
* @param instance which exit the cell. |
| 110 |
*/ |
| 111 |
virtual void onInstanceExitedCell(Cell* cell, Instance* instance) = 0; |
| 112 |
|
| 113 |
/** Called when some instance changed its blocking property. |
| 114 |
* @param cell where exit occurred. |
| 115 |
* @param type blocking type @see CellType. |
| 116 |
* @param blocks true if the CellType indicates the cell as a blocker, otherwise false. |
| 117 |
*/ |
| 118 |
virtual void onBlockingChangedCell(Cell* cell, CellTypeInfo type, bool blocks) = 0; |
| 119 |
}; |
| 120 |
|
| 121 |
/** A basic cell on a CellCache. |
| 122 |
*/ |
| 123 |
class Cell: public FifeClass, public CellDeleteListener { |
| 124 |
public: |
| 125 |
/** Constructor |
| 126 |
* @param coordint A integer value that represents the cell identifier. Based on coordinates. |
| 127 |
* @param coordinate A ModelCoordinate that specifies the coordinates of the cell. |
| 128 |
* @param layer A pointer to the layer which is associated with the cell. |
| 129 |
*/ |
| 130 |
Cell(int32_t coordint, ModelCoordinate coordinate, Layer* layer); |
| 131 |
|
| 132 |
/** Destructor |
| 133 |
*/ |
| 134 |
~Cell(); |
| 135 |
|
| 136 |
/** Adds instances to this cell. |
| 137 |
* @param instances A const reference to list that contains instances. |
| 138 |
*/ |
| 139 |
void addInstances(const std::list<Instance*>& instances); |
| 140 |
|
| 141 |
/** Adds a instance to this cell. |
| 142 |
* @param instance A pointer to the instance. |
| 143 |
*/ |
| 144 |
void addInstance(Instance* instance); |
| 145 |
|
| 146 |
/** Changes a instance on this cell. |
| 147 |
* @param instance A pointer to the instance. |
| 148 |
*/ |
| 149 |
void changeInstance(Instance* instance); |
| 150 |
|
| 151 |
/** Removes a instance from this cell. |
| 152 |
* @param instance A pointer to the instance. |
| 153 |
*/ |
| 154 |
void removeInstance(Instance* instance); |
| 155 |
|
| 156 |
/** Called to check if given cell is a neighbor. |
| 157 |
* @param cell A pointer to cell. |
| 158 |
* @return True if cell is a neighbor, otherwise false. |
| 159 |
*/ |
| 160 |
bool isNeighbor(Cell* cell); |
| 161 |
|
| 162 |
/** Called to update cell data. |
| 163 |
* @return True if cell was changed since the last update, false otherwise. |
| 164 |
*/ |
| 165 |
void updateCellInfo(); |
| 166 |
|
| 167 |
/** Returns if cell use default cost. |
| 168 |
* @return True if the cell use the default cost, otherwise false. |
| 169 |
*/ |
| 170 |
bool defaultCost(); |
| 171 |
|
| 172 |
/** Changes the cell cost. |
| 173 |
* @param multi A double its value is used instead of the default cost. |
| 174 |
*/ |
| 175 |
void setCostMultiplier(double multi); |
| 176 |
|
| 177 |
/** Returns the current cell cost. |
| 178 |
* @return The currently used cost value as a double. |
| 179 |
*/ |
| 180 |
double getCostMultiplier(); |
| 181 |
|
| 182 |
/** Resets the cell cost to default, 1.0. |
| 183 |
*/ |
| 184 |
void resetCostMultiplier(); |
| 185 |
|
| 186 |
/** Returns if cell use default speed. |
| 187 |
* @return True if the cell use the default speed, otherwise false. |
| 188 |
*/ |
| 189 |
bool defaultSpeed(); |
| 190 |
|
| 191 |
/** Changes the cell speed. |
| 192 |
* @param multi A double its value is used instead of the default speed. |
| 193 |
*/ |
| 194 |
void setSpeedMultiplier(double multi); |
| 195 |
|
| 196 |
/** Returns the current cell speed. |
| 197 |
* @return The currently used speed value as a double. |
| 198 |
*/ |
| 199 |
double getSpeedMultiplier(); |
| 200 |
|
| 201 |
/** Resets the cell speed to default, 1.0. |
| 202 |
*/ |
| 203 |
void resetSpeedMultiplier(); |
| 204 |
|
| 205 |
/** Returns zone. |
| 206 |
* @return A pointer to the zone. |
| 207 |
*/ |
| 208 |
Zone* getZone(); |
| 209 |
|
| 210 |
/** Sets zone. |
| 211 |
* @param zone A pointer to the zone. |
| 212 |
*/ |
| 213 |
void setZone(Zone* zone); |
| 214 |
|
| 215 |
/** Resets zone. |
| 216 |
* Zone pointer is NULL and isInserted() returns false. |
| 217 |
*/ |
| 218 |
void resetZone(); |
| 219 |
|
| 220 |
/** Returns whether the cell is part of a zone. |
| 221 |
* @return True if the cell is inserted into a zone, otherwise false. |
| 222 |
*/ |
| 223 |
bool isInserted(); |
| 224 |
|
| 225 |
/** Mark cell as inserted. |
| 226 |
* @param inserted A boolean, true marks the cell as inserted, false as not inserted. |
| 227 |
*/ |
| 228 |
void setInserted(bool inserted); |
| 229 |
|
| 230 |
/** Returns whether the zone on this cell is protected. |
| 231 |
* @return True if the zone is protected, otherwise false. |
| 232 |
*/ |
| 233 |
bool isZoneProtected(); |
| 234 |
|
| 235 |
/** Mark zone on this cell as protected. |
| 236 |
* @param protect A boolean, true marks the cell as inserted. |
| 237 |
*/ |
| 238 |
void setZoneProtected(bool protect); |
| 239 |
|
| 240 |
/** Returns blocker type. |
| 241 |
* @see CellType |
| 242 |
*/ |
| 243 |
CellTypeInfo getCellType(); |
| 244 |
|
| 245 |
/** Sets blocker type. |
| 246 |
* @see CellType |
| 247 |
*/ |
| 248 |
void setCellType(CellTypeInfo type); |
| 249 |
|
| 250 |
/** Returns all instances on this cell. |
| 251 |
* @return A const reference to a set that refer to the instances on this cell. |
| 252 |
*/ |
| 253 |
const std::set<Instance*>& getInstances(); |
| 254 |
|
| 255 |
/** Sets the cell identifier. |
| 256 |
* @param id A unique int value that is used as identifier. Based on the cell position. |
| 257 |
*/ |
| 258 |
void setCellId(int32_t id); |
| 259 |
|
| 260 |
/** Returns the cell identifier. |
| 261 |
* @return A unique int value that is used as identifier. Based on the cell position. |
| 262 |
*/ |
| 263 |
int32_t getCellId(); |
| 264 |
|
| 265 |
/** Returns the layer coordinates of this cell. |
| 266 |
* @return A const reference to a ModelCoordinate. |
| 267 |
*/ |
| 268 |
const ModelCoordinate getLayerCoordinates() const; |
| 269 |
|
| 270 |
/** Adds a neighbor cell to this cell. |
| 271 |
* @param cell A pointer to the cell that should added as neighbor. |
| 272 |
*/ |
| 273 |
void addNeighbor(Cell* cell); |
| 274 |
|
| 275 |
/** Returns the layer coordinates of this cell. |
| 276 |
* @return A const reference to a set of all neighbor cells. |
| 277 |
*/ |
| 278 |
const std::vector<Cell*>& getNeighbors(); |
| 279 |
|
| 280 |
/** Removes all neighbors from cell. |
| 281 |
*/ |
| 282 |
void resetNeighbors(); |
| 283 |
|
| 284 |
/** Returns the current layer. |
| 285 |
* @return A pointer to the currently used layer. |
| 286 |
*/ |
| 287 |
Layer* getLayer(); |
| 288 |
|
| 289 |
/** Creates a transistion from this cell to the given layer and coordinates. |
| 290 |
* @param layer A pointer to the layer whereto the transition takes. |
| 291 |
* @param mc A const reference to a ModelCoordinate whereto the transition takes. |
| 292 |
* @param immediate A boolean, true means the transition is always used, |
| 293 |
if false the transistion is only used if it is a part of the path e.g. as an abbreviation. |
| 294 |
*/ |
| 295 |
void createTransition(Layer* layer, const ModelCoordinate& mc, bool immediate = false); |
| 296 |
|
| 297 |
/** Removes the transistion from Cell and CellCache. |
| 298 |
*/ |
| 299 |
void deleteTransition(); |
| 300 |
|
| 301 |
/** Returns the transition. |
| 302 |
* @return A pointer to the transition. |
| 303 |
*/ |
| 304 |
TransitionInfo* getTransition(); |
| 305 |
|
| 306 |
/** Adds new cell delete listener. |
| 307 |
* @param listener A pointer to the listener. |
| 308 |
*/ |
| 309 |
void addDeleteListener(CellDeleteListener* listener); |
| 310 |
|
| 311 |
/** Removes cell delete listener. |
| 312 |
* @param listener A pointer to the listener. |
| 313 |
*/ |
| 314 |
void removeDeleteListener(CellDeleteListener* listener); |
| 315 |
|
| 316 |
/** Called when a cell gets deleted on this cell. If a delete listener was added. |
| 317 |
* @param cell A pointer to the cell which will be deleted. |
| 318 |
* @see CellDeleteListener |
| 319 |
*/ |
| 320 |
void onCellDeleted(Cell* cell); |
| 321 |
|
| 322 |
/** Adds new cell change listener. |
| 323 |
* @param listener A pointer to the listener. |
| 324 |
* @see CellChangeListener |
| 325 |
*/ |
| 326 |
void addChangeListener(CellChangeListener* listener); |
| 327 |
|
| 328 |
/** Removes cell change listener. |
| 329 |
* @param listener A pointer to the listener. |
| 330 |
* @see CellChangeListener |
| 331 |
*/ |
| 332 |
void removeChangeListener(CellChangeListener* listener); |
| 333 |
|
| 334 |
/** Called when a instance entered this cell. |
| 335 |
* @param instance A pointer to the instance which will be entered. |
| 336 |
* @see CellChangeListener |
| 337 |
*/ |
| 338 |
void callOnInstanceEntered(Instance* instance); |
| 339 |
|
| 340 |
/** Called when a instance exited this cell. |
| 341 |
* @param instance A pointer to the instance which will be exited. |
| 342 |
* @see CellChangeListener |
| 343 |
*/ |
| 344 |
void callOnInstanceExited(Instance* instance); |
| 345 |
|
| 346 |
/** Called when the blocking property of this cell changed. |
| 347 |
* @param blocks A boolean, true mark the cell as blocker. |
| 348 |
* @see CellChangeListener |
| 349 |
*/ |
| 350 |
void callOnBlockingChanged(bool blocks); |
| 351 |
|
| 352 |
private: |
| 353 |
|
| 354 |
void updateCellBlockingInfo(); |
| 355 |
|
| 356 |
//! holds coordinate as a unique integer id |
| 357 |
int32_t m_coordId; |
| 358 |
|
| 359 |
//! holds coordinate |
| 360 |
ModelCoordinate m_coordinate; |
| 361 |
|
| 362 |
//! parent layer |
| 363 |
Layer* m_layer; |
| 364 |
|
| 365 |
//! parent Zone |
| 366 |
Zone* m_zone; |
| 367 |
|
| 368 |
//! Pointer to Transistion |
| 369 |
TransitionInfo* m_transition; |
| 370 |
|
| 371 |
//! already inserted |
| 372 |
bool m_inserted; |
| 373 |
|
| 374 |
//! protected |
| 375 |
bool m_protect; |
| 376 |
|
| 377 |
//! CellType |
| 378 |
CellTypeInfo m_type; |
| 379 |
|
| 380 |
// contained Instances |
| 381 |
std::set<Instance*> m_instances; |
| 382 |
|
| 383 |
//! neighbor cells |
| 384 |
std::vector<Cell*> m_neighbors; |
| 385 |
|
| 386 |
//! delete listener |
| 387 |
std::vector<CellDeleteListener*> m_deleteListeners; |
| 388 |
|
| 389 |
//! change listener |
| 390 |
std::vector<CellChangeListener*> m_changeListeners; |
| 391 |
}; |
| 392 |
|
| 393 |
} // FIFE |
| 394 |
|
| 395 |
#endif |