Repository navigation
Expand file tree
/
Copy pathTile.cpp
More file actions
398 lines (358 loc) · 7.57 KB
/
Copy pathTile.cpp
File metadata and controls
398 lines (358 loc) · 7.57 KB
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
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Tile.h"
#include "../Ruleset/MapData.h"
#include "../Ruleset/MapDataSet.h"
#include "../Engine/SurfaceSet.h"
namespace OpenXcom
{
/**
* constructor
* @param pos Position.
*/
Tile::Tile(const Position& pos): _discovered(false), _shadeSun(0), _light(0), _lastLight(0), _smoke(0), _fire(0), _pos(pos), _cached(false), _unit(0)
{
for (int i = 0; i < 4; i++)
{
_objects[i] = 0;
_currentFrame[i] = 0;
}
}
/**
* destructor
*/
Tile::~Tile()
{
}
/**
* Get the MapData pointer of a part of the tile.
* @param part the part 0-3.
* @return pointer to terrainobject
*/
MapData *Tile::getMapData(int part)
{
if (part < 0 || part > 3)
{
throw "unkown MapDataID part";
}
return _objects[part];
}
/**
* Set the MapData references of part 0 to 3.
* @param dat pointer to the data object
* @param part the part number
*/
void Tile::setMapData(MapData *dat, int part)
{
_objects[part] = dat;
setCached(false);
}
/**
* Gets wether this tile has no objects.
* @return bool
*/
bool Tile::isVoid()
{
return _objects[0] == 0 && _objects[1] == 0 && _objects[2] == 0 && _objects[3] == 0;
}
/**
* Get the TU cost to walk over a certain part of the tile.
* @param part
* @param movementType
* @return TU cost
*/
int Tile::getTUCost(int part, MovementType movementType)
{
if (_objects[part])
return _objects[part]->getTUCost(movementType);
else
return 0;
}
/**
* Whether this tile has a floor or not. If no object defined as floor, it has no floor.
* @return bool
*/
bool Tile::hasNoFloor()
{
if (_objects[O_FLOOR])
return _objects[O_FLOOR]->isNoFloor();
else
return true;
}
/**
* Whether this tile has a big wall.
* @return bool
*/
bool Tile::isBigWall()
{
if (_objects[O_OBJECT])
return _objects[O_OBJECT]->isBigWall();
else
return false;
}
/**
* If an object stand on this tile, how high is it standing.
* @return the level in pixels
*/
int Tile::getTerrainLevel()
{
int level = 0;
if (_objects[O_FLOOR])
level = _objects[O_FLOOR]->getTerrainLevel();
if (_objects[O_OBJECT])
level += _objects[O_OBJECT]->getTerrainLevel();
return level;
}
/**
* Gets the tile's position.
* @return position
*/
const Position& Tile::getPosition() const
{
return _pos;
}
/**
* Gets the tile's footstep sound.
* @return position
*/
int Tile::getFootstepSound()
{
int sound = 0;
if (_objects[O_FLOOR])
sound = _objects[O_FLOOR]->getFootstepSound();
if (_objects[O_OBJECT])
sound = _objects[O_OBJECT]->getFootstepSound();
return sound;
}
/**
* Open a door on this tile.
* @param part
* @return an ID, 0(normal), 1(ufo) or -1 if no door opened or 3 if ufo door is still opening
*/
int Tile::openDoor(int part)
{
if (!_objects[part]) return -1;
if (_objects[part]->isDoor())
{
setMapData(_objects[part]->getDataset()->getObjects()->at(_objects[part]->getAltMCD()),
_objects[part]->getDataset()->getObjects()->at(_objects[part]->getAltMCD())->getObjectType());
setMapData(0, part);
return 0;
}
if (_objects[part]->isUFODoor() && _currentFrame[part] == 0) // ufo door part 0 - door is closed
{
_currentFrame[part] = 1; // start opening door
return 1;
}
if (_objects[part]->isUFODoor() && _currentFrame[part] != 7) // ufo door != part 7 - door is still opening
{
return 3;
}
return -1;
}
/**
* Check if the ufo door is open or opening. Used for visibility/light blocking checks.
* @param part
* @return bool
*/
bool Tile::isUfoDoorOpen(int part)
{
if (_objects[part] && _objects[part]->isUFODoor() && _currentFrame[part] != 0)
{
return true;
}
return false;
}
/**
* Sets the tile's cache flag.
* @param cached
*/
void Tile::setCached(bool cached)
{
_cached = cached;
}
/**
* Check if the tile is still cached in the Map cache.
* When the tile changes (door/lighting/destroyed), it needs to be re-cached.
* @return bool
*/
bool Tile::isCached()
{
return _cached;
}
/**
* Sets the tile's cache flag.
* @param flag true/false
*/
void Tile::setDiscovered(bool flag)
{
_discovered = flag;
}
bool Tile::isDiscovered()
{
return _discovered;
}
/**
* Sets the tile's shade amount 0-15.
* Determined by the sun. Absolute amount.
* @param shade
*/
void Tile::setShadeSun(int shade)
{
if (_shadeSun != shade)
{
_shadeSun = shade;
setCached(false);
}
}
/**
* Change a light amount (+ ligther / - dimmer). Relative to the existing light amount.
* Can be caused by adding/removing objects, fire, personal light.
* Normally resulting light level should not go below 0. (lights normally can not darken a place...)
* @param light Amount of light to add.
* @param sessionID Unique number to avoid tiles are lit only once in a session.
*/
void Tile::addLight(int light, int sessionID)
{
if (sessionID == _sessionID) return;
_sessionID = sessionID;
// highest light wins
if (_light < light)
_light = light;
// uncomment following and comment previous to have additive lighting (which is more realistic)
/*
if (light != 0)
{
_light += light;
}
*/
}
void Tile::isSeenBy(BattleUnit *unit, int sessionID)
{
if (sessionID == _sessionID) return;
if (!_discovered)
{
_discovered = true;
setCached(false);
}
_sessionID = sessionID;
_visibleByUnit.push_back(unit);
}
void Tile::resetLight()
{
_lastLight = _light;
_light = 0;
_sessionID = 0;
_visibleByUnit.clear();
}
void Tile::setLight()
{
if (_lastLight != _light)
{
setCached(false);
}
}
/**
* Gets the tile's shade amount 0-15.
* @return shade
*/
int Tile::getShade()
{
int shade = _shadeSun - _light;
if (shade < 0) shade = 0;
return shade;
}
/**
* Destroy a part on this tile.
* @param part
*/
void Tile::destroy(int part)
{
if (_objects[part])
{
MapData *originalPart = _objects[part];
setMapData(0, part);
if (originalPart->getDieMCD())
{
MapData *dead = originalPart->getDataset()->getObjects()->at(originalPart->getDieMCD());
setMapData(dead, dead->getObjectType());
}
}
}
/**
* Animate the tile. This means to advance the current frame for every object.
*/
void Tile::animate()
{
int newframe;
for (int i=0; i < 4; i++)
{
if (_objects[i])
{
if (_objects[i]->isUFODoor() && (_currentFrame[i] == 0 || _currentFrame[i] == 7)) // ufo door is static
{
continue;
}
newframe = _currentFrame[i] + 1;
if (newframe == 8)
{
newframe = 0;
}
if (_objects[i]->getSprite(_currentFrame[i]) != _objects[i]->getSprite(newframe))
{
setCached(false);
}
_currentFrame[i] = newframe;
}
}
}
/**
* Get the sprite of a certain part of the tile.
* @param part
* @return Pointer to the sprite.
*/
Surface *Tile::getSprite(int part)
{
return _objects[part]->getDataset()->getSurfaceset()->getFrame(_objects[part]->getSprite(_currentFrame[part]));
}
/**
* Set a unit on this tile.
* @param unit
*/
void Tile::setUnit(BattleUnit *unit)
{
_unit = unit;
}
/**
* Get the unit on this tile.
* @return BattleUnit.
*/
BattleUnit *Tile::getUnit()
{
return _unit;
}
void Tile::setFire(int fire)
{
_fire = fire;
}
int Tile::getFire()
{
return _fire;
}
}