File: cell.h

package info (click to toggle)
fife 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,012 kB
  • ctags: 26,660
  • sloc: cpp: 84,903; sh: 10,269; python: 8,753; xml: 1,213; makefile: 265; objc: 245; ansic: 229
file content (440 lines) | stat: -rw-r--r-- 13,430 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/***************************************************************************
 *   Copyright (C) 2005-2013 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

#ifndef FIFE_CELL_H
#define FIFE_CELL_H

// Standard C++ library includes
#include <vector>
#include <set>
#include <list>
#include <map>

// 3rd party library includes

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/base/fifeclass.h"
#include "model/metamodel/modelcoords.h"


namespace FIFE {

	class Instance;
	class Layer;
	class Cell;
	class Zone;

	static const double MIN_CELL_Z = -9999999;

	/** Defines different blocker types.
	 *
	 * CTYPE_NO_BLOCKER means there is no blocker.
	 * CTYPE_STATIC_BLOCKER means there is at least one blocking instance which is static.
	 * CTYPE_DYNAMIC_BLOCKER means there is at least one blocking instance which is not static.
	 * CTYPE_CELL_NO_BLOCKER means there will never be a blocker. Regardless of the instances on this cell.
	 * CTYPE_CELL_BLOCKER means there will always be a blocker. Regardless of the instances on this cell.
	 */
	enum CellType {
		CTYPE_NO_BLOCKER = 0,
		CTYPE_STATIC_BLOCKER = 1,
		CTYPE_DYNAMIC_BLOCKER = 2,
		CTYPE_CELL_NO_BLOCKER = 3,
		CTYPE_CELL_BLOCKER = 4
	};
	typedef uint8_t CellTypeInfo;

	/** Defines different cell visuals which are used for Fog of War.
	 *
	 * CELLV_CONCEALED mark the cell as hidden.
	 * CELLV_REVEALED mark the cell as explored and in visible range.
	 * CELLV_MASKED mark the cell as explored but currently not in visible range.
	 */
	enum CellVisualType {
		CELLV_CONCEALED = 0,
		CELLV_REVEALED,
		CELLV_MASKED
	};
	typedef uint8_t CellVisualEffect;

	/** Simple class to hold the data for transistions.
	 */
	class TransitionInfo {
		public:
			TransitionInfo(Layer* layer) { m_layer = layer; m_difflayer = false; m_immediate = true;}
			~TransitionInfo() {};
			//! target layer
			Layer* m_layer;
			//! target coordinates
			ModelCoordinate m_mc;
			//! is target on another layer
			bool m_difflayer;
			//! use immediate
			bool m_immediate;
	};

	/** Listener interface for deletions happening on a cell, used for transistions.
	 */
	class CellDeleteListener {
	public:
		virtual ~CellDeleteListener() {};
		
		/** Called when a cell gets deleted.
		 * @param cell which will be deleted.
		 */
		virtual void onCellDeleted(Cell* cell) = 0;
	};
	
	/** Listener interface for changes happening on a cell.
	 */
	class CellChangeListener {
	public:
		virtual ~CellChangeListener() {};
		
		/** Called when some instance entered the cell.
		 * @param cell where enter occurred.
		 * @param instance which enter the cell.
		 */
		virtual void onInstanceEnteredCell(Cell* cell, Instance* instance) = 0;

		/** Called when some instance exited the cell.
		 * @param cell where exit occurred.
		 * @param instance which exit the cell.
		 */
		virtual void onInstanceExitedCell(Cell* cell, Instance* instance) = 0;

		/** Called when some instance changed its blocking property.
		 * @param cell where exit occurred.
		 * @param type blocking type @see CellType.
		 * @param blocks true if the CellType indicates the cell as a blocker, otherwise false.
		 */
		virtual void onBlockingChangedCell(Cell* cell, CellTypeInfo type, bool blocks) = 0;
	};

	/** A basic cell on a CellCache.
	 */
	class Cell: public FifeClass, public CellDeleteListener {
		public:
			/** Constructor
			 * @param coordint A integer value that represents the cell identifier. Based on coordinates.
			 * @param coordinate A ModelCoordinate that specifies the coordinates of the cell.
			 * @param layer A pointer to the layer which is associated with the cell.
			 */
			Cell(int32_t coordint, ModelCoordinate coordinate, Layer* layer);
			
			/** Destructor
			 */
			~Cell();

			/** Adds instances to this cell.
			 * @param instances A const reference to list that contains instances.
			 */
			void addInstances(const std::list<Instance*>& instances);
			
			/** Adds a instance to this cell.
			 * @param instance A pointer to the instance.
			 */
			void addInstance(Instance* instance);

			/** Changes a instance on this cell.
			 * @param instance A pointer to the instance.
			 */
			void changeInstance(Instance* instance);

			/** Removes a instance from this cell.
			 * @param instance A pointer to the instance.
			 */
			void removeInstance(Instance* instance);

			/** Called to check if given cell is a neighbor.
			 * @param cell A pointer to cell.
			 * @return True if cell is a neighbor, otherwise false.
			 */
			bool isNeighbor(Cell* cell);

			/** Called to update cell data.
			 * @return True if cell was changed since the last update, false otherwise.
			 */
			void updateCellInfo();

			/** Returns if cell use default cost.
			 * @return True if the cell use the default cost, otherwise false.
			 */
			bool defaultCost();

			/** Changes the cell cost.
			 * @param multi A double its value is used instead of the default cost.
			 */
			void setCostMultiplier(double multi);

			/** Returns the current cell cost.
			 * @return The currently used cost value as a double.
			 */
			double getCostMultiplier();

			/** Resets the cell cost to default, 1.0.
			 */
			void resetCostMultiplier();

			/** Returns if cell use default speed.
			 * @return True if the cell use the default speed, otherwise false.
			 */
			bool defaultSpeed();

			/** Changes the cell speed.
			 * @param multi A double its value is used instead of the default speed.
			 */
			void setSpeedMultiplier(double multi);

			/** Returns the current cell speed.
			 * @return The currently used speed value as a double.
			 */
			double getSpeedMultiplier();

			/** Resets the cell speed to default, 1.0.
			 */
			void resetSpeedMultiplier();

			/** Returns zone.
			 * @return A pointer to the zone.
			 */
			Zone* getZone();

			/** Sets zone.
			 * @param zone A pointer to the zone.
			 */
			void setZone(Zone* zone);

			/** Resets zone.
			 * Zone pointer is NULL and isInserted() returns false.
			 */
			void resetZone();

			/** Returns whether the cell is part of a zone.
			 * @return True if the cell is inserted into a zone, otherwise false.
			 */
			bool isInserted();

			/** Mark cell as inserted. 
			 * @param inserted A boolean, true marks the cell as inserted, false as not inserted.
			 */
			void setInserted(bool inserted);

			/** Returns whether the zone on this cell is protected.
			 * @return True if the zone is protected, otherwise false.
			 */
			bool isZoneProtected();

			/** Mark zone on this cell as protected.
			 * @param protect A boolean, true marks the cell as inserted.
			 */
			void setZoneProtected(bool protect);

			/** Returns blocker type.
			 * @see CellType
			 */
			CellTypeInfo getCellType();

			/** Sets blocker type.
			 * @see CellType
			 */
			void setCellType(CellTypeInfo type);

			/** Returns all instances on this cell.
			 * @return A const reference to a set that refer to the instances on this cell.
			 */
			const std::set<Instance*>& getInstances();

			/** Sets the cell identifier.
			 * @param id A unique int value that is used as identifier. Based on the cell position.
			 */
			void setCellId(int32_t id);

			/** Returns the cell identifier.
			 * @return A unique int value that is used as identifier. Based on the cell position.
			 */
			int32_t getCellId();

			/** Returns the layer coordinates of this cell.
			 * @return A const reference to a ModelCoordinate.
			 */
			const ModelCoordinate getLayerCoordinates() const;

			/** Adds a neighbor cell to this cell.
			 * @param cell A pointer to the cell that should added as neighbor.
			 */
			void addNeighbor(Cell* cell);

			/** Returns the layer coordinates of this cell.
			 * @return A const reference to a set of all neighbor cells.
			 */
			const std::vector<Cell*>& getNeighbors();

			/** Removes all neighbors from cell.
			 */
			void resetNeighbors();

			/** Returns the current layer.
			 * @return A pointer to the currently used layer.
			 */
			Layer* getLayer();

			/** Creates a transistion from this cell to the given layer and coordinates.
			 * @param layer A pointer to the layer whereto the transition takes.
			 * @param mc A const reference to a ModelCoordinate whereto the transition takes.
			 * @param immediate A boolean, true means the transition is always used,
				if false the transistion is only used if it is a part of the path e.g. as an abbreviation.
			 */
			void createTransition(Layer* layer, const ModelCoordinate& mc, bool immediate = false);

			/** Removes the transistion from Cell and CellCache.
			 */
			void deleteTransition();

			/** Returns the transition.
			 * @return A pointer to the transition.
			 */
			TransitionInfo* getTransition();
			
			/** Sets the cell visual.
			 * @see CellVisualType
			 */
			void setFoWType(CellVisualEffect type);

			/** Returns the cell visual.
			 * @see CellVisualType
			 */
			CellVisualEffect getFoWType();

			/** Adds a instance as visitor to this cell. Instance visitor radius is then used e.g. for Fog of War.
			 * @param instance A pointer to the instance.
			 */
			void addVisitorInstance(Instance* instance);

			/** Removes the visitor instance from this cell.
			 * @param instance A pointer to the instance.
			 */
			void removeVisitorInstance(Instance* instance);

			/** Returns all visitor instances on this cell.
			 * @return A const reference to a set of all visitor instances.
			 */
			const std::vector<Instance*>& getVisitorInstances();

			/** Adds new cell delete listener.
			 * @param listener A pointer to the listener.
			 */
			void addDeleteListener(CellDeleteListener* listener);

			/** Removes cell delete listener.
			 * @param listener A pointer to the listener.
			 */
			void removeDeleteListener(CellDeleteListener* listener);

			/** Called when a cell gets deleted on this cell. If a delete listener was added.
			 * @param cell A pointer to the cell which will be deleted.
			 * @see CellDeleteListener
			 */
			void onCellDeleted(Cell* cell);

			/** Adds new cell change listener.
			 * @param listener A pointer to the listener.
			 * @see CellChangeListener
			 */
			void addChangeListener(CellChangeListener* listener);

			/** Removes cell change listener.
			 * @param listener A pointer to the listener.
			 * @see CellChangeListener
			 */
			void removeChangeListener(CellChangeListener* listener);

			/** Called when a instance entered this cell.
			 * @param instance A pointer to the instance which will be entered.
			 * @see CellChangeListener
			 */
			void callOnInstanceEntered(Instance* instance);

			/** Called when a instance exited this cell.
			 * @param instance A pointer to the instance which will be exited.
			 * @see CellChangeListener
			 */
			void callOnInstanceExited(Instance* instance);

			/** Called when the blocking property of this cell changed.
			 * @param block A boolean, true mark the cell as blocker.
			 * @see CellChangeListener
			 */
			void callOnBlockingChanged(bool blocks);

		private:

			void updateCellBlockingInfo();
			void updateCellFowInfo();

			//! holds coordinate as a unique integer id
			int32_t m_coordId;
			
			//! holds coordinate
			ModelCoordinate m_coordinate;

			//! parent layer
			Layer* m_layer;

			//! parent Zone
			Zone* m_zone;

			//! Pointer to Transistion
			TransitionInfo* m_transition;

			//! already inserted
			bool m_inserted;

			//! protected
			bool m_protect;

			//! CellType
			CellTypeInfo m_type;

			//! visual cell status
			CellVisualEffect m_fowType;

			// contained Instances
			std::set<Instance*> m_instances;

			//! contains visitor instances
			std::vector<Instance*> m_visitors;

			//! neighbor cells
			std::vector<Cell*> m_neighbors;

			//! delete listener
			std::vector<CellDeleteListener*> m_deleteListeners;

			//! change listener
			std::vector<CellChangeListener*> m_changeListeners;
	};

} // FIFE

#endif