-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCObjectTracker.cpp
1327 lines (987 loc) · 34.9 KB
/
CObjectTracker.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
// CObjectTracker.cpp
//
// CObjectTracker class
// Copyright (c) 2018 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define LANGID_DESC_GALACTIC_MAP_CUSTOM CONSTLIT("core.mapDescCustom")
#define LANGID_DESC_GALACTIC_MAP_ABANDONED_CUSTOM CONSTLIT("core.mapDescAbandonedCustom")
const int MAX_ALLOC_GRANULARITY = 10000;
CObjectTracker::~CObjectTracker (void)
// CObjectTracker destructor
{
DeleteAll();
}
bool CObjectTracker::AccumulateEntries (TArray<SObjList *> &Table, const CObjectTrackerCriteria &Criteria, DWORD dwFlags, TArray<SObjEntry> *retResult) const
// AccumulateEntries
//
// Looks for objects that match the criteria and adds them to the result list.
{
int i;
int j;
for (i = 0; i < Table.GetCount(); i++)
{
SObjList *pList = Table[i];
// If we don't match, then continue
if (!pList->pType->MatchesCriteria(Criteria.GetTypeCriteria()))
continue;
// If all we care about is whether we have any entries, then we're done.
if (retResult == NULL)
return true;
// Otherwise, add all objects to the results
retResult->GrowToFit(pList->Objects.GetCount());
for (j = 0; j < pList->Objects.GetCount(); j++)
{
const SObjBasics &Basics = pList->Objects[j];
// If this object does not match, skip
if (Criteria.SelectsActiveOnly() && Basics.fInactive && !pList->pType->IsVirtual())
continue;
if (Criteria.SelectsKilledOnly() && (!Basics.fInactive || pList->pType->IsVirtual()))
continue;
// Otherwise, add
AccumulateEntry(*pList, pList->Objects.GetKey(j), Basics, dwFlags, *retResult);
}
}
// Done
return (retResult && retResult->GetCount() > 0);
}
void CObjectTracker::AccumulateEntry (const SObjList &ObjList, DWORD dwObjID, const SObjBasics &ObjData, DWORD dwFlags, TArray<SObjEntry> &Results) const
// AccumulateEntry
//
// Adds the given entry to the list
{
SObjEntry *pEntry = Results.Insert();
pEntry->pNode = ObjList.pNode;
pEntry->pType = ObjList.pType;
pEntry->dwObjID = dwObjID;
pEntry->fKnown = ObjData.fKnown;
pEntry->fShowDestroyed = ObjData.fShowDestroyed;
pEntry->fShowInMap = ObjData.fShowInMap;
pEntry->fFriendly = ObjData.fFriendly;
pEntry->fEnemy = ObjData.fEnemy;
pEntry->fInactive = ObjData.fInactive;
if (ObjData.pExtra)
{
pEntry->sName = ObjData.pExtra->sName;
pEntry->dwNameFlags = ObjData.pExtra->dwNameFlags;
pEntry->ImageSel = ObjData.pExtra->ImageSel;
pEntry->sNotes = ObjData.pExtra->sNotes;
}
else
pEntry->sName = ObjList.pType->GetNamePattern(0, &pEntry->dwNameFlags);
// If we don't need notes, then don't initialize them (this can save
// execution time).
if (!(dwFlags & FLAG_ACCUMULATE_NOTES))
;
// If we've got custom notes stored with the object, then use those.
else if (!pEntry->sNotes.IsBlank())
;
// Otherwise, we let the type compose it
else
{
CDesignType::SMapDescriptionCtx Ctx;
Ctx.bShowDestroyed = pEntry->fShowDestroyed;
Ctx.bEnemy = pEntry->fEnemy;
Ctx.bFriend = pEntry->fFriendly;
pEntry->sNotes = pEntry->pType->GetMapDescription(Ctx);
if (pEntry->sNotes.IsBlank())
pEntry->sNotes = CONSTLIT("No services available");
}
}
void CObjectTracker::Delete (CSpaceObject *pObj)
// Delete
//
// Delete an object
{
SObjList *pList = GetList(pObj);
if (pList == NULL)
return;
DWORD dwID = pObj->GetID();
pList->Objects.DeleteAt(dwID);
}
void CObjectTracker::DeleteAll (void)
// DeleteAll
//
// Delete all entries
{
int i;
for (i = 0; i < m_AllLists.GetCount(); i++)
delete m_AllLists[i];
m_AllLists.DeleteAll();
m_ByNode.DeleteAll();
}
bool CObjectTracker::Find (const CString &sNodeID, const CObjectTrackerCriteria &Criteria, TArray<SObjEntry> *retResult)
// Find
//
// Find a set of objects matching the given criteria.
{
if (retResult)
retResult->DeleteAll();
// If necessary, refresh current system so we get the latests data.
CSystem *pSystem = g_pUniverse->GetCurrentSystem();
if (Criteria.NeedsRefresh()
&& pSystem
&& (sNodeID.IsBlank()
|| (pSystem->GetTopology() && strEquals(sNodeID, pSystem->GetTopology()->GetID()))))
{
Refresh(pSystem);
}
// If no node ID, then we look through all nodes
if (sNodeID.IsBlank())
return AccumulateEntries(m_AllLists, Criteria, 0, retResult);
// Otherwise, check the specific node
else
{
// Look for the node ID.
SNodeData *pNodeData = m_ByNode.GetAt(sNodeID);
if (pNodeData == NULL)
return false;
// Accumulate entries for this table
return AccumulateEntries(pNodeData->ObjLists, Criteria, 0, retResult);
}
}
bool CObjectTracker::Find (CTopologyNode *pNode, CSpaceObject *pObj, SObjBasics **retpObjData) const
// Find
//
// Returns a pointer to the object data for the given object.
{
SObjList *pList = GetList(pObj);
if (pList == NULL)
return false;
SObjBasics *pObjData = pList->Objects.GetAt(pObj->GetID());
if (pObjData == NULL)
return false;
if (retpObjData)
*retpObjData = pObjData;
return true;
}
bool CObjectTracker::Find (SNodeData *pNodeData, CSpaceObject *pObj, SObjBasics **retpObjData) const
// Find
//
// Returns a pointer to the object data for the given object.
{
int i;
// Look for the UNID. If found, return it.
CDesignType *pType = pObj->GetType();
SObjList *pList = NULL;
for (i = 0; i < pNodeData->ObjLists.GetCount(); i++)
{
if (pNodeData->ObjLists.GetAt(i)->pType == pType)
{
pList = pNodeData->ObjLists.GetAt(i);
break;
}
}
if (pList == NULL)
return false;
SObjBasics *pObjData = pList->Objects.GetAt(pObj->GetID());
if (pObjData == NULL)
return false;
if (retpObjData)
*retpObjData = pObjData;
return true;
}
bool CObjectTracker::GetCustomDesc (CSpaceObject *pObj, const SObjBasics &ObjData, CString *retsDesc) const
// GetCustomDesc
//
// Returns a custom description for object services.
{
CDesignType *pType = pObj->GetType();
if (pType == NULL || !pType->HasCustomMapDescLanguage())
return false;
// Compose a data block which has the default description.
CCodeChain &CC = g_pUniverse->GetCC();
ICCItemPtr pData = ICCItemPtr(CC.CreateSymbolTable());
// Add the default trade description, in case we need it.
CTradingDesc *pTrade;
CString sTradeDesc;
if (!ObjData.fShowDestroyed
&& (pTrade = pType->GetTradingDesc())
&& pTrade->ComposeDescription(&sTradeDesc))
{
pData->SetStringAt(CC, CONSTLIT("tradeDesc"), sTradeDesc);
}
// Translate
return pObj->Translate((ObjData.fShowDestroyed ? LANGID_DESC_GALACTIC_MAP_ABANDONED_CUSTOM : LANGID_DESC_GALACTIC_MAP_CUSTOM), pData, retsDesc);
}
void CObjectTracker::GetGalacticMapObjects (const CTopologyNode *pNode, TArray<SObjEntry> &Results) const
// GetGalacticMapObjects
//
// Returns the list of objects in this node that should be shown on the
// galactic map.
{
int i, j;
// Initialize
Results.DeleteAll();
// Look in the index of nodes
SNodeData *pNodeData = m_ByNode.GetAt(pNode->GetID());
if (pNodeData == NULL)
return;
// Loop over all objects
for (i = 0; i < pNodeData->ObjLists.GetCount(); i++)
{
const SObjList *pList = pNodeData->ObjLists.GetAt(i);
if (pList == NULL)
continue;
// Add all objects
for (j = 0; j < pList->Objects.GetCount(); j++)
{
const SObjBasics &ObjData = pList->Objects[j];
// We only care about known, significant objects
if (!ObjData.fKnown || !ObjData.fShowInMap)
continue;
// Add the object to the result
AccumulateEntry(*pList, pList->Objects.GetKey(j), ObjData, FLAG_ACCUMULATE_NOTES, Results);
}
}
}
CObjectTracker::SObjList *CObjectTracker::GetList (CSpaceObject *pObj) const
// GetList
//
// Returns the list that corresponds to the given object's UNID and NodeID.
{
CSystem *pSystem = pObj->GetSystem();
if (pSystem == NULL)
return NULL;
CTopologyNode *pNode = pSystem->GetTopology();
if (pNode == NULL)
return NULL;
CDesignType *pType = pObj->GetType();
if (pType == NULL)
return NULL;
// Done
return GetList(pNode, pType);
}
CObjectTracker::SObjList *CObjectTracker::GetList (CTopologyNode *pNode, CDesignType *pType) const
// GetList
//
// Returns the list that corresponds to the given object's UNID and NodeID.
{
int i;
// Look in the index of nodes
SNodeData *pNodeData = m_ByNode.GetAt(pNode->GetID());
if (pNodeData == NULL)
return NULL;
// Look for the UNID. If found, return it.
for (i = 0; i < pNodeData->ObjLists.GetCount(); i++)
{
SObjList *pList = pNodeData->ObjLists.GetAt(i);
if (pList->pType == pType)
return pList;
}
// Otherwise, not found
return NULL;
}
void CObjectTracker::GetSystemBackgroundObjects (const CTopologyNode *pNode, TSortMap<Metric, SBackgroundObjEntry> &Results) const
// GetSystemObjects
//
// Returns a list of stars, worlds, and asteroids, sorted by Y-coordinate.
{
int i, j;
Results.DeleteAll();
Results.GrowToFit(1000);
SNodeData *pNodeData = m_ByNode.GetAt(pNode->GetID());
if (pNodeData == NULL)
return;
for (i = 0; i < pNodeData->ObjLists.GetCount(); i++)
{
CStationType *pType = CStationType::AsType(pNodeData->ObjLists[i]->pType);
if (pType == NULL)
continue;
if (pType->GetScale() != scaleStar && pType->GetScale() != scaleWorld)
continue;
for (j = 0; j < pNodeData->ObjLists[i]->Objects.GetCount(); j++)
{
SObjBasics *pObjData = &pNodeData->ObjLists[i]->Objects[j];
SBackgroundObjEntry *pSysObj = Results.Insert(pObjData->vPos.GetY());
pSysObj->pType = pType;
pSysObj->vPos = pObjData->vPos;
pSysObj->pImageSel = (pObjData->pExtra ? &pObjData->pExtra->ImageSel : NULL);
}
}
}
void CObjectTracker::GetSystemStarObjects (const CTopologyNode *pNode, TArray<SBackgroundObjEntry> &Results) const
// GetSystemStarObjects
//
// Returns a list of stars
{
int i, j;
Results.DeleteAll();
SNodeData *pNodeData = m_ByNode.GetAt(pNode->GetID());
if (pNodeData == NULL)
return;
for (i = 0; i < pNodeData->ObjLists.GetCount(); i++)
{
CStationType *pType = CStationType::AsType(pNodeData->ObjLists[i]->pType);
if (pType == NULL)
continue;
if (pType->GetScale() != scaleStar)
continue;
for (j = 0; j < pNodeData->ObjLists[i]->Objects.GetCount(); j++)
{
SObjBasics *pObjData = &pNodeData->ObjLists[i]->Objects[j];
SBackgroundObjEntry *pSysObj = Results.Insert();
pSysObj->pType = pType;
pSysObj->vPos = pObjData->vPos;
pSysObj->pImageSel = (pObjData->pExtra ? &pObjData->pExtra->ImageSel : NULL);
}
}
}
const TArray<COrbit> &CObjectTracker::GetSystemOrbits (const CTopologyNode *pNode) const
// GetSystemOrbits
//
// Returns a list of orbits for the system.
{
SNodeData *pNodeData = m_ByNode.GetAt(pNode->GetID());
ASSERT(pNodeData);
return pNodeData->Orbits;
}
void CObjectTracker::GetTradingObjects (const CTopologyNode *pNode, TArray<SObjEntry> &Results) const
// GetTradingObjects
//
// Returns the list of objects in this node that have a trade descriptor and
// that are friendly to the player.
{
int i, j;
// Initialize
Results.DeleteAll();
// Look in the index of nodes
SNodeData *pNodeData = m_ByNode.GetAt(pNode->GetID());
if (pNodeData == NULL)
return;
// Loop over all objects
for (i = 0; i < pNodeData->ObjLists.GetCount(); i++)
{
const SObjList *pList = pNodeData->ObjLists.GetAt(i);
if (pList == NULL)
continue;
// Skip objects that don't have a trade descriptor in their design
// type.
if (pList->pType->GetTradingDesc() == NULL)
continue;
// Add all objects
for (j = 0; j < pList->Objects.GetCount(); j++)
{
const SObjBasics &ObjData = pList->Objects[j];
// We only care about friendly objects.
if (ObjData.fEnemy)
continue;
// Add the object to the result
AccumulateEntry(*pList, pList->Objects.GetKey(j), ObjData, 0, Results);
}
}
}
void CObjectTracker::Insert (CSpaceObject *pObj)
// Insert
//
// Insert a new object.
//
// NOTE: We rely on our caller to NOT insert the same object twice.
{
SObjList *pList = SetList(pObj);
if (pList == NULL)
return;
// We need the player ship to track disposition (but it's OK if we get back
// NULL).
CSpaceObject *pPlayer = g_pUniverse->GetPlayerShip();
// Insert and update
SObjBasics *pEntry = pList->Objects.Insert(pObj->GetID());
Refresh(pObj, pEntry, pPlayer);
}
void CObjectTracker::ReadFromStream (SUniverseLoadCtx &Ctx)
// ReadFromStream
//
// DWORD No. of nodes
//
// For each node:
// CString NodeID
// DWORD No. of lists
//
// For each list:
// DWORD Type UNID
// DWORD No. of objects
//
// For each object:
// DWORD ObjID
// CVector vPos
// DWORD Flags
//
// If extra data
// DWORD Name Flags
// CString Name
// CCompositeImageSelector
// CString Notes
//
// DWORD No. of orbits
// COrbit List of orbits
//
// DWORD No. of commands
//
// For each command:
// DWORD Type
// CDesignTypeCriteria Criteria
{
SLoadCtx SystemCtx(Ctx);
int i;
int j;
DWORD dwLoad;
DeleteAll();
// New version has a different format
if (SystemCtx.dwVersion >= 133)
{
int iNodeCount;
Ctx.pStream->Read((char *)&iNodeCount, sizeof(DWORD));
// Load all nodes
int iNode;
for (iNode = 0; iNode < iNodeCount; iNode++)
{
// Read the node ID
CString sNodeID;
sNodeID.ReadFromStream(Ctx.pStream);
CTopologyNode *pNode = g_pUniverse->FindTopologyNode(sNodeID);
if (pNode == NULL)
::kernelDebugLogPattern("Galactic Map: Unable to find nodeID: %s", sNodeID);
// Get the node data entry
SNodeData *pNodeData = (pNode ? m_ByNode.SetAt(pNode->GetID()) : NULL);
// Get the number of lists
int iListCount;
Ctx.pStream->Read((char *)&iListCount, sizeof(DWORD));
// Loop over every list
for (i = 0; i < iListCount; i++)
{
// Read the UNID for the object in this list
DWORD dwUNID;
Ctx.pStream->Read((char *)&dwUNID, sizeof(DWORD));
CDesignType *pType = g_pUniverse->FindDesignType(dwUNID);
if (pType == NULL)
::kernelDebugLogPattern("Galactic Map: Unable to find type: %08x", dwUNID);
// Number of objects
int iObjCount;
Ctx.pStream->Read((char *)&iObjCount, sizeof(DWORD));
// Get the appropriate table
SObjList *pList = (pNodeData && pType ? SetList(pNodeData, pNode, pType) : NULL);
if (pList)
pList->Objects.GrowToFit(iObjCount);
// Loop over all objects in this list
for (j = 0; j < iObjCount; j++)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
SObjBasics *pObjData = (pList ? pList->Objects.Insert(dwLoad) : NULL);
if (pObjData)
{
Ctx.pStream->Read((char *)&pObjData->vPos, sizeof(CVector));
// Flags
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
pObjData->fKnown = ((dwLoad & 0x00000002) ? true : false);
pObjData->fShowDestroyed = ((dwLoad & 0x00000004) ? true : false);
pObjData->fShowInMap = ((dwLoad & 0x00000008) ? true : false);
pObjData->fFriendly = ((dwLoad & 0x00000010) ? true : false);
pObjData->fEnemy = ((dwLoad & 0x00000020) ? true : false);
pObjData->fInactive = ((dwLoad & 0x00000040) ? true : false);
// Extra, if we've got it
if (dwLoad & 0x00000001)
{
SObjExtra &Extra = pObjData->SetExtra();
Ctx.pStream->Read((char *)&Extra.dwNameFlags, sizeof(DWORD));
Extra.sName.ReadFromStream(Ctx.pStream);
Extra.ImageSel.ReadFromStream(SystemCtx);
Extra.sNotes.ReadFromStream(Ctx.pStream);
}
}
else
{
CString sDummy;
CVector vDummy;
CCompositeImageSelector DummySel;
Ctx.pStream->Read((char *)&vDummy, sizeof(CVector));
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
if (dwLoad & 0x00000001)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
sDummy.ReadFromStream(Ctx.pStream);
DummySel.ReadFromStream(SystemCtx);
sDummy.ReadFromStream(Ctx.pStream);
}
}
}
}
// Read the orbit list
int iOrbitCount;
Ctx.pStream->Read((char *)&iOrbitCount, sizeof(DWORD));
if (pNodeData)
{
pNodeData->Orbits.InsertEmpty(iOrbitCount);
// Read orbits
for (i = 0; i < iOrbitCount; i++)
Ctx.pStream->Read((char *)&pNodeData->Orbits[i], sizeof(COrbit));
}
else
{
COrbit Dummy;
for (i = 0; i < iOrbitCount; i++)
Ctx.pStream->Read((char *)&Dummy, sizeof(COrbit));
}
// Read the command list
if (SystemCtx.dwVersion >= 135)
{
int iCommandCount;
Ctx.pStream->Read((char *)&iCommandCount, sizeof(DWORD));
if (pNodeData)
{
pNodeData->Commands.InsertEmpty(iCommandCount);
for (i = 0; i < iCommandCount; i++)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
pNodeData->Commands[i].iCmd = (EDelayedCommandTypes)dwLoad;
pNodeData->Commands[i].Criteria.ReadFromStream(SystemCtx);
}
}
else
{
CDesignTypeCriteria Dummy;
for (i = 0; i < iCommandCount; i++)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
Dummy.ReadFromStream(SystemCtx);
}
}
}
}
}
// Backwards compatibility
else
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
int iListCount = (int)dwLoad;
for (i = 0; i < iListCount; i++)
{
CString sNodeID;
sNodeID.ReadFromStream(Ctx.pStream);
CTopologyNode *pNode = g_pUniverse->FindTopologyNode(sNodeID);
DWORD dwUNID;
Ctx.pStream->Read((char *)&dwUNID, sizeof(DWORD));
CDesignType *pType = g_pUniverse->FindDesignType(dwUNID);
int iObjCount;
Ctx.pStream->Read((char *)&iObjCount, sizeof(DWORD));
// Get the appropriate table
SObjList *pList = (pNode && pType ? SetList(pNode, pType) : NULL);
if (pList)
pList->Objects.GrowToFit(iObjCount);
// The new version stores everything in one list
if (SystemCtx.dwVersion >= 132)
{
for (j = 0; j < iObjCount; j++)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
SObjBasics *pObjData = (pList ? pList->Objects.Insert(dwLoad) : NULL);
// Flags
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
pObjData->fKnown = ((dwLoad & 0x00000002) ? true : false);
pObjData->fShowDestroyed = ((dwLoad & 0x00000004) ? true : false);
pObjData->fShowInMap = ((dwLoad & 0x00000008) ? true : false);
pObjData->fFriendly = ((dwLoad & 0x00000010) ? true : false);
pObjData->fEnemy = ((dwLoad & 0x00000020) ? true : false);
// Extra, if we've got it
if (dwLoad & 0x00000001)
{
SObjExtra &Extra = pObjData->SetExtra();
Ctx.pStream->Read((char *)&Extra.dwNameFlags, sizeof(DWORD));
Extra.sName.ReadFromStream(Ctx.pStream);
Extra.ImageSel.ReadFromStream(SystemCtx);
Extra.sNotes.ReadFromStream(Ctx.pStream);
}
}
}
// In old version we read multiple lists
else
{
// Read all the objects
for (j = 0; j < iObjCount; j++)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
if (pList)
pList->Objects.Insert(dwLoad);
}
// Read names
if (Ctx.dwVersion >= 21)
{
Ctx.pStream->Read((char *)&iObjCount, sizeof(DWORD));
for (j = 0; j < iObjCount; j++)
{
DWORD dwObjID;
Ctx.pStream->Read((char *)&dwObjID, sizeof(DWORD));
SObjBasics *pObjData;
if (pList && (pObjData = pList->Objects.GetAt(dwObjID)))
{
SObjExtra &Extra = pObjData->SetExtra();
Ctx.pStream->Read((char *)&Extra.dwNameFlags, sizeof(DWORD));
Extra.sName.ReadFromStream(Ctx.pStream);
if (SystemCtx.dwVersion >= 131)
{
Extra.ImageSel.ReadFromStream(SystemCtx);
Extra.sNotes.ReadFromStream(Ctx.pStream);
// Flags
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
pObjData->fShowDestroyed = ((dwLoad & 0x00000001) ? true : false);
}
}
else
{
CString sDummy;
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
sDummy.ReadFromStream(Ctx.pStream);
if (SystemCtx.dwVersion >= 131)
{
CCompositeImageSelector Dummy;
Dummy.ReadFromStream(SystemCtx);
sDummy.ReadFromStream(Ctx.pStream);
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
}
}
}
}
}
}
}
}
void CObjectTracker::Refresh (CSystem *pSystem)
// Refresh
//
// Refresh from current system, if we're invalid.
{
int i;
CTopologyNode *pNode = pSystem->GetTopology();
if (pNode == NULL)
return;
SNodeData *pNodeData = m_ByNode.SetAt(pNode->GetID());
// Get the player so we can compute disposition
CSpaceObject *pPlayer = g_pUniverse->GetPlayerShip();
// See if we need to accumulate orbit information
bool bAddOrbits = (pNodeData->Orbits.GetCount() == 0);
// Loop over all objects and refresh them
for (i = 0; i < pSystem->GetObjectCount(); i++)
{
CSpaceObject *pObj = pSystem->GetObject(i);
if (pObj == NULL
|| pObj->IsDestroyed()
|| (pObj->GetCategory() != CSpaceObject::catShip && pObj->GetCategory() != CSpaceObject::catStation))
continue;
// Look for the object in our list
SObjBasics *pObjData;
if (!Find(pNodeData, pObj, &pObjData))
{
// There are certain (old) bugs which caused us to lose all the
// objects in CObjectTracker. If that happens, we just re-add the
// object here.
//
// But this should really never happen.
Insert(pObj);
if (!Find(pNodeData, pObj, &pObjData))
{
// Should never happen because objects are added/deleted at the
// appropriate times.
ASSERT(false);
continue;
}
}
// If this object has an orbit, store it.
const COrbit *pOrbit;
if (bAddOrbits
&& pObj->ShowMapOrbit()
&& (pOrbit = pObj->GetMapOrbit()))
pNodeData->Orbits.Insert(*pOrbit);
// Refresh
Refresh(pObj, pObjData, pPlayer);
}
}
void CObjectTracker::Refresh (CSpaceObject *pObj, SObjBasics *pObjData, CSpaceObject *pPlayer)
// Refresh
//
// Refresh the object data from the actual object.
{
CDesignType *pType = pObj->GetType();
if (pType == NULL)
return;
// Update flags
pObjData->vPos = pObj->GetPos();
pObjData->fKnown = pObj->IsKnown();
pObjData->fShowDestroyed = pObj->ShowStationDamage();
pObjData->fShowInMap = pObj->IsShownInGalacticMap();
pObjData->fInactive = pObj->IsInactive();
// Track our disposition relative to the player
if (pPlayer)
{
CSovereign::Disposition iDisp = pObj->GetDispositionTowards(pPlayer);
pObjData->fFriendly = (iDisp == CSovereign::dispFriend);
pObjData->fEnemy = (iDisp == CSovereign::dispEnemy);
}
// See if we have a custom services description
CString sCustomNotes;
bool bHasCustomNotes = GetCustomDesc(pObj, *pObjData, &sCustomNotes);
// If the name of this object does not match the type, then we store it.
//
// NOTE: We need to pass a flags var to GetTypeName because it has slightly
// different behavior if you omit them.
DWORD dwNameFlags;
DWORD dwDummyFlags;
CString sName = pObj->GetNamePattern(0, &dwNameFlags);
if (!pType->GetTypeImage().IsConstant()
|| bHasCustomNotes
|| !strEquals(sName, pType->GetNamePattern(0, &dwDummyFlags)))
{
SObjExtra &Extra = pObjData->SetExtra();
Extra.sName = sName;
Extra.dwNameFlags = dwNameFlags;
Extra.ImageSel = pObj->GetImageSelector();
Extra.sNotes = sCustomNotes;
}
else
pObjData->DeleteExtra();
}
void CObjectTracker::ReplayCommands (CSystem *pSystem)
// ReplayCommands
//
// Replay any delayed commands so that the system can be synchronized with any
// changes made while in a different system.
{
int i;
CTopologyNode *pNode = pSystem->GetTopology();
if (pNode == NULL)
return;