Repository navigation
Expand file tree
/
Copy pathTile.cpp
More file actions
686 lines (622 loc) · 14.8 KB
/
Copy pathTile.cpp
File metadata and controls
686 lines (622 loc) · 14.8 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* 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"
#include "../Engine/Surface.h"
#include "../Engine/RNG.h"
#include "../Engine/Exception.h"
#include "BattleUnit.h"
#include "BattleItem.h"
#include "../Ruleset/RuleItem.h"
namespace OpenXcom
{
/**
* constructor
* @param pos Position.
*/
Tile::Tile(const Position& pos): _smoke(0), _fire(0), _explosive(0), _pos(pos), _unit(0), _animationOffset(0)
{
for (int i = 0; i < 4; ++i)
{
_objects[i] = 0;
_mdsID[i] = 0;
_mdID[i] = 0;
_currentFrame[i] = 0;
}
for (int layer = 0; layer < LIGHTLAYERS; layer++)
{
_light[layer] = 0;
_lastLight[layer] = -1;
}
for (int i = 0; i < 3; ++i)
{
_discovered[i] = false;
}
}
/**
* destructor
*/
Tile::~Tile()
{
_inventory.clear();
}
/**
* Get the MapData pointer of a part of the tile.
* @param part the part 0-3.
* @return pointer to mapdata
*/
MapData *Tile::getMapData(int part) const
{
if (part < 0 || part > 3)
{
throw Exception("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;
}
/**
* Set the MapData references of part 0 to 3.
* @param mdsID MapDataSet ID
* @param mdID MapData ID
* @param part the part number
*/
void Tile::load(int mdsID, int mdID, int part)
{
_mdsID[part] = mdsID;
_mdID[part] = mdID;
}
/**
* get the MapData references of part 0 to 3.
* @param mdsID MapDataSet ID
* @param mdID MapData ID
* @param part the part number
*/
void Tile::getSaveGameData(int *mdsID, int *mdID, int part) const
{
*mdsID = _mdsID[part];
*mdID = _mdID[part];
}
/**
* Gets wether this tile has no objects. Note that we can have a unit or smoke on this tile.
* @return bool True if there is nothing but air on this tile.
*/
bool Tile::isVoid() const
{
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) const
{
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() const
{
if (_objects[MapData::O_FLOOR])
return _objects[MapData::O_FLOOR]->isNoFloor();
else
return true;
}
/**
* Whether this tile has a big wall.
* @return bool
*/
bool Tile::isBigWall() const
{
if (_objects[MapData::O_OBJECT])
return _objects[MapData::O_OBJECT]->isBigWall();
else
return false;
}
/**
* If an object stand on this tile, this returns how high the unit is it standing.
* @return the level in pixels
*/
int Tile::getTerrainLevel() const
{
int level = 0;
if (_objects[MapData::O_FLOOR])
level = _objects[MapData::O_FLOOR]->getTerrainLevel();
if (_objects[MapData::O_OBJECT])
level += _objects[MapData::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 sound ID
*/
int Tile::getFootstepSound() const
{
int sound = 0;
if (_objects[MapData::O_FLOOR])
sound = _objects[MapData::O_FLOOR]->getFootstepSound();
if (_objects[MapData::O_OBJECT])
sound = _objects[MapData::O_OBJECT]->getFootstepSound();
return sound;
}
/**
* Open a door on this tile.
* @param part
* @return a value: 0(normal door), 1(ufo door) or -1 if no door opened or 3 if ufo door(=animated) 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.
* This function assumes that there never are 2 doors on 1 tile or a door and another wall on 1 tile.
* @param part
* @return bool
*/
bool Tile::isUfoDoorOpen(int part) const
{
if (_objects[part] && _objects[part]->isUFODoor() && _currentFrame[part] != 0)
{
return true;
}
return false;
}
int Tile::closeUfoDoor()
{
int retval = 0;
for (int part = 0; part < 4; part++)
{
if (isUfoDoorOpen(part))
{
_currentFrame[part] = 0;
retval = 1;
}
}
return retval;
}
/**
* Sets the tile's cache flag. - TODO: set this for each object seperatly?
* @param flag true/false
* @param part 0-2 westwall/northwall/content+floor
*/
void Tile::setDiscovered(bool flag, int part)
{
if (_discovered[part] != flag)
{
_discovered[part] = flag;
if (part == 2)
{
_discovered[0] = flag;
_discovered[1] = flag;
}
// if light on tile changes, units and objects on it change light too
if (_unit != 0)
{
_unit->setCache(0);
}
}
}
/**
* Get the black fog of war state of this tile.
* @param part 0-2 westwall/northwall/content+floor
* @return bool True = discovered the tile.
*/
bool Tile::isDiscovered(int part) const
{
return _discovered[part];
}
/**
* Reset the light amount on the tile. This is done before a light level recalculation.
* @param layer Light is seperated in 3 layers: Ambient, Static and Dynamic.
*/
void Tile::resetLight(int layer)
{
_light[layer] = 0;
_lastLight[layer] = _light[layer];
}
/**
* Add the light amount on the tile. Only add light if the current light is lower.
* @param light Amount of light to add.
* @param layer Light is seperated in 3 layers: Ambient, Static and Dynamic.
*/
void Tile::addLight(int light, int layer)
{
if (_light[layer] < light)
_light[layer] = light;
}
/**
* Gets the tile's shade amount 0-15. It returns the brightest of all light layers.
* Shade level is the inverse of light level. So a maximum amount of light (15) returns shade level 0.
* @return shade
*/
int Tile::getShade() const
{
int light = 0;
for (int layer = 0; layer < LIGHTLAYERS; layer++)
{
if (_light[layer] > light)
light = _light[layer];
}
return 15 - light;
}
/**
* Destroy a part on this tile. We first remove the old object, then replace it with the destroyed one.
* This is because the object type of the old and new one are not nescessarly the same.
* If the destroyed part is an explosive, set the tile's explosive value, which will trigger a chained explosion.
* @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());
}
if (originalPart->getExplosive())
{
setExplosive(originalPart->getExplosive());
}
}
/* check if the floor on the lowest level is gone */
if (part == MapData::O_FLOOR && getPosition().z == 0 && _objects[MapData::O_FLOOR] == 0)
{
/* replace with scourched earth */
setMapData(MapDataSet::getScourgedEarthTile(), MapData::O_FLOOR);
}
}
/* damage terrain - check against armor*/
void Tile::damage(int part, int power)
{
if (power >= _objects[part]->getArmor())
destroy(part);
}
/**
* Set a "virtual" explosive on this tile. We mark a tile this way to detonate it later.
* We do it this way, because the same tile can be visited multiple times by an "explosion ray".
* The explosive power on the tile is some kind of moving average of the explosive rays that passes it.
* @param power
*/
void Tile::setExplosive(int power)
{
if (_explosive)
{
_explosive = (_explosive + power) / 2;
}
else
{
_explosive = power;
}
}
int Tile::getExplosive() const
{
return _explosive;
}
/**
* Apply the explosive power to the tile parts. This is where the actual destruction takes place.
*/
void Tile::detonate()
{
int explosive = _explosive;
_explosive = 0;
if (explosive)
{
// explosions create smoke which only stays 1 or 2 turns
addSmoke(1);
for (int i = 0; i < 4; ++i)
{
if(_objects[i])
{
if ((explosive) >= _objects[i]->getArmor())
{
int decrease = _objects[i]->getArmor();
destroy(i);
addSmoke(2);
if (_objects[i] && (explosive - decrease) >= _objects[i]->getArmor())
{
destroy(i);
}
}
}
}
// flammable of the tile needs to be 20 or lower (lower is better chance of catching fire) to catch fire
// note that when we get here, flammable objects can already be destroyed by the explosion, thus not catching fire.
int flam = getFlammability();
if (flam <= 20)
{
if (RNG::generate(0, 20) - flam >= 0)
{
ignite();
}
}
}
}
/*
* Flammability of a tile is the lowest flammability of it's objects.
* @return Flammability : the lower the value, the higher the chance the tile/object catches fire.
*/
int Tile::getFlammability() const
{
int flam = 255;
for (int i=0; i < 4; ++i)
{
if (_objects[i])
{
if (_objects[i]->getFlammable() < flam)
{
flam = _objects[i]->getFlammable();
}
}
}
return flam;
}
/*
* Ignite starts fire on a tile, it will burn <fuel> rounds. Fuel of a tile is the highest fuel of it's objects.
* NOT the sum of the fuel of the objects! TODO: check if this is like in the original.
*/
void Tile::ignite()
{
int fuel = 0;
for (int i=0; i < 4; ++i)
{
if (_objects[i])
{
if (_objects[i]->getFuel() > fuel)
{
fuel = _objects[i]->getFuel();
}
}
}
setFire(fuel + 1);
if (fuel > 1)
{
addSmoke(fuel * 2); // not sure
}
}
/**
* Animate the tile. This means to advance the current frame for every object.
* Ufo doors are a bit special, they animated only when triggered.
* When ufo doors are on frame 0(closed) or frame 7(open) they are not animated further.
*/
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;
}
_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) const
{
if (_objects[part] == 0)
return 0;
return _objects[part]->getDataset()->getSurfaceset()->getFrame(_objects[part]->getSprite(_currentFrame[part]));
}
/**
* Set a unit on this tile.
* @param unit
*/
void Tile::setUnit(BattleUnit *unit)
{
if (unit != 0)
{
unit->setTile(this);
}
_unit = unit;
}
/**
* Get the unit on this tile.
* @return BattleUnit.
*/
BattleUnit *Tile::getUnit() const
{
if (_unit != 0 && _unit->isOut())
return 0;
else
return _unit;
}
/**
* Set the amount of turns this tile is on fire. 0 = no fire.
* @param fire : amount of turns this tile is on fire.
*/
void Tile::setFire(int fire)
{
_fire = fire;
_animationOffset = RNG::generate(0,3);
}
/**
* Get the amount of turns this tile is on fire. 0 = no fire.
* @return fire : amount of turns this tile is on fire.
*/
int Tile::getFire() const
{
return _fire;
}
/**
* Set the amount of turns this tile is smoking. 0 = no smoke.
* @param smoke : amount of turns this tile is smoking.
*/
void Tile::addSmoke(int smoke)
{
_smoke += smoke;
if (_smoke > 40) _smoke = 40;
_animationOffset = RNG::generate(0,3);
}
/**
* Get the amount of turns this tile is smoking. 0 = no smoke.
* @return smoke : amount of turns this tile is smoking.
*/
int Tile::getSmoke() const
{
return _smoke;
}
/**
* Get the number of frames the fire or smoke animation is off-sync.
* To void fire and smoke animations of different tiles moving nice in sync - it looks fake.
* @return offset
*/
int Tile::getAnimationOffset() const
{
return _animationOffset;
}
/**
* Add an item on the tile.
* @param item
*/
void Tile::addItem(BattleItem *item)
{
_inventory.push_back(item);
}
/**
* Remove an item from the tile.
* @param item
*/
void Tile::removeItem(BattleItem *item)
{
for (std::vector<BattleItem*>::iterator i = _inventory.begin(); i != _inventory.end(); ++i)
{
if ((*i) == item)
{
_inventory.erase(i);
break;
}
}
}
/**
* Get the topmost item sprite to draw on the battlescape.
* @return item sprite ID in floorob, or -1 when no item
*/
int Tile::getTopItemSprite() const
{
if (!_inventory.empty())
{
return _inventory.at(0)->getRules()->getFloorSprite();
}
else
{
return -1;
}
}
/**
* New turn preparations. Decrease smoke and fire timers.
*/
void Tile::prepareNewTurn()
{
_smoke--;
if (_smoke < 0) _smoke = 0;
if (_fire == 1)
{
// fire will be finished in this turn
// destroy all objects that burned, and try to ignite again
for (int i = 0; i < 4; ++i)
{
if(_objects[i])
{
if (_objects[i]->getFlammable() < 255)
{
destroy(i);
}
}
}
if (getFlammability() < 255)
{
ignite();
}
else
{
_fire = 0;
}
}
else
{
_fire--;
if (_fire < 0) _fire = 0;
}
}
/**
* Get the inventory on this tile.
* @return pointer to a vector of battleitems.
*/
std::vector<BattleItem *> *Tile::getInventory()
{
return &_inventory;
}
}