Repository navigation
Expand file tree
/
Copy pathSavedBattleGame.cpp
More file actions
1123 lines (1002 loc) · 25.5 KB
/
Copy pathSavedBattleGame.cpp
File metadata and controls
1123 lines (1002 loc) · 25.5 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
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/>.
*/
#include "SavedBattleGame.h"
#include "SavedGame.h"
#include "Tile.h"
#include "Node.h"
#include <SDL.h>
#include "../Ruleset/MapDataSet.h"
#include "../Battlescape/Pathfinding.h"
#include "../Battlescape/TileEngine.h"
#include "../Battlescape/Position.h"
#include "../Resource/ResourcePack.h"
#include "../Ruleset/Ruleset.h"
#include "../Ruleset/Armor.h"
#include "../Engine/Language.h"
#include "../Ruleset/RuleInventory.h"
#include "../Battlescape/PatrolBAIState.h"
#include "../Battlescape/AggroBAIState.h"
#include "../Engine/RNG.h"
#include "../Savegame/NodeLink.h"
namespace OpenXcom
{
/**
* Initializes a brand new battlescape saved game.
*/
SavedBattleGame::SavedBattleGame() : _width(0), _length(0), _height(0), _tiles(), _selectedUnit(0), _nodes(), _units(), _items(), _pathfinding(0), _tileEngine(0), _missionType(""), _globalShade(0), _side(FACTION_PLAYER), _turn(1), _debugMode(false), _aborted(false), _itemId(0)
{
}
/**
* Deletes the game content from memory.
*/
SavedBattleGame::~SavedBattleGame()
{
for (int i = 0; i < _height * _length * _width; ++i)
{
delete _tiles[i];
}
delete[] _tiles;
for (std::vector<Node*>::iterator i = _nodes.begin(); i != _nodes.end(); ++i)
{
delete *i;
}
for (std::vector<BattleUnit*>::iterator i = _units.begin(); i != _units.end(); ++i)
{
delete *i;
}
for (std::vector<BattleItem*>::iterator i = _items.begin(); i != _items.end(); ++i)
{
delete *i;
}
delete _pathfinding;
delete _tileEngine;
}
/**
* Loads the saved battle game from a YAML file.
* @param node YAML node.
*/
void SavedBattleGame::load(const YAML::Node &node, Ruleset *rule, SavedGame* savedGame)
{
int a,b;
int selectedUnit = 0;
node["width"] >> _width;
node["length"] >> _length;
node["height"] >> _height;
node["missionType"] >> _missionType;
node["globalshade"] >> _globalShade;
node["turn"] >> _turn;
node["selectedUnit"] >> selectedUnit;
for (YAML::Iterator i = node["mapdatasets"].begin(); i != node["mapdatasets"].end(); ++i)
{
std::string name;
*i >> name;
MapDataSet *mds = new MapDataSet(name);
_mapDataSets.push_back(mds);
}
initMap(_width, _length, _height);
for (YAML::Iterator i = node["tiles"].begin(); i != node["tiles"].end(); ++i)
{
Position pos;
(*i)["position"][0] >> pos.x;
(*i)["position"][1] >> pos.y;
(*i)["position"][2] >> pos.z;
getTile(pos)->load((*i));
}
for (YAML::Iterator i = node["nodes"].begin(); i != node["nodes"].end(); ++i)
{
Node *n = new Node();
n->load(*i);
_nodes.push_back(n);
}
for (YAML::Iterator i = node["units"].begin(); i != node["units"].end(); ++i)
{
UnitFaction faction;
(*i)["faction"] >> a;
faction = (UnitFaction)a;
(*i)["soldierId"] >> a;
BattleUnit *b;
if (a < BattleUnit::MAX_SOLDIER_ID) // Unit is linked to a geoscape soldier
{
// look up the matching soldier
b = new BattleUnit(savedGame->getSoldier(a), faction);
}
else
{
std::string type, armor;
(*i)["genUnitType"] >> type;
(*i)["genUnitArmor"] >> armor;
// create a new Unit.
b = new BattleUnit(rule->getUnit(type), faction, a, rule->getArmor(armor));
}
b->load(*i);
_units.push_back(b);
if (faction == FACTION_PLAYER)
{
if (b->getId() == selectedUnit)
_selectedUnit = b;
}
else if (b->getStatus() != STATUS_DEAD)
{
std::string state;
BattleAIState *aiState;
(*i)["AI"]["state"] >> state;
if (state == "PATROL")
{
aiState = new PatrolBAIState(this, b, 0);
}
else if (state == "AGGRO")
{
aiState = new AggroBAIState(this, b);
}
else
{
continue;
}
aiState->load((*i)["AI"]);
b->setAIState(aiState);
}
}
// matches up tiles and units
resetUnitTiles();
for (YAML::Iterator i = node["items"].begin(); i != node["items"].end(); ++i)
{
std::string type;
(*i)["type"] >> type;
if (type != "0")
{
BattleItem *item = new BattleItem(rule->getItem(type), &_itemId);
item->load(*i);
(*i)["inventoryslot"] >> type;
if (type != "NULL")
item->setSlot(rule->getInventory(type));
(*i)["owner"] >> a;
(*i)["unit"] >> b;
// match up items and units
for (std::vector<BattleUnit*>::iterator bu = _units.begin(); bu != _units.end(); ++bu)
{
if ((*bu)->getId() == a)
{
item->moveToOwner(*bu);
}
if ((*bu)->getId() == b)
{
item->setUnit(*bu);
}
}
// match up items and tiles
if (item->getSlot() && item->getSlot()->getType() == INV_GROUND)
{
Position pos;
(*i)["position"][0] >> pos.x;
(*i)["position"][1] >> pos.y;
(*i)["position"][2] >> pos.z;
if (pos.x != -1)
getTile(pos)->addItem(item);
}
_items.push_back(item);
}
}
// tie ammo items to their weapons, running through the items again
std::vector<BattleItem*>::iterator weaponi = _items.begin();
for (YAML::Iterator i = node["items"].begin(); i != node["items"].end(); ++i, ++weaponi)
{
(*i)["ammoItem"] >> a;
if (a != -1)
{
for (std::vector<BattleItem*>::iterator ammoi = _items.begin(); ammoi != _items.end(); ++ammoi)
{
if ((*ammoi)->getId() == a)
{
(*weaponi)->setAmmoItem((*ammoi));
break;
}
}
}
}
}
/**
* Loads the resources required by the map in the battle save.
* @param res Pointer to resource pack.
*/
void SavedBattleGame::loadMapResources(ResourcePack *res)
{
for (std::vector<MapDataSet*>::const_iterator i = _mapDataSets.begin(); i != _mapDataSets.end(); ++i)
{
(*i)->loadData();
}
int mdsID, mdID;
for (int i = 0; i < _height * _length * _width; ++i)
{
for (int part = 0; part < 4; part++)
{
_tiles[i]->getMapData(&mdID, &mdsID, part);
if (mdID != -1 && mdsID != -1)
{
_tiles[i]->setMapData(_mapDataSets[mdsID]->getObjects()->at(mdID), mdID, mdsID, part);
}
}
}
initUtilities(res);
getTileEngine()->calculateSunShading();
getTileEngine()->calculateTerrainLighting();
getTileEngine()->calculateUnitLighting();
for (std::vector<BattleUnit*>::iterator bu = _units.begin(); bu != _units.end(); ++bu)
{
_tileEngine->calculateFOV(*bu);
}
}
/**
* Saves the saved battle game to a YAML file.
* @param out YAML emitter.
*/
void SavedBattleGame::save(YAML::Emitter &out) const
{
out << YAML::BeginMap;
out << YAML::Key << "width" << YAML::Value << _width;
out << YAML::Key << "length" << YAML::Value << _length;
out << YAML::Key << "height" << YAML::Value << _height;
out << YAML::Key << "missionType" << YAML::Value << _missionType;
out << YAML::Key << "globalshade" << YAML::Value << _globalShade;
out << YAML::Key << "turn" << YAML::Value << _turn;
out << YAML::Key << "selectedUnit" << YAML::Value << (_selectedUnit?_selectedUnit->getId():-1);
out << YAML::Key << "mapdatasets" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<MapDataSet*>::const_iterator i = _mapDataSets.begin(); i != _mapDataSets.end(); ++i)
{
out << (*i)->getName();
}
out << YAML::EndSeq;
out << YAML::Key << "tiles" << YAML::Value;
out << YAML::BeginSeq;
for (int i = 0; i < _height * _length * _width; ++i)
{
if (!_tiles[i]->isVoid())
{
_tiles[i]->save(out);
}
}
out << YAML::EndSeq;
out << YAML::Key << "nodes" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<Node*>::const_iterator i = _nodes.begin(); i != _nodes.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "units" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<BattleUnit*>::const_iterator i = _units.begin(); i != _units.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "items" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<BattleItem*>::const_iterator i = _items.begin(); i != _items.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::EndMap;
}
/**
* Gets a pointer to the array of tiles.
* @return A pointer to Tile array.
*/
Tile **SavedBattleGame::getTiles() const
{
return _tiles;
}
/**
* Initializes the array of tiles + creates a pathfinding object.
* @param width
* @param length
* @param height
*/
void SavedBattleGame::initMap(int width, int length, int height)
{
_width = width;
_length = length;
_height = height;
_tiles = new Tile*[_height * _length * _width];
/* create tile objects */
for (int i = 0; i < _height * _length * _width; ++i)
{
Position pos;
getTileCoords(i, &pos.x, &pos.y, &pos.z);
_tiles[i] = new Tile(pos);
}
}
/**
* Initializes the map utilities.
* @param res Pointer to resource pack.
*/
void SavedBattleGame::initUtilities(ResourcePack *res)
{
_pathfinding = new Pathfinding(this);
_tileEngine = new TileEngine(this, res->getVoxelData());
}
/**
* Sets the mission type.
* @param missionType
*/
void SavedBattleGame::setMissionType(const std::string &missionType)
{
_missionType = missionType;
}
/**
* Gets the mission type.
* @return missionType
*/
std::string SavedBattleGame::getMissionType() const
{
return _missionType;
}
/**
* Sets the global shade.
* @param shade
*/
void SavedBattleGame::setGlobalShade(int shade)
{
_globalShade = shade;
}
/**
* Gets the global shade.
* @return int
*/
int SavedBattleGame::getGlobalShade() const
{
return _globalShade;
}
/**
* Gets the map width.
* @return Width in tiles.
*/
int SavedBattleGame::getWidth() const
{
return _width;
}
/**
* Gets the map length.
* @return Length in tiles.
*/
int SavedBattleGame::getLength() const
{
return _length;
}
/**
* Gets the map height.
* @return Height in layers.
*/
int SavedBattleGame::getHeight() const
{
return _height;
}
/**
* This method converts coordinates into a unique index.
* @param pos position
* @return Unique index.
*/
int SavedBattleGame::getTileIndex(const Position& pos) const
{
return pos.z * _length * _width + pos.y * _width + pos.x;
}
/**
* This method converts an index to coords.
* @param index tileindex
* @param x pointer to X coordinate.
* @param y pointer to Y coordinate.
* @param z pointer to Z coordinate.
* @return Unique index.
*/
void SavedBattleGame::getTileCoords(int index, int *x, int *y, int *z) const
{
*z = index / (_length * _width);
*y = (index % (_length * _width)) / _width;
*x = (index % (_length * _width)) % _width;
}
/**
* Gets the Tile on a given position on the map.
* @param pos position
* @return Pointer to tile.
*/
Tile *SavedBattleGame::getTile(const Position& pos) const
{
if (pos.x < 0 || pos.y < 0 || pos.z < 0
|| pos.x >= _width || pos.y >= _length || pos.z >= _height)
return 0;
return _tiles[getTileIndex(pos)];
}
/**
* Gets the currently selected unit
* @return pointer to BattleUnit.
*/
BattleUnit *SavedBattleGame::getSelectedUnit() const
{
return _selectedUnit;
}
/**
* Sets the currently selected unit.
* @param unit pointer to BattleUnit.
*/
void SavedBattleGame::setSelectedUnit(BattleUnit *unit)
{
_selectedUnit = unit;
}
/**
* Select the previous player unit TODO move this to BattlescapeState ?
* @return pointer to BattleUnit.
*/
BattleUnit *SavedBattleGame::selectPreviousPlayerUnit()
{
std::vector<BattleUnit*>::iterator i = _units.begin();
bool bPrev = false;
int wraps = 0;
if (_selectedUnit == 0)
{
bPrev = true;
}
do
{
if (bPrev && (*i)->getFaction() == _side && !(*i)->isOut())
{
break;
}
if ((*i) == _selectedUnit)
{
bPrev = true;
}
if (i == _units.begin())
{
i = _units.end();
wraps++;
}
--i;
// back to where we started... no more units found
if (wraps == 3)
{
_selectedUnit = 0;
return _selectedUnit;
}
}
while (true);
_selectedUnit = (*i);
return _selectedUnit;
}
/**
* Select the next player unit.
* TODO move this to BattlescapeState ?
* @param checkReselect Don't reselect unit.
* @return pointer to BattleUnit.
*/
BattleUnit *SavedBattleGame::selectNextPlayerUnit(bool checkReselect)
{
std::vector<BattleUnit*>::iterator i = _units.begin();
bool bNext = false;
int wraps = 0;
if (_selectedUnit == 0)
{
bNext = true;
}
else
if (checkReselect)
{
_selectedUnit->dontReselect();
}
do
{
if (bNext && (*i)->getFaction() == _side && !(*i)->isOut() && ((checkReselect && (*i)->reselectAllowed())||!checkReselect))
{
break;
}
if ((*i) == _selectedUnit)
{
bNext = true;
}
++i;
if (i == _units.end())
{
i = _units.begin();
wraps++;
}
// back to where we started... no more units found
if (wraps == 2)
{
_selectedUnit = 0;
return _selectedUnit;
}
}
while (true);
_selectedUnit = (*i);
return _selectedUnit;
}
/**
* Select unit with position on map.
* @param pos Position
* @return pointer to BattleUnit - 0 when nothing found
*/
BattleUnit *SavedBattleGame::selectUnit(const Position& pos)
{
BattleUnit *bu = getTile(pos)->getUnit();
if (bu && bu->isOut())
{
return 0;
}
else
{
return bu;
}
}
/**
* Gets the list of nodes.
* @return pointer to the list of nodes
*/
std::vector<Node*> *const SavedBattleGame::getNodes()
{
return &_nodes;
}
/**
* Gets the list of units.
* @return pointer to the list of units
*/
std::vector<BattleUnit*> *const SavedBattleGame::getUnits()
{
return &_units;
}
/**
* Gets the list of items.
* @return pointer to the list of items
*/
std::vector<BattleItem*> *const SavedBattleGame::getItems()
{
return &_items;
}
/**
* Get the pathfinding object.
* @return pointer to the pathfinding object
*/
Pathfinding *const SavedBattleGame::getPathfinding() const
{
return _pathfinding;
}
/**
* Get the terrain modifier object.
* @return pointer to the terrain modifier object
*/
TileEngine *const SavedBattleGame::getTileEngine() const
{
return _tileEngine;
}
/**
* gets a pointer to the array of mapblock
* @return pointer to the array of mapblocks
*/
std::vector<MapDataSet*> *const SavedBattleGame::getMapDataSets()
{
return &_mapDataSets;
}
/**
* Gets the side currently playing.
* @return Unit faction.
*/
UnitFaction SavedBattleGame::getSide() const
{
return _side;
}
/**
* Gets the current turn number.
* @return Turn.
*/
int SavedBattleGame::getTurn() const
{
return _turn;
}
/**
* Ends the current turn and progresses to the next one.
*/
void SavedBattleGame::endTurn()
{
if (_side == FACTION_PLAYER)
{
_lastSelectedUnit = _selectedUnit;
_side = FACTION_HOSTILE;
}
else if (_side == FACTION_HOSTILE)
{
_side = FACTION_NEUTRAL;
// if there is no neutral team, we skip this and instantly prepare the new turn for the player
if (selectNextPlayerUnit() == 0)
{
prepareNewTurn();
_turn++;
_side = FACTION_PLAYER;
if (_lastSelectedUnit && !_lastSelectedUnit->isOut())
_selectedUnit = _lastSelectedUnit;
else
selectNextPlayerUnit();
}
}
else if (_side == FACTION_NEUTRAL)
{
prepareNewTurn();
_turn++;
_side = FACTION_PLAYER;
if (_lastSelectedUnit && !_lastSelectedUnit->isOut())
_selectedUnit = _lastSelectedUnit;
else
selectNextPlayerUnit();
}
// hide all aliens (VOF calculations below will turn them visible again)
for (std::vector<BattleUnit*>::iterator i = _units.begin(); i != _units.end(); ++i)
{
if ((*i)->getFaction() != FACTION_PLAYER)
{
(*i)->setVisible(false);
}
}
for (std::vector<BattleUnit*>::iterator i = _units.begin(); i != _units.end(); ++i)
{
if ((*i)->getFaction() == _side)
{
(*i)->prepareNewTurn();
}
_tileEngine->calculateFOV(*i);
}
if (_side != FACTION_PLAYER)
selectNextPlayerUnit();
}
/**
* Turns on debug mode.
*/
void SavedBattleGame::setDebugMode()
{
for (int i = 0; i < _height * _length * _width; ++i)
{
_tiles[i]->setDiscovered(true, 2);
}
_debugMode = true;
}
/**
* Gets the current debug mode.
* @return Debug mode.
*/
bool SavedBattleGame::getDebugMode() const
{
return _debugMode;
}
/**
* Resets all the units to their current standing tile(s).
*/
void SavedBattleGame::resetUnitTiles()
{
for (std::vector<BattleUnit*>::iterator i = _units.begin(); i != _units.end(); ++i)
{
if (!(*i)->isOut())
{
if ((*i)->getTile() && (*i)->getTile()->getUnit() == (*i))
{
(*i)->getTile()->setUnit(0);
}
int size = (*i)->getArmor()->getSize() - 1;
for (int x = size; x >= 0; x--)
{
for (int y = size; y >= 0; y--)
{
Tile *t = getTile((*i)->getPosition() + Position(x,y,0));
t->setUnit((*i));
}
}
}
if ((*i)->getFaction() == FACTION_PLAYER)
{
(*i)->setVisible(true);
}
}
}
/**
* Removes an item from the game. Eg. when ammo item is depleted.
* @param item The Item to remove.
*/
void SavedBattleGame::removeItem(BattleItem *item)
{
for (std::vector<BattleItem*>::iterator i = _items.begin(); i != _items.end(); ++i)
{
if (*i == item)
{
_items.erase(i);
break;
}
}
// due to strange design, the item has to be removed from the tile it is on too (if it is on a tile)
for (int i = 0; i < getWidth() * getLength() * getHeight(); ++i)
{
for (std::vector<BattleItem*>::iterator it = _tiles[i]->getInventory()->begin(); it != _tiles[i]->getInventory()->end(); )
{
if ((*it) == item)
{
it = _tiles[i]->getInventory()->erase(it);
return;
}
++it;
}
}
}
/**
* Is the mission aborted or successful.
* @param bool.
*/
void SavedBattleGame::setAborted(bool flag)
{
_aborted = flag;
}
/**
* Is the mission aborted or successful.
* @return bool.
*/
bool SavedBattleGame::isAborted()
{
return _aborted;
}
/**
* Gets the current item ID.
* @return Current item ID pointer.
*/
int *SavedBattleGame::getCurrentItemId()
{
return &_itemId;
}
/**
* Finds a fitting node where a unit can spawn.
* @param nodeRank Rank of the node (is not rank of the alien!).
* @param unit Pointer to the unit (to get its position)
* @return pointer to the chosen node.
*/
Node *SavedBattleGame::getSpawnNode(int nodeRank, BattleUnit *unit)
{
int highestPriority = -1;
std::vector<Node*> compliantNodes;
for (std::vector<Node*>::iterator i = getNodes()->begin(); i != getNodes()->end(); ++i)
{
if ((*i)->getRank() == nodeRank // ranks must match
&& (!((*i)->getType() & Node::TYPE_SMALL)
|| unit->getArmor()->getSize() == 1) // the small unit bit is not set or the unit is small
&& (!((*i)->getType() & Node::TYPE_FLYING)
|| unit->getArmor()->getMovementType() == MT_FLY)// the flying unit bit is not set or the unit can fly
&& (*i)->getPriority() > 0 // priority 0 is no spawnplace
&& setUnitPosition(unit, (*i)->getPosition(), true)) // check if not already occupied
{
if ((*i)->getPriority() > highestPriority)
{
highestPriority = (*i)->getPriority();
compliantNodes.clear(); // drop the last nodes, as we found a higher priority now
}
if ((*i)->getPriority() == highestPriority)
{
compliantNodes.push_back((*i));
}
}
}
if (compliantNodes.empty()) return 0;
int n = RNG::generate(0, compliantNodes.size() - 1);
return compliantNodes[n];
}
/**
* Finds a fitting node where a unit can patrol to.
* @param nodeRank Rank of the node (is not rank of the alien!).
* @param unit Pointer to the unit (to get its position)
* @return pointer to the choosen node.
*/
Node *SavedBattleGame::getPatrolNode(bool scout, BattleUnit *unit, Node *fromNode)
{
std::vector<Node*> compliantNodes;
for (int i = 0; i < 4; i ++)
{
if (fromNode->getNodeLink(i)->getConnectedNodeID() > -1)
{
Node *n = getNodes()->at(fromNode->getNodeLink(i)->getConnectedNodeID());
if ((n->getRank() > 0 || scout) // for no scouts we find a node with a rank above 0
&& (!(n->getType() & Node::TYPE_SMALL)
|| unit->getArmor()->getSize() == 1) // the small unit bit is not set or the unit is small
&& (!(n->getType() & Node::TYPE_FLYING)
|| unit->getArmor()->getMovementType() == MT_FLY)// the flying unit bit is not set or the unit can fly
&& !n->isAllocated() // check if not allocated
&& setUnitPosition(unit, n->getPosition(), true)) // check if not already occupied
{
compliantNodes.push_back(n);
}
}
}
if (compliantNodes.empty()) return 0;
return compliantNodes[RNG::generate(0, compliantNodes.size() - 1)];
}
/**
* New turn preparations. Like fire and smoke spreading.
*/
void SavedBattleGame::prepareNewTurn()
{
std::vector<Tile*> tilesOnFire;
std::vector<Tile*> tilesOnSmoke;
// prepare a list of tiles on fire/smoke
for (int i = 0; i < getWidth() * getLength() * getHeight(); ++i)
{
if (getTiles()[i]->getFire() > 0)
{
tilesOnFire.push_back(getTiles()[i]);
}
if (getTiles()[i]->getSmoke() > 0)
{
tilesOnSmoke.push_back(getTiles()[i]);
}
}
// smoke spreads in 1 random direction, but the direction is same for all smoke
int spreadX = RNG::generate(-1, +1);
int spreadY = RNG::generate(-1, +1);
for (std::vector<Tile*>::iterator i = tilesOnSmoke.begin(); i != tilesOnSmoke.end(); ++i)
{
int x = (*i)->getPosition().x;
int y = (*i)->getPosition().y;
int z = (*i)->getPosition().z;
if ((*i)->getUnit() && !(*i)->getUnit()->isOut())
{
// units in smoke suffer stun
(*i)->getUnit()->damage(Position(), ((*i)->getSmoke()/5)+1, DT_STUN, true);
}
Tile *t = getTile(Position(x+spreadX, y+spreadY, z));
if (t && !t->getSmoke() && getTileEngine()->horizontalBlockage((*i), t, DT_SMOKE) == 0)
{
t->addSmoke((*i)->getSmoke()/2);
}
Tile *t2 = getTile(Position(x+spreadX+spreadX, y+spreadY+spreadY, z));
if (t && t2 && !t2->getSmoke() && getTileEngine()->horizontalBlockage(t, t2, DT_SMOKE) == 0)
{
t2->addSmoke((*i)->getSmoke()/4);
}
// smoke also spreads upwards
t = getTile(Position(x, y, z+1));
if (t && !t->getSmoke() && getTileEngine()->verticalBlockage((*i), t, DT_SMOKE) == 0)
{
t->addSmoke((*i)->getSmoke()/2);
}
(*i)->prepareNewTurn();
}
for (std::vector<Tile*>::iterator i = tilesOnFire.begin(); i != tilesOnFire.end(); ++i)
{
if ((*i)->getUnit())
{
// units on a flaming tile suffer damage
(*i)->getUnit()->damage(Position(0,0,0), RNG::generate(1,12), DT_IN, true);
// units on a flaming tile can catch fire 33% chance
if (RNG::generate(0,2) == 1)
{
(*i)->getUnit()->setFire(RNG::generate(1,5));
}
}
int z = (*i)->getPosition().z;
for (int x = (*i)->getPosition().x-1; x <= (*i)->getPosition().x+1; ++x)