Repository navigation
Expand file tree
/
Copy pathSavedGame.cpp
More file actions
908 lines (838 loc) · 24 KB
/
Copy pathSavedGame.cpp
File metadata and controls
908 lines (838 loc) · 24 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
/*
* 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 "SavedGame.h"
#include <fstream>
#include <sstream>
#include <iomanip>
#include <yaml-cpp/yaml.h>
#include "../Ruleset/Ruleset.h"
#include "../Engine/RNG.h"
#include "../Engine/Language.h"
#include "../Interface/TextList.h"
#include "../Engine/Exception.h"
#include "../Engine/Options.h"
#include "../Engine/CrossPlatform.h"
#include "SavedBattleGame.h"
#include "GameTime.h"
#include "Country.h"
#include "Base.h"
#include "Craft.h"
#include "Region.h"
#include "Ufo.h"
#include "Waypoint.h"
#include "UfopaediaSaved.h"
#include "../Ruleset/RuleResearch.h"
#include "ResearchProject.h"
#include "ItemContainer.h"
#include "Soldier.h"
#include "../Ruleset/RuleManufacture.h"
#include "Production.h"
#include "TerrorSite.h"
namespace OpenXcom
{
struct findRuleResearch : public std::unary_function<ResearchProject *,
bool>
{
RuleResearch * _toFind;
findRuleResearch(RuleResearch * toFind);
bool operator()(const ResearchProject *r) const;
};
findRuleResearch::findRuleResearch(RuleResearch * toFind) : _toFind(toFind)
{
}
bool findRuleResearch::operator()(const ResearchProject *r) const
{
return _toFind == r->getRules();
}
struct equalProduction : public std::unary_function<Production *,
bool>
{
RuleManufacture * _item;
equalProduction(RuleManufacture * item);
bool operator()(const Production * p) const;
};
equalProduction::equalProduction(RuleManufacture * item) : _item(item)
{
}
bool equalProduction::operator()(const Production * p) const
{
return p->getRules() == _item;
}
/**
* Initializes a brand new saved game according to the specified difficulty.
*/
SavedGame::SavedGame() : _difficulty(DIFF_BEGINNER), _funds(0), _ids(), _countries(), _regions(), _bases(), _ufos(), _waypoints(), _terrorSites(), _battleGame(0), _discovered(), _debug(false)
{
RNG::init();
_time = new GameTime(6, 1, 1, 1999, 12, 0, 0);
_ufopaedia = new UfopaediaSaved();
}
/**
* Deletes the game content from memory.
*/
SavedGame::~SavedGame()
{
delete _time;
for (std::vector<Country*>::iterator i = _countries.begin(); i != _countries.end(); ++i)
{
delete *i;
}
for (std::vector<Region*>::iterator i = _regions.begin(); i != _regions.end(); ++i)
{
delete *i;
}
for (std::vector<Base*>::iterator i = _bases.begin(); i != _bases.end(); ++i)
{
delete *i;
}
for (std::vector<Ufo*>::iterator i = _ufos.begin(); i != _ufos.end(); ++i)
{
delete *i;
}
for (std::vector<Waypoint*>::iterator i = _waypoints.begin(); i != _waypoints.end(); ++i)
{
delete *i;
}
for (std::vector<TerrorSite*>::iterator i = _terrorSites.begin(); i != _terrorSites.end(); ++i)
{
delete *i;
}
delete _battleGame;
delete _ufopaedia;
}
/**
* Gets all the saves found in the user folder
* and adds them to a text list.
* @param list Text list.
* @param lang Loaded language.
*/
void SavedGame::getList(TextList *list, Language *lang)
{
std::vector<std::string> saves = CrossPlatform::getFolderContents(Options::getUserFolder(), "sav");
for (std::vector<std::string>::iterator i = saves.begin(); i != saves.end(); ++i)
{
std::string file = (*i);
std::string fullname = Options::getUserFolder() + file;
std::ifstream fin(fullname.c_str());
try
{
if (!fin)
{
throw Exception("Failed to load savegame");
}
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
GameTime time = GameTime(6, 1, 1, 1999, 12, 0, 0);
time.load(doc["time"]);
std::stringstream saveTime;
std::wstringstream saveDay, saveMonth, saveYear;
saveTime << time.getHour() << ":" << std::setfill('0') << std::setw(2) << time.getMinute();
saveDay << time.getDay() << lang->getString(time.getDayString());
saveMonth << lang->getString(time.getMonthString());
saveYear << time.getYear();
list->addRow(5, Language::utf8ToWstr(file.substr(0, file.length()-4)).c_str(), Language::utf8ToWstr(saveTime.str()).c_str(), saveDay.str().c_str(), saveMonth.str().c_str(), saveYear.str().c_str());
fin.close();
}
catch (Exception &e)
{
std::cerr << e.what() << std::endl;
continue;
}
catch (YAML::Exception &e)
{
std::cerr << e.what() << std::endl;
continue;
}
}
}
/**
* Loads a saved game's contents from a YAML file.
* @note Assumes the saved game is blank.
* @param filename YAML filename.
* @param rule Ruleset for the saved game.
*/
void SavedGame::load(const std::string &filename, Ruleset *rule)
{
std::string s = Options::getUserFolder() + filename + ".sav";
std::ifstream fin(s.c_str());
if (!fin)
{
throw Exception("Failed to load savegame");
}
YAML::Parser parser(fin);
YAML::Node doc;
// Get brief save info
parser.GetNextDocument(doc);
std::string v;
doc["version"] >> v;
if (v != Options::getVersion())
{
throw Exception("Version mismatch");
}
_time->load(doc["time"]);
// Get full save data
parser.GetNextDocument(doc);
int a = 0;
doc["difficulty"] >> a;
_difficulty = (GameDifficulty)a;
doc["funds"] >> _funds;
doc["ids"] >> _ids;
for (YAML::Iterator i = doc["countries"].begin(); i != doc["countries"].end(); ++i)
{
std::string type;
(*i)["type"] >> type;
Country *c = new Country(rule->getCountry(type), false);
c->load(*i);
_countries.push_back(c);
}
for (YAML::Iterator i = doc["regions"].begin(); i != doc["regions"].end(); ++i)
{
std::string type;
(*i)["type"] >> type;
Region *r = new Region(rule->getRegion(type));
r->load(*i);
_regions.push_back(r);
}
for (YAML::Iterator i = doc["ufos"].begin(); i != doc["ufos"].end(); ++i)
{
std::string type;
(*i)["type"] >> type;
Ufo *u = new Ufo(rule->getUfo(type));
u->load(*i);
_ufos.push_back(u);
}
for (YAML::Iterator i = doc["waypoints"].begin(); i != doc["waypoints"].end(); ++i)
{
Waypoint *w = new Waypoint();
w->load(*i);
_waypoints.push_back(w);
}
for (YAML::Iterator i = doc["terrorSites"].begin(); i != doc["terrorSites"].end(); ++i)
{
TerrorSite *t = new TerrorSite();
t->load(*i);
_terrorSites.push_back(t);
}
for (YAML::Iterator i = doc["bases"].begin(); i != doc["bases"].end(); ++i)
{
Base *b = new Base(rule);
b->load(*i, this);
_bases.push_back(b);
}
for(YAML::Iterator it=doc["discovered"].begin();it!=doc["discovered"].end();++it)
{
std::string research;
*it >> research;
_discovered.push_back(rule->getResearch(research));
}
if (const YAML::Node *pName = doc.FindValue("battleGame"))
{
_battleGame = new SavedBattleGame();
_battleGame->load(*pName, rule, this);
}
if (const YAML::Node *pName = doc.FindValue("ufopaedia"))
{
_ufopaedia->load(*pName, rule);
}
fin.close();
}
/**
* Saves a saved game's contents to a YAML file.
* @param filename YAML filename.
*/
void SavedGame::save(const std::string &filename) const
{
std::string s = Options::getUserFolder() + filename + ".sav";
std::ofstream sav(s.c_str());
if (!sav)
{
throw Exception("Failed to save savegame");
}
YAML::Emitter out;
// Saves the brief game info used in the saves list
out << YAML::BeginDoc;
out << YAML::BeginMap;
out << YAML::Key << "version" << YAML::Value << Options::getVersion();
out << YAML::Key << "time" << YAML::Value;
_time->save(out);
out << YAML::EndMap;
// Saves the full game data to the save
out << YAML::BeginDoc;
out << YAML::BeginMap;
out << YAML::Key << "difficulty" << YAML::Value << _difficulty;
out << YAML::Key << "funds" << YAML::Value << _funds;
out << YAML::Key << "ids" << YAML::Value << _ids;
out << YAML::Key << "countries" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<Country*>::const_iterator i = _countries.begin(); i != _countries.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "regions" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<Region*>::const_iterator i = _regions.begin(); i != _regions.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "bases" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<Base*>::const_iterator i = _bases.begin(); i != _bases.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "ufos" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<Ufo*>::const_iterator i = _ufos.begin(); i != _ufos.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "waypoints" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<Waypoint*>::const_iterator i = _waypoints.begin(); i != _waypoints.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "terrorSites" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<TerrorSite*>::const_iterator i = _terrorSites.begin(); i != _terrorSites.end(); ++i)
{
(*i)->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "discovered" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<const RuleResearch *>::const_iterator i = _discovered.begin(); i != _discovered.end(); ++i)
{
out << (*i)->getName ();
}
out << YAML::EndSeq;
if (_battleGame != 0)
{
out << YAML::Key << "battleGame" << YAML::Value;
_battleGame->save(out);
}
out << YAML::Key << "ufopaedia" << YAML::Value;
_ufopaedia->save(out);
out << YAML::EndMap;
sav << out.c_str();
sav.close();
}
/**
* Returns the game's difficulty level.
* @return Difficulty level.
*/
GameDifficulty SavedGame::getDifficulty() const
{
return _difficulty;
}
/**
* Changes the game's difficulty to a new level.
* @param difficulty New difficulty.
*/
void SavedGame::setDifficulty(GameDifficulty difficulty)
{
_difficulty = difficulty;
}
/**
* Returns the player's current funds.
* @return Current funds.
*/
int SavedGame::getFunds() const
{
return _funds;
}
/**
* Changes the player's funds to a new value.
* @param funds New funds.
*/
void SavedGame::setFunds(int funds)
{
_funds = funds;
}
/**
* Gives the player his monthly funds, taking in account
* all maintenance and profit costs.
*/
void SavedGame::monthlyFunding()
{
_funds += getCountryFunding() - getBaseMaintenance();
}
/**
* Returns the current time of the game.
* @return Pointer to the game time.
*/
GameTime *const SavedGame::getTime() const
{
return _time;
}
/**
* Returns the latest ID for the specified object
* and increases it.
* @param name Object name.
* @return Latest ID number.
*/
int SavedGame::getId(const std::string &name)
{
int id = _ids[name];
_ids[name]++;
return id;
}
/**
* Initializes the list of object IDs.
* @param ids ID number list.
*/
void SavedGame::initIds(const std::map<std::string, int> &ids)
{
_ids = ids;
}
/**
* Returns the list of countries in the game world.
* @return Pointer to country list.
*/
std::vector<Country*> *const SavedGame::getCountries()
{
return &_countries;
}
/**
* Adds up the monthly funding of all the countries.
* @return Total funding.
*/
int SavedGame::getCountryFunding() const
{
int total = 0;
for (std::vector<Country*>::const_iterator i = _countries.begin(); i != _countries.end(); ++i)
{
total += (*i)->getFunding();
}
return total;
}
/**
* Returns the list of world regions.
* @return Pointer to region list.
*/
std::vector<Region*> *const SavedGame::getRegions()
{
return &_regions;
}
/**
* Returns the list of player bases.
* @return Pointer to base list.
*/
std::vector<Base*> *const SavedGame::getBases()
{
return &_bases;
}
/**
* Adds up the monthly maintenance of all the bases.
* @return Total maintenance.
*/
int SavedGame::getBaseMaintenance() const
{
int total = 0;
for (std::vector<Base*>::const_iterator i = _bases.begin(); i != _bases.end(); ++i)
{
total += (*i)->getMonthlyMaintenace();
}
return total;
}
/**
* Returns the list of alien UFOs.
* @return Pointer to UFO list.
*/
std::vector<Ufo*> *const SavedGame::getUfos()
{
return &_ufos;
}
/**
* Returns the list of craft waypoints.
* @return Pointer to waypoint list.
*/
std::vector<Waypoint*> *const SavedGame::getWaypoints()
{
return &_waypoints;
}
/**
* Returns the list of terror sites.
* @return Pointer to terror site list.
*/
std::vector<TerrorSite*> *const SavedGame::getTerrorSites()
{
return &_terrorSites;
}
/**
* Get pointer to the battleGame object.
* @return Pointer to the battleGame object.
*/
SavedBattleGame *const SavedGame::getBattleGame()
{
return _battleGame;
}
/**
* Set battleGame object.
* @param battleGame Pointer to the battleGame object.
*/
void SavedGame::setBattleGame(SavedBattleGame *battleGame)
{
delete _battleGame;
_battleGame = battleGame;
}
/**
* Get pointer to the ufopaedia object.
* @return Pointer to the ufopaedia object.
*/
UfopaediaSaved *SavedGame::getUfopaedia()
{
return _ufopaedia;
}
/**
* Add a ResearchProject to the list of already discovered ResearchProject
* @param r The newly found ResearchProject
*/
void SavedGame::addFinishedResearch (const RuleResearch * r, Ruleset * ruleset)
{
_discovered.push_back(r);
if(ruleset)
{
std::vector<RuleResearch*> availableResearch;
for(std::vector<Base*>::const_iterator it = _bases.begin (); it != _bases.end (); ++it)
{
getDependableResearchBasic(availableResearch, r, ruleset, *it);
}
for(std::vector<RuleResearch*>::iterator it = availableResearch.begin (); it != availableResearch.end (); ++it)
{
if((*it)->getCost() == 0)
{
addFinishedResearch(*it, ruleset);
}
}
}
}
/**
Returns the list of already discovered ResearchProject
* @return the list of already discovered ResearchProject
*/
const std::vector<const RuleResearch *> & SavedGame::getDiscoveredResearch() const
{
return _discovered;
}
/**
Get the list of RuleResearch which can be researched in a Base.
* @param projects the list of ResearchProject which are available.
* @param ruleset the Game Ruleset
* @param base a pointer to a Base
*/
void SavedGame::getAvailableResearchProjects (std::vector<RuleResearch *> & projects, Ruleset * ruleset, Base * base) const
{
const std::vector<const RuleResearch *> & discovered(getDiscoveredResearch());
std::vector<std::string> researchProjects = ruleset->getResearchList();
const std::vector<ResearchProject *> & baseResearchProjects = base->getResearch();
std::vector<const RuleResearch *> unlocked;
for(std::vector<const RuleResearch *>::const_iterator it = discovered.begin (); it != discovered.end (); ++it)
{
for(std::vector<std::string>::const_iterator itUnlocked = (*it)->getUnlocked ().begin (); itUnlocked != (*it)->getUnlocked ().end (); ++itUnlocked)
{
unlocked.push_back(ruleset->getResearch(*itUnlocked));
}
}
for(std::vector<std::string>::const_iterator iter = researchProjects.begin (); iter != researchProjects.end (); ++iter)
{
RuleResearch *research = ruleset->getResearch(*iter);
if (!isResearchAvailable(research, unlocked, ruleset))
{
continue;
}
std::vector<const RuleResearch *>::const_iterator itDiscovered = std::find(discovered.begin (), discovered.end (), research);
if (itDiscovered != discovered.end ())
{
continue;
}
if (std::find_if (baseResearchProjects.begin(), baseResearchProjects.end (), findRuleResearch(research)) != baseResearchProjects.end ())
{
continue;
}
if (research->needItem() && base->getItems()->getItem(research->getName ()) == 0)
{
continue;
}
projects.push_back (research);
}
}
/**
Get the list of RuleManufacture which can be manufacture in a Base.
* @param productions the list of Productions which are available.
* @param ruleset the Game Ruleset
* @param base a pointer to a Base
*/
void SavedGame::getAvailableProductions (std::vector<RuleManufacture *> & productions, Ruleset * ruleset, Base * base) const
{
const std::vector<std::string> items (ruleset->getManufactureList ());
const std::vector<Production *> baseProductions (base->getProductions ());
for(std::vector<std::string>::const_iterator iter = items.begin ();
iter != items.end ();
++iter)
{
RuleManufacture *m = ruleset->getManufacture(*iter);
if(!isResearched(m->getRequirements()))
{
continue;
}
if(std::find_if(baseProductions.begin (), baseProductions.end (), equalProduction(m)) != baseProductions.end ())
{
continue;
}
productions.push_back(m);
}
}
/**
Check whether a ResearchProject can be researched.
* @param r the RuleResearch to test.
* @param unlocked the list of currently unlocked RuleResearch
* @return true if the RuleResearch can be researched
*/
bool SavedGame::isResearchAvailable (RuleResearch * r, const std::vector<const RuleResearch *> & unlocked, Ruleset * ruleset) const
{
std::vector<std::string> deps = r->getDependencies();
const std::vector<const RuleResearch *> & discovered(getDiscoveredResearch());
if(std::find(unlocked.begin (), unlocked.end (),
r) != unlocked.end ())
{
return true;
}
for(std::vector<std::string>::const_iterator iter = deps.begin (); iter != deps.end (); ++ iter)
{
RuleResearch *research = ruleset->getResearch(*iter);
std::vector<const RuleResearch *>::const_iterator itDiscovered = std::find(discovered.begin (), discovered.end (), research);
if (itDiscovered == discovered.end ())
{
return false;
}
++iter;
}
return true;
}
/**
Get the list of newly available research projects once a ResearchProject has been completed. This function check for fake ResearchProject.
* @param dependables the list of RuleResearch which are now available.
* @param research The RuleResearch which has just been discovered
* @param ruleset the Game Ruleset
* @param base a pointer to a Base
*/
void SavedGame::getDependableResearch (std::vector<RuleResearch *> & dependables, const RuleResearch *research, Ruleset * ruleset, Base * base) const
{
getDependableResearchBasic(dependables, research, ruleset, base);
for(std::vector<const RuleResearch *>::const_iterator iter = _discovered.begin (); iter != _discovered.end (); ++iter)
{
if((*iter)->getCost() == 0)
{
if (std::find((*iter)->getDependencies().begin (), (*iter)->getDependencies().end (), research->getName()) != (*iter)->getDependencies().end ())
{
getDependableResearchBasic(dependables, *iter, ruleset, base);
}
}
}
}
/**
Get the list of newly available research projects once a ResearchProject has been completed. This function doesn't check for fake ResearchProject.
* @param dependables the list of RuleResearch which are now available.
* @param research The RuleResearch which has just been discovered
* @param ruleset the Game Ruleset
* @param base a pointer to a Base
*/
void SavedGame::getDependableResearchBasic (std::vector<RuleResearch *> & dependables, const RuleResearch *research, Ruleset * ruleset, Base * base) const
{
std::vector<RuleResearch *> possibleProjects;
getAvailableResearchProjects(possibleProjects, ruleset, base);
for(std::vector<RuleResearch *>::iterator iter = possibleProjects.begin (); iter != possibleProjects.end (); ++iter)
{
if (std::find((*iter)->getDependencies().begin (), (*iter)->getDependencies().end (), research->getName()) != (*iter)->getDependencies().end ()
||
std::find((*iter)->getUnlocked().begin (), (*iter)->getUnlocked().end (), research->getName()) != (*iter)->getUnlocked().end ()
)
{
dependables.push_back(*iter);
if ((*iter)->getCost() == 0)
{
getDependableResearchBasic(dependables, *iter, ruleset, base);
}
else
{
}
}
}
}
/**
* Returns if a certain research has been completed.
* @param research Research ID.
* @return Whether it's researched or not.
*/
bool SavedGame::isResearched(const std::string &research) const
{
if (research.empty() || _debug)
return true;
for (std::vector<const RuleResearch *>::const_iterator i = _discovered.begin(); i != _discovered.end(); ++i)
{
if ((*i)->getName() == research)
return true;
}
return false;
}
/**
* Returns if a certain list of research has been completed.
* @param research List of research IDs.
* @return Whether it's researched or not.
*/
bool SavedGame::isResearched(const std::vector<std::string> &research) const
{
if (research.empty() || _debug)
return true;
std::vector<std::string> matches = research;
for (std::vector<const RuleResearch *>::const_iterator i = _discovered.begin(); i != _discovered.end(); ++i)
{
for (std::vector<std::string>::iterator j = matches.begin(); j != matches.end(); ++j)
{
if ((*i)->getName() == *j)
{
j = matches.erase(j);
break;
}
}
if (matches.empty())
return true;
}
return false;
}
/**
* Returns pointer to the Soldier given it's unique ID.
* @param id A soldier's unique id.
* @return Pointer to Soldier.
*/
Soldier *const SavedGame::getSoldier(int id) const
{
for (std::vector<Base*>::const_iterator i = _bases.begin(); i != _bases.end(); ++i)
{
for (std::vector<Soldier*>::const_iterator j = (*i)->getSoldiers()->begin(); j != (*i)->getSoldiers()->end(); ++j)
{
if ((*j)->getId() == id)
{
return (*j);
}
}
}
return 0;
}
/**
* Handles the higher promotions (not the rookie-squaddie ones).
* @return Whether or not some promotions happened - to show the promotions screen.
*/
bool SavedGame::handlePromotions()
{
size_t soldiersPromoted = 0, soldiersTotal = 0;
for (std::vector<Base*>::iterator i = _bases.begin(); i != _bases.end(); ++i)
{
soldiersTotal += (*i)->getSoldiers()->size();
}
Soldier *highestRanked = 0;
// now determine the number of positions we have of each rank,
// and the soldier with the heighest promotion score of the rank below it
size_t filledPositions = 0, filledPositions2 = 0;
inspectSoldiers(&highestRanked, &filledPositions, RANK_COMMANDER);
inspectSoldiers(&highestRanked, &filledPositions2, RANK_COLONEL);
if (filledPositions < 1 && filledPositions2 > 0)
{
// only promote one colonel to commander
highestRanked->promoteRank();
soldiersPromoted++;
}
inspectSoldiers(&highestRanked, &filledPositions, RANK_COLONEL);
inspectSoldiers(&highestRanked, &filledPositions2, RANK_CAPTAIN);
if (filledPositions < (soldiersTotal / 23) && filledPositions2 > 0)
{
highestRanked->promoteRank();
soldiersPromoted++;
}
inspectSoldiers(&highestRanked, &filledPositions, RANK_CAPTAIN);
inspectSoldiers(&highestRanked, &filledPositions2, RANK_SERGEANT);
if (filledPositions < (soldiersTotal / 11) && filledPositions2 > 0)
{
highestRanked->promoteRank();
soldiersPromoted++;
}
inspectSoldiers(&highestRanked, &filledPositions, RANK_SERGEANT);
inspectSoldiers(&highestRanked, &filledPositions2, RANK_SQUADDIE);
if (filledPositions < (soldiersTotal / 5) && filledPositions2 > 0)
{
highestRanked->promoteRank();
soldiersPromoted++;
}
return (soldiersPromoted > 0);
}
/**
* Checks how many soldiers of a rank exist and which one has the highest score.
* @param Pointer to an int to store the total in.
* @param Rank to inspect.
* @return The highest scoring soldier of that rank.
*/
void SavedGame::inspectSoldiers(Soldier **highestRanked, size_t *total, int rank)
{
int highestScore = 0;
*total = 0;
for (std::vector<Base*>::iterator i = _bases.begin(); i != _bases.end(); ++i)
{
for (std::vector<Soldier*>::iterator j = (*i)->getSoldiers()->begin(); j != (*i)->getSoldiers()->end(); ++j)
{
if ((*j)->getRank() == (SoldierRank)rank)
{
(*total)++;
UnitStats *s = (*j)->getCurrentStats();
int v1 = 2 * s->health + 2 * s->stamina + 4 * s->reactions + 4 * s->bravery;
int v2 = v1 + 3*( s->tu + 2*( s->firing ) );
int v3 = v2 + s->melee + s->throwing + s->strength;
//if (PsiSkill>0) c3 += PsiStrength + 2* PsiSkill
int score = v3 + 10 * ( (*j)->getMissions() + (*j)->getKills() );
if (score > highestScore)
{
highestScore = score;
*highestRanked = (*j);
}
}
}
}
}
/**
* Toggles debug mode.
*/
void SavedGame::setDebugMode()
{
_debug = !_debug;
}
/**
* Gets the current debug mode.
* @return Debug mode.
*/
bool SavedGame::getDebugMode() const
{
return _debug;
}
}