forked from OpenXcom/OpenXcom
/
ProjectileFlyBState.cpp
722 lines (685 loc) · 23.4 KB
/
ProjectileFlyBState.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
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* 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/>.
*/
#define _USE_MATH_DEFINES
#include <cmath>
#include "ProjectileFlyBState.h"
#include "ExplosionBState.h"
#include "Projectile.h"
#include "TileEngine.h"
#include "Map.h"
#include "Pathfinding.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/Armor.h"
#include "../Ruleset/RuleItem.h"
#include "../Engine/Options.h"
#include "AlienBAIState.h"
#include "Camera.h"
#include "Explosion.h"
#include "BattlescapeState.h"
namespace OpenXcom
{
/**
* Sets up an ProjectileFlyBState.
*/
ProjectileFlyBState::ProjectileFlyBState(BattlescapeGame *parent, BattleAction action, Position origin) : BattleState(parent, action), _unit(0), _ammo(0), _projectileItem(0), _origin(origin), _originVoxel(-1,-1,-1), _projectileImpact(0), _initialized(false), _targetFloor(false)
{
}
ProjectileFlyBState::ProjectileFlyBState(BattlescapeGame *parent, BattleAction action) : BattleState(parent, action), _unit(0), _ammo(0), _projectileItem(0), _origin(action.actor->getPosition()), _originVoxel(-1,-1,-1), _projectileImpact(0), _initialized(false), _targetFloor(false)
{
}
/**
* Deletes the ProjectileFlyBState.
*/
ProjectileFlyBState::~ProjectileFlyBState()
{
}
/**
* Initializes the sequence:
* - checks if the shot is valid,
* - calculates the base accuracy.
*/
void ProjectileFlyBState::init()
{
if (_initialized) return;
_initialized = true;
BattleItem *weapon = _action.weapon;
_projectileItem = 0;
if (!weapon) // can't shoot without weapon
{
_parent->popState();
return;
}
if (!_parent->getSave()->getTile(_action.target)) // invalid target position
{
_parent->popState();
return;
}
if (_parent->getPanicHandled() &&
_action.type != BA_HIT &&
_action.type != BA_STUN &&
_action.actor->getTimeUnits() < _action.TU)
{
_action.result = "STR_NOT_ENOUGH_TIME_UNITS";
_parent->popState();
return;
}
_unit = _action.actor;
_ammo = weapon->getAmmoItem();
if (_unit->isOut() || _unit->getHealth() == 0 || _unit->getHealth() < _unit->getStunlevel())
{
// something went wrong - we can't shoot when dead or unconscious, or if we're about to fall over.
_parent->popState();
return;
}
// reaction fire
if (_unit->getFaction() != _parent->getSave()->getSide())
{
// no ammo or target is dead: give the time units back and cancel the shot.
if (_ammo == 0
|| !_parent->getSave()->getTile(_action.target)->getUnit()
|| _parent->getSave()->getTile(_action.target)->getUnit()->isOut()
|| _parent->getSave()->getTile(_action.target)->getUnit() != _parent->getSave()->getSelectedUnit())
{
_unit->setTimeUnits(_unit->getTimeUnits() + _unit->getActionTUs(_action.type, _action.weapon));
_parent->popState();
return;
}
}
// autoshot will default back to snapshot if it's not possible
if (weapon->getRules()->getAccuracyAuto() == 0 && _action.type == BA_AUTOSHOT)
_action.type = BA_SNAPSHOT;
// snapshot defaults to "hit" if it's a melee weapon
// (in case of reaction "shots" with a melee weapon)
if (weapon->getRules()->getBattleType() == BT_MELEE && _action.type == BA_SNAPSHOT)
_action.type = BA_HIT;
Tile *endTile = _parent->getSave()->getTile(_action.target);
switch (_action.type)
{
case BA_SNAPSHOT:
case BA_AIMEDSHOT:
case BA_AUTOSHOT:
case BA_LAUNCH:
if (_ammo == 0)
{
_action.result = "STR_NO_AMMUNITION_LOADED";
_parent->popState();
return;
}
if (_ammo->getAmmoQuantity() == 0)
{
_action.result = "STR_NO_ROUNDS_LEFT";
_parent->popState();
return;
}
if (_parent->getTileEngine()->distance(_action.actor->getPosition(), _action.target) > weapon->getRules()->getMaxRange())
{
// out of range
_action.result = "STR_OUT_OF_RANGE";
_parent->popState();
return;
}
break;
case BA_THROW:
if (!validThrowRange(&_action, _parent->getTileEngine()->getOriginVoxel(_action, 0), _parent->getSave()->getTile(_action.target)))
{
// out of range
_action.result = "STR_OUT_OF_RANGE";
_parent->popState();
return;
}
if (endTile &&
endTile->getTerrainLevel() == -24 &&
endTile->getPosition().z + 1 < _parent->getSave()->getMapSizeZ())
{
_action.target.z += 1;
}
_projectileItem = weapon;
break;
case BA_HIT:
if (!_parent->getTileEngine()->validMeleeRange(_action.actor->getPosition(), _action.actor->getDirection(), _action.actor, 0, &_action.target))
{
_action.result = "STR_THERE_IS_NO_ONE_THERE";
_parent->popState();
return;
}
performMeleeAttack();
return;
case BA_PANIC:
case BA_MINDCONTROL:
_parent->statePushFront(new ExplosionBState(_parent, Position((_action.target.x*16)+8,(_action.target.y*16)+8,(_action.target.z*24)+10), weapon, _action.actor));
return;
default:
_parent->popState();
return;
}
if (_action.type == BA_LAUNCH || (Options::forceFire && (SDL_GetModState() & KMOD_CTRL) != 0 && _parent->getSave()->getSide() == FACTION_PLAYER) || !_parent->getPanicHandled())
{
// target nothing, targets the middle of the tile
_targetVoxel = Position(_action.target.x*16 + 8, _action.target.y*16 + 8, _action.target.z*24 + 12);
if (_action.type == BA_LAUNCH)
{
if (_targetFloor)
{
// launched missiles with two waypoints placed on the same tile: target the floor.
_targetVoxel.z -= 10;
}
else
{
// launched missiles go slightly higher than the middle.
_targetVoxel.z += 4;
}
}
}
else
{
// determine the target voxel.
// aim at the center of the unit, the object, the walls or the floor (in that priority)
// if there is no LOF to the center, try elsewhere (more outward).
// Store this target voxel.
Tile *targetTile = _parent->getSave()->getTile(_action.target);
Position hitPos;
Position originVoxel = _parent->getTileEngine()->getOriginVoxel(_action, _parent->getSave()->getTile(_origin));
if (targetTile->getUnit() != 0)
{
if (_origin == _action.target || targetTile->getUnit() == _unit)
{
// don't shoot at yourself but shoot at the floor
_targetVoxel = Position(_action.target.x*16 + 8, _action.target.y*16 + 8, _action.target.z*24);
}
else
{
_parent->getTileEngine()->canTargetUnit(&originVoxel, targetTile, &_targetVoxel, _unit);
}
}
else if (targetTile->getMapData(MapData::O_OBJECT) != 0)
{
if (!_parent->getTileEngine()->canTargetTile(&originVoxel, targetTile, MapData::O_OBJECT, &_targetVoxel, _unit))
{
_targetVoxel = Position(_action.target.x*16 + 8, _action.target.y*16 + 8, _action.target.z*24 + 10);
}
}
else if (targetTile->getMapData(MapData::O_NORTHWALL) != 0)
{
if (!_parent->getTileEngine()->canTargetTile(&originVoxel, targetTile, MapData::O_NORTHWALL, &_targetVoxel, _unit))
{
_targetVoxel = Position(_action.target.x*16 + 8, _action.target.y*16, _action.target.z*24 + 9);
}
}
else if (targetTile->getMapData(MapData::O_WESTWALL) != 0)
{
if (!_parent->getTileEngine()->canTargetTile(&originVoxel, targetTile, MapData::O_WESTWALL, &_targetVoxel, _unit))
{
_targetVoxel = Position(_action.target.x*16, _action.target.y*16 + 8, _action.target.z*24 + 9);
}
}
else if (targetTile->getMapData(MapData::O_FLOOR) != 0)
{
if (!_parent->getTileEngine()->canTargetTile(&originVoxel, targetTile, MapData::O_FLOOR, &_targetVoxel, _unit))
{
_targetVoxel = Position(_action.target.x*16 + 8, _action.target.y*16 + 8, _action.target.z*24 + 2);
}
}
else
{
// target nothing, targets the middle of the tile
_targetVoxel = Position(_action.target.x*16 + 8, _action.target.y*16 + 8, _action.target.z*24 + 12);
}
}
if(createNewProjectile())
{
_parent->getMap()->setCursorType(CT_NONE);
_parent->getMap()->getCamera()->stopMouseScrolling();
}
}
/**
* Tries to create a projectile sprite and add it to the map,
* calculating its trajectory.
* @return True, if the projectile was successfully created.
*/
bool ProjectileFlyBState::createNewProjectile()
{
++_action.autoShotCounter;
int bulletSprite = -1;
if (_action.type != BA_THROW)
{
bulletSprite = _ammo->getRules()->getBulletSprite();
if (bulletSprite == -1)
{
bulletSprite = _action.weapon->getRules()->getBulletSprite();
}
}
// create a new projectile
Projectile *projectile = new Projectile(_parent->getResourcePack(), _parent->getSave(), _action, _origin, _targetVoxel, bulletSprite);
// add the projectile on the map
_parent->getMap()->setProjectile(projectile);
// set the speed of the state think cycle to 16 ms (roughly one think cycle per frame)
_parent->setStateInterval(1000/60);
// let it calculate a trajectory
_projectileImpact = V_EMPTY;
if (_action.type == BA_THROW)
{
_projectileImpact = projectile->calculateThrow(_unit->getThrowingAccuracy() / 100.0);
if (_projectileImpact == V_FLOOR || _projectileImpact == V_UNIT || _projectileImpact == V_OBJECT)
{
if (_unit->getFaction() != FACTION_PLAYER && _projectileItem->getRules()->getBattleType() == BT_GRENADE)
{
_projectileItem->setFuseTimer(0);
}
_projectileItem->moveToOwner(0);
_unit->setCache(0);
_parent->getMap()->cacheUnit(_unit);
_parent->getResourcePack()->getSound("BATTLE.CAT", 39)->play();
_unit->addThrowingExp();
}
else
{
// unable to throw here
delete projectile;
_parent->getMap()->setProjectile(0);
_action.result = "STR_UNABLE_TO_THROW_HERE";
_action.TU = 0;
_parent->popState();
return false;
}
}
else if (_action.weapon->getRules()->getArcingShot()) // special code for the "spit" trajectory
{
_projectileImpact = projectile->calculateThrow(_unit->getFiringAccuracy(_action.type, _action.weapon) / 100.0);
if (_projectileImpact != V_EMPTY && _projectileImpact != V_OUTOFBOUNDS)
{
// set the soldier in an aiming position
_unit->aim(true);
_unit->setCache(0);
_parent->getMap()->cacheUnit(_unit);
// and we have a lift-off
if (_ammo->getRules()->getFireSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _ammo->getRules()->getFireSound())->play();
}
else if (_action.weapon->getRules()->getFireSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _action.weapon->getRules()->getFireSound())->play();
}
if (!_parent->getSave()->getDebugMode() && _action.type != BA_LAUNCH && _ammo->spendBullet() == false)
{
_parent->getSave()->removeItem(_ammo);
_action.weapon->setAmmoItem(0);
}
}
else
{
// no line of fire
delete projectile;
_parent->getMap()->setProjectile(0);
_action.result = "STR_NO_LINE_OF_FIRE";
_unit->abortTurn();
_parent->popState();
return false;
}
}
else
{
if (_originVoxel != Position(-1,-1,-1))
{
_projectileImpact = projectile->calculateTrajectory(_unit->getFiringAccuracy(_action.type, _action.weapon) / 100.0, _originVoxel);
}
else
{
_projectileImpact = projectile->calculateTrajectory(_unit->getFiringAccuracy(_action.type, _action.weapon) / 100.0);
}
if (_projectileImpact != V_EMPTY || _action.type == BA_LAUNCH)
{
// set the soldier in an aiming position
_unit->aim(true);
_unit->setCache(0);
_parent->getMap()->cacheUnit(_unit);
// and we have a lift-off
if (_ammo->getRules()->getFireSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _ammo->getRules()->getFireSound())->play();
}
else if (_action.weapon->getRules()->getFireSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _action.weapon->getRules()->getFireSound())->play();
}
if (!_parent->getSave()->getDebugMode() && _action.type != BA_LAUNCH && _ammo->spendBullet() == false)
{
_parent->getSave()->removeItem(_ammo);
_action.weapon->setAmmoItem(0);
}
}
else
{
// no line of fire
delete projectile;
_parent->getMap()->setProjectile(0);
_action.result = "STR_NO_LINE_OF_FIRE";
_unit->abortTurn();
_parent->popState();
return false;
}
}
return true;
}
/**
* Animates the projectile (moves to the next point in its trajectory).
* If the animation is finished the projectile sprite is removed from the map,
* and this state is finished.
*/
void ProjectileFlyBState::think()
{
_parent->getSave()->getBattleState()->clearMouseScrollingState();
/* TODO refactoring : store the projectile in this state, instead of getting it from the map each time? */
if (_parent->getMap()->getProjectile() == 0)
{
Tile *t = _parent->getSave()->getTile(_action.actor->getPosition());
Tile *bt = _parent->getSave()->getTile(_action.actor->getPosition() + Position(0,0,-1));
bool hasFloor = t && !t->hasNoFloor(bt);
bool unitCanFly = _action.actor->getArmor()->getMovementType() == MT_FLY;
if (_action.type == BA_AUTOSHOT
&& _action.autoShotCounter < _action.weapon->getRules()->getAutoShots()
&& !_action.actor->isOut()
&& _ammo->getAmmoQuantity() != 0
&& (hasFloor || unitCanFly))
{
createNewProjectile();
if (_action.cameraPosition.z != -1)
{
_parent->getMap()->getCamera()->setMapOffset(_action.cameraPosition);
_parent->getMap()->invalidate();
}
}
else
{
if (_action.cameraPosition.z != -1)
{
_parent->getMap()->getCamera()->setMapOffset(_action.cameraPosition);
_parent->getMap()->invalidate();
}
if (_action.type != BA_PANIC && _action.type != BA_MINDCONTROL && !_parent->getSave()->getUnitsFalling())
{
_parent->getTileEngine()->checkReactionFire(_unit);
}
if (!_unit->isOut() && _action.type != BA_HIT)
{
_unit->abortTurn();
}
if (_parent->getSave()->getSide() == FACTION_PLAYER || _parent->getSave()->getDebugMode())
{
_parent->setupCursor();
}
_parent->popState();
}
}
else
{
if (_action.type != BA_THROW && _ammo && _ammo->getRules()->getShotgunPellets() != 0)
{
// shotgun pellets move to their terminal location instantly as fast as possible
_parent->getMap()->getProjectile()->skipTrajectory();
}
if(!_parent->getMap()->getProjectile()->move())
{
// impact !
if (_action.type == BA_THROW)
{
Position pos = _parent->getMap()->getProjectile()->getPosition(-1);
pos.x /= 16;
pos.y /= 16;
pos.z /= 24;
if (pos.y > _parent->getSave()->getMapSizeY())
{
pos.y--;
}
if (pos.x > _parent->getSave()->getMapSizeX())
{
pos.x--;
}
BattleItem *item = _parent->getMap()->getProjectile()->getItem();
_parent->getResourcePack()->getSound("BATTLE.CAT", 38)->play();
if (Options::battleInstantGrenade && item->getRules()->getBattleType() == BT_GRENADE && item->getFuseTimer() == 0)
{
// it's a hot grenade to explode immediately
_parent->statePushFront(new ExplosionBState(_parent, _parent->getMap()->getProjectile()->getPosition(-1), item, _action.actor));
}
else
{
_parent->dropItem(pos, item);
if (_unit->getFaction() != FACTION_PLAYER && _projectileItem->getRules()->getBattleType() == BT_GRENADE)
{
_parent->getTileEngine()->setDangerZone(pos, item->getRules()->getExplosionRadius(), _action.actor);
}
}
}
else if (_action.type == BA_LAUNCH && _action.waypoints.size() > 1 && _projectileImpact == -1)
{
_origin = _action.waypoints.front();
_action.waypoints.pop_front();
_action.target = _action.waypoints.front();
// launch the next projectile in the waypoint cascade
ProjectileFlyBState *nextWaypoint = new ProjectileFlyBState(_parent, _action, _origin);
nextWaypoint->setOriginVoxel(_parent->getMap()->getProjectile()->getPosition(-1));
if (_origin == _action.target)
{
nextWaypoint->targetFloor();
}
_parent->statePushNext(nextWaypoint);
}
else
{
if (_ammo && _action.type == BA_LAUNCH && _ammo->spendBullet() == false)
{
_parent->getSave()->removeItem(_ammo);
_action.weapon->setAmmoItem(0);
}
if (_projectileImpact != V_OUTOFBOUNDS)
{
int offset = 0;
// explosions impact not inside the voxel but two steps back (projectiles generally move 2 voxels at a time)
if (_ammo && _ammo->getRules()->getExplosionRadius() != 0 && _projectileImpact != V_UNIT)
{
offset = -2;
}
_parent->statePushFront(new ExplosionBState(_parent, _parent->getMap()->getProjectile()->getPosition(offset), _ammo, _action.actor, 0, (_action.type != BA_AUTOSHOT || _action.autoShotCounter == _action.weapon->getRules()->getAutoShots() || !_action.weapon->getAmmoItem())));
// special shotgun behaviour: trace extra projectile paths, and add bullet hits at their termination points.
if (_ammo && _ammo->getRules()->getShotgunPellets() != 0 && _ammo->getRules()->getDamageType()->isDirect())
{
int i = 1;
int bulletSprite = _ammo->getRules()->getBulletSprite();
if (bulletSprite == -1)
{
bulletSprite = _action.weapon->getRules()->getBulletSprite();
}
while (i != _ammo->getRules()->getShotgunPellets())
{
// create a projectile
Projectile *proj = new Projectile(_parent->getResourcePack(), _parent->getSave(), _action, _origin, _targetVoxel, bulletSprite);
// let it trace to the point where it hits
_projectileImpact = proj->calculateTrajectory(std::max(0.0, (_unit->getFiringAccuracy(_action.type, _action.weapon) / 100.0) - i * 5.0));
if (_projectileImpact != V_EMPTY)
{
// as above: skip the shot to the end of it's path
proj->skipTrajectory();
// insert an explosion and hit
if (_projectileImpact != V_OUTOFBOUNDS)
{
Explosion *explosion = new Explosion(proj->getPosition(1), _ammo->getRules()->getHitAnimation(), false, false);
_parent->getMap()->getExplosions()->push_back(explosion);
_parent->getSave()->getTileEngine()->hit(proj->getPosition(1), _ammo->getRules()->getPower(), _ammo->getRules()->getDamageType(), 0);
}
++i;
}
delete proj;
}
}
// if the unit burns floortiles, burn floortiles
if (_unit->getSpecialAbility() == SPECAB_BURNFLOOR)
{
_parent->getSave()->getTile(_action.target)->ignite(15);
}
if (_projectileImpact == 4)
{
BattleUnit *victim = _parent->getSave()->getTile(_parent->getMap()->getProjectile()->getPosition(offset) / Position(16,16,24))->getUnit();
if (victim && !victim->isOut() && victim->getFaction() == FACTION_HOSTILE)
{
AlienBAIState *aggro = dynamic_cast<AlienBAIState*>(victim->getCurrentAIState());
if (aggro != 0)
{
aggro->setWasHitBy(_unit);
_unit->setTurnsSinceSpotted(0);
}
}
}
}
else if (_action.type != BA_AUTOSHOT || _action.autoShotCounter == _action.weapon->getRules()->getAutoShots() || !_action.weapon->getAmmoItem())
{
_unit->aim(false);
_unit->setCache(0);
_parent->getMap()->cacheUnits();
}
}
delete _parent->getMap()->getProjectile();
_parent->getMap()->setProjectile(0);
}
}
}
/**
* Flying projectiles cannot be cancelled,
* but they can be "skipped".
*/
void ProjectileFlyBState::cancel()
{
if (_parent->getMap()->getProjectile())
{
_parent->getMap()->getProjectile()->skipTrajectory();
Position p = _parent->getMap()->getProjectile()->getPosition();
if (!_parent->getMap()->getCamera()->isOnScreen(Position(p.x/16, p.y/16, p.z/24), false))
_parent->getMap()->getCamera()->centerOnPosition(Position(p.x/16, p.y/16, p.z/24));
}
}
/**
* Validates the throwing range.
* @param action Pointer to throw action.
* @param origin Position to throw from.
* @param target Tile to throw to.
* @return True when the range is valid.
*/
bool ProjectileFlyBState::validThrowRange(BattleAction *action, Position origin, Tile *target)
{
// note that all coordinates and thus also distances below are in number of tiles (not in voxels).
if (action->type != BA_THROW)
{
return true;
}
int offset = 2;
int zd = (origin.z)-((action->target.z * 24 + offset) - target->getTerrainLevel());
int weight = action->weapon->getRules()->getWeight();
if (action->weapon->getAmmoItem() && action->weapon->getAmmoItem() != action->weapon)
{
weight += action->weapon->getAmmoItem()->getRules()->getWeight();
}
double maxDistance = (getMaxThrowDistance(weight, action->actor->getStats()->strength, zd) + 8) / 16.0;
int xdiff = action->target.x - action->actor->getPosition().x;
int ydiff = action->target.y - action->actor->getPosition().y;
double realDistance = sqrt((double)(xdiff*xdiff)+(double)(ydiff*ydiff));
return realDistance <= maxDistance;
}
/**
* Validates the throwing range.
* @param weight the weight of the object.
* @param strength the strength of the thrower.
* @param level the difference in height between the thrower and the target.
* @return the maximum throwing range.
*/
int ProjectileFlyBState::getMaxThrowDistance(int weight, int strength, int level)
{
double curZ = level + 0.5;
double dz = 1.0;
int dist = 0;
while (dist < 4000) //just in case
{
dist += 8;
if (dz<-1)
curZ -= 8;
else
curZ += dz * 8;
if (curZ < 0 && dz < 0) //roll back
{
dz = std::max(dz, -1.0);
if (abs(dz)>1e-10) //rollback horizontal
dist -= curZ / dz;
break;
}
dz -= (double)(50 * weight / strength)/100;
if (dz <= -2.0) //become falling
break;
}
return dist;
}
/**
* Set the origin voxel, used for the blaster launcher.
* @param pos the origin voxel.
*/
void ProjectileFlyBState::setOriginVoxel(Position pos)
{
_originVoxel = pos;
}
/**
* Set the boolean flag to angle a blaster bomb towards the floor.
*/
void ProjectileFlyBState::targetFloor()
{
_targetFloor = true;
}
void ProjectileFlyBState::performMeleeAttack()
{
BattleUnit *target = _parent->getSave()->getTile(_action.target)->getUnit();
int height = target->getFloatHeight() + (target->getHeight() / 2);
Position voxel;
_parent->getSave()->getPathfinding()->directionToVector(_unit->getDirection(), &voxel);
voxel = _action.target * Position(16, 16, 24) + Position(8,8,height - _parent->getSave()->getTile(_action.target)->getTerrainLevel()) - voxel;
// set the soldier in an aiming position
_unit->aim(true);
_unit->setCache(0);
_parent->getMap()->cacheUnit(_unit);
// and we have a lift-off
if (_ammo && _ammo->getRules()->getMeleeAttackSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _ammo->getRules()->getMeleeAttackSound())->play();
}
else if (_action.weapon->getRules()->getMeleeAttackSound() != -1)
{
_parent->getResourcePack()->getSound("BATTLE.CAT", _action.weapon->getRules()->getMeleeAttackSound())->play();
}
if (!_parent->getSave()->getDebugMode() && _action.weapon->getRules()->getBattleType() == BT_MELEE && _ammo && _ammo->spendBullet() == false)
{
_parent->getSave()->removeItem(_ammo);
_action.weapon->setAmmoItem(0);
}
_parent->getMap()->setCursorType(CT_NONE);
_parent->statePushNext(new ExplosionBState(_parent, voxel, _action.weapon, _action.actor, 0, true));
}
}