forked from CaravelGames/drod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDb.cpp
More file actions
1193 lines (1055 loc) · 36.2 KB
/
Copy pathDb.cpp
File metadata and controls
1193 lines (1055 loc) · 36.2 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
// $Id: Db.cpp 10108 2012-04-22 04:54:24Z mrimer $
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Deadly Rooms of Death.
*
* The Initial Developer of the Original Code is
* Caravel Software.
* Portions created by the Initial Developer are Copyright (C) 1995, 1996,
* 1997, 2000, 2001, 2002, 2005 Caravel Software. All Rights Reserved.
*
* Contributor(s):
* Mike Rimer (mrimer)
*
* ***** END LICENSE BLOCK ***** */
//Db.cpp
//Implementation of CDb.
#define INCLUDED_FROM_DB_CPP
#include "Db.h"
#undef INCLUDED_FROM_DB_CPP
#include "DbXML.h"
#include "CurrentGame.h"
#include "MonsterFactory.h"
#include "NetInterface.h"
#include "Swordsman.h"
#include <map>
using std::map;
//Holds the only instance of CDb for the app.
CDb *g_pTheDB = NULL;
//Reset hold and player.
UINT CDb::dwCurrentHoldID = 0L;
UINT CDb::dwCurrentPlayerID = 0L;
bool CDb::bFreezeTimeStamps = false;
//
//CDb public methods.
//
//*****************************************************************************
CDb::CDb()
{
this->Players.FilterByLocal();
}
//*****************************************************************************
CDb::~CDb()
{
//There shouldn't be any unused rows remaining at this point.
ASSERT(!EmptyRowsExist());
}
//*****************************************************************************
void CDb::Commit()
//Commits changes in all the databases.
//
//Should only be called from the single global CDb pointer to ensure
//non-static data stored in the view handlers is processed correctly.
{
RemoveEmptyRows();
CDbBase::Commit();
}
//*****************************************************************************
void CDb::Rollback()
//Rolls back changes in all the databases.
{
RemoveEmptyRows(); //ResetEmptyRowCount(); //unstable if metakit rollback doesn't work right
CDbBase::Rollback();
}
//*****************************************************************************
UINT CDb::GetHoldID()
//Returns: the current hold ID. If it is zero, try to find the first ID.
{
if (!CDb::dwCurrentHoldID)
{
CDbHold *pHold = this->Holds.GetFirst(true);
while (pHold)
{
const CDbHold::HoldStatus status = pHold->status;
const UINT holdID = pHold->dwHoldID;
delete pHold;
//Skip tutorial holds
if (status != CDbHold::Tutorial) {
CDb::dwCurrentHoldID = holdID;
pHold = NULL;
} else {
pHold = this->Holds.GetNext();
}
}
}
return CDb::dwCurrentHoldID;
}
//*****************************************************************************
UINT CDb::GetPlayerID()
//Returns: the current player ID. If it is zero, try to find the first local ID.
{
if (!CDb::dwCurrentPlayerID)
{
this->Players.FilterByLocal();
CDbPlayer *pPlayer = this->Players.GetFirst(true);
if (pPlayer)
{
if (g_pTheNet) g_pTheNet->ClearActiveAction();
CDb::dwCurrentPlayerID = pPlayer->dwPlayerID;
if (g_pTheNet) g_pTheNet->DownloadHoldList();
delete pPlayer;
}
}
return CDb::dwCurrentPlayerID;
}
//*****************************************************************************
CDbPlayer* CDb::GetCurrentPlayer()
{
return this->Players.GetByID(GetPlayerID());
}
CDbPackedVars CDb::GetCurrentPlayerSettings()
{
return this->Players.GetSettings(GetPlayerID());
}
//*****************************************************************************
CCurrentGame* CDb::GetDummyCurrentGame()
//If something needs access to an empty CCurrentGame object.
{
CCurrentGame *pCCG = new CCurrentGame();
pCCG->bNoSaves = true;
CCurrentGame::InitRPGStats(pCCG->pPlayer->st);
pCCG->pPlayer->wAppearance = M_NONE; //not in room
return pCCG;
}
//*****************************************************************************
CCurrentGame *CDb::GetSavedCurrentGame(
//Gets a current game object from a saved game.
//
//Params:
const UINT dwSavedGameID, //(in) Indicates saved game to load from.
CCueEvents &CueEvents, //(out) Cue events generated by swordsman's first step
// into the room.
bool bRestoreAtRoomStart, //(in) If true, current game will be loaded to beginning
// of room in saved game. If false, (default)
// current game will be loaded to the exact room
// state specified in the saved game.
const bool bNoSaves) //whether DB saves should be prevented [default=false]
//
//Returns:
//Pointer to loaded current game which caller must delete, or NULL saved game did not exist
//or loading failures occurred.
{
CCurrentGame *pCCG = new CCurrentGame();
if (pCCG)
{
if (!pCCG->LoadFromSavedGame(dwSavedGameID, CueEvents, bRestoreAtRoomStart, bNoSaves))
{
delete pCCG;
pCCG=NULL;
}
}
return pCCG;
}
//*****************************************************************************
CCurrentGame *CDb::GetNewCurrentGame(
//Gets a current game object from a hold. The current game will be set to the
//starting settings for the hold.
//
//Params:
const UINT dwHoldID, //(in) Indicates hold to load from.
CCueEvents &CueEvents) //(out) Cue events generated by swordsman's
// first step into the room.
// const UINT dwAutoSaveOptions) //(in) game save options [default=ASO_DEFAULT]
//
//Returns:
//Pointer to loaded current game which caller must delete, or NULL if hold did
//not exist or loading failures occurred.
{
CCurrentGame *pCCG = new CCurrentGame();
if (pCCG)
{
// pCCG->SetAutoSaveOptions(dwAutoSaveOptions);
if (!pCCG->LoadFromHold(dwHoldID, CueEvents))
{
delete pCCG;
pCCG=NULL;
}
}
return pCCG;
}
//*****************************************************************************
CCurrentGame *CDb::GetNewTestGame(
//Gets a current game object from a room.
//The current game will be set to the starting settings for the room.
//Used for testing.
//
//Params:
const UINT dwRoomID, //(in) Indicates room to load from.
CCueEvents &CueEvents, //(out) Cue events generated by swordsman's
// first step into the room.
const UINT wX, const UINT wY, const UINT wO, //(in) Starting position
const PlayerStats& st, //(in) starting stats
const bool bNoSaves) //whether DB saves should be prevented [default=false]
//
//Returns:
//Pointer to loaded current game which caller must delete, or NULL if room did
//not exist or loading failures occurred.
{
CCurrentGame *pCCG = new CCurrentGame();
if (pCCG)
{
pCCG->pPlayer->st = st;
if (!pCCG->LoadFromRoom(dwRoomID, CueEvents, wX, wY, wO, defaultPlayerType(), false, bNoSaves))
{
delete pCCG;
pCCG=NULL;
}
}
//Activate any custom equipment
const PlayerStats& stats = pCCG->pPlayer->st;
if (bIsCustomEquipment(stats.sword)) {
CCueEvents ignore;
pCCG->activateCustomEquipment(ignore, ScriptFlag::Weapon, stats.sword);
}
if (bIsCustomEquipment(stats.shield)) {
CCueEvents ignore;
pCCG->activateCustomEquipment(ignore, ScriptFlag::Armor, stats.shield);
}
if (bIsCustomEquipment(stats.accessory)) {
CCueEvents ignore;
pCCG->activateCustomEquipment(ignore, ScriptFlag::Accessory, stats.accessory);
}
return pCCG;
}
//*******************************************************************************
bool CDb::ValidateSavedGame(
//Validates a saved game for correctness (play sequence is still valid) and stats.
//
//Returns: whether saved game exists, is correct and valid
//
//Params:
const UINT savedGameID,
std::vector<ScoreCheckpointData>& scoresData) //(out) scorepoints when last checkpoint is encountered
{
CDbSavedGame *pSavedGame = this->SavedGames.GetByID(savedGameID);
if (!pSavedGame)
return false; //doesn't exist
CDbSavedGameMove *pSavedGameMoves = NULL;
if (!pSavedGame->ExploredRooms.empty())
{
//If the player has been to other rooms, a move sequence must exist
//in order to validate the entire saved game playthrough.
pSavedGameMoves = this->SavedGameMoves.GetByID(savedGameID);
if (!pSavedGameMoves) //doesn't exist -- can't validate
{
delete pSavedGame;
return false;
}
} else {
pSavedGameMoves = this->SavedGameMoves.GetNew();
}
//Include the moves in the current room as well.
pSavedGameMoves->Append(pSavedGame->Commands, true); //mark end-of-sequence
const CStretchyBuffer& moveSequence = pSavedGameMoves->getMoves();
const UINT holdID = this->SavedGames.GetHoldIDofSavedGame(savedGameID);
const bool bGood = ValidateMoveSequence(holdID, moveSequence, scoresData);
delete pSavedGameMoves;
delete pSavedGame;
return bGood;
}
//*******************************************************************************
bool CDb::ValidateMoveSequence(
//Validates a saved game for correctness (play sequence is still valid) and stats.
//
//Returns: whether hold exists and move sequence is correct and valid
//
//Params:
const UINT holdID,
const CStretchyBuffer& moves, //full move sequence
std::vector<ScoreCheckpointData>& scoresData) //(out) scorepoints when last checkpoint is encountered
{
const UINT bufSize = moves.Size();
//Start from the beginning of the game.
CCueEvents CueEvents;
CCurrentGame *pGame = GetNewCurrentGame(holdID, CueEvents);
if (!pGame)
return false; //can't start a game for whatever reason -- can't validate
//playback only
pGame->bNoSaves = pGame->bValidatingPlayback = true;
pGame->FreezeCommands();
//Play through all moves in the game session.
bool bGood = true;
UINT wX, wY;
UINT index = 0;
while (index < bufSize)
{
const UINT command = moves.GetUINTat(index); //get next command
//Ensure no play-testing "cheat" commands are used in scoreable play-throughs.
if (command == CMD_SETVAR)
{
bGood = false;
break;
}
if (!ValidateMoveSequenceCheckCueEvents(CueEvents, pGame, command, bGood, scoresData))
break;
if (CueEvents.HasAnyOccurred(IDCOUNT(CIDA_PlayerLeftRoom), CIDA_PlayerLeftRoom))
{
//Load new level, if needed.
if (CueEvents.HasOccurred(CID_ExitLevelPending))
{
const CCoord *pExitInfo =
DYN_CAST(const CCoord*, const CAttachableObject*,
CueEvents.GetFirstPrivateData(CID_ExitLevelPending));
const UINT dwEntranceID = pExitInfo->wX;
CueEvents.Clear();
pGame->UnfreezeCommands(); //must be done before loading
pGame->LoadFromLevelEntrance(holdID, dwEntranceID, CueEvents);
pGame->FreezeCommands();
//The "continue" below lets us validate these new cue events
//before playing the next command.
} else if (CueEvents.HasOccurred(CID_ExitToWorldMapPending)) {
const CAttachableWrapper<UINT>* pInfo = DYN_CAST(const CAttachableWrapper<UINT>*, const CAttachableObject*,
CueEvents.GetFirstPrivateData(CID_ExitToWorldMapPending));
const UINT dwEntranceID = pInfo->data;
CueEvents.Clear();
pGame->UnfreezeCommands(); //must be done before loading
pGame->LoadFromWorldMap(dwEntranceID);
pGame->FreezeCommands();
} else {
CueEvents.Clear(); //we're done checking these events
}
continue; //advance to next command
}
//No end room marker should exist without room exit cue events.
if (command == CMD_EXITROOM)
{
bGood = false; //validation failed
break;
}
//If game is still not ready to process a play command at this point,
//something bad happened on the last turn causing play to halt.
if (!pGame->bIsGameActive)
{
bGood = false; //validation failed
break;
}
if (command == CMD_ENDMOVE)
break; //done validating
//Now that we've checked for everything, execute the next command.
//Complex command parameters.
if (bIsComplexCommand(command))
{
wX = moves.GetUINTat(index);
wY = moves.GetUINTat(index);
} else {
wX = wY = UINT(-1);
}
if (command == CMD_WORLD_MAP) {
CueEvents.Clear();
ExitType exitType = (ExitType)wY;
if (pGame->IsValidWorldMapTransfer(wX, exitType)) {
pGame->UnfreezeCommands(); //must be done before loading
if (exitType == ET_Entrance) {
pGame->LoadFromLevelEntrance(holdID, wX, CueEvents);
} else {
pGame->LoadFromWorldMap(wX);
}
pGame->FreezeCommands();
} else {
//Illegal world map transfer
bGood = false;
break;
}
} else {
//Execute this command.
pGame->ProcessCommand(command, CueEvents, wX, wY);
}
}
//Resolve any combat initiated on final move
while (bGood && pGame->InCombat())
{
CueEvents.Clear();
pGame->ProcessCommand(CMD_ADVANCE_COMBAT, CueEvents);
if (!ValidateMoveSequenceCheckCueEvents(CueEvents, pGame, CMD_ADVANCE_COMBAT, bGood, scoresData))
break;
}
delete pGame;
return bGood;
}
//Returns: true if play continues, false if ended
bool CDb::ValidateMoveSequenceCheckCueEvents(
CCueEvents& CueEvents, CCurrentGame* pGame, const UINT command,
bool& bGood, std::vector<ScoreCheckpointData>& scoresData) //(out)
const
{
const bool bPlayerDied = CueEvents.HasAnyOccurred(IDCOUNT(CIDA_PlayerDied), CIDA_PlayerDied);
if (bPlayerDied)
{
bGood = false; //validation failed
return false;
}
//Check for a score checkpoint.
if (CueEvents.HasOccurred(CID_ScoreCheckpoint))
{
//Clear any data from previous turns
scoresData.clear();
for (const CAttachableObject* pObj = CueEvents.GetFirstPrivateData(CID_ScoreCheckpoint);
pObj != NULL; pObj = CueEvents.GetNextPrivateData()) {
//Output name of score checkpoint and player stats at that checkpoint.
const ScoreCheckpointData *pScoreData = DYN_CAST(const ScoreCheckpointData*, const CAttachableObject*,
pObj);
ASSERT(pScoreData);
scoresData.push_back(*pScoreData);
}
}
//Was room exited?
const bool bDidPlayerLeaveRoom = CueEvents.HasAnyOccurred(
IDCOUNT(CIDA_PlayerLeftRoom), CIDA_PlayerLeftRoom);
if (bDidPlayerLeaveRoom)
{
if (CueEvents.HasOccurred(CID_WinGame))
return false; //count as valid play sequence to end of game (even if more play moves exist)
//Whenever the player leaves the room,
//the current command should be an end room marker.
if (command != CMD_EXITROOM)
{
bGood = false; //validation failed
return false;
}
}
return true;
}
//*****************************************************************************
UINT CDb::LookupRowByPrimaryKey(
//Looks up a row in a view by its primary key ID property.
//Assumes primary key property values are monotonically increasing.
//
//Params:
const UINT dwID, //(in) Primary key value to match.
const VIEWTYPE eViewType, //(in) View/table to scan
c4_View &View) //(out) specific view containing this ID
//
//Returns:
//Row index in the outputted view or ROW_NO_MATCH.
{
//Determine prop for primary key based on table.
c4_IntProp *pPropID = GetPrimaryKeyProp(eViewType); //Reference to the primary key field.
//Determine the rows available for search based on the count maintained
//in the respective view interface of the global DB object.
UINT dwRowCount;
switch (eViewType)
{
case V_Data: dwRowCount = g_pTheDB->Data.GetViewSize(dwID); break;
case V_Demos: dwRowCount = g_pTheDB->Demos.GetViewSize(dwID); break;
case V_Holds: dwRowCount = g_pTheDB->Holds.GetViewSize(dwID); break;
case V_Levels: dwRowCount = g_pTheDB->Levels.GetViewSize(dwID); break;
case V_MessageTexts: dwRowCount = GetView(eViewType, dwID).GetSize(); break;
case V_Players: dwRowCount = g_pTheDB->Players.GetViewSize(dwID); break;
case V_Rooms: dwRowCount = g_pTheDB->Rooms.GetViewSize(dwID); break;
case V_SavedGames: dwRowCount = g_pTheDB->SavedGames.GetViewSize(dwID); break;
case V_SavedGameMoves: dwRowCount = g_pTheDB->SavedGameMoves.GetViewSize(dwID); break;
case V_Speech: dwRowCount = g_pTheDB->Speech.GetViewSize(dwID); break;
case V_LocalHighScores: dwRowCount = g_pTheDB->HighScores.GetViewSize(dwID); break;
default:
ASSERT(!"CDb::LookupRowByPrimaryKey: Unexpected property type.");
return ROW_NO_MATCH;
}
return CDbBase::LookupRowByPrimaryKey(dwID, eViewType, pPropID, dwRowCount, View);
}
//*****************************************************************************
void CDb::ResetMembership()
//Reset all table memberships.
{
this->Data.ResetMembership();
this->Demos.ResetMembership();
this->Holds.ResetMembership();
this->Levels.ResetMembership();
this->Players.ResetMembership();
this->Rooms.ResetMembership();
this->SavedGames.ResetMembership();
this->Speech.ResetMembership();
}
//*****************************************************************************
void CDb::SetHoldID(const UINT dwNewHoldID)
//Set active hold ID.
{
dwCurrentHoldID = dwNewHoldID;
this->SavedGames.FilterByHold(dwNewHoldID);
}
//*****************************************************************************
void CDb::SetPlayerID(const UINT dwNewPlayerID, const bool bCaravelLogin) //[default=true]
//Set active player and filter saved games for them.
{
if (CDb::dwCurrentPlayerID == dwNewPlayerID)
return; //nothing to change
//Resolve any transactions in progress before changing the player.
if (g_pTheNet && bCaravelLogin)
g_pTheNet->ClearActiveAction();
CDb::dwCurrentPlayerID = dwNewPlayerID;
//Get hold list according to settings for new player.
if (g_pTheNet && bCaravelLogin)
g_pTheNet->DownloadHoldList();
this->SavedGames.FilterByPlayer(dwNewPlayerID);
}
//*****************************************************************************
//Acceleration structure -- indexed for fast hierarchical ID set lookup.
struct HoldOwnership {
CIDSet levelIDs, dataIDs;
};
typedef map<UINT,HoldOwnership> holdMap;
typedef CIDSet LevelOwnership;
typedef map<UINT,LevelOwnership> levelMap;
struct RoomOwnership {
CIDSet demoIDs, savedGameIDs;
};
typedef map<UINT,RoomOwnership> roomMap;
typedef map<UINT,UINT> idMap;
holdMap holdIndex; //hold -> levels + data
levelMap levelIndex; //level -> rooms
roomMap roomIndex; //room -> saved games + demos
idMap demoIndex; //demo -> saved game
idMap demosHoldIndex; //demo -> hold
//*****************************************************************************
void CDb::addDataToHold(const UINT dataID, const UINT holdID)
//Adds dataID to hold's data set.
{
CDbBase::DirtyData();
if (!holdID)
return; //no hold to attach this data to
holdMap::iterator holdIter = holdIndex.find(holdID);
if (holdIter == holdIndex.end())
{
ASSERT(!"Data exists in DB with dangling hold ID");
} else {
//Add the datum to its parent hold's level ID set.
holdIter->second.dataIDs += dataID;
}
}
//*****************************************************************************
void CDb::addDemo(const UINT demoID, const UINT savedGameID)
//Adds demo to index.
{
CDbBase::DirtySave();
ASSERT(demoIndex.find(demoID) == demoIndex.end());
ASSERT(demosHoldIndex.find(demoID) == demosHoldIndex.end());
ASSERT(savedGameID); //each demo must be attached to a saved game
if (!savedGameID)
return; //robust to bad data
//Link demo to its saved game, room and hold.
demoIndex[demoID] = savedGameID;
const UINT demosRoomID = CDbSavedGames::GetRoomIDofSavedGame(savedGameID);
ASSERT(demosRoomID);
const UINT demosHoldID = CDbRooms::GetHoldIDForRoom(demosRoomID);
ASSERT(demosHoldID);
roomMap::iterator room = roomIndex.find(demosRoomID);
ASSERT(room != roomIndex.end());
room->second.demoIDs += demoID;
demosHoldIndex[demoID] = demosHoldID;
}
//*****************************************************************************
void CDb::addHold(const UINT holdID)
//Adds hold to index.
{
CDbBase::DirtyHold();
ASSERT(holdIndex.find(holdID) == holdIndex.end());
HoldOwnership IDs;
holdIndex[holdID] = IDs;
}
//*****************************************************************************
void CDb::addLevelToHold(const UINT levelID, const UINT holdID)
//Adds level to index.
{
CDbBase::DirtyHold();
ASSERT(levelIndex.find(levelID) == levelIndex.end());
LevelOwnership IDs;
levelIndex[levelID] = IDs;
//Add level to hold.
holdMap::iterator hold = holdIndex.find(holdID);
ASSERT(hold != holdIndex.end());
hold->second.levelIDs += levelID;
}
//*****************************************************************************
void CDb::addRoomToLevel(const UINT roomID, const UINT levelID)
//Adds room to index.
{
CDbBase::DirtyHold();
ASSERT(roomIndex.find(roomID) == roomIndex.end());
RoomOwnership roomIDs;
roomIndex[roomID] = roomIDs;
//Add room to level.
levelMap::iterator level = levelIndex.find(levelID);
ASSERT(level != levelIndex.end());
level->second += roomID;
}
//*****************************************************************************
void CDb::addSavedGameToRoom(const UINT savedGameID, const UINT roomID)
//Adds saved game to index.
{
CDbBase::DirtySave();
if (roomID) //some special saved game records are not associated with a room
{
roomMap::iterator room = roomIndex.find(roomID);
ASSERT(room != roomIndex.end());
room->second.savedGameIDs += savedGameID;
}
}
//*****************************************************************************
void CDb::deleteData(const UINT dataID)
//Remove data ID from any hold that owns it.
{
CDbBase::DirtyData();
for (holdMap::iterator hold = holdIndex.begin(); hold != holdIndex.end(); ++hold)
hold->second.dataIDs -= dataID;
}
//*****************************************************************************
void CDb::deleteDemo(const UINT demoID)
//Remove demo from hold and room.
{
CDbBase::DirtySave();
//Find demo's room to remove demoID from room index.
const UINT savedGameID = getSavedGameOfDemo(demoID);
const UINT roomID = CDbSavedGames::GetRoomIDofSavedGame(savedGameID);
roomMap::iterator room = roomIndex.find(roomID);
if (room != roomIndex.end())
room->second.demoIDs -= demoID;
//Now remove demo mappings.
demoIndex.erase(demoID);
demosHoldIndex.erase(demoID);
}
//*****************************************************************************
void CDb::deleteHold(const UINT holdID)
//Remove hold from index.
{
CDbBase::DirtyHold();
holdMap::iterator holdIter = holdIndex.find(holdID);
if (holdIter != holdIndex.end())
holdIndex.erase(holdIter);
}
//*****************************************************************************
void CDb::deleteLevel(const UINT levelID)
//Remove level from index.
{
CDbBase::DirtyHold();
levelMap::iterator levelIter = levelIndex.find(levelID);
if (levelIter != levelIndex.end())
{
levelIndex.erase(levelIter);
//Delete level from hold that owns it.
const UINT holdID = CDbLevels::GetHoldIDForLevel(levelID);
if (holdID)
{
holdMap::iterator hold = holdIndex.find(holdID);
ASSERT(hold != holdIndex.end());
hold->second.levelIDs -= levelID;
}
}
}
//*****************************************************************************
void CDb::deleteRoom(const UINT roomID)
//Remove room from index.
{
CDbBase::DirtyHold();
roomMap::iterator roomIter = roomIndex.find(roomID);
if (roomIter != roomIndex.end())
{
roomIndex.erase(roomIter);
//Delete room from level that owns it.
const UINT levelID = CDbRooms::GetLevelIDForRoom(roomID);
if (levelID)
{
levelMap::iterator level = levelIndex.find(levelID);
ASSERT(level != levelIndex.end());
level->second -= roomID;
}
}
}
//*****************************************************************************
void CDb::deleteSavedGame(const UINT savedGameID)
//Remove saved game from room.
{
CDbBase::DirtySave();
//Find saved game's room to remove savedGameID from room index.
const UINT roomID = CDbSavedGames::GetRoomIDofSavedGame(savedGameID);
roomMap::iterator room = roomIndex.find(roomID);
if (room != roomIndex.end())
room->second.savedGameIDs -= savedGameID;
}
//*****************************************************************************
CIDSet CDb::getDataInHold(const UINT holdID)
//Returns: set of dataIDs belonging to this hold, or empty set if hold doesn't exist
{
holdMap::iterator holdIter = holdIndex.find(holdID);
if (holdIter == holdIndex.end())
return CIDSet(); //no entry
return holdIter->second.dataIDs;
}
//*****************************************************************************
CIDSet CDb::getDemosInHold(const UINT holdID)
//Returns: set of demos in hold
{
CIDSet demosInHold, levelsInHold = CDb::getLevelsInHold(holdID);
for (CIDSet::const_iterator level = levelsInHold.begin();
level != levelsInHold.end(); ++level)
demosInHold += CDb::getDemosInLevel(*level);
return demosInHold;
}
//*****************************************************************************
CIDSet CDb::getDemosInLevel(const UINT levelID)
//Returns: set of demos in level
{
CIDSet demosInLevel, roomsInLevel = CDb::getRoomsInLevel(levelID);
for (CIDSet::const_iterator room = roomsInLevel.begin();
room != roomsInLevel.end(); ++room)
demosInLevel += CDb::getDemosInRoom(*room);
return demosInLevel;
}
//*****************************************************************************
CIDSet CDb::getDemosInRoom(const UINT roomID)
//Returns: set of demos in room
{
roomMap::iterator roomIter = roomIndex.find(roomID);
if (roomIter == roomIndex.end())
return CIDSet(); //no entry
return roomIter->second.demoIDs;
}
//*****************************************************************************
UINT CDb::getHoldOfDemo(const UINT demoID)
//Returns: holdID of the hold that this demo is in
{
idMap::iterator demoIter = demosHoldIndex.find(demoID);
return demoIter != demosHoldIndex.end() ? demoIter->second : 0;
}
//*****************************************************************************
CIDSet CDb::getLevelsInHold(const UINT holdID)
//Returns: set of levelIDs belonging to this hold, or empty set if hold doesn't exist
{
holdMap::iterator holdIter = holdIndex.find(holdID);
if (holdIter == holdIndex.end())
return CIDSet(); //no entry
return holdIter->second.levelIDs;
}
//*****************************************************************************
CIDSet CDb::getLocalHighscoresForHold(const UINT holdID)
//Returns: set of local highScoreIDs belong to this hold
{
CIDSet IDs;
CDb db;
db.HighScores.FilterByHold(holdID);
db.HighScores.GetIDs(IDs);
return IDs;
}
//*****************************************************************************
CIDSet CDb::getRoomsInHold(const UINT holdID)
//Returns: set of roomIDs belonging to this hold, or empty set if level doesn't exist
{
CIDSet roomsInHold;
const CIDSet levelsInHold = CDb::getLevelsInHold(holdID);
for (CIDSet::const_iterator iter = levelsInHold.begin(); iter != levelsInHold.end(); ++iter)
roomsInHold += CDb::getRoomsInLevel(*iter);
return roomsInHold;
}
//*****************************************************************************
CIDSet CDb::getRoomsInLevel(const UINT levelID)
//Returns: set of roomIDs belonging to this level, or empty set if level doesn't exist
{
levelMap::iterator levelIter = levelIndex.find(levelID);
if (levelIter == levelIndex.end())
return CIDSet(); //no entry
return levelIter->second;
}
//*****************************************************************************
UINT CDb::getSavedGameOfDemo(const UINT demoID)
//Returns: savedGameID that this demo is tied to
{
idMap::iterator demoIter = demoIndex.find(demoID);
return demoIter != demoIndex.end() ? demoIter->second : 0;
}
//*****************************************************************************
CIDSet CDb::getSavedGamesInHold(const UINT holdID)
{
CIDSet savedGamesInHold, levelsInHold = CDb::getLevelsInHold(holdID);
for (CIDSet::const_iterator level = levelsInHold.begin();
level != levelsInHold.end(); ++level)
savedGamesInHold += CDb::getSavedGamesInLevel(*level);
return savedGamesInHold;
}
//*****************************************************************************
CIDSet CDb::getSavedGamesInLevel(const UINT levelID)
{
CIDSet savedGamesInLevel, roomsInLevel = CDb::getRoomsInLevel(levelID);
for (CIDSet::const_iterator room = roomsInLevel.begin();
room != roomsInLevel.end(); ++room)
savedGamesInLevel += getSavedGamesInRoom(*room);
return savedGamesInLevel;
}
//*****************************************************************************
CIDSet CDb::getSavedGamesInRoom(const UINT roomID)
//Returns: set of saved games in room
{
roomMap::iterator roomIter = roomIndex.find(roomID);
if (roomIter == roomIndex.end())
return CIDSet(); //no entry
return roomIter->second.savedGameIDs;
}
//*****************************************************************************
bool CDb::holdExists(const UINT holdID)
{
holdMap::iterator holdIter = holdIndex.find(holdID);
return holdIter != holdIndex.end();
}
//*****************************************************************************
bool CDb::levelExists(const UINT levelID)
{
levelMap::iterator levelIter = levelIndex.find(levelID);
return levelIter != levelIndex.end();
}
//*****************************************************************************
void CDb::moveData(const UINT dataID, const UINT fromHoldID, const UINT toHoldID)
//Updates data-hold indexing when data object might have changed holds.
{
CDbBase::DirtyData();
if (fromHoldID == toHoldID)
return; //data object is in the same hold as before
//Remove data index from previous hold.
holdMap::iterator hold;
if (fromHoldID)
{
hold = holdIndex.find(fromHoldID);
ASSERT(hold != holdIndex.end());
hold->second.dataIDs -= dataID;
}
//Add data index to current hold.
if (toHoldID)
{
hold = holdIndex.find(toHoldID);
ASSERT(hold != holdIndex.end());
hold->second.dataIDs += dataID;
}
}
//*****************************************************************************
void CDb::moveRoom(const UINT roomID, const UINT fromLevelID, const UINT toLevelID)
//Updates room-level indexing when room might have changed levels.
{
CDbBase::DirtyHold();
if (fromLevelID == toLevelID)
return; //room is in the same level as before
//Remove room index from previous level.
levelMap::iterator level = levelIndex.find(fromLevelID);
ASSERT(level != levelIndex.end());
level->second -= roomID;
//Add room index to current level.
level = levelIndex.find(toLevelID);
ASSERT(level != levelIndex.end());
level->second += roomID;
}
//*****************************************************************************
void CDb::moveSavedGame(const UINT savedGameID, const UINT fromRoomID, const UINT toRoomID)
//Updates savedgame-room indexing when room might have changed levels.
{
CDbBase::DirtySave();
if (fromRoomID == toRoomID)
return; //saved game is in the same room as before
//Remove savedgame index from previous room.
if (fromRoomID) //some special saved game records are not associated with a room
{
roomMap::iterator room = roomIndex.find(fromRoomID);
ASSERT(room != roomIndex.end());
room->second.savedGameIDs -= savedGameID;
}
//Add saved game index to current room.
if (toRoomID)
{
roomMap::iterator room = roomIndex.find(toRoomID);
ASSERT(room != roomIndex.end());
room->second.savedGameIDs += savedGameID;
}
}
//*****************************************************************************
void CDb::resetIndex()
//Resets database ID hierarchy.
{
holdIndex.clear();