forked from OpenXcom/OpenXcom
/
ExplosionBState.cpp
321 lines (300 loc) · 9.23 KB
/
ExplosionBState.cpp
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
/*
* Copyright 2010-2014 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 "ExplosionBState.h"
#include "BattlescapeState.h"
#include "Explosion.h"
#include "TileEngine.h"
#include "UnitDieBState.h"
#include "Map.h"
#include "Camera.h"
#include "../Engine/Game.h"
#include "../Savegame/BattleUnit.h"
#include "../Savegame/BattleItem.h"
#include "../Savegame/SavedGame.h"
#include "../Savegame/SavedBattleGame.h"
#include "../Savegame/Tile.h"
#include "../Resource/ResourcePack.h"
#include "../Engine/Sound.h"
#include "../Ruleset/RuleItem.h"
#include "../Ruleset/Ruleset.h"
#include "../Ruleset/Armor.h"
#include "../Engine/RNG.h"
namespace OpenXcom
{
/**
* Sets up an ExplosionBState.
* @param parent Pointer to the BattleScape.
* @param center Center position in voxelspace.
* @param item Item involved in the explosion (eg grenade).
* @param unit Unit involved in the explosion (eg unit throwing the grenade).
* @param tile Tile the explosion is on.
* @param lowerWeapon Whether the unit causing this explosion should now lower their weapon.
*/
ExplosionBState::ExplosionBState(BattlescapeGame *parent, Position center, BattleItem *item, BattleUnit *unit, Tile *tile, bool lowerWeapon) : BattleState(parent), _unit(unit), _center(center), _item(item), _damageType(), _tile(tile), _power(0), _radius(6), _areaOfEffect(false), _lowerWeapon(lowerWeapon), _pistolWhip(false), _hit(false)
{
}
/**
* Deletes the ExplosionBState.
*/
ExplosionBState::~ExplosionBState()
{
}
/**
* Initializes the explosion.
* The animation and sound starts here.
* If the animation is finished, the actual effect takes place.
*/
void ExplosionBState::init()
{
if (_item)
{
_power = _item->getRules()->getPower();
_damageType = _item->getRules()->getDamageType();
_radius = _item->getRules()->getExplosionRadius();
// getCurrentAction() only works for player actions: aliens cannot melee attack with rifle butts.
_pistolWhip = (_item->getRules()->getBattleType() != BT_MELEE &&
_parent->getCurrentAction()->type == BA_HIT);
if (_pistolWhip)
{
_power = _item->getRules()->getMeleePower();
_damageType = _parent->getRuleset()->getDamageType(DT_MELEE);
_radius = 0;
}
// since melee aliens don't use a conventional weapon type, we use their strength instead.
if (_item->getRules()->isStrengthApplied())
{
_power += _unit->getStats()->strength;
}
_areaOfEffect = _item->getRules()->getBattleType() != BT_MELEE &&
_item->getRules()->getExplosionRadius() != 0 &&
!_pistolWhip;
}
else if (_tile)
{
_power = _tile->getExplosive();
_tile->setExplosive(0, true);
_damageType = _parent->getRuleset()->getDamageType(DT_HE);
_radius = _power /10;
_areaOfEffect = true;
}
else if (_unit && _unit->getSpecialAbility() == SPECAB_EXPLODEONDEATH)
{
RuleItem* corpse = _parent->getRuleset()->getItem(_unit->getArmor()->getCorpseGeoscape());
_power = corpse->getPower();
_damageType = corpse->getDamageType();
_radius = corpse->getExplosionRadius();
_areaOfEffect = true;
}
else
{
_power = 120;
_damageType = _parent->getRuleset()->getDamageType(DT_HE);
_areaOfEffect = true;
}
Tile *t = _parent->getSave()->getTile(Position(_center.x/16, _center.y/16, _center.z/24));
if (_areaOfEffect)
{
if (_power)
{
int frame = 0;
int counter = std::max(1, (_power/5) / 5);
for (int i = 0; i < _power/5; i++)
{
int X = RNG::generate(-_power/2,_power/2);
int Y = RNG::generate(-_power/2,_power/2);
Position p = _center;
p.x += X; p.y += Y;
Explosion *explosion = new Explosion(p, frame, true);
// add the explosion on the map
_parent->getMap()->getExplosions()->push_back(explosion);
if (i > 0 && i % counter == 0)
{
--frame;
}
}
_parent->setStateInterval(BattlescapeState::DEFAULT_ANIM_SPEED/2);
// explosion sound
if (_power <= 80)
_parent->getResourcePack()->getSound("BATTLE.CAT", 2)->play();
else
_parent->getResourcePack()->getSound("BATTLE.CAT", 5)->play();
_parent->getMap()->getCamera()->centerOnPosition(t->getPosition(), false);
}
else
{
_parent->popState();
}
}
else
// create a bullet hit
{
_parent->setStateInterval(std::max(1, ((BattlescapeState::DEFAULT_ANIM_SPEED/2) - (10 * _item->getRules()->getExplosionSpeed()))));
_hit = _pistolWhip || _item->getRules()->getBattleType() == BT_MELEE;
bool psi = _item->getRules()->getBattleType() == BT_PSIAMP;
int anim = _item->getRules()->getHitAnimation();
int sound = _item->getRules()->getHitSound();
if (_hit || psi) // Play melee animation on hitting and psiing
{
anim = _item->getRules()->getMeleeAnimation();
}
if (sound != -1)
{
// bullet hit sound
_parent->getResourcePack()->getSound("BATTLE.CAT", sound)->play();
}
Explosion *explosion = new Explosion(_center, anim, false, _hit || psi); // Don't burn the tile
_parent->getMap()->getExplosions()->push_back(explosion);
_parent->getMap()->getCamera()->setViewLevel(_center.z / 24);
BattleUnit *target = t->getUnit();
if ((_hit || psi) && _parent->getSave()->getSide() == FACTION_HOSTILE && target && target->getFaction() == FACTION_PLAYER)
{
_parent->getMap()->getCamera()->centerOnPosition(t->getPosition(), false);
}
}
}
/**
* Animates explosion sprites. If their animation is finished remove them from the list.
* If the list is empty, this state is finished and the actual calculations take place.
*/
void ExplosionBState::think()
{
for (std::list<Explosion*>::iterator i = _parent->getMap()->getExplosions()->begin(); i != _parent->getMap()->getExplosions()->end();)
{
if(!(*i)->animate())
{
delete (*i);
i = _parent->getMap()->getExplosions()->erase(i);
if (_parent->getMap()->getExplosions()->empty())
{
explode();
return;
}
}
else
{
++i;
}
}
}
/**
* Explosions cannot be cancelled.
*/
void ExplosionBState::cancel()
{
}
/**
* Calculates the effects of the explosion.
*/
void ExplosionBState::explode()
{
bool terrainExplosion = false;
SavedBattleGame *save = _parent->getSave();
// last minute adjustment: determine if we actually
if (_hit)
{
save->getBattleGame()->getCurrentAction()->type = BA_NONE;
BattleUnit *targetUnit = save->getTile(_center / Position(16, 16, 24))->getUnit();
if (_unit && !_unit->isOut())
{
_unit->aim(false);
_unit->setCache(0);
}
if (!RNG::percent(_unit->getFiringAccuracy(BA_HIT, _item)))
{
_parent->getMap()->cacheUnits();
_parent->popState();
return;
}
else if (targetUnit && targetUnit->getOriginalFaction() == FACTION_HOSTILE &&
_unit->getOriginalFaction() == FACTION_PLAYER)
{
_unit->addMeleeExp();
}
if (_item->getRules()->getMeleeHitSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _item->getRules()->getMeleeHitSound())->play();
}
}
// after the animation is done, the real explosion/hit takes place
if (_item)
{
if (!_unit && _item->getPreviousOwner())
{
_unit = _item->getPreviousOwner();
}
}
if (_areaOfEffect)
{
save->getTileEngine()->explode(_center, _power, _damageType, _radius, _unit);
}
else
{
BattleUnit *victim = save->getTileEngine()->hit(_center, _power, _damageType, _unit);
// check if this unit turns others into zombies
if (!_item->getRules()->getZombieUnit().empty()
&& victim
&& victim->getArmor()->getSize() == 1
&& victim->getSpawnUnit().empty()
&& victim->getOriginalFaction() != FACTION_HOSTILE)
{
// converts the victim to a zombie on death
victim->setSpecialAbility(SPECAB_RESPAWN);
victim->setSpawnUnit(_item->getRules()->getZombieUnit());
}
}
if (_tile)
{
terrainExplosion = true;
}
if (!_tile && !_item)
{
terrainExplosion = true;
}
// now check for new casualties
_parent->checkForCasualties(_item, _unit, false, terrainExplosion);
// if this explosion was caused by a unit shooting, now it's the time to put the gun down
if (_unit && !_unit->isOut() && _lowerWeapon)
{
_unit->aim(false);
_unit->setCache(0);
}
_parent->getMap()->cacheUnits();
_parent->popState();
// check for terrain explosions
Tile *t = save->getTileEngine()->checkForTerrainExplosions();
if (t)
{
Position p = Position(t->getPosition().x * 16, t->getPosition().y * 16, t->getPosition().z * 24);
p += Position(8,8,0);
_parent->statePushFront(new ExplosionBState(_parent, p, 0, _unit, t));
}
if (_item && (_item->getRules()->getBattleType() == BT_GRENADE || _item->getRules()->getBattleType() == BT_PROXIMITYGRENADE))
{
for (std::vector<BattleItem*>::iterator j = _parent->getSave()->getItems()->begin(); j != _parent->getSave()->getItems()->end(); ++j)
{
if (_item->getId() == (*j)->getId())
{
delete *j;
_parent->getSave()->getItems()->erase(j);
break;
}
}
}
}
}