/
BattlescapeGame.cpp
1171 lines (1075 loc) · 29.6 KB
/
BattlescapeGame.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2012 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 <sstream>
#include "BattlescapeGame.h"
#include "BattlescapeState.h"
#include "../Engine/Timer.h"
#include "Map.h"
#include "Camera.h"
#include "NextTurnState.h"
#include "AbortMissionState.h"
#include "BattleState.h"
#include "UnitTurnBState.h"
#include "UnitWalkBState.h"
#include "ProjectileFlyBState.h"
#include "ExplosionBState.h"
#include "TileEngine.h"
#include "ActionMenuState.h"
#include "UnitInfoState.h"
#include "UnitDieBState.h"
#include "InventoryState.h"
#include "AggroBAIState.h"
#include "PatrolBAIState.h"
#include "Pathfinding.h"
#include "../Engine/Game.h"
#include "../Engine/Music.h"
#include "../Engine/Language.h"
#include "../Engine/Font.h"
#include "../Engine/Palette.h"
#include "../Engine/Surface.h"
#include "../Engine/SurfaceSet.h"
#include "../Engine/Screen.h"
#include "../Engine/SoundSet.h"
#include "../Engine/Sound.h"
#include "../Engine/Action.h"
#include "../Resource/ResourcePack.h"
#include "../Interface/Cursor.h"
#include "../Interface/FpsCounter.h"
#include "../Interface/Text.h"
#include "../Interface/Bar.h"
#include "../Interface/ImageButton.h"
#include "../Interface/NumberText.h"
#include "../Savegame/SavedGame.h"
#include "../Savegame/SavedBattleGame.h"
#include "../Savegame/Tile.h"
#include "../Savegame/BattleUnit.h"
#include "../Savegame/Soldier.h"
#include "../Savegame/BattleItem.h"
#include "../Ruleset/Ruleset.h"
#include "../Ruleset/RuleItem.h"
#include "../Ruleset/Armor.h"
#include "../Engine/Options.h"
#include "WarningMessage.h"
#include "BattlescapeOptionsState.h"
#include "DebriefingState.h"
#include "../Engine/RNG.h"
#include "InfoboxState.h"
#include "InfoboxOKState.h"
#include "MiniMapState.h"
namespace OpenXcom
{
/**
* Initializes all the elements in the Battlescape screen.
* @param save Pointer to the save game.
* @param parentState Pointer to the parent battlescape state.
*/
BattlescapeGame::BattlescapeGame(SavedBattleGame *save, BattlescapeState *parentState) : _save(save), _parentState(parentState)
{
_tuReserved = BA_NONE;
_debugPlay = false;
_playerPanicHandled = true;
_AIActionCounter = 0;
_currentAction.actor = 0;
checkForCasualties(0, 0, true);
cancelCurrentAction();
_currentAction.targeting = false;
_currentAction.type = BA_NONE;
}
/**
* Delete BattlescapeGame.
*/
BattlescapeGame::~BattlescapeGame()
{
}
/**
* think
*/
void BattlescapeGame::think()
{
// nothing is happening - see if we need some alien AI or units panicking or what have you
if (_states.empty())
{
// it's a non player side (ALIENS or CIVILIANS)
if (_save->getSide() != FACTION_PLAYER)
{
if (!_debugPlay)
{
if (_save->getSelectedUnit())
{
if (!handlePanickingUnit(_save->getSelectedUnit()))
handleAI(_save->getSelectedUnit());
}
else
{
if (_save->selectNextPlayerUnit(true) == 0)
{
if (!_save->getDebugMode())
{
statePushBack(0); // end AI turn
}
else
{
_save->selectNextPlayerUnit(false);
_debugPlay = true;
}
}
}
}
}
else
{
// it's a player side && we have not handled all panicking units
if (!_playerPanicHandled)
{
_playerPanicHandled = handlePanickingPlayer();
}
}
}
}
void BattlescapeGame::init()
{
if (_save->getSide() == FACTION_PLAYER)
{
_playerPanicHandled = false;
}
}
/**
* Handles the processing of the AI states of a unit.
* @param unit Pointer to an unit.
*/
void BattlescapeGame::handleAI(BattleUnit *unit)
{
BattleAIState *ai = unit->getCurrentAIState();
if (!ai)
{
// for some reason the unit had no AI routine assigned..
unit->setAIState(new PatrolBAIState(_save, unit, 0));
ai = unit->getCurrentAIState();
}
_AIActionCounter++;
AggroBAIState *aggro = dynamic_cast<AggroBAIState*>(ai);
BattleAction action;
unit->think(&action);
if (action.type == BA_WALK)
{
_save->getPathfinding()->calculate(action.actor, action.target);
statePushBack(new UnitWalkBState(this, action));
}
if (action.type == BA_SNAPSHOT || action.type == BA_AUTOSHOT || action.type == BA_THROW)
{
statePushBack(new ProjectileFlyBState(this, action));
}
if (action.type == BA_NONE)
{
_AIActionCounter = 0;
if (aggro != 0)
{
// we lost aggro
unit->setAIState(new PatrolBAIState(_save, unit, 0));
}
if (_save->selectNextPlayerUnit(true) == 0)
{
if (!_save->getDebugMode())
{
statePushBack(0); // end AI turn
}
else
{
_save->selectNextPlayerUnit(false);
_debugPlay = true;
}
}
if (_save->getSelectedUnit())
{
getMap()->getCamera()->centerOnPosition(_save->getSelectedUnit()->getPosition());
}
}
}
/**
* Kneel/Standup.
* @param bu Pointer to a unit.
*/
void BattlescapeGame::kneel(BattleUnit *bu)
{
int tu = bu->isKneeled()?8:4;
if (checkReservedTU(bu, tu))
{
if (bu->spendTimeUnits(tu, _save->getDebugMode()))
{
bu->kneel(!bu->isKneeled());
// kneeling or standing up can reveal new terrain or units. I guess.
getTileEngine()->calculateFOV(bu);
getMap()->cacheUnits();
_parentState->updateSoldierInfo();
BattleAction action;
if (getTileEngine()->checkReactionFire(bu, &action, 0, false))
{
statePushBack(new ProjectileFlyBState(this, action));
}
}
else
{
_parentState->warning("STR_NOT_ENOUGH_TIME_UNITS");
}
}
}
/**
* End turn.
*/
void BattlescapeGame::endTurn()
{
Position p;
_debugPlay = false;
_currentAction.type = BA_NONE;
// check for hot grenades on the ground
for (int i = 0; i < _save->getWidth() * _save->getLength() * _save->getHeight(); ++i)
{
for (std::vector<BattleItem*>::iterator it = _save->getTiles()[i]->getInventory()->begin(); it != _save->getTiles()[i]->getInventory()->end(); )
{
if ((*it)->getRules()->getBattleType() == BT_GRENADE && (*it)->getExplodeTurn() > 0 && (*it)->getExplodeTurn() <= _save->getTurn()) // it's a grenade to explode now
{
p.x = _save->getTiles()[i]->getPosition().x*16 + 8;
p.y = _save->getTiles()[i]->getPosition().y*16 + 8;
p.z = _save->getTiles()[i]->getPosition().z*24 + _save->getTiles()[i]->getTerrainLevel();
statePushNext(new ExplosionBState(this, p, (*it), (*it)->getPreviousOwner()));
_save->removeItem((*it));
statePushBack(0);
return;
}
++it;
}
}
if (_save->getTileEngine()->closeUfoDoors())
{
getResourcePack()->getSoundSet("BATTLE.CAT")->getSound(21)->play(); // ufo door closed
}
_save->endTurn();
bool bBattleIsOver = checkForCasualties(0, 0, false, false);
if (bBattleIsOver)
{
return;
}
if (_save->getSide() == FACTION_PLAYER)
{
setupCursor();
}
else
{
getMap()->setCursorType(CT_NONE);
}
// 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);
statePushNext(new ExplosionBState(this, p, 0, 0, t));
}
bBattleIsOver = checkForCasualties(0, 0, false, true);
if (bBattleIsOver)
{
return;
}
_parentState->updateSoldierInfo();
if (playableUnitSelected())
{
getMap()->getCamera()->centerOnPosition(_save->getSelectedUnit()->getPosition());
setupCursor();
}
if (_save->getSide() != FACTION_NEUTRAL)
{
_parentState->getGame()->pushState(new NextTurnState(_parentState->getGame(), _save, _parentState));
}
}
/**
* Checks for casualties and adjusts morale accordingly.
* @param murderweapon Need to know this, for a HE explosion there is an instant death.
* @param murderer This is needed for credits for the kill.
* @param hiddenExplosion Set to true for the explosions of UFO Power sources at start of battlescape.
* @param terrainExplosion Set to true for the explosions of terrain.
* @return Whether the battle is finished.
*/
bool BattlescapeGame::checkForCasualties(BattleItem *murderweapon, BattleUnit *murderer, bool hiddenExplosion, bool terrainExplosion)
{
for (std::vector<BattleUnit*>::iterator j = _save->getUnits()->begin(); j != _save->getUnits()->end(); ++j)
{
if ((*j)->getHealth() == 0 && (*j)->getStatus() != STATUS_DEAD && (*j)->getStatus() != STATUS_FALLING)
{
BattleUnit *victim = (*j);
if (murderer)
{
murderer->addKillCount();
// if there is a known murderer, he will get a morale bonus if he is of a different faction (what with neutral?)
if ((victim->getFaction() == FACTION_PLAYER && murderer->getFaction() == FACTION_HOSTILE) ||
(victim->getFaction() == FACTION_HOSTILE && murderer->getFaction() == FACTION_PLAYER))
{
murderer->moraleChange(+20);
}
// murderer will get a penalty with friendly fire
if (victim->getFaction() == murderer->getFaction())
{
murderer->moraleChange(-20);
}
}
for (std::vector<BattleUnit*>::iterator i = _save->getUnits()->begin(); i != _save->getUnits()->end(); ++i)
{
// the losing squad all get a morale loss
if ((*i)->getFaction() == victim->getFaction())
{
(*i)->moraleChange(-(22 - ((*i)->getStats()->bravery / 10)*2));
// revenge procedure:
// if the victim is hostile, the nearest other hostile will aggro if he wasn't already
if (victim->getFaction() == FACTION_HOSTILE && murderer)
{
int closest = 1000000;
BattleUnit *revenger = 0;
bool revenge = RNG::generate(0,100) < 50;
for (std::vector<BattleUnit*>::iterator h = _save->getUnits()->begin(); h != _save->getUnits()->end(); ++h)
{
if ((*h)->getFaction() == FACTION_HOSTILE && !(*h)->isOut() && (*h) != victim)
{
int d = _save->getTileEngine()->distance(victim->getPosition(), (*h)->getPosition());
if (d < closest)
{
revenger = (*h);
closest = d;
}
}
}
// aliens with aggression level 2 always revenge
// aliens with aggression level 1 have 50% chance to revenge
// aliens with aggression level 0 never revenge
if (revenger && (revenger->getAggression() == 2 || (revenger->getAggression() == 1 && revenge)))
{
AggroBAIState *aggro = dynamic_cast<AggroBAIState*>(revenger->getCurrentAIState());
if (aggro == 0)
{
aggro = new AggroBAIState(_save, revenger);
revenger->setAIState(aggro);
}
aggro->setAggroTarget(murderer);
}
}
}
// the winning squad all get a morale increase
if ((*i)->getFaction() != victim->getFaction())
{
(*i)->moraleChange(+10);
}
}
if (murderweapon)
{
statePushNext(new UnitDieBState(this, (*j), murderweapon->getRules()->getDamageType(), false));
}
else
{
if (hiddenExplosion)
{
// this is instant death from UFO powersources, without screaming sounds
statePushNext(new UnitDieBState(this, (*j), DT_HE, true));
}
else
{
if (terrainExplosion)
{
// terrain explosion
statePushNext(new UnitDieBState(this, (*j), DT_HE, false));
}
else
{
// no murderer, and no terrain explosion, must be fatal wounds
statePushNext(new UnitDieBState(this, (*j), DT_AP, false)); // STR_HAS_DIED_FROM_A_FATAL_WOUND
// show a little infobox with the name of the unit and "... is panicking"
_parentState->getGame()->pushState(new InfoboxOKState(_parentState->getGame(), (*j)->getName(_parentState->getGame()->getLanguage()), "STR_HAS_DIED_FROM_A_FATAL_WOUND"));
}
}
}
}
else if ((*j)->getStunlevel() >= (*j)->getHealth() && (*j)->getStatus() != STATUS_DEAD && (*j)->getStatus() != STATUS_UNCONSCIOUS && (*j)->getStatus() != STATUS_FALLING && (*j)->getStatus() != STATUS_TURNING)
{
if (!murderer)
{
// fell unconscious from stun level
statePushNext(new UnitDieBState(this, (*j), DT_STUN, true)); // STR_HAS_BECOME_UNCONSCIOUS
// show a little infobox with the name of the unit and "... is panicking"
_parentState->getGame()->pushState(new InfoboxOKState(_parentState->getGame(), (*j)->getName(_parentState->getGame()->getLanguage()), "STR_HAS_BECOME_UNCONSCIOUS"));
}
else
{
statePushNext(new UnitDieBState(this, (*j), DT_STUN, true));
}
}
}
return false;
}
/**
* Handles the result of non target actions, like priming a grenade.
*/
void BattlescapeGame::handleNonTargetAction()
{
if (!_currentAction.targeting)
{
if (_currentAction.type == BA_PRIME && _currentAction.value > -1)
{
if (_currentAction.actor->spendTimeUnits(_currentAction.TU, dontSpendTUs()))
{
_currentAction.weapon->setExplodeTurn(_save->getTurn() + _currentAction.value);
}
else
{
_parentState->warning("STR_NOT_ENOUGH_TIME_UNITS");
}
}
if (_currentAction.type == BA_USE || _currentAction.type == BA_LAUNCH)
{
if (_currentAction.result.length() > 0)
{
_parentState->warning(_currentAction.result);
_currentAction.result = "";
}
_save->reviveUnconsciousUnits();
}
_currentAction.type = BA_NONE;
_parentState->updateSoldierInfo();
}
setupCursor();
}
/**
* Set the cursor according to the selected action.
*/
void BattlescapeGame::setupCursor()
{
if (_currentAction.targeting)
{
if (_currentAction.type == BA_THROW)
{
getMap()->setCursorType(CT_THROW);
}
else if (_currentAction.type == BA_MINDCONTROL || _currentAction.type == BA_PANIC)
{
getMap()->setCursorType(CT_PSI);
}
else if (_currentAction.type == BA_LAUNCH)
{
getMap()->setCursorType(CT_WAYPOINT);
}
else
{
getMap()->setCursorType(CT_AIM);
}
}
else
{
_currentAction.actor = _save->getSelectedUnit();
if (_currentAction.actor)
{
getMap()->setCursorType(CT_NORMAL, _currentAction.actor->getArmor()->getSize());
}
}
}
/**
* Whether a playable unit is selected. Normally only player side units can be selected, but in debug mode one can play with aliens too :)
* Is used to see if stats can be displayed and action buttons will work.
* @return whether a playable unit is selected.
*/
bool BattlescapeGame::playableUnitSelected()
{
return _save->getSelectedUnit() != 0 && (_save->getSide() == FACTION_PLAYER || _save->getDebugMode());
}
/**
* Give time slice to the front state.
*/
void BattlescapeGame::handleState()
{
if (!_states.empty())
{
// end turn request?
if (_states.front() == 0)
{
_states.pop_front();
endTurn();
return;
}
else
{
_states.front()->think();
}
getMap()->draw(); // redraw map
}
}
/**
* Push a state at the front of the queue and start it.
* @param bs Battlestate.
*/
void BattlescapeGame::statePushFront(BattleState *bs)
{
_states.push_front(bs);
bs->init();
}
/**
* Push a state as the next state after the current one.
* @param bs Battlestate.
*/
void BattlescapeGame::statePushNext(BattleState *bs)
{
if (_states.empty())
{
_states.push_front(bs);
bs->init();
}
else
{
_states.insert(++_states.begin(), bs);
}
}
/**
* Push a state at the back.
* @param bs Battlestate.
*/
void BattlescapeGame::statePushBack(BattleState *bs)
{
if (_states.empty())
{
_states.push_front(bs);
// end turn request?
if (_states.front() == 0)
{
_states.pop_front();
endTurn();
return;
}
else
{
bs->init();
}
}
else
{
_states.push_back(bs);
}
}
/**
* This is a very important function. It is called by a BattleState (walking, projectile is flying, explosions,...) at the moment this state has finished it's action.
* Here we check the result of that action and do all the aftermath.
* The state is popped off the list.
*/
void BattlescapeGame::popState()
{
bool actionFailed = false;
if (_states.empty()) return;
BattleAction action = _states.front()->getAction();
if (action.result.length() > 0 && action.actor->getFaction() == FACTION_PLAYER && _playerPanicHandled && (_save->getSide() == FACTION_PLAYER || _debugPlay))
{
_parentState->warning(action.result);
actionFailed = true;
}
_states.pop_front();
// handle the end of this unit's actions
if (action.actor && noActionsPending(action.actor))
{
if (action.actor->getFaction() == FACTION_PLAYER)
{
// spend TUs of "target triggered actions" (shooting, throwing) only
// the other actions' TUs (healing,scanning,..) are already take care of
if (action.targeting && _save->getSelectedUnit() && !actionFailed)
{
action.actor->spendTimeUnits(action.TU, dontSpendTUs());
}
if (_save->getSide() == FACTION_PLAYER)
{
// after throwing the cursor returns to default cursor, after shooting it stays in targeting mode and the player can shoot again in the same mode (autoshot,snap,aimed)
if ((action.type == BA_THROW || action.type == BA_LAUNCH) && !actionFailed)
{
// clean up the waypoints
if (action.type == BA_LAUNCH)
{
_currentAction.waypoints.clear();
}
cancelCurrentAction();
}
_parentState->getGame()->getCursor()->setVisible(true);
setupCursor();
}
}
else
{
// spend TUs
action.actor->spendTimeUnits(action.TU, false);
if (_save->getSide() != FACTION_PLAYER && !_debugPlay)
{
// AI does two things per unit, before switching to the next, or it got killed before doing the second thing
if (_AIActionCounter > 1 || _save->getSelectedUnit() == 0 || _save->getSelectedUnit()->isOut())
{
_AIActionCounter = 0;
if (_save->selectNextPlayerUnit(true) == 0)
{
if (!_save->getDebugMode())
{
statePushBack(0); // end AI turn
}
else
{
_save->selectNextPlayerUnit(false);
_debugPlay = true;
}
}
if (_save->getSelectedUnit())
{
getMap()->getCamera()->centerOnPosition(_save->getSelectedUnit()->getPosition());
}
}
}
else if (_debugPlay)
{
_parentState->getGame()->getCursor()->setVisible(true);
setupCursor();
}
}
}
if (!_states.empty())
{
// end turn request?
if (_states.front() == 0)
{
_states.pop_front();
endTurn();
return;
}
// init the next state in queue
_states.front()->init();
}
// the currently selected unit died or became unconscious or disappeared inexplicably
if (_save->getSelectedUnit() == 0 || _save->getSelectedUnit()->isOut())
{
cancelCurrentAction();
getMap()->setCursorType(CT_NORMAL, 1);
_parentState->getGame()->getCursor()->setVisible(true);
_save->setSelectedUnit(0);
}
_parentState->updateSoldierInfo();
// the unit became unconscious - show popup
if (action.actor && action.actor->getStatus() == STATUS_UNCONSCIOUS)
{
std::wstringstream ss;
ss << action.actor->getName(_parentState->getGame()->getLanguage()) << L'\n' << _parentState->getGame()->getLanguage()->getString("STR_HAS_BECOME_UNCONSCIOUS");
_parentState->getGame()->pushState(new InfoboxState(_parentState->getGame(), ss.str()));
}
}
bool BattlescapeGame::noActionsPending(BattleUnit *bu)
{
if (_states.empty()) return true;
for (std::list<BattleState*>::iterator i = _states.begin(); i != _states.end(); ++i)
{
if ((*i) != 0 && (*i)->getAction().actor == bu)
return false;
}
return true;
}
/**
* Sets the timer interval for think() calls of the state.
* @param interval An interval in ms.
*/
void BattlescapeGame::setStateInterval(Uint32 interval)
{
_parentState->setStateInterval(interval);
}
/**
* Check against reserved time units.
* @param bu Pointer to the unit.
* @param tu Number of time units to check.
* @return bool Whether or not we got enough time units.
*/
bool BattlescapeGame::checkReservedTU(BattleUnit *bu, int tu)
{
if (dontSpendTUs() || _save->getSide() != FACTION_PLAYER) return true; // aliens don't reserve TUs
if (_tuReserved != BA_NONE &&
tu + bu->getActionTUs(_tuReserved, bu->getMainHandWeapon()) > bu->getTimeUnits() &&
bu->getActionTUs(_tuReserved, bu->getMainHandWeapon()) <= bu->getTimeUnits())
{
if (_save->getSide() == FACTION_PLAYER)
{
switch (_tuReserved)
{
case BA_SNAPSHOT: _parentState->warning("STR_TIME_UNITS_RESERVED_FOR_SNAP_SHOT"); break;
case BA_AUTOSHOT: _parentState->warning("STR_TIME_UNITS_RESERVED_FOR_AUTO_SHOT"); break;
case BA_AIMEDSHOT: _parentState->warning("STR_TIME_UNITS_RESERVED_FOR_AIMED_SHOT"); break;
default: ;
}
}
return false;
}
return true;
}
/**
* Pick the first soldier from the list in status panic.
* @return True when all panicking is over.
*/
bool BattlescapeGame::handlePanickingPlayer()
{
for (std::vector<BattleUnit*>::iterator j = _save->getUnits()->begin(); j != _save->getUnits()->end(); ++j)
{
if (handlePanickingUnit(*j))
return false;
}
return true;
}
/**
* Common function for panicking units.
* @return False when unit not in panicking mode.
*/
bool BattlescapeGame::handlePanickingUnit(BattleUnit *unit)
{
UnitStatus status = unit->getStatus();
if (status != STATUS_PANICKING && status != STATUS_BERSERK) return false;
unit->setVisible(true);
getMap()->getCamera()->centerOnPosition(unit->getPosition());
_save->setSelectedUnit(unit);
// show a little infobox with the name of the unit and "... is panicking"
std::wstringstream ss;
ss << unit->getName(_parentState->getGame()->getLanguage()) << L'\n' << _parentState->getGame()->getLanguage()->getString(status==STATUS_PANICKING?"STR_HAS_PANICKED":"STR_HAS_GONE_BERSERK");
_parentState->getGame()->pushState(new InfoboxState(_parentState->getGame(), ss.str()));
unit->abortTurn(); //makes the unit go to status STANDING :p
int flee = RNG::generate(0,100);
switch (status)
{
case STATUS_PANICKING: // 1/2 chance to freeze and 1/2 chance try to flee
if (flee <= 50)
{
BattleItem *item = unit->getItem("STR_RIGHT_HAND");
if (item)
{
dropItem(unit->getPosition(), item);
item->moveToOwner(0);
}
item = unit->getItem("STR_LEFT_HAND");
if (item)
{
dropItem(unit->getPosition(), item);
item->moveToOwner(0);
}
unit->setCache(0);
BattleAction ba;
ba.actor = unit;
ba.target = Position(unit->getPosition().x + RNG::generate(-5,5), unit->getPosition().y + RNG::generate(-5,5), unit->getPosition().z);
if (_save->getTile(ba.target)) // only walk towards it when the place exists
{
_save->getPathfinding()->calculate(ba.actor, ba.target);
statePushBack(new UnitWalkBState(this, ba));
}
}
break;
case STATUS_BERSERK: // berserk - do some weird turning around and then aggro towards an enemy unit or shoot towards random place
BattleAction ba;
for (int i= 0; i < 4; i++)
{
ba.actor = unit;
ba.target = Position(unit->getPosition().x + RNG::generate(-5,5), unit->getPosition().y + RNG::generate(-5,5), unit->getPosition().z);
statePushBack(new UnitTurnBState(this, ba));
}
for (std::vector<BattleUnit*>::iterator j = unit->getVisibleUnits()->begin(); j != unit->getVisibleUnits()->end(); ++j)
{
ba.target = (*j)->getPosition();
statePushBack(new UnitTurnBState(this, ba));
}
if (_save->getTile(ba.target) != 0)
{
ba.type = BA_SNAPSHOT;
ba.weapon = unit->getMainHandWeapon();
for (int i= 0; i < 10; i++)
{
statePushBack(new ProjectileFlyBState(this, ba));
}
}
ba.type = BA_NONE;
break;
}
unit->setTimeUnits(0);
unit->moraleChange(+15);
return true;
}
/**
* TUs are not spent when handling panicking mode or in debug mode.
* @return Whether TUs are spent or not.
*/
bool BattlescapeGame::dontSpendTUs()
{
if (_save->getDebugMode())
return true;
if (!_playerPanicHandled)
return true;
return false;
}
/**
* This will cancel the current action the user had selected (firing, throwing,..)
* @return Whether an action was cancelled or not.
*/
bool BattlescapeGame::cancelCurrentAction()
{
bool bPreviewed = Options::getBool("battlePreviewPath");
if (_save->getPathfinding()->removePreview() && bPreviewed) return true;
if (_states.empty())
{
if (_currentAction.targeting)
{
if (_currentAction.type == BA_LAUNCH && _currentAction.waypoints.size() > 0)
{
_currentAction.waypoints.pop_back();
if (getMap()->getWaypoints()->size() > 0)
{
getMap()->getWaypoints()->pop_back();
}
if (_currentAction.waypoints.size() == 0)
{
_parentState->showLaunchButton(false);
}
return true;
}
else
{
_currentAction.targeting = false;
_currentAction.type = BA_NONE;
setupCursor();
_parentState->getGame()->getCursor()->setVisible(true);
return true;
}
}
}
else
{
_states.front()->cancel();
return true;
}
return false;
}
/**
* Get pointer to access action members directly.
* @return pointer to action.
*/
BattleAction *BattlescapeGame::getCurrentAction()
{
return &_currentAction;
}
/**
* Is there currently an action going on.
* @return true or false.
*/
bool BattlescapeGame::isBusy()
{
return !_states.empty();
}
/**
* Activate primary action (left click)
* @param pos Position on the map.
*/
void BattlescapeGame::primaryAction(const Position &pos)
{
bool bPreviewed = Options::getBool("battlePreviewPath");
if (_currentAction.targeting && _save->getSelectedUnit())
{
if (_currentAction.type == BA_LAUNCH)
{
_parentState->showLaunchButton(true);
_currentAction.waypoints.push_back(pos);
getMap()->getWaypoints()->push_back(pos);
}
else
{
_currentAction.target = pos;
getMap()->setCursorType(CT_NONE);
_parentState->getGame()->getCursor()->setVisible(false);
_states.push_back(new ProjectileFlyBState(this, _currentAction));
statePushFront(new UnitTurnBState(this, _currentAction)); // first of all turn towards the target
}
}
else
{
BattleUnit *unit = _save->selectUnit(pos);
if (unit && unit != _save->getSelectedUnit())
{
// -= select unit =-
if (unit->getFaction() == _save->getSide())
{
_save->setSelectedUnit(unit);
_parentState->updateSoldierInfo();
cancelCurrentAction();
setupCursor();
_currentAction.actor = unit;
}
}
else if (playableUnitSelected())
{
if (_currentAction.target != pos && bPreviewed)
_save->getPathfinding()->removePreview();
_currentAction.target = pos;