-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCTopologyNode.cpp
1451 lines (1109 loc) · 33.6 KB
/
CTopologyNode.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
// CTopologyNode.cpp
//
// Star system topology
#include "PreComp.h"
#define ATTRIBUTES_TAG CONSTLIT("Attributes")
#define CHANCE_TAG CONSTLIT("Chance")
#define DISTANCE_BETWEEN_NODES_TAG CONSTLIT("DistanceBetweenNodes")
#define DISTANCE_TO_TAG CONSTLIT("DistanceTo")
#define STARGATE_COUNT_TAG CONSTLIT("StargateCount")
#define SYSTEM_TAG CONSTLIT("System")
#define STARGATES_TAG CONSTLIT("StarGates")
#define ATTRIBUTES_ATTRIB CONSTLIT("attributes")
#define CHANCE_ATTRIB CONSTLIT("chance")
#define CRITERIA_ATTRIB CONSTLIT("criteria")
#define DEST_FRAGMENT_ROTATION_ATTRIB CONSTLIT("destFragmentRotation")
#define DESTGATE_ATTRIB CONSTLIT("destGate")
#define DESTID_ATTRIB CONSTLIT("destID")
#define EPITAPH_ATTRIB CONSTLIT("epitaph")
#define END_GAME_ATTRIB CONSTLIT("endGame")
#define END_GAME_REASON_ATTRIB CONSTLIT("endGameReason")
#define ID_ATTRIB CONSTLIT("ID")
#define LEVEL_ATTRIB CONSTLIT("level")
#define MAX_ATTRIB CONSTLIT("max")
#define MIN_ATTRIB CONSTLIT("min")
#define NAME_ATTRIB CONSTLIT("name")
#define NODE_ID_ATTRIB CONSTLIT("nodeID")
#define ROOT_NODE_ATTRIB CONSTLIT("rootNode")
#define UNID_ATTRIB CONSTLIT("UNID")
#define VARIANT_ATTRIB CONSTLIT("variant")
#define X_ATTRIB CONSTLIT("x")
#define Y_ATTRIB CONSTLIT("y")
#define FIELD_KNOWN_ONLY CONSTLIT("knownOnly")
#define FIELD_MAX_DIST CONSTLIT("maxDist")
#define FIELD_MIN_DIST CONSTLIT("minDist")
#define PREV_DEST CONSTLIT("[Prev]")
#define PROPERTY_DEST_GATE_ID CONSTLIT("destGateID")
#define PROPERTY_DEST_ID CONSTLIT("destID")
#define PROPERTY_GATE_ID CONSTLIT("gateID")
#define PROPERTY_KNOWN CONSTLIT("known")
#define PROPERTY_LAST_VISITED_GAME_SECONDS CONSTLIT("lastVisitSeconds")
#define PROPERTY_LAST_VISITED_ON CONSTLIT("lastVisitOn")
#define PROPERTY_LEVEL CONSTLIT("level")
#define PROPERTY_NAME CONSTLIT("name")
#define PROPERTY_NODE_ID CONSTLIT("nodeID")
#define PROPERTY_POS CONSTLIT("pos")
#define PROPERTY_UNCHARTED CONSTLIT("uncharted")
#define SPECIAL_LEVEL CONSTLIT("level:")
#define SPECIAL_NODE_ID CONSTLIT("nodeID:")
#define SPECIAL_SYSTEM_TYPE CONSTLIT("systemType:")
// CTopologyNode class --------------------------------------------------------
CTopologyNode::CTopologyNode (const CString &sID, DWORD SystemUNID, CSystemMap *pMap) :
m_sID(sID),
m_SystemUNID(SystemUNID),
m_pMap(pMap)
// CTopology constructor
{
#ifdef DEBUG_ALL_NODES
m_bKnown = true;
#endif
}
CTopologyNode::~CTopologyNode (void)
// CTopology destructor
{
}
void CTopologyNode::AddAttributes (const CString &sAttribs)
// AddAttributes
//
// Append the given attributes
{
if (m_sAttributes.IsBlank())
m_sAttributes = sAttribs;
else
{
TArray<CString> Attribs;
::ParseAttributes(sAttribs, &Attribs);
for (int i = 0; i < Attribs.GetCount(); i++)
if (!::HasModifier(m_sAttributes, Attribs[i]))
m_sAttributes = ::AppendModifiers(m_sAttributes, Attribs[i]);
}
}
ALERROR CTopologyNode::AddStargate (const SStargateDesc &GateDesc)
// AddStargate
//
// Adds a new stargate to the topology
{
// Add the gate
bool bNew;
SStargateEntry *pDesc = m_NamedGates.SetAt(GateDesc.sName, &bNew);
// If not new, then this is an error
if (!bNew)
return ERR_FAIL;
// Initialize
pDesc->sDestNode = GateDesc.sDestNode;
pDesc->sDestEntryPoint = GateDesc.sDestName;
if (GateDesc.pMidPoints)
pDesc->MidPoints = *GateDesc.pMidPoints;
pDesc->fUncharted = GateDesc.bUncharted;
return NOERROR;
}
ALERROR CTopologyNode::AddStargateAndReturn (const SStargateDesc &GateDesc)
// AddStargate
//
// Adds a new stargate to the topology
{
// Get the destination node
CTopologyNode *pDestNode = g_pUniverse->FindTopologyNode(GateDesc.sDestNode);
if (pDestNode == NULL)
{
kernelDebugLogPattern("Unable to find destination node: %s", GateDesc.sDestNode);
return ERR_FAIL;
}
// Look for the destination stargate
CString sReturnNodeID;
CString sReturnEntryPoint;
if (!pDestNode->FindStargate(GateDesc.sDestName, &sReturnNodeID, &sReturnEntryPoint))
{
// If we can't find the stargate in the destination node, then we
// create it if we can.
SStargateDesc ReturnGateDesc;
ReturnGateDesc.sName = GateDesc.sDestName;
ReturnGateDesc.sDestNode = GetID();
ReturnGateDesc.sDestName = GateDesc.sName;
if (pDestNode->AddStargate(ReturnGateDesc) != NOERROR)
{
::kernelDebugLogPattern("Unable to find or add destination stargate: %s", GateDesc.sDestName);
return ERR_FAIL;
}
sReturnNodeID = GetID();
sReturnEntryPoint = GateDesc.sName;
}
// Add the gate
AddStargate(GateDesc);
// See if we need to fix up the return gate
if (strEquals(sReturnNodeID, PREV_DEST))
pDestNode->SetStargateDest(GateDesc.sDestName, GetID(), GateDesc.sName);
return NOERROR;
}
int CTopologyNode::CalcMatchStrength (const CAttributeCriteria &Criteria)
// CalcMatchStrength
//
// Calculates the match strength of topology node and the criteria.
{
int i;
int iStrength = 1000;
for (i = 0; i < Criteria.GetCount(); i++)
{
DWORD dwMatchStrength;
bool bIsSpecial;
const CString &sAttrib = Criteria.GetAttribAndWeight(i, &dwMatchStrength, &bIsSpecial);
bool bHasAttrib = (bIsSpecial ? HasSpecialAttribute(sAttrib) : HasAttribute(sAttrib));
int iAdj = CAttributeCriteria::CalcWeightAdj(bHasAttrib, dwMatchStrength);
iStrength = iStrength * iAdj / 1000;
}
return iStrength;
}
void CTopologyNode::CreateFromStream (SUniverseLoadCtx &Ctx, CTopologyNode **retpNode)
// CreateFromStream
//
// Creates a node from a stream
//
// CString m_sID
// CString m_sCreatorID
// DWORD m_SystemUNID
// DWORD m_pMap (UNID)
// DWORD m_xPos
// DWORD m_yPos
// CString m_sName
// CString m_sAttributes
// DWORD m_iLevel
// DWORD m_dwID
//
// DWORD No of named gates
// CString gate: sName
// CString gate: sDestNode
// CString gate: sDestEntryPoint
// DWORD gate: flags
// DWORD gate: xMid
// DWORD gate: yMid
//
// DWORD No of variant labels
// CString variant label
//
// CAttributeDataBlock m_Data
// DWORD flags
//
// CString m_sEpitaph
// CString m_sEndGameReason
//
// CTradingEconomy m_Trading
{
int i;
DWORD dwLoad;
CTopologyNode *pNode;
CString sID;
sID.ReadFromStream(Ctx.pStream);
CString sCreatorID;
if (Ctx.dwVersion >= 34)
sCreatorID.ReadFromStream(Ctx.pStream);
DWORD dwSystemUNID;
Ctx.pStream->Read((char *)&dwSystemUNID, sizeof(DWORD));
CSystemMap *pMap;
if (Ctx.dwVersion >= 6)
{
DWORD dwMapUNID;
Ctx.pStream->Read((char *)&dwMapUNID, sizeof(DWORD));
pMap = CSystemMap::AsType(g_pUniverse->FindDesignType(dwMapUNID));
}
else
pMap = NULL;
pNode = new CTopologyNode(sID, dwSystemUNID, pMap);
pNode->m_sCreatorID = sCreatorID;
if (Ctx.dwVersion >= 6)
{
Ctx.pStream->Read((char *)&pNode->m_xPos, sizeof(DWORD));
Ctx.pStream->Read((char *)&pNode->m_yPos, sizeof(DWORD));
}
pNode->m_sName.ReadFromStream(Ctx.pStream);
if (Ctx.dwVersion >= 23)
pNode->m_sAttributes.ReadFromStream(Ctx.pStream);
Ctx.pStream->Read((char *)&pNode->m_iLevel, sizeof(DWORD));
Ctx.pStream->Read((char *)&pNode->m_dwID, sizeof(DWORD));
DWORD dwCount;
Ctx.pStream->Read((char *)&dwCount, sizeof(DWORD));
for (i = 0; i < (int)dwCount; i++)
{
CString sName;
sName.ReadFromStream(Ctx.pStream);
SStargateEntry *pDesc = pNode->m_NamedGates.SetAt(sName);
pDesc->sDestNode.ReadFromStream(Ctx.pStream);
pDesc->sDestEntryPoint.ReadFromStream(Ctx.pStream);
if (Ctx.dwVersion >= 27)
{
DWORD dwFlags;
Ctx.pStream->Read((char *)&dwFlags, sizeof(DWORD));
bool bCurved = ((dwFlags & 0x00000001) ? true : false);
pDesc->fUncharted = ((dwFlags & 0x00000002) ? true : false);
if (bCurved)
{
DWORD dwCount;
Ctx.pStream->Read((char *)&dwCount, sizeof(DWORD));
pDesc->MidPoints.InsertEmpty(dwCount);
Ctx.pStream->Read((char *)&pDesc->MidPoints[0], dwCount * sizeof(SPoint));
}
}
}
Ctx.pStream->Read((char *)&dwCount, sizeof(DWORD));
for (i = 0; i < (int)dwCount; i++)
{
CString sLabel;
sLabel.ReadFromStream(Ctx.pStream);
pNode->m_VariantLabels.Insert(sLabel);
}
if (Ctx.dwVersion >= 1)
pNode->m_Data.ReadFromStream(Ctx.pStream);
// Flags
if (Ctx.dwVersion >= 6)
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
else
dwLoad = 0;
pNode->m_bKnown = (dwLoad & 0x00000001 ? true : false);
pNode->m_bPosKnown = (dwLoad & 0x00000002 ? true : false);
pNode->m_bDeferCreate = (dwLoad & 0x00000004 ? true : false);
pNode->m_bMarked = false;
// More
if (Ctx.dwVersion >= 5)
{
pNode->m_sEpitaph.ReadFromStream(Ctx.pStream);
pNode->m_sEndGameReason.ReadFromStream(Ctx.pStream);
}
else
{
// For previous version, we forgot to save this, so do it now
if (pNode->IsEndGame())
{
pNode->m_sEpitaph = CONSTLIT("left Human Space on a journey to the Galactic Core");
pNode->m_sEndGameReason = CONSTLIT("leftHumanSpace");
}
}
if (Ctx.dwVersion >= 32)
pNode->m_Trading.ReadFromStream(Ctx);
// Done
*retpNode = pNode;
}
bool CTopologyNode::FindStargate (const CString &sName, CString *retsDestNode, CString *retsEntryPoint)
// FindStargate
//
// Looks for the stargate by name and returns the destination node id and entry point
{
SStargateEntry *pDesc = m_NamedGates.GetAt(sName);
if (pDesc == NULL)
return false;
if (retsDestNode)
*retsDestNode = pDesc->sDestNode;
if (retsEntryPoint)
*retsEntryPoint = pDesc->sDestEntryPoint;
return true;
}
CString CTopologyNode::FindStargateName (const CString &sDestNode, const CString &sEntryPoint)
// FindStargateName
//
// Returns the name of the stargate that matches the node and entry point
{
int i;
for (i = 0; i < m_NamedGates.GetCount(); i++)
{
SStargateEntry *pDesc = &m_NamedGates[i];
if (strEquals(pDesc->sDestNode, sDestNode)
&& strEquals(pDesc->sDestEntryPoint, sEntryPoint))
return m_NamedGates.GetKey(i);
}
return NULL_STR;
}
bool CTopologyNode::FindStargateTo (const CString &sDestNode, CString *retsName, CString *retsDestGateID)
// FindStargateTo
//
// Looks for a stargate to the given node; returns info on the first one.
// Returns FALSE if none found.
{
int i;
for (i = 0; i < m_NamedGates.GetCount(); i++)
{
SStargateEntry *pDesc = &m_NamedGates[i];
if (strEquals(pDesc->sDestNode, sDestNode))
{
if (retsName)
*retsName = m_NamedGates.GetKey(i);
if (retsDestGateID)
*retsDestGateID = pDesc->sDestEntryPoint;
return true;
}
}
return false;
}
CString CTopologyNode::GenerateStargateName (void) const
// GenerateStargateName
//
// Generates a unique stargate name
{
CString sName;
int iIndex = m_NamedGates.GetCount();
do
{
iIndex++;
sName = strPatternSubst(CONSTLIT("SG%d"), iIndex);
}
while (m_NamedGates.GetAt(sName) != NULL);
// Done
return sName;
}
CTopologyNode *CTopologyNode::GetGateDest (const CString &sName, CString *retsEntryPoint)
// GetGateDest
//
// Get stargate destination
{
SStargateEntry *pDesc = m_NamedGates.GetAt(sName);
if (pDesc == NULL)
return NULL;
if (retsEntryPoint)
*retsEntryPoint = pDesc->sDestEntryPoint;
if (pDesc->pDestNode == NULL)
pDesc->pDestNode = g_pUniverse->FindTopologyNode(pDesc->sDestNode);
return pDesc->pDestNode;
}
DWORD CTopologyNode::GetLastVisitedTime (void) const
// GetLastVisitedTime
//
// Returns the most recent tick when the player was in the given system. If
// the player never visited the system, we return 0xFFFFFFFF. If the player
// is currently in the system, we return the current tick.
{
IPlayerController *pPlayer = g_pUniverse->GetPlayer();
CPlayerGameStats *pStats = (pPlayer ? pPlayer->GetGameStats() : NULL);
return (pStats ? pStats->GetSystemLastVisitedTime(GetID()) : 0xffffffff);
}
Metric CTopologyNode::GetLinearDistanceTo (const CTopologyNode *pNode) const
// GetLinearDistanceTo
//
// Returns the distance to the given node (or -1 if the node is in a different map).
{
Metric rDist2 = GetLinearDistanceTo2(pNode);
if (rDist2 < 0.0)
return -1.0;
return sqrt(rDist2);
}
Metric CTopologyNode::GetLinearDistanceTo2 (const CTopologyNode *pNode) const
// GetLinearDistanceTo2
//
// Returns the distance to the given node (or -1 if the node is in a different map).
{
int xSrc, ySrc;
CSystemMap *pSrcMap = GetDisplayPos(&xSrc, &ySrc);
int xDest, yDest;
CSystemMap *pDestMap = pNode->GetDisplayPos(&xDest, &yDest);
if (pSrcMap != pDestMap)
return -1.0;
Metric xDiff = (xSrc - xDest);
Metric yDiff = (ySrc - yDest);
return ((xDiff * xDiff) + (yDiff * yDiff));
}
ICCItem *CTopologyNode::GetProperty (const CString &sName)
// GetProperty
//
// Get topology node property
{
CCodeChain &CC = g_pUniverse->GetCC();
if (strEquals(sName, PROPERTY_KNOWN))
return CC.CreateBool(IsKnown());
else if (strEquals(sName, PROPERTY_LAST_VISITED_GAME_SECONDS))
{
DWORD dwLastVisited = GetLastVisitedTime();
if (dwLastVisited == 0xffffffff)
return CC.CreateNil();
CTimeSpan Span = g_pUniverse->GetElapsedGameTimeAt(g_pUniverse->GetTicks()) - g_pUniverse->GetElapsedGameTimeAt(GetLastVisitedTime());
return CC.CreateInteger(Span.Seconds());
}
else if (strEquals(sName, PROPERTY_LAST_VISITED_ON))
{
DWORD dwLastVisited = GetLastVisitedTime();
if (dwLastVisited == 0xffffffff)
return CC.CreateNil();
else
return CC.CreateInteger(dwLastVisited);
}
else if (strEquals(sName, PROPERTY_LEVEL))
return CC.CreateInteger(GetLevel());
else if (strEquals(sName, PROPERTY_NAME))
return CC.CreateString(GetSystemName());
else if (strEquals(sName, PROPERTY_POS))
{
// If no map, then no position
if (m_pMap == NULL)
return CC.CreateNil();
// Create a list
ICCItem *pResult = CC.CreateLinkedList();
if (pResult->IsError())
return pResult;
CCLinkedList *pList = (CCLinkedList *)pResult;
pList->AppendInteger(CC, m_xPos);
pList->AppendInteger(CC, m_yPos);
return pResult;
}
else
return CC.CreateNil();
}
CString CTopologyNode::GetStargate (int iIndex)
// GetStargate
//
// Returns the stargate ID
{
return m_NamedGates.GetKey(iIndex);
}
CTopologyNode *CTopologyNode::GetStargateDest (int iIndex, CString *retsEntryPoint) const
// GetStargateDest
//
// Returns the destination node for the given stargate
{
SStargateEntry *pDesc = &m_NamedGates[iIndex];
if (retsEntryPoint)
*retsEntryPoint = pDesc->sDestEntryPoint;
if (pDesc->pDestNode == NULL)
pDesc->pDestNode = g_pUniverse->FindTopologyNode(pDesc->sDestNode);
return pDesc->pDestNode;
}
ICCItemPtr CTopologyNode::GetStargateProperty (const CString &sName, const CString &sProperty) const
// GetStargateProperty
//
// Returns a property of the given stargate.
{
CCodeChain &CC = g_pUniverse->GetCC();
SStargateEntry *pDesc = m_NamedGates.GetAt(sName);
if (pDesc == NULL)
return ICCItemPtr(CC.CreateNil());
if (strEquals(sProperty, PROPERTY_DEST_GATE_ID))
return ICCItemPtr(CC.CreateString(pDesc->sDestEntryPoint));
else if (strEquals(sProperty, PROPERTY_DEST_ID))
return ICCItemPtr(CC.CreateString(pDesc->sDestNode));
else if (strEquals(sProperty, PROPERTY_GATE_ID))
return ICCItemPtr(CC.CreateString(sName));
else if (strEquals(sProperty, PROPERTY_NODE_ID))
return ICCItemPtr(CC.CreateString(GetID()));
else if (strEquals(sProperty, PROPERTY_UNCHARTED))
return ICCItemPtr(CC.CreateBool(pDesc->fUncharted));
else
return ICCItemPtr(CC.CreateNil());
}
void CTopologyNode::GetStargateRouteDesc (int iIndex, SStargateRouteDesc *retRouteDesc)
// GetStargateRouteDesc
//
// Returns route description
{
SStargateEntry *pDesc = &m_NamedGates[iIndex];
retRouteDesc->pFromNode = this;
retRouteDesc->sFromName = m_NamedGates.GetKey(iIndex);
if (pDesc->pDestNode == NULL)
pDesc->pDestNode = g_pUniverse->FindTopologyNode(pDesc->sDestNode);
retRouteDesc->pToNode = pDesc->pDestNode;
retRouteDesc->sToName = pDesc->sDestEntryPoint;
retRouteDesc->MidPoints = pDesc->MidPoints;
retRouteDesc->bUncharted = pDesc->fUncharted;
}
bool CTopologyNode::HasSpecialAttribute (const CString &sAttrib) const
// HasSpecialAttribute
//
// Returns TRUE if we have the special attribute
{
if (strStartsWith(sAttrib, SPECIAL_LEVEL))
{
int iLevel = strToInt(strSubString(sAttrib, SPECIAL_LEVEL.GetLength()), -1);
return (iLevel == GetLevel());
}
else if (strStartsWith(sAttrib, SPECIAL_NODE_ID))
{
CString sNodeID = strSubString(sAttrib, SPECIAL_NODE_ID.GetLength());
return strEquals(sNodeID, GetID());
}
else if (strStartsWith(sAttrib, SPECIAL_SYSTEM_TYPE))
{
DWORD dwUNID = strToInt(strSubString(sAttrib, SPECIAL_SYSTEM_TYPE.GetLength()), 0xffffffff);
return (dwUNID == m_SystemUNID);
}
else
return false;
}
bool CTopologyNode::HasVariantLabel (const CString &sVariant)
// HasVariantLabel
//
// Returns TRUE if it has the given variant label
{
for (int i = 0; i < m_VariantLabels.GetCount(); i++)
{
if (strEquals(sVariant, m_VariantLabels[i]))
return true;
}
return false;
}
void CTopologyNode::InitCriteriaCtx (SCriteriaCtx &Ctx, const SCriteria &Criteria)
// InitCriteriaCtx
//
// Initializes criteria context. This is needed to optimize distance
// calculations.
{
int i;
Ctx.pTopology = &g_pUniverse->GetTopology();
// Initialize the distance cache, if necessary.
for (i = 0; i < Criteria.DistanceTo.GetCount(); i++)
{
// If we're restricting nodes to a distance from a particular node,
// then we pre-calculate distances.
if (!Criteria.DistanceTo[i].sNodeID.IsBlank())
{
CTopologyNode *pSource = Ctx.pTopology->FindTopologyNode(Criteria.DistanceTo[i].sNodeID);
if (pSource == NULL)
continue;
bool bNew;
TSortMap<CString, int> *pDistMap = Ctx.DistanceCache.SetAt(pSource->GetID(), &bNew);
if (bNew)
Ctx.pTopology->CalcDistances(pSource, *pDistMap);
}
}
}
ALERROR CTopologyNode::InitFromAdditionalXML (CTopology &Topology, CXMLElement *pDesc, CString *retsError)
// InitFromAdditionalXML
//
// Adds additional information
{
ALERROR error;
if (strEquals(pDesc->GetTag(), SYSTEM_TAG))
{
if (error = InitFromSystemXML(Topology, pDesc, retsError))
return error;
}
else if (strEquals(pDesc->GetTag(), ATTRIBUTES_TAG))
{
if (error = InitFromAttributesXML(pDesc, retsError))
return error;
}
return NOERROR;
}
ALERROR CTopologyNode::InitFromAttributesXML (CXMLElement *pAttributes, CString *retsError)
// InitFromAttributesXML
//
// Adds attributes
{
AddAttributes(pAttributes->GetAttribute(ATTRIBUTES_ATTRIB));
return NOERROR;
}
ALERROR CTopologyNode::InitFromSystemXML (CTopology &Topology, CXMLElement *pSystem, CString *retsError)
// InitFromSystemXML
//
// Initializes the system information based on an XML element.
// NOTE: We assume the universe is fully bound at this point.
{
ALERROR error;
SDesignLoadCtx LoadCtx;
CTopologySystemDesc SystemDesc;
if (error = SystemDesc.InitFromXML(LoadCtx, pSystem))
{
if (retsError) *retsError = strPatternSubst(CONSTLIT("Topology %s: Unable to load <System> desc: %s"), m_sID, LoadCtx.sError);
return error;
}
SystemDesc.Apply(Topology, this);
return NOERROR;
}
bool CTopologyNode::IsCriteriaAll (const SCriteria &Crit)
// IsCriteriaAll
//
// Returns TRUE if the criteria matches all nodes
{
return (Crit.iChance == 100
&& Crit.iMaxInterNodeDist == -1
&& Crit.iMinInterNodeDist == 0
&& Crit.iMaxStargates == -1
&& Crit.iMinStargates == 0
&& Crit.AttribCriteria.AttribsNotAllowed.GetCount() == 0
&& Crit.AttribCriteria.AttribsRequired.GetCount() == 0
&& Crit.DistanceTo.GetCount() == 0
&& Crit.AttribCriteria.SpecialRequired.GetCount() == 0
&& Crit.AttribCriteria.SpecialNotAllowed.GetCount() == 0);
}
bool CTopologyNode::MatchesAttributeCriteria (const SAttributeCriteria &Crit) const
// MatchesAttributeCriteria
//
// Returns TRUE if this node matches the given criteria
{
int i;
// Check required attributes
for (i = 0; i < Crit.AttribsRequired.GetCount(); i++)
if (!::HasModifier(m_sAttributes, Crit.AttribsRequired[i]))
return false;
// Check disallowed attributes
for (i = 0; i < Crit.AttribsNotAllowed.GetCount(); i++)
if (::HasModifier(m_sAttributes, Crit.AttribsNotAllowed[i]))
return false;
// Check special required attributes
for (i = 0; i < Crit.SpecialRequired.GetCount(); i++)
if (!HasSpecialAttribute(Crit.SpecialRequired[i]))
return false;
// Check disallowed special attributes
for (i = 0; i < Crit.SpecialNotAllowed.GetCount(); i++)
if (HasSpecialAttribute(Crit.SpecialNotAllowed[i]))
return false;
return true;
}
bool CTopologyNode::MatchesCriteria (SCriteriaCtx &Ctx, const SCriteria &Crit)
// MatchesCriteria
//
// Returns TRUE if this node matches the given criteria
{
int i;
// Chance
if (Crit.iChance < 100 && mathRandom(1, 100) > Crit.iChance)
return false;
// Check attributes
if (!MatchesAttributeCriteria(Crit.AttribCriteria))
return false;
// Stargates
if (m_NamedGates.GetCount() < Crit.iMinStargates)
return false;
if (Crit.iMaxStargates != -1 && m_NamedGates.GetCount() > Crit.iMaxStargates)
return false;
// Flags
if (Crit.bKnownOnly && !IsKnown())
return false;
// Distance to other nodes
if (Ctx.pTopology)
{
for (i = 0; i < Crit.DistanceTo.GetCount(); i++)
{
// If we don't have a specified nodeID then we need to find the distance
// to any node with the appropriate attributes
if (Crit.DistanceTo[i].sNodeID.IsBlank())
{
CTopologyNodeList Checked;
if (!Ctx.pTopology->GetTopologyNodeList().IsNodeInRangeOf(this,
Crit.DistanceTo[i].iMinDist,
Crit.DistanceTo[i].iMaxDist,
Crit.DistanceTo[i].AttribCriteria,
Checked))
return false;
}
// Otherwise, find the distance to the given node
else
{
int iDist;
// See if we can use the cache to get the distance. If not, then
// we just compute it.
const TSortMap<CString, int> *pDistMap = Ctx.DistanceCache.GetAt(Crit.DistanceTo[i].sNodeID);
if (pDistMap == NULL || !pDistMap->Find(GetID(), &iDist))
iDist = Ctx.pTopology->GetDistance(Crit.DistanceTo[i].sNodeID, GetID());
// In range?
if (iDist != -1 && iDist < Crit.DistanceTo[i].iMinDist)
return false;
if (iDist == -1 || (Crit.DistanceTo[i].iMaxDist != -1 && iDist > Crit.DistanceTo[i].iMaxDist))
return false;
}
}
}
// Done
return true;
}
ALERROR CTopologyNode::ParseAttributeCriteria (const CString &sCriteria, SAttributeCriteria *retCrit)
// ParseAttributeCriteria
//
// Parses a string criteria
{
*retCrit = SAttributeCriteria();
char *pPos = sCriteria.GetASCIIZPointer();
while (*pPos != '\0')
{
switch (*pPos)
{
case '+':
case '-':
{
bool bRequired = (*pPos == '+');
bool bBinaryParam;
CString sParam = ::ParseCriteriaParam(&pPos, false, &bBinaryParam);
if (bRequired)
{
if (bBinaryParam)
retCrit->SpecialRequired.Insert(sParam);
else
retCrit->AttribsRequired.Insert(sParam);
}
else
{
if (bBinaryParam)
retCrit->SpecialNotAllowed.Insert(sParam);
else
retCrit->AttribsNotAllowed.Insert(sParam);
}
break;
}
}
pPos++;
}
return NOERROR;
}
ALERROR CTopologyNode::ParseCriteria (const CString &sCriteria, SCriteria *retCrit, CString *retsError)
// ParseCriteria
//
// Parses a string criteria
{
retCrit->iChance = 100;
retCrit->iMaxInterNodeDist = -1;
retCrit->iMinInterNodeDist = 0;
retCrit->iMaxStargates = -1;
retCrit->iMinStargates = 0;
return ParseAttributeCriteria(sCriteria, &retCrit->AttribCriteria);
}
ALERROR CTopologyNode::ParseCriteria (CXMLElement *pCrit, SCriteria *retCrit, CString *retsError)
// ParseCriteria
//
// Parses an XML element into a criteria desc
{
int i;
retCrit->iChance = 100;