-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCvPreGame.cpp
3439 lines (3004 loc) · 87.2 KB
/
CvPreGame.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
/* -------------------------------------------------------------------------------------------------------
© 1991-2012 Take-Two Interactive Software and its subsidiaries. Developed by Firaxis Games.
Sid Meier's Civilization V, Civ, Civilization, 2K Games, Firaxis Games, Take-Two Interactive Software
and their respective logos are all trademarks of Take-Two interactive Software, Inc.
All other marks and trademarks are the property of their respective owners.
All rights reserved.
------------------------------------------------------------------------------------------------------- */
#include "CvGameCoreDLLPCH.h"
#include "CvGameCoreUtils.h"
#include "CvPreGame.h"
#include "FIGameIniParser.h"
#include "FLua/Include/FLua.h"
#include "FStlContainerSerialization.h"
#include "FFileStream.h"
#include "ICvDLLDatabaseUtility.h"
#include "CvInfosSerializationHelper.h"
#include <unordered_map>
// Include this after all other headers.
#include "LintFree.h"
//////////////////////////////////////////////////////////////////////////
//
// WARNING: Do not use any of the GC.*Info tables in this file.
// These Game Core tables are not always current to what the loaded
// database set contains. This is the case when setting up a multiplayer
// game or even in a single player game if you have returned from a multiplayer game.
// The reason the Game Core data is not kept in sync is because the caching of all the data
// can take a significant amount of time.
#define PREGAMEVARDEFAULT(a, b) FAutoVariable<a, Phony> b("CvPreGame::"#b, s_preGameArchive);
#define PREGAMEVAR(a, b, c) FAutoVariable<a, Phony> b("CvPreGame::"#b, s_preGameArchive, c, false);
//Basic translation function to help with the glue between the old
//GameOptionTypes enumeration usage vs the newer system that uses strings.
const char* ConvertGameOptionTypeToString(GameOptionTypes eOption)
{
switch(eOption)
{
case GAMEOPTION_NO_CITY_RAZING:
return "GAMEOPTION_NO_CITY_RAZING";
case GAMEOPTION_NO_BARBARIANS:
return "GAMEOPTION_NO_BARBARIANS";
case GAMEOPTION_RAGING_BARBARIANS:
return "GAMEOPTION_RAGING_BARBARIANS";
case GAMEOPTION_ALWAYS_WAR:
return "GAMEOPTION_ALWAYS_WAR";
case GAMEOPTION_ALWAYS_PEACE:
return "GAMEOPTION_ALWAYS_PEACE";
case GAMEOPTION_ONE_CITY_CHALLENGE:
return "GAMEOPTION_ONE_CITY_CHALLENGE";
case GAMEOPTION_NO_CHANGING_WAR_PEACE:
return "GAMEOPTION_NO_CHANGING_WAR_PEACE";
case GAMEOPTION_NEW_RANDOM_SEED:
return "GAMEOPTION_NEW_RANDOM_SEED";
case GAMEOPTION_LOCK_MODS:
return "GAMEOPTION_LOCK_MODS";
case GAMEOPTION_COMPLETE_KILLS:
return "GAMEOPTION_COMPLETE_KILLS";
case GAMEOPTION_NO_GOODY_HUTS:
return "GAMEOPTION_NO_GOODY_HUTS";
case GAMEOPTION_RANDOM_PERSONALITIES:
return "GAMEOPTION_RANDOM_PERSONALITIES";
case GAMEOPTION_POLICY_SAVING:
return "GAMEOPTION_POLICY_SAVING";
case GAMEOPTION_PROMOTION_SAVING:
return "GAMEOPTION_PROMOTION_SAVING";
case GAMEOPTION_END_TURN_TIMER_ENABLED:
return "GAMEOPTION_END_TURN_TIMER_ENABLED";
case GAMEOPTION_QUICK_COMBAT:
return "GAMEOPTION_QUICK_COMBAT";
case GAMEOPTION_NO_SCIENCE:
return "GAMEOPTION_NO_SCIENCE";
case GAMEOPTION_NO_POLICIES:
return "GAMEOPTION_NO_POLICIES";
case GAMEOPTION_NO_HAPPINESS:
return "GAMEOPTION_NO_HAPPINESS";
case GAMEOPTION_NO_TUTORIAL:
return "GAMEOPTION_NO_TUTORIAL";
}
return NULL;
}
namespace CvPreGame
{
static const GUID s_emptyGUID = { 0x00000000, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
static PackageIDList s_emptyGUIDList;
//------------------------------------------------------------------------------
// CustomOption
//------------------------------------------------------------------------------
CustomOption::CustomOption()
: m_iValue(-1)
{
memset(m_szOptionName, 0, 64);
}
//------------------------------------------------------------------------------
CustomOption::CustomOption(const char* szOptionName, int iVal)
: m_iValue(iVal)
{
strcpy_s(m_szOptionName, 64, szOptionName);
}
//------------------------------------------------------------------------------
CustomOption::CustomOption(const CustomOption& copy)
{
m_iValue = copy.m_iValue;
strcpy_s(m_szOptionName, copy.m_szOptionName);
}
//------------------------------------------------------------------------------
CustomOption& CustomOption::operator=(const CustomOption& rhs)
{
m_iValue = rhs.m_iValue;
strcpy_s(m_szOptionName, rhs.m_szOptionName);
return (*this);
}
//------------------------------------------------------------------------------
bool CustomOption::operator ==(const CustomOption& option) const
{
if(m_iValue == option.m_iValue)
{
if(strncmp(m_szOptionName, option.m_szOptionName, 64) == 0)
return true;
}
return false;
}
//------------------------------------------------------------------------------
const char* CustomOption::GetName(size_t& bytes) const
{
bytes = strlen(m_szOptionName);
return m_szOptionName;
}
//------------------------------------------------------------------------------
const char* CustomOption::GetName() const
{
return m_szOptionName;
}
//------------------------------------------------------------------------------
int CustomOption::GetValue() const
{
return m_iValue;
}
//------------------------------------------------------------------------------
FDataStream & operator>>(FDataStream & stream, CustomOption & option)
{
FString optionName;
stream >> optionName;
stream >> option.m_iValue;
strcpy_s(option.m_szOptionName, 64, optionName.c_str());
return stream;
}
//------------------------------------------------------------------------------
FDataStream & operator<<(FDataStream & stream, const CustomOption & option)
{
FString optionName(option.m_szOptionName, strlen(option.m_szOptionName));
stream << optionName;
stream << option.m_iValue;
return stream;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void StringToBools(const char* szString, int* iNumBools, bool** ppBools);
struct Phony {
std::string debugDump(const FAutoVariableBase &) { return "";}
std::string stackTraceRemark(const FAutoVariableBase &) { return "";}
};
Phony phony;
FAutoArchiveClassContainer<Phony> s_preGameArchive (phony);
PREGAMEVAR (PlayerTypes, s_activePlayer, NO_PLAYER);
PREGAMEVARDEFAULT (CvString, s_adminPassword);
PREGAMEVAR (int, s_advancedStartPoints, 0);
PREGAMEVARDEFAULT (CvString, s_alias);
PREGAMEVAR (std::vector<ArtStyleTypes>, s_artStyles, MAX_PLAYERS);
PREGAMEVAR (bool, s_autorun, false);
PREGAMEVAR (float, s_autorunTurnDelay, 0.0f);
PREGAMEVAR (int, s_autorunTurnLimit, 0);
PREGAMEVAR (BandwidthType, s_bandwidth, NO_BANDWIDTH);
PREGAMEVAR (CalendarTypes, s_calendar, NO_CALENDAR);
PREGAMEVARDEFAULT (CvBaseInfo, s_calendarInfo);
PREGAMEVAR (std::vector<CvString>, s_civAdjectives, MAX_PLAYERS);
PREGAMEVAR (std::vector<CvString>, s_civDescriptions, MAX_PLAYERS);
PREGAMEVAR (std::vector<CivilizationTypes>, s_civilizations, MAX_PLAYERS);
PREGAMEVAR (std::vector<CvString>, s_civPasswords, MAX_PLAYERS);
PREGAMEVAR (std::vector<CvString>, s_civShortDescriptions, MAX_PLAYERS);
PREGAMEVAR (ClimateTypes, s_climate, NO_CLIMATE);
PREGAMEVARDEFAULT (CvClimateInfo, s_climateInfo);
PREGAMEVAR (EraTypes, s_era, NO_ERA);
PREGAMEVAR (std::vector<CvString>, s_emailAddresses, MAX_PLAYERS);
PREGAMEVAR (float, s_endTurnTimerLength, 0.0f);
PREGAMEVAR (std::vector<CvString>, s_flagDecals, MAX_PLAYERS);
PREGAMEVAR (std::vector<bool>, s_DEPRECATEDforceControls, 7); //This was removed during the Day 0 patch since it is no longer used anywhere.
PREGAMEVAR (GameMode, s_gameMode, NO_GAMEMODE);
PREGAMEVARDEFAULT (CvString, s_gameName);
PREGAMEVAR (GameSpeedTypes, s_gameSpeed, NO_GAMESPEED);
PREGAMEVAR (bool, s_gameStarted, false);
PREGAMEVAR (int, s_gameTurn, -1);
PREGAMEVAR (GameTypes, s_gameType, GAME_TYPE_NONE);
PREGAMEVAR (GameMapTypes, s_gameMapType, GAME_USER_PARAMETERS);
PREGAMEVAR (int, s_gameUpdateTime, 0);
PREGAMEVAR (std::vector<HandicapTypes>, s_handicaps, MAX_PLAYERS);
PREGAMEVAR (std::vector<HandicapTypes>, s_lastHumanHandicaps, MAX_PLAYERS);
PREGAMEVAR (bool, s_isEarthMap, false);
PREGAMEVAR (bool, s_isInternetGame, false);
PREGAMEVAR (std::vector<LeaderHeadTypes>, s_leaderHeads, MAX_PLAYERS);
PREGAMEVAR (std::vector<CvString>, s_leaderNames, MAX_PLAYERS);
PREGAMEVARDEFAULT (CvString, s_loadFileName);
PREGAMEVARDEFAULT (CvString, s_localPlayerEmailAddress);
PREGAMEVAR (bool, s_mapNoPlayers, false);
PREGAMEVAR (unsigned int, s_mapRandomSeed, 0);
PREGAMEVAR (bool, s_loadWBScenario, false);
PREGAMEVAR (bool, s_overrideScenarioHandicap, false);
PREGAMEVARDEFAULT (CvString, s_mapScriptName);
PREGAMEVAR (int, s_maxCityElimination, 0);
PREGAMEVAR (int, s_maxTurns, 0);
PREGAMEVAR (int, s_numMinorCivs, -1);
PREGAMEVAR (std::vector<MinorCivTypes>, s_minorCivTypes, MAX_PLAYERS);
PREGAMEVAR (std::vector<bool>, s_minorNationCivs, MAX_PLAYERS);
PREGAMEVAR (bool, s_dummyvalue, false);
PREGAMEVAR (std::vector<bool>, s_multiplayerOptions, NUM_MPOPTION_TYPES);
PREGAMEVAR (std::vector<int>, s_netIDs, MAX_PLAYERS);
PREGAMEVAR (std::vector<CvString>, s_nicknames, MAX_PLAYERS);
PREGAMEVAR (int, s_numVictoryInfos, GC.getNumVictoryInfos());
PREGAMEVAR (int, s_pitBossTurnTime, 0);
PREGAMEVAR (std::vector<bool>, s_playableCivs, MAX_PLAYERS);
PREGAMEVAR (std::vector<PlayerColorTypes>, s_playerColors, MAX_PLAYERS);
PREGAMEVAR (bool, s_privateGame, false);
PREGAMEVAR (bool, s_quickCombat, false);
PREGAMEVAR (bool, s_quickCombatDefault, false);
PREGAMEVAR (HandicapTypes, s_quickHandicap, NO_HANDICAP);
PREGAMEVAR (bool, s_quickstart, false);
PREGAMEVAR (bool, s_randomWorldSize, false);
PREGAMEVAR (bool, s_randomMapScript, false);
PREGAMEVAR (std::vector<bool>, s_readyPlayers, MAX_PLAYERS);
PREGAMEVAR (SeaLevelTypes, s_seaLevel, NO_SEALEVEL);
PREGAMEVARDEFAULT (CvSeaLevelInfo, s_seaLevelInfo);
PREGAMEVAR (bool, s_dummyvalue2, false);
PREGAMEVAR (std::vector<SlotClaim>, s_slotClaims, MAX_PLAYERS);
PREGAMEVAR (std::vector<SlotStatus>, s_slotStatus, MAX_PLAYERS);
PREGAMEVARDEFAULT (CvString, s_smtpHost);
PREGAMEVAR (unsigned int, s_syncRandomSeed, 0);
PREGAMEVAR (int, s_targetScore, 0);
PREGAMEVAR (std::vector<TeamTypes>, s_teamTypes, MAX_PLAYERS);
PREGAMEVAR (bool, s_transferredMap, false);
PREGAMEVARDEFAULT (CvTurnTimerInfo, s_turnTimer);
PREGAMEVAR (TurnTimerTypes, s_turnTimerType, NO_TURNTIMER);
PREGAMEVAR (bool, s_bCityScreenBlocked, false);
PREGAMEVAR (std::vector<bool>, s_victories, s_numVictoryInfos);
PREGAMEVAR (std::vector<bool>, s_whiteFlags, MAX_PLAYERS);
PREGAMEVARDEFAULT (CvWorldInfo, s_worldInfo);
PREGAMEVAR (WorldSizeTypes, s_worldSize, NO_WORLDSIZE);
PREGAMEVARDEFAULT (std::vector<CustomOption>, s_GameOptions);
PREGAMEVARDEFAULT (std::vector<CustomOption>, s_MapOptions);
PREGAMEVARDEFAULT (std::string, s_versionString);
PREGAMEVAR (std::vector<bool>, s_turnNotifySteamInvite, MAX_PLAYERS);
PREGAMEVAR(std::vector<bool>, s_turnNotifyEmail, MAX_PLAYERS);
PREGAMEVAR(std::vector<CvString>, s_turnNotifyEmailAddress, MAX_PLAYERS);
typedef std::map<uint, uint> HashToOptionMap;
HashToOptionMap s_GameOptionsHash;
HashToOptionMap s_MapOptionsHash;
bool s_multiplayerAIEnabled = true; // default for RTM, change to true on street patch
std::map<PlayerTypes, CvString> s_displayNicknames; // JAR - workaround duplicate IDs vs display names
const std::vector<TeamTypes>& sr_TeamTypes = s_teamTypes;
PackageIDList s_AllowedDLC;
std::vector<CvString> s_civilizationKeys(MAX_PLAYERS);
std::vector<GUID> s_civilizationPackageID(MAX_PLAYERS);
std::vector<bool> s_civilizationKeysAvailable(MAX_PLAYERS);
std::vector<bool> s_civilizationKeysPlayable(MAX_PLAYERS);
std::vector<CvString> s_leaderKeys(MAX_PLAYERS);
std::vector<GUID> s_leaderPackageID(MAX_PLAYERS);
std::vector<bool> s_leaderKeysAvailable(MAX_PLAYERS);
std::vector<PackageIDList> s_DLCPackagesAvailable(MAX_PLAYERS);
std::vector<CvString> s_civAdjectivesLocalized(MAX_PLAYERS);
std::vector<CvString> s_civDescriptionsLocalized(MAX_PLAYERS);
std::vector<CvString> s_civShortDescriptionsLocalized(MAX_PLAYERS);
std::vector<CvString> s_leaderNamesLocalized(MAX_PLAYERS);
GameStartTypes s_gameStartType;
StorageLocation s_loadFileStorage;
// -----------------------------------------------------------------------
// Bind a leader head key to the leader head using the current leader head ID
bool bindLeaderKeys(PlayerTypes p)
{
LeaderHeadTypes l = leaderHead(p);
bool bFailed = false;
if (l != NO_LEADER)
{
// During the pre-game, we can't be sure the cached *Infos are current, so query the database
Database::Connection* pDB = GC.GetGameDatabase();
if (pDB)
{
Database::Results kResults;
if(pDB->Execute(kResults, "SELECT Type, PackageID from Leaders where ID = ? LIMIT 1"))
{
kResults.Bind(1, l);
if(kResults.Step())
{
s_leaderKeys[p] = kResults.GetText(0);
if (!ExtractGUID(kResults.GetText(1), s_leaderPackageID[p]))
ClearGUID(s_leaderPackageID[p]);
s_leaderKeysAvailable[p] = true;
}
else
bFailed = true;
}
else
bFailed = true;
}
}
else
bFailed = true;
if (bFailed)
{
s_leaderKeys[p].clear();
ClearGUID(s_leaderPackageID[p]);
s_leaderKeysAvailable[p] = false;
}
return bFailed;
}
// Set the unique key value for the civilization
bool bindCivilizationKeys(PlayerTypes p)
{
bool bFailed = false;
CivilizationTypes c = civilization(p);
if (c != NO_CIVILIZATION)
{
// During the pre-game, we can't be sure the cached *Infos are current, so query the database
Database::Connection* pDB = GC.GetGameDatabase();
if (pDB)
{
Database::Results kResults;
if(pDB->Execute(kResults, "SELECT Type, PackageID, Playable from Civilizations where ID = ? LIMIT 1"))
{
kResults.Bind(1, c);
if(kResults.Step())
{
s_civilizationKeys[p] = kResults.GetText(0);
if (!ExtractGUID(kResults.GetText(1), s_civilizationPackageID[p]))
ClearGUID(s_civilizationPackageID[p]);
s_civilizationKeysAvailable[p] = true;
s_civilizationKeysPlayable[p] = kResults.GetBool(2);
}
else
bFailed = true;
}
else
bFailed = true;
}
}
else
bFailed = true;
if (bFailed)
{
s_civilizationKeys[p].clear();
ClearGUID(s_civilizationPackageID[p]);
s_civilizationKeysAvailable[p] = false;
s_civilizationKeysPlayable[p] = false;
}
return bFailed;
}
void writeCivilizations(FDataStream& saveTo)
{
if(s_gameStarted || !isNetworkMultiplayerGame()){
//full save, preserve everything.
saveTo << s_civilizations;
}
else{
//game cfg save only. Scrub player specific data from our save output.
//reset all non-AI players to random civ.
int i = 0;
std::vector<CivilizationTypes> civsTemp = s_civilizations;
for(std::vector<CivilizationTypes>::iterator civIter = civsTemp.begin(); civIter != civsTemp.end(); ++civIter, ++i){
if(slotStatus((PlayerTypes)i) != SS_COMPUTER){
*civIter = NO_CIVILIZATION;
}
}
saveTo << civsTemp;
}
}
void writeNicknames(FDataStream& saveTo)
{
if(s_gameStarted || !isNetworkMultiplayerGame()){
//full save, preserve everything.
saveTo << s_nicknames;
}
else{
//game cfg save only. Scrub player specific data from our save output.
std::vector<CvString> nicksTemp = s_nicknames;
for(std::vector<CvString>::iterator nickIter = nicksTemp.begin(); nickIter != nicksTemp.end(); ++nickIter){
*nickIter = "";
}
saveTo << nicksTemp;
}
}
void writeSlotStatus(FDataStream& saveTo)
{
if(s_gameStarted || !isNetworkMultiplayerGame()){
//full save, preserve everything.
saveTo << s_slotStatus;
}
else{
//game cfg save only. Scrub player specific data from our save output.
//Revert human occupied slots to open.
std::vector<SlotStatus> slotTemp = s_slotStatus;
for(std::vector<SlotStatus>::iterator slotIter = slotTemp.begin(); slotIter != slotTemp.end(); ++slotIter){
if(*slotIter == SS_TAKEN){
*slotIter = SS_OPEN;
}
}
saveTo << slotTemp;
}
}
// -----------------------------------------------------------------------
void saveSlotHints(FDataStream & saveTo)
{
uint uiVersion = 3;
saveTo << uiVersion;
saveTo << s_gameSpeed;
saveTo << s_worldSize;
saveTo << s_mapScriptName;
writeCivilizations(saveTo);
writeNicknames(saveTo);
writeSlotStatus(saveTo);
saveTo << s_slotClaims;
saveTo << s_teamTypes;
saveTo << s_handicaps;
saveTo << s_civilizationKeys;
saveTo << s_leaderKeys;
}
void ReseatConnectedPlayers()
{//This function realigns network connected players into the correct slots for the current pregame data. (Typically after loading in saved data)
//A network player's pregame data can and will be totally wrong until this function is run and the resulting net messages are processed.
if(isNetworkMultiplayerGame()){
int i = 0;
for(i = 0; i < MAX_PLAYERS; ++i){
// reseat the network connected player in this slot.
if(gDLL->ReseatConnectedPlayer((PlayerTypes)i)){
//a player needs to be reseated. We need to wait for the net message to come from the host. We will rerun ReseatConnectedPlayers then.
return;
}
}
}
}
int calcActiveSlotCount(const std::vector<SlotStatus> & slotStatus, const std::vector<SlotClaim> & slotClaims )
{
int iCount = 0;
int i = 0;
for(i = 0; i < MAX_PLAYERS; ++i)
{
SlotStatus eStatus = slotStatus[i];
SlotClaim eClaim = slotClaims[i];
if((eStatus == SS_TAKEN || eStatus == SS_COMPUTER || eStatus == SS_OBSERVER)
&& (eClaim == SLOTCLAIM_ASSIGNED || eClaim == SLOTCLAIM_RESERVED))
++iCount;
}
return iCount;
}
// ---------------------------------------------------------------------------
static void loadSlotsHelper(
FDataStream & loadFrom,
uint uiVersion,
GameSpeedTypes & gameSpeed,
WorldSizeTypes & worldSize,
CvString & mapScriptName,
std::vector<CivilizationTypes> & civilizations,
std::vector<CvString> & nicknames,
std::vector<SlotStatus> & slotStatus,
std::vector<SlotClaim> & slotClaims,
std::vector<TeamTypes> & teamTypes,
std::vector<HandicapTypes>& handicapTypes,
std::vector<CvString>& civilizationKeys,
std::vector<CvString>& leaderKeys)
{
loadFrom >> gameSpeed;
loadFrom >> worldSize;
loadFrom >> mapScriptName;
loadFrom >> civilizations;
loadFrom >> nicknames;
loadFrom >> slotStatus;
loadFrom >> slotClaims;
loadFrom >> teamTypes;
if (uiVersion >= 2)
loadFrom >> handicapTypes;
else
handicapTypes.clear();
if(uiVersion >= 3)
{
loadFrom >> civilizationKeys;
loadFrom >> leaderKeys;
}
else
{
civilizationKeys.clear();
leaderKeys.clear();
}
}
int readActiveSlotCountFromSaveGame(FDataStream & loadFrom, bool bReadVersion)
{
uint uiVersion = 0;
if (bReadVersion)
loadFrom >> uiVersion;
GameSpeedTypes dummyGameSpeed;
WorldSizeTypes dummyWorldSize;
CvString dummyMapScriptName;
std::vector<CivilizationTypes> dummyCivilizations;
std::vector<CvString> dummyNicknames;
std::vector<TeamTypes> dummyTeamTypes;
std::vector<SlotStatus> slotStatus;
std::vector<SlotClaim> slotClaims;
std::vector<HandicapTypes> dummyHandicapTypes;
std::vector<CvString> civilizationKeys;
std::vector<CvString> leaderKeys;
loadSlotsHelper(loadFrom, uiVersion, dummyGameSpeed, dummyWorldSize, dummyMapScriptName, dummyCivilizations, dummyNicknames, slotStatus, slotClaims, dummyTeamTypes, dummyHandicapTypes, civilizationKeys, leaderKeys);
return calcActiveSlotCount(slotStatus, slotClaims);
}
void loadSlotHints(FDataStream & loadFrom, bool bReadVersion)
{
uint uiVersion = 0;
if (bReadVersion)
loadFrom >> uiVersion;
GameSpeedTypes gameSpeed;
WorldSizeTypes worldSize;
CvString mapScriptName;
std::vector<CivilizationTypes> civilizations;
std::vector<CvString> nicknames;
std::vector<TeamTypes> teamTypes;
std::vector<SlotStatus> slotStatus;
std::vector<SlotClaim> slotClaims;
std::vector<HandicapTypes> handicapTypes;
std::vector<CvString> civilizationKeys;
std::vector<CvString> leaderKeys;
loadSlotsHelper(loadFrom, uiVersion, gameSpeed, worldSize, mapScriptName, civilizations, nicknames, slotStatus, slotClaims, teamTypes, handicapTypes, civilizationKeys, leaderKeys);
s_gameSpeed = gameSpeed;
s_worldSize = worldSize;
s_mapScriptName = mapScriptName;
if (uiVersion >= 3)
{
// Set the civilizations and leaders from the key values
if (civilizationKeys.size() > 0)
{
for (uint i = 0; i < civilizationKeys.size(); ++i)
setCivilizationKey((PlayerTypes)i, civilizationKeys[i]);
}
else
// Fall back to using the indices. This shouldn't happen.
s_civilizations = civilizations;
if (leaderKeys.size() > 0)
{
for (uint i = 0; i < leaderKeys.size(); ++i)
setLeaderKey((PlayerTypes)i, leaderKeys[i]);
}
}
else
{
// Civilization saved as indices, not a good idea.
s_civilizations = civilizations;
}
s_nicknames = nicknames;
s_teamTypes = teamTypes;
s_slotStatus = slotStatus;
s_slotClaims = slotClaims;
// The slots hints handicaps may be invalid, only copy them over if they are not
if (handicapTypes.size() == MAX_PLAYERS)
s_handicaps = handicapTypes;
size_t i;
for(i = 0; i < s_nicknames.size(); ++i)
{
PlayerTypes p = static_cast<PlayerTypes>(i);
setNickname(p, s_nicknames[i]); // fix display names
}
ReseatConnectedPlayers();
}
PlayerTypes activePlayer()
{
return s_activePlayer;
}
const CvString & adminPassword()
{
return s_adminPassword;
}
int advancedStartPoints()
{
return s_advancedStartPoints;
}
const CvString & alias()
{
return s_alias;
}
ArtStyleTypes artStyle(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
return s_artStyles[p];
return NO_ARTSTYLE;
}
bool autorun()
{
return s_autorun;
}
float autorunTurnDelay()
{
return s_autorunTurnDelay;
}
int autorunTurnLimit()
{
return s_autorunTurnLimit;
}
BandwidthType bandwidth()
{
return s_bandwidth;
}
CvString bandwidthDescription()
{
if ( bandwidth() == BANDWIDTH_BROADBAND )
{
//return CvString("broadband");
return GetLocalizedText("broadband");
}
else
{
return GetLocalizedText("modem");
//return CvString("modem");
}
}
CalendarTypes calendar()
{
return s_calendar;
}
CivilizationTypes civilization(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
return s_civilizations[p];
return NO_CIVILIZATION;
}
const CvString& civilizationAdjective(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
{
s_civAdjectivesLocalized[p] = GetLocalizedText(s_civAdjectives[p]);
return s_civAdjectivesLocalized[p];
}
static const CvString empty = "";
return empty;
}
const CvString & civilizationAdjectiveKey(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
return s_civAdjectives[p];
static const CvString empty = "";
return empty;
}
const CvString& civilizationDescription(PlayerTypes p)
{
if ( p >= 0 && p < MAX_PLAYERS)
{
s_civDescriptionsLocalized[p] = GetLocalizedText(s_civDescriptions[p]);
return s_civDescriptionsLocalized[p];
}
static const CvString empty = "";
return empty;
}
const CvString & civilizationDescriptionKey(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
return s_civDescriptions[p];
static const CvString empty = "";
return empty;
}
const CvString & civilizationPassword(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
return s_civPasswords[p];
static const CvString empty = "";
return empty;
}
const CvString& civilizationShortDescription(PlayerTypes p)
{
if ( p >= 0 && p < MAX_PLAYERS)
{
s_civShortDescriptionsLocalized[p] = GetLocalizedText(s_civShortDescriptions[p]);
return s_civShortDescriptionsLocalized[p];
}
static const CvString empty = "";
return empty;
}
const CvString & civilizationShortDescriptionKey(PlayerTypes p)
{
if(p >= 0 && p < MAX_PLAYERS)
return s_civShortDescriptions[p];
static const CvString empty = "";
return empty;
}
ClimateTypes climate()
{
return s_climate;
}
const CvClimateInfo & climateInfo()
{
if(s_climate != s_climateInfo.get().GetID())
{
Database::SingleResult kResult;
DB.SelectAt(kResult, "Climates", s_climate);
s_climateInfo.dirtyGet().CacheResult(kResult);
}
return s_climateInfo;
}
void closeAllSlots()
{
for (int i = 0; i < MAX_CIV_PLAYERS; i++)
{
PlayerTypes eID = (PlayerTypes)i;
setSlotStatus(eID, SS_CLOSED);
setSlotClaim(eID, SLOTCLAIM_UNASSIGNED);
}
}
void closeInactiveSlots()
{
s_gameStarted = true;
// Open inactive slots mean different things to different game modes and types...
// Let's figure out what they mean for us
gDLL->BeginSendBundle();
for (int i = 0; i < MAX_CIV_PLAYERS; i++)
{
PlayerTypes eID = (PlayerTypes)i;
if (slotStatus(eID) == SS_OPEN)
{
if (gameType() == GAME_NETWORK_MULTIPLAYER && gameMapType() == GAME_SCENARIO)
{
// Multiplayer scenario - all "open" slots should be filled with an AI player
setSlotStatus(eID, SS_COMPUTER);
}
else
{
// If it's a normal game, all "open" slots should be closed.
setSlotStatus(eID, SS_CLOSED);
}
setSlotClaim(eID, SLOTCLAIM_UNASSIGNED);
gDLL->sendPlayerInfo(eID);
}
}
gDLL->EndSendBundle();
}
const CvString & emailAddress(PlayerTypes p)
{
static const CvString empty("");
if(p >= 0 && p < MAX_PLAYERS)
return s_emailAddresses[p];
return empty;
}
const CvString & emailAddress()
{
return s_localPlayerEmailAddress;
}
float endTurnTimerLength()
{
return s_endTurnTimerLength;
}
EraTypes era()
{
return s_era;
}
PlayerTypes findPlayerByNickname(const char * const name)
{
PlayerTypes result = NO_PLAYER;
if(name)
{
int i = 0;
for(i = 0; i < MAX_PLAYERS; ++i)
{
if(s_nicknames[i] == name)
{
result = static_cast<PlayerTypes>(i);
break;
}
}
}
return result;
}
GameMode gameMode()
{
return s_gameMode;
}
const CvString & gameName()
{
return s_gameName;
}
//! This does not need to be an auto var!
//! The actual game options vector s_GameOptions is an auto var.
//! This is only here to make retrieving enum-based options fast
//! since GameCore does it way to effing often.
int s_EnumBasedGameOptions[NUM_GAMEOPTION_TYPES];
//! This method sync's up an enum-based array of game options w/ the actual
//! game options structure. This should be called every time new options
//! are set.
void SyncGameOptionsWithEnumList()
{
const char* str;
for(int i = 0; i < NUM_GAMEOPTION_TYPES; i++)
{
int value = 0;
str = ConvertGameOptionTypeToString((GameOptionTypes)i);
if (str)
{
GetGameOption(str, value);
}
s_EnumBasedGameOptions[i] = value;
bool v = static_cast<bool>(value);
switch(i)
{
case GAMEOPTION_QUICK_COMBAT:
s_quickCombat = v; // set directly, to avoid infinite recursion.
break;
default:
break;
}
}
}
bool GetGameOption(const char* szOptionName, int& iValue)
{
if (szOptionName == NULL || *szOptionName == 0)
return false;
for(std::vector<CustomOption>::const_iterator it = s_GameOptions.begin();
it != s_GameOptions.end(); ++it)
{
size_t bytes = 0;
const char* szCurrentOptionName = (*it).GetName(bytes);
if(strncmp(szCurrentOptionName, szOptionName, bytes) == 0)
{
iValue = (*it).GetValue();
return true;
}
}
//Try and lookup the default value.
Database::Results kLookup;
if(GC.GetGameDatabase()->Execute(kLookup, "Select \"Default\" from GameOptions where Type = ? LIMIT 1"))
{
kLookup.Bind(1, szOptionName);
if(kLookup.Step())
{
iValue = kLookup.GetInt(0);
return true;
}
}
return false;
}
bool GetGameOption(GameOptionTypes eOption, int& iValue)
{
if ((uint)eOption >= 0 && (uint)eOption < (uint)NUM_GAMEOPTION_TYPES)
{
iValue = s_EnumBasedGameOptions[(size_t)eOption];
return true;
}
else
{
// Hash lookup
HashToOptionMap::const_iterator itr = s_GameOptionsHash.find((uint)eOption);
if (itr != s_GameOptionsHash.end())
{
if (itr->second <= s_GameOptions.size())
{
iValue = s_GameOptions[itr->second].GetValue();
return true;
}
}
// Must get the string from the hash
GameOptionTypes eOptionIndex = (GameOptionTypes)GC.getInfoTypeForHash((uint)eOption);
if (eOptionIndex >= 0)
{
CvGameOptionInfo* pkInfo = GC.getGameOptionInfo(eOptionIndex);
if (pkInfo)
{
//Try and lookup the default value.
Database::Results kLookup;
if(GC.GetGameDatabase()->Execute(kLookup, "Select \"Default\" from GameOptions where Type = ? LIMIT 1"))
{
kLookup.Bind(1, pkInfo->GetType());
if(kLookup.Step())
{
iValue = kLookup.GetInt(0);
return true;
}
}
}
}
return false;
}
}
const std::vector<CustomOption>& GetGameOptions()
{
return s_GameOptions;
}
bool SetGameOption(const char* szOptionName, int iValue)
{
//Do not allow NULL entries :P
if(szOptionName == NULL || strlen(szOptionName) == 0)
return false;