forked from OpenXcom/OpenXcom
/
Ufo.cpp
1377 lines (1250 loc) · 30 KB
/
Ufo.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-2016 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 "Ufo.h"
#include <assert.h>
#include <algorithm>
#include "../fmath.h"
#include "Craft.h"
#include "AlienMission.h"
#include "../Engine/Exception.h"
#include "../Engine/Language.h"
#include "../Engine/RNG.h"
#include "../Engine/ScriptBind.h"
#include "../Mod/Mod.h"
#include "../Mod/RuleUfo.h"
#include "../Mod/UfoTrajectory.h"
#include "../Mod/RuleAlienMission.h"
#include "Base.h"
#include "SavedGame.h"
#include "Waypoint.h"
namespace OpenXcom
{
const char *Ufo::ALTITUDE_STRING[] = {
"STR_GROUND",
"STR_VERY_LOW",
"STR_LOW_UC",
"STR_HIGH_UC",
"STR_VERY_HIGH"
};
/**
* Initializes a UFO of the specified type.
* @param rules Pointer to ruleset.
* @param uniqueId unique ID to assign to the UFO (0 to not assign).
*/
Ufo::Ufo(const RuleUfo *rules, int uniqueId, int hunterKillerPercentage, int huntMode, int huntBehavior) : MovingTarget(),
_rules(rules), _missionWaveNumber(-1), _crashId(0), _landId(0), _damage(0), _direction("STR_NORTH"),
_altitude("STR_HIGH_UC"), _status(FLYING), _secondsRemaining(0),
_inBattlescape(false), _mission(0), _trajectory(0),
_trajectoryPoint(0), _detected(false), _hyperDetected(false), _processedIntercept(false),
_shootingAt(0), _hitFrame(0), _fireCountdown(0), _escapeCountdown(0), _stats(), _shield(-1), _shieldRechargeHandle(0),
_tractorBeamSlowdown(0), _isHunterKiller(false), _isEscort(false), _huntMode(0), _huntBehavior(0), _isHunting(false), _isEscorting(false), _origWaypoint(0)
{
_stats = rules->getStats();
if (uniqueId != 0)
{
_uniqueId = uniqueId;
}
if (hunterKillerPercentage > 0)
{
_isHunterKiller = RNG::percent(hunterKillerPercentage);
if (_isHunterKiller)
{
_huntMode = huntMode > 1 ? RNG::generate(0, 1) : huntMode;
_huntBehavior = huntBehavior > 1 ? RNG::generate(0, 1) : huntBehavior;
}
}
}
/**
* Make sure our mission forget's us, and we only delete targets we own (waypoints).
*
*/
Ufo::~Ufo()
{
if (_mission)
{
_mission->decreaseLiveUfos();
}
resetOriginalDestination(false);
if (_dest)
{
Waypoint *wp = dynamic_cast<Waypoint*>(_dest);
if (wp != 0)
{
delete _dest;
_dest = 0;
}
}
}
/**
* Match AlienMission based on the unique ID.
*/
class matchMissionID
{
typedef const AlienMission* argument_type;
typedef bool result_type;
public:
/// Store ID for later comparisons.
matchMissionID(int id) : _id(id) { /* Empty by design. */ }
/// Match with stored ID.
bool operator()(const AlienMission *am) const { return am->getId() == _id; }
private:
int _id;
};
/**
* Loads the UFO from a YAML file.
* @param node YAML node.
* @param mod The game mod. Use to access the trajectory rules.
* @param game The game data. Used to find the UFO's mission.
*/
void Ufo::load(const YAML::Node &node, const Mod &mod, SavedGame &game)
{
MovingTarget::load(node);
_uniqueId = node["uniqueId"].as<int>(_uniqueId);
_missionWaveNumber = node["missionWaveNumber"].as<int>(_missionWaveNumber);
_crashId = node["crashId"].as<int>(_crashId);
_landId = node["landId"].as<int>(_landId);
_damage = node["damage"].as<int>(_damage);
_shield = node["shield"].as<int>(_shield);
_shieldRechargeHandle = node["shieldRechargeHandle"].as<int>(_shieldRechargeHandle);
_altitude = node["altitude"].as<std::string>(_altitude);
_direction = node["direction"].as<std::string>(_direction);
_detected = node["detected"].as<bool>(_detected);
_hyperDetected = node["hyperDetected"].as<bool>(_hyperDetected);
_secondsRemaining = node["secondsRemaining"].as<size_t>(_secondsRemaining);
_inBattlescape = node["inBattlescape"].as<bool>(_inBattlescape);
double lon = _lon;
double lat = _lat;
if (const YAML::Node &dest = node["dest"])
{
lon = dest["lon"].as<double>();
lat = dest["lat"].as<double>();
}
_dest = new Waypoint();
_dest->setLongitude(lon);
_dest->setLatitude(lat);
if (const YAML::Node &status = node["status"])
{
_status = (UfoStatus)status.as<int>();
}
else
{
_huntBehavior = node["huntBehavior"].as<int>(_huntBehavior);
if (isDestroyed())
{
_status = DESTROYED;
}
else if (isCrashed())
{
_status = CRASHED;
}
else if (_altitude == "STR_GROUND")
{
_status = LANDED;
}
else
{
_status = FLYING;
}
}
if (game.getMonthsPassed() != -1)
{
int missionID = node["mission"].as<int>();
std::vector<AlienMission *>::const_iterator found = std::find_if (game.getAlienMissions().begin(), game.getAlienMissions().end(), matchMissionID(missionID));
if (found == game.getAlienMissions().end())
{
// Corrupt save file.
throw Exception("Unknown UFO mission, save file is corrupt.");
}
_mission = *found;
_stats += _rules->getRaceBonus(_mission->getRace());
std::string tid = node["trajectory"].as<std::string>();
_trajectory = mod.getUfoTrajectory(tid);
if (_trajectory == 0)
{
// Corrupt save file.
throw Exception("Unknown UFO trajectory, save file is corrupt.");
}
_trajectoryPoint = node["trajectoryPoint"].as<size_t>(_trajectoryPoint);
}
_fireCountdown = node["fireCountdown"].as<int>(_fireCountdown);
_escapeCountdown = node["escapeCountdown"].as<int>(_escapeCountdown);
if (_inBattlescape)
setSpeed(0);
}
/**
* Finishes loading the UFO from YAML (called after XCOM craft and other UFOs are loaded).
* @param node YAML node.
* @param save The game data. Used to find the UFO's target (= XCOM craft or another UFO).
*/
void Ufo::finishLoading(const YAML::Node &node, SavedGame &save)
{
_isHunterKiller = node["isHunterKiller"].as<bool>(_isHunterKiller);
_isEscort = node["isEscort"].as<bool>(_isEscort);
_huntMode = node["huntMode"].as<int>(_huntMode);
_huntBehavior = node["huntBehavior"].as<int>(_huntBehavior);
_isHunting = node["isHunting"].as<bool>(_isHunting);
_isEscorting = node["isEscorting"].as<bool>(_isEscorting);
if (_isHunting)
{
if (const YAML::Node &dest = node["dest"])
{
std::string type = dest["type"].as<std::string>();
int id = dest["id"].as<int>();
bool found = false;
for (std::vector<Base*>::iterator b = save.getBases()->begin(); b != save.getBases()->end(); ++b)
{
for (std::vector<Craft*>::iterator c = (*b)->getCrafts()->begin(); c != (*b)->getCrafts()->end(); ++c)
{
if ((*c)->getId() == id && (*c)->getRules()->getType() == type)
{
if (_dest)
{
// this is just a dummy waypoint created during normal loading, not a craft... yet
delete _dest;
_dest = 0;
}
setDestination((*c));
found = true;
break;
}
}
if (found) break;
}
}
}
else if (_isEscorting)
{
if (const YAML::Node &dest = node["dest"])
{
std::string type = dest["type"].as<std::string>();
if (type == "STR_UFO")
{
int uniqueUfoId = dest["uniqueId"].as<int>(0);
if (uniqueUfoId > 0)
{
for (std::vector<Ufo*>::iterator u = save.getUfos()->begin(); u != save.getUfos()->end(); ++u)
{
if ((*u)->getUniqueId() == uniqueUfoId)
{
if (_dest)
{
// this is just a dummy waypoint created during normal loading, not a UFO... yet
delete _dest;
_dest = 0;
}
setDestination((*u));
break;
}
}
}
}
}
}
//if (_isHunting || _isEscorting)
{
if (const YAML::Node &origWaypoint = node["origWaypoint"])
{
_origWaypoint = new Waypoint();
_origWaypoint->setLongitude(origWaypoint["lon"].as<double>());
_origWaypoint->setLatitude(origWaypoint["lat"].as<double>());
}
}
}
/**
* Saves the UFO to a YAML file.
* @return YAML node.
*/
YAML::Node Ufo::save(bool newBattle) const
{
YAML::Node node = MovingTarget::save();
node["type"] = _rules->getType();
node["uniqueId"] = _uniqueId;
node["missionWaveNumber"] = _missionWaveNumber;
if (_crashId)
{
node["crashId"] = _crashId;
}
else if (_landId)
{
node["landId"] = _landId;
}
node["damage"] = _damage;
node["shield"] = _shield;
node["shieldRechargeHandle"] = _shieldRechargeHandle;
node["altitude"] = _altitude;
node["direction"] = _direction;
node["status"] = (int)_status;
if (_detected)
node["detected"] = _detected;
if (_hyperDetected)
node["hyperDetected"] = _hyperDetected;
if (_secondsRemaining)
node["secondsRemaining"] = _secondsRemaining;
if (_inBattlescape)
node["inBattlescape"] = _inBattlescape;
if (_isHunterKiller)
node["isHunterKiller"] = _isHunterKiller;
if (_isEscort)
node["isEscort"] = _isEscort;
node["huntMode"] = _huntMode;
node["huntBehavior"] = _huntBehavior;
if (_isHunting)
node["isHunting"] = _isHunting;
if (_isEscorting)
node["isEscorting"] = _isEscorting;
if (_origWaypoint)
node["origWaypoint"] = _origWaypoint->save();
if (!newBattle)
{
node["mission"] = _mission->getId();
node["trajectory"] = _trajectory->getID();
node["trajectoryPoint"] = _trajectoryPoint;
}
node["fireCountdown"] = _fireCountdown;
node["escapeCountdown"] = _escapeCountdown;
return node;
}
/**
* Saves the UFO's unique identifiers to a YAML file.
* @return YAML node.
*/
YAML::Node Ufo::saveId() const
{
YAML::Node node = MovingTarget::saveId();
// this is needed, because _id is NOT unique until the UFO is detected
// and UFOs can be referenced by other entities even before they are detected
// (e.g. when they escort other UFOs)
node["uniqueId"] = _uniqueId;
return node;
}
/**
* Returns the UFO's unique type used for
* savegame purposes.
* @return ID.
*/
std::string Ufo::getType() const
{
return "STR_UFO";
}
/**
* Returns the ruleset for the UFO's type.
* @return Pointer to ruleset.
*/
const RuleUfo *Ufo::getRules() const
{
return _rules;
}
/**
* Changes the ruleset for the UFO's type.
* @param rules Pointer to ruleset.
* @warning ONLY FOR NEW BATTLE USE!
*/
void Ufo::changeRules(const RuleUfo *rules)
{
_rules = rules;
}
/**
* Returns the UFO's unique ID.
* @return Unique ID.
*/
int Ufo::getUniqueId() const
{
return _uniqueId;
}
/**
* Returns the UFO's unique default name.
* @param lang Language to get strings from.
* @return Full name.
*/
std::string Ufo::getDefaultName(Language *lang) const
{
switch (_status)
{
case LANDED:
return lang->getString(getMarkerName()).arg(_landId);
case CRASHED:
return lang->getString(getMarkerName()).arg(_crashId);
default:
return lang->getString(getMarkerName()).arg(_id);
}
}
/**
* Returns the name on the globe for the UFO.
* @return String ID.
*/
std::string Ufo::getMarkerName() const
{
switch (_status)
{
case LANDED:
return "STR_LANDING_SITE_";
case CRASHED:
return "STR_CRASH_SITE_";
default:
return "STR_UFO_";
}
}
/**
* Returns the marker ID on the globe for the UFO.
* @return Marker ID.
*/
int Ufo::getMarkerId() const
{
switch (_status)
{
case LANDED:
return _landId;
case CRASHED:
return _crashId;
default:
return _id;
}
}
/**
* Returns the globe marker for the UFO.
* @return Marker sprite, -1 if none.
*/
int Ufo::getMarker() const
{
if (!_detected)
return -1;
switch (_status)
{
case LANDED:
return _rules->getLandMarker() == -1 ? 3 : _rules->getLandMarker();
case CRASHED:
return _rules->getCrashMarker() == -1 ? 4 : _rules->getCrashMarker();
default:
return _rules->getMarker() == -1 ? 2 : _rules->getMarker();
}
}
/**
* Returns the amount of damage this UFO has taken.
* @return Amount of damage.
*/
int Ufo::getDamage() const
{
return _damage;
}
/**
* Changes the amount of damage this UFO has taken.
* @param damage Amount of damage.
*/
void Ufo::setDamage(int damage, const Mod *mod)
{
_damage = damage;
if (_damage < 0)
{
_damage = 0;
}
if (isDestroyed())
{
_status = DESTROYED;
}
else if (isCrashed())
{
_status = CRASHED;
}
if (_status == CRASHED || _status == DESTROYED)
{
int waveNumber = _missionWaveNumber;
// special case for retaliation UFO attacking the base
const RuleUfo *battleshipRule = mod->getUfo(_mission->getRules().getSpawnUfo(), true);
const UfoTrajectory *assaultTrajectory = mod->getUfoTrajectory(UfoTrajectory::RETALIATION_ASSAULT_RUN, true);
if (_rules == battleshipRule && _trajectory == assaultTrajectory)
{
waveNumber = _mission->getRules().getWaveCount() - 1; // last wave
}
// backwards save compatibility
if (waveNumber > -1)
{
const MissionWave &wave = _mission->getRules().getWave(waveNumber);
if (wave.interruptPercentage > 0)
{
if (RNG::percent(wave.interruptPercentage))
{
_mission->setInterrupted(true);
}
}
}
}
}
/**
* Returns whether this UFO has been detected by radars.
* @return Detection status.
*/
bool Ufo::getDetected() const
{
return _detected;
}
/**
* Changes whether this UFO has been detected by radars.
* @param detected Detection status.
*/
void Ufo::setDetected(bool detected)
{
_detected = detected;
}
/**
* Returns the amount of remaining seconds the UFO has left on the ground.
* After this many seconds the UFO will take off, if landed, or disappear, if
* crashed.
* @return Amount of seconds.
*/
size_t Ufo::getSecondsRemaining() const
{
return _secondsRemaining;
}
/**
* Changes the amount of remaining seconds the UFO has left on the ground.
* After this many seconds the UFO will take off, if landed, or disappear, if
* crashed.
* @param seconds Amount of seconds.
*/
void Ufo::setSecondsRemaining(size_t seconds)
{
_secondsRemaining = seconds;
}
/**
* Returns the current direction the UFO is heading in.
* @return Direction.
*/
std::string Ufo::getDirection() const
{
return _direction;
}
/**
* Returns the current altitude of the UFO.
* @return Altitude as string ID.
*/
std::string Ufo::getAltitude() const
{
return _altitude;
}
/**
* Returns the current altitude of the UFO.
* @return Altitude as integer (0-4).
*/
int Ufo::getAltitudeInt() const
{
for (size_t i = 0; i < 5; ++i)
{
if (ALTITUDE_STRING[i] == _altitude)
{
return i;
}
}
return -1;
}
/**
* Changes the current altitude of the UFO.
* @param altitude Altitude.
*/
void Ufo::setAltitude(const std::string &altitude)
{
_altitude = altitude;
if (_altitude != "STR_GROUND")
{
_status = FLYING;
}
else
{
_status = isCrashed() ? CRASHED : LANDED;
}
}
/**
* Returns if this UFO took enough damage
* to cause it to crash.
* @return Crashed status.
*/
bool Ufo::isCrashed() const
{
// Note: yes, this condition is necessary (in OXCE) and cannot be removed!
if (isDestroyed())
return true;
if (_huntBehavior == 1 || _rules->isUnmanned())
{
// kamikaze never crash lands; unmanned ditto
return false;
}
return (_damage > _stats.damageMax / 2);
}
/**
* Returns if this UFO took enough damage
* to cause it to crash.
* @return Crashed status.
*/
bool Ufo::isDestroyed() const
{
return (_damage >= _stats.damageMax);
}
/**
* Calculates the direction for the UFO based
* on the current raw speed and destination.
*/
void Ufo::calculateSpeed()
{
MovingTarget::calculateSpeed();
double x = _speedLon;
double y = -_speedLat;
// This section guards vs. divide-by-zero.
if (AreSame(x, 0.0) || AreSame(y, 0.0))
{
if (AreSame(x, 0.0) && AreSame(y, 0.0))
{
_direction = "STR_NONE_UC";
}
else if (AreSame(x, 0.0))
{
if (y > 0.f)
{
_direction = "STR_NORTH";
}
else if (y < 0.f)
{
_direction = "STR_SOUTH";
}
}
else if (AreSame(y, 0.0))
{
if (x > 0.f)
{
_direction = "STR_EAST";
}
else if (x < 0.f)
{
_direction = "STR_WEST";
}
}
return;
}
double theta = atan2(y, x); // radians
theta = theta * 180.f / M_PI; // +/- 180 deg.
if (22.5f > theta && theta > -22.5f)
{
_direction = "STR_EAST";
}
else if (-22.5f > theta && theta > -67.5f)
{
_direction = "STR_SOUTH_EAST";
}
else if (-67.5f > theta && theta > -112.5f)
{
_direction = "STR_SOUTH";
}
else if (-112.5f > theta && theta > -157.5f)
{
_direction = "STR_SOUTH_WEST";
}
else if (-157.5f > theta || theta > 157.5f)
{
_direction = "STR_WEST";
}
else if (157.5f > theta && theta > 112.5f)
{
_direction = "STR_NORTH_WEST";
}
else if (112.5f > theta && theta > 67.5f)
{
_direction = "STR_NORTH";
}
else
{
_direction = "STR_NORTH_EAST";
}
}
/**
* Moves the UFO to its destination.
*/
void Ufo::think()
{
switch (_status)
{
case FLYING:
move();
if (reachedDestination() && !isHunting() && !isEscorting())
{
// Prevent further movement.
setSpeed(0);
}
break;
case LANDED:
assert(_secondsRemaining >= 5 && "Wrong time management.");
_secondsRemaining -= 5;
break;
case CRASHED:
if (!_detected)
{
_detected = true;
}
// This gets handled in GeoscapeState::time30Minutes()
// Because the original game processes it every 30 minutes!
case DESTROYED:
// Do nothing
break;
}
}
/**
* Gets the UFO's battlescape status.
* @return Is the UFO currently in battle?
*/
bool Ufo::isInBattlescape() const
{
return _inBattlescape;
}
/**
* Sets the UFO's battlescape status.
* @param inbattle True if it's in battle, False otherwise.
*/
void Ufo::setInBattlescape(bool inbattle)
{
if (inbattle)
setSpeed(0);
_inBattlescape = inbattle;
}
/**
* Returns the alien race currently residing in the UFO.
* @return Alien race.
*/
const std::string &Ufo::getAlienRace() const
{
return _mission->getRace();
}
void Ufo::setShotDownByCraftId(const CraftId& craft)
{
_shotDownByCraftId = craft;
}
CraftId Ufo::getShotDownByCraftId() const
{
return _shotDownByCraftId;
}
/**
* Returns a UFO's visibility to radar detection.
* The UFO's size and altitude affect the chances
* of it being detected by radars.
* @return Visibility modifier.
*/
int Ufo::getVisibility() const
{
int size = 0;
// size = 15*(3-ufosize);
if (_rules->getSize() == "STR_VERY_SMALL")
size = -30;
else if (_rules->getSize() == "STR_SMALL")
size = -15;
else if (_rules->getSize() == "STR_MEDIUM_UC")
size = 0;
else if (_rules->getSize() == "STR_LARGE")
size = 15;
else if (_rules->getSize() == "STR_VERY_LARGE")
size = 30;
int visibility = 0;
if (_altitude == "STR_GROUND")
visibility = -30;
else if (_altitude == "STR_VERY_LOW")
visibility = size - 20;
else if (_altitude == "STR_LOW_UC")
visibility = size - 10;
else if (_altitude == "STR_HIGH_UC")
visibility = size;
else if (_altitude == "STR_VERY_HIGH")
visibility = size - 10;
return visibility;
}
/**
* Returns the Mission type of the UFO.
* @return Mission.
*/
const std::string &Ufo::getMissionType() const
{
return _mission->getRules().getType();
}
/**
* Sets the mission information of the UFO.
* The UFO will start at the first point of the trajectory. The actual UFO
* information is not changed here, this only sets the information kept on
* behalf of the mission.
* @param mission Pointer to the actual mission object.
* @param trajectory Pointer to the actual mission trajectory.
*/
void Ufo::setMissionInfo(AlienMission *mission, const UfoTrajectory *trajectory)
{
assert(!_mission && mission && trajectory);
_mission = mission;
_mission->increaseLiveUfos();
_trajectoryPoint = 0;
_trajectory = trajectory;
_stats += _rules->getRaceBonus(_mission->getRace());
}
/**
* Returns whether this UFO has been detected by hyper-wave.
* @return Detection status.
*/
bool Ufo::getHyperDetected() const
{
return _hyperDetected;
}
/**
* Changes whether this UFO has been detected by hyper-wave.
* @param hyperdetected Detection status.
*/
void Ufo::setHyperDetected(bool hyperdetected)
{
_hyperDetected = hyperdetected;
}
/**
* Gets the Xcom craft targeted by this UFO.
* @return Pointer to Xcom craft.
*/
Craft *Ufo::getTargetedXcomCraft() const
{
Craft *craft = dynamic_cast<Craft*>(_dest);
return craft;
}
/**
* Resets the original destination if targeting the given craft.
* @param dest Pointer to Xcom craft.
*/
void Ufo::resetOriginalDestination(Craft *target)
{
if (target == getTargetedXcomCraft())
{
resetOriginalDestination(true);
}
}
/**
* Resets the original destination.
*/
void Ufo::resetOriginalDestination(bool debugHelper)
{
if (_origWaypoint)
{
Waypoint *wp = new Waypoint();
wp->setLatitude(_origWaypoint->getLatitude());
wp->setLongitude(_origWaypoint->getLongitude());
setDestination(wp); // hunting and escorting flags will be reset too
delete _origWaypoint;
_origWaypoint = 0;
}
else if (debugHelper)
{
throw Exception("Corrupt state: Unknown original UFO destination.");
}
}
/**
* Sets the Xcom craft targeted by this UFO.
* @param dest Pointer to Xcom craft.
*/
void Ufo::setTargetedXcomCraft(Craft *craft)
{
if (craft)
{
backupOriginalDestination();
setDestination(craft);
_isHunting = true; // must be after setDestination()
// go hunt your target as quickly as you can
if (_rules->getHuntSpeed() > 0)
{
setSpeed(_stats.speedMax * _rules->getHuntSpeed() / 100);
}
}
}
/**
* Sets the UFO escorted by this UFO.
* @param ufo Pointer to escorted UFO.
*/
void Ufo::setEscortedUfo(Ufo *ufo)
{
if (ufo)
{
backupOriginalDestination();
setDestination(ufo);
_isEscorting = true; // must be after setDestination()
// go protect your target as quickly as you can
setSpeed(_stats.speedMax);
}
}
/**
* Backs up the original destination.
*/
void Ufo::backupOriginalDestination()
{
// if we're not hunting or escorting yet, remember the original destination waypoint
// if we're already occupied, keep the original information
if (!_isHunting && !_isEscorting)
{
if (_origWaypoint)
{
delete _origWaypoint;
_origWaypoint = 0;
}
_origWaypoint = new Waypoint();
_origWaypoint->setLatitude(_dest->getLatitude());
_origWaypoint->setLongitude(_dest->getLongitude());
}
}
/**
* Handle destination changes, making sure to delete old waypoint destinations.
* @param dest Pointer to the new destination.
*/
void Ufo::setDestination(Target *dest)
{
Waypoint *old = dynamic_cast<Waypoint*>(_dest);
MovingTarget::setDestination(dest);
if (old && !_isHunting && !_isEscorting)
{
// delete only waypoints, not xcom craft or other UFOs
delete old;
}
_isHunting = false; // reset flag, will be set in setTargetedXcomCraft()
_isEscorting = false; // reset flag, will be set in setEscortedUfo()
}
/**
* Gets which interception window the UFO is active in.
* @return which interception window the UFO is active in.
*/
int Ufo::getShootingAt() const
{
return _shootingAt;
}
/**