-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCDesignType.cpp
3214 lines (2396 loc) · 72.9 KB
/
CDesignType.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
// CDesignType.cpp
//
// CDesignType class
// Copyright (c) 2017 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define ADVENTURE_DESC_TAG CONSTLIT("AdventureDesc")
#define ARMOR_MASS_DESC_TAG CONSTLIT("ArmorMassDesc")
#define ATTRIBUTE_DESC_TAG CONSTLIT("AttributeDesc")
#define DATA_TAG CONSTLIT("Data")
#define DISPLAY_ATTRIBUTES_TAG CONSTLIT("DisplayAttributes")
#define DOCK_SCREEN_TAG CONSTLIT("DockScreen")
#define DOCK_SCREENS_TAG CONSTLIT("DockScreens")
#define ECONOMY_TYPE_TAG CONSTLIT("EconomyType")
#define EFFECT_TAG CONSTLIT("Effect")
#define EFFECT_TYPE_TAG CONSTLIT("EffectType")
#define ENCOUNTER_TABLE_TAG CONSTLIT("EncounterTable")
#define ENCOUNTER_TYPE_TAG CONSTLIT("EncounterType")
#define EVENTS_TAG CONSTLIT("Events")
#define GLOBAL_DATA_TAG CONSTLIT("GlobalData")
#define IMAGE_TAG CONSTLIT("Image")
#define IMAGE_COMPOSITE_TAG CONSTLIT("ImageComposite")
#define INITIAL_DATA_TAG CONSTLIT("InitialData")
#define ITEM_TABLE_TAG CONSTLIT("ItemTable")
#define ITEM_TYPE_TAG CONSTLIT("ItemType")
#define LANGUAGE_TAG CONSTLIT("Language")
#define LOOKUP_TAG CONSTLIT("Lookup")
#define MISSION_TYPE_TAG CONSTLIT("MissionType")
#define OVERLAY_TYPE_TAG CONSTLIT("OverlayType")
#define POWER_TAG CONSTLIT("Power")
#define SHIP_CLASS_TAG CONSTLIT("ShipClass")
#define SHIP_CLASS_OVERRIDE_TAG CONSTLIT("ShipClassOverride")
#define SHIP_ENERGY_FIELD_TYPE_TAG CONSTLIT("ShipEnergyFieldType")
#define SHIP_TABLE_TAG CONSTLIT("ShipTable")
#define SOUND_TAG CONSTLIT("Sound")
#define SOUNDTRACK_TAG CONSTLIT("Soundtrack")
#define SOVEREIGN_TAG CONSTLIT("Sovereign")
#define SPACE_ENVIRONMENT_TYPE_TAG CONSTLIT("SpaceEnvironmentType")
#define STATIC_DATA_TAG CONSTLIT("StaticData")
#define STATION_TYPE_TAG CONSTLIT("StationType")
#define SYSTEM_MAP_TAG CONSTLIT("SystemMap")
#define SYSTEM_FRAGMENT_TABLE_TAG CONSTLIT("SystemPartTable")
#define SYSTEM_TYPE_TAG CONSTLIT("SystemType")
#define TEMPLATE_TYPE_TAG CONSTLIT("TemplateType")
#define TYPE_TAG CONSTLIT("Type")
#define ATTRIBUTES_ATTRIB CONSTLIT("attributes")
#define EFFECT_ATTRIB CONSTLIT("effect")
#define EXCLUDES_ATTRIB CONSTLIT("excludes")
#define EXTENDS_ATTRIB CONSTLIT("extends")
#define INHERIT_ATTRIB CONSTLIT("inherit")
#define MODIFIERS_ATTRIB CONSTLIT("modifiers")
#define OBSOLETE_ATTRIB CONSTLIT("obsolete")
#define OBSOLETE_VERSION_ATTRIB CONSTLIT("obsoleteVersion")
#define REQUIRED_VERSION_ATTRIB CONSTLIT("requiredVersion")
#define UNID_ATTRIB CONSTLIT("UNID")
#define GET_CREATE_POS_EVENT CONSTLIT("GetCreatePos")
#define GET_GLOBAL_ACHIEVEMENTS_EVENT CONSTLIT("GetGlobalAchievements")
#define GET_GLOBAL_DOCK_SCREEN_EVENT CONSTLIT("GetGlobalDockScreen")
#define GET_GLOBAL_PLAYER_PRICE_ADJ_EVENT CONSTLIT("GetGlobalPlayerPriceAdj")
#define GET_GLOBAL_RESURRECT_POTENTIAL_EVENT CONSTLIT("GetGlobalResurrectPotential")
#define ON_GLOBAL_END_DIAGNOSTICS_EVENT CONSTLIT("OnGlobalEndDiagnostics")
#define ON_GLOBAL_INTRO_COMMAND_EVENT CONSTLIT("OnGlobalIntroCommand")
#define ON_GLOBAL_INTRO_STARTED_EVENT CONSTLIT("OnGlobalIntroStarted")
#define ON_GLOBAL_MARK_IMAGES_EVENT CONSTLIT("OnGlobalMarkImages")
#define ON_GLOBAL_OBJ_DESTROYED_EVENT CONSTLIT("OnGlobalObjDestroyed")
#define ON_GLOBAL_OBJ_GATE_CHECK_EVENT CONSTLIT("OnGlobalObjGateCheck")
#define ON_GLOBAL_DOCK_PANE_INIT_EVENT CONSTLIT("OnGlobalPaneInit")
#define ON_GLOBAL_PLAYER_BOUGHT_ITEM_EVENT CONSTLIT("OnGlobalPlayerBoughtItem")
#define ON_GLOBAL_PLAYER_CHANGED_SHIPS_EVENT CONSTLIT("OnGlobalPlayerChangedShips")
#define ON_GLOBAL_PLAYER_ENTERED_SYSTEM_EVENT CONSTLIT("OnGlobalPlayerEnteredSystem")
#define ON_GLOBAL_PLAYER_LEFT_SYSTEM_EVENT CONSTLIT("OnGlobalPlayerLeftSystem")
#define ON_GLOBAL_PLAYER_SOLD_ITEM_EVENT CONSTLIT("OnGlobalPlayerSoldItem")
#define ON_GLOBAL_RESURRECT_EVENT CONSTLIT("OnGlobalResurrect")
#define ON_GLOBAL_TOPOLOGY_CREATED_EVENT CONSTLIT("OnGlobalTopologyCreated")
#define ON_GLOBAL_START_DIAGNOSTICS_EVENT CONSTLIT("OnGlobalStartDiagnostics")
#define ON_GLOBAL_SYSTEM_DIAGNOSTICS_EVENT CONSTLIT("OnGlobalSystemDiagnostics")
#define ON_GLOBAL_SYSTEM_CREATED_EVENT CONSTLIT("OnGlobalSystemCreated")
#define ON_GLOBAL_SYSTEM_STARTED_EVENT CONSTLIT("OnGlobalSystemStarted")
#define ON_GLOBAL_SYSTEM_STOPPED_EVENT CONSTLIT("OnGlobalSystemStopped")
#define ON_GLOBAL_UNIVERSE_CREATED_EVENT CONSTLIT("OnGlobalUniverseCreated")
#define ON_GLOBAL_UNIVERSE_LOAD_EVENT CONSTLIT("OnGlobalUniverseLoad")
#define ON_GLOBAL_UNIVERSE_SAVE_EVENT CONSTLIT("OnGlobalUniverseSave")
#define ON_GLOBAL_UPDATE_EVENT CONSTLIT("OnGlobalUpdate")
#define ON_RANDOM_ENCOUNTER_EVENT CONSTLIT("OnRandomEncounter")
#define SPECIAL_EVENT CONSTLIT("event:")
#define SPECIAL_EXTENSION CONSTLIT("extension:")
#define SPECIAL_INHERIT CONSTLIT("inherit:")
#define SPECIAL_PROPERTY CONSTLIT("property:")
#define SPECIAL_SYSTEM_LEVEL CONSTLIT("systemLevel:")
#define SPECIAL_UNID CONSTLIT("unid:")
#define LANGID_CORE_MAP_DESC CONSTLIT("core.mapDesc")
#define LANGID_CORE_MAP_DESC_ABANDONED CONSTLIT("core.mapDescAbandoned")
#define LANGID_CORE_MAP_DESC_ABANDONED_CUSTOM CONSTLIT("core.mapDescAbandonedCustom")
#define LANGID_CORE_MAP_DESC_CUSTOM CONSTLIT("core.mapDescCustom")
#define LANGID_CORE_MAP_DESC_EXTRA CONSTLIT("core.mapDescExtra")
#define LANGID_CORE_MAP_DESC_MAIN CONSTLIT("core.mapDescMain")
#define PROPERTY_API_VERSION CONSTLIT("apiVersion")
#define PROPERTY_ATTRIBUTES CONSTLIT("attributes")
#define PROPERTY_CLASS CONSTLIT("class")
#define PROPERTY_EXTENSION CONSTLIT("extension")
#define PROPERTY_MAP_DESCRIPTION CONSTLIT("mapDescription")
#define PROPERTY_MERGED CONSTLIT("merged")
#define PROPERTY_NAME_PATTERN CONSTLIT("namePattern")
#define PROPERTY_OBSOLETE_VERSION CONSTLIT("obsoleteVersion")
#define PROPERTY_REQUIRED_VERSION CONSTLIT("requiredVersion")
#define PROPERTY_UNID CONSTLIT("unid")
#define FIELD_ENTITY CONSTLIT("entity")
#define FIELD_EXTENSION_UNID CONSTLIT("extensionUNID")
#define FIELD_NAME CONSTLIT("name")
#define FIELD_UNID CONSTLIT("unid")
#define FIELD_VERSION CONSTLIT("version")
#define FIELD_ATTRIB_PREFIX CONSTLIT("attrib-")
static char DESIGN_CHAR[designCount] =
{
'i',
'b',
's',
'f',
'y',
't',
'v',
'd',
'c',
'p',
'e',
'h',
'a',
'g',
'm',
'u',
'n',
'q',
'z',
'w',
'$',
'_',
'x',
'o',
};
static char *DESIGN_CLASS_NAME[designCount] =
{
"ItemType",
"ItemTable",
"ShipClass",
"OverlayType",
"SystemType",
"StationType",
"Sovereign",
"DockScreen",
"EffectType",
"Power",
"SpaceEnvironment",
"ShipTable",
"AdventureDesc",
"Globals",
"Image",
"Sound",
"MissionType",
"SystemTable",
"SystemMap",
"NameGenerator",
"EconomyType",
"TemplateType",
"Type",
"ImageComposite",
};
static char *CACHED_EVENTS[CDesignType::evtCount] =
{
"CanInstallItem",
"CanRemoveItem",
"OnDestroyCheck",
"OnGlobalTypesInit",
"OnObjDestroyed",
"OnSystemObjAttacked",
"OnSystemStarted",
"OnSystemStopped",
"OnSystemWeaponFire",
"OnUpdate",
};
CDesignType::~CDesignType (void)
// CDesignType destructor
{
}
void CDesignType::AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// AddTypesUsed
//
// Adds types used by this type to the list
{
if (m_dwInheritFrom)
retTypesUsed->SetAt(m_dwInheritFrom, true);
OnAddTypesUsed(retTypesUsed);
}
ALERROR CDesignType::BindDesign (SDesignLoadCtx &Ctx)
// BindDesign
//
// Bind design elements
{
ALERROR error;
int i;
Ctx.pType = this;
// Now that we've connected to our based classes, update the event cache
// with events from our ancestors.
if (m_pInheritFrom)
{
ASSERT(m_pInheritFrom->GetUNID() != GetUNID());
for (i = 0; i < evtCount; i++)
{
if (!HasCachedEvent((ECachedHandlers)i))
{
SEventHandlerDesc *pInherit = m_pInheritFrom->GetInheritedCachedEvent((ECachedHandlers)i);
if (pInherit)
SetExtra()->EventsCache[i] = *pInherit;
}
}
}
// Cache some flags
m_fHasCustomMapDescLang = (HasLanguageEntry(LANGID_CORE_MAP_DESC_CUSTOM) || HasLanguageEntry(LANGID_CORE_MAP_DESC_ABANDONED_CUSTOM));
// Type-specific
try
{
error = OnBindDesign(Ctx);
}
catch (...)
{
::kernelDebugLogPattern("Crash in OnBindDesign [UNID: %08x]", m_dwUNID);
Ctx.pType = NULL;
throw;
}
Ctx.pType = NULL;
return error;
}
ALERROR CDesignType::ComposeLoadError (SDesignLoadCtx &Ctx, const CString &sError) const
// ComposeLoadError
//
// Sets Ctx.sError appropriately and returns ERR_FAIL
{
CString sEntity = GetEntityName();
if (sEntity.IsBlank())
sEntity = strPatternSubst("%08x", GetUNID());
Ctx.sError = strPatternSubst("%s (%s): %s", GetNounPhrase(), sEntity, sError);
return ERR_FAIL;
}
void CDesignType::CreateClone (CDesignType **retpType)
// CreateClone
//
// Creates a clone of this type. Caller is responsible for freeing it.
{
CDesignType *pClone;
switch (GetType())
{
case designItemType:
pClone = new CItemType;
break;
case designItemTable:
pClone = new CItemTable;
break;
case designShipClass:
pClone = new CShipClass;
break;
case designOverlayType:
pClone = new COverlayType;
break;
case designSystemType:
pClone = new CSystemType;
break;
case designStationType:
pClone = new CStationType;
break;
case designSovereign:
pClone = new CSovereign;
break;
case designDockScreen:
pClone = new CDockScreenType;
break;
case designEffectType:
{
CEffectCreator *pEffectType = CEffectCreator::AsType(this);
if (pEffectType == NULL)
{
ASSERT(false);
return;
}
CEffectCreator *pEffectClone;
if (CEffectCreator::CreateFromTag(pEffectType->GetTag(), &pEffectClone) != NOERROR)
{
ASSERT(false);
return;
}
pClone = pEffectClone;
break;
}
case designPower:
pClone = new CPower;
break;
case designSpaceEnvironmentType:
pClone = new CSpaceEnvironmentType;
break;
case designShipTable:
pClone = new CShipTable;
break;
case designAdventureDesc:
pClone = new CAdventureDesc;
break;
case designImage:
pClone = new CObjectImage;
break;
case designMusic:
pClone = new CMusicResource;
break;
case designMissionType:
pClone = new CMissionType;
break;
case designSound:
pClone = new CSoundResource;
break;
case designSystemTable:
pClone = new CSystemTable;
break;
case designSystemMap:
pClone = new CSystemMap;
break;
case designEconomyType:
pClone = new CEconomyType;
break;
case designTemplateType:
pClone = new CTemplateType;
break;
case designGenericType:
pClone = new CGenericType;
break;
default:
ASSERT(false);
return;
}
// Initialize
pClone->m_bIsClone = true;
pClone->m_dwUNID = m_dwUNID;
pClone->m_pExtension = m_pExtension;
pClone->m_dwVersion = m_dwVersion;
pClone->m_dwInheritFrom = m_dwInheritFrom;
pClone->m_pInheritFrom = m_pInheritFrom;
pClone->m_sAttributes = m_sAttributes;
pClone->m_Events = m_Events;
if (m_pExtra)
pClone->m_pExtra = m_pExtra;
// Let our subclass initialize
pClone->OnInitFromClone(this);
// Done
*retpType = pClone;
}
ALERROR CDesignType::CreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, CDesignType **retpType)
// CreateFromXML
//
// Creates a design type from an XML element
{
try
{
ALERROR error;
CDesignType *pType = NULL;
bool bOverride = false;
if (strEquals(pDesc->GetTag(), ITEM_TYPE_TAG))
pType = new CItemType;
else if (strEquals(pDesc->GetTag(), ITEM_TABLE_TAG))
pType = new CItemTable;
else if (strEquals(pDesc->GetTag(), SHIP_CLASS_TAG))
pType = new CShipClass;
else if (strEquals(pDesc->GetTag(), SHIP_ENERGY_FIELD_TYPE_TAG))
pType = new COverlayType;
else if (strEquals(pDesc->GetTag(), MISSION_TYPE_TAG))
pType = new CMissionType;
else if (strEquals(pDesc->GetTag(), OVERLAY_TYPE_TAG))
pType = new COverlayType;
else if (strEquals(pDesc->GetTag(), SYSTEM_TYPE_TAG))
pType = new CSystemType;
else if (strEquals(pDesc->GetTag(), STATION_TYPE_TAG)
|| strEquals(pDesc->GetTag(), ENCOUNTER_TYPE_TAG))
pType = new CStationType;
else if (strEquals(pDesc->GetTag(), SOUNDTRACK_TAG))
pType = new CMusicResource;
else if (strEquals(pDesc->GetTag(), SOVEREIGN_TAG))
pType = new CSovereign;
else if (strEquals(pDesc->GetTag(), DOCK_SCREEN_TAG))
pType = new CDockScreenType;
else if (strEquals(pDesc->GetTag(), POWER_TAG))
pType = new CPower;
else if (strEquals(pDesc->GetTag(), SPACE_ENVIRONMENT_TYPE_TAG))
pType = new CSpaceEnvironmentType;
else if (strEquals(pDesc->GetTag(), ENCOUNTER_TABLE_TAG))
pType = new CShipTable;
else if (strEquals(pDesc->GetTag(), SHIP_TABLE_TAG))
pType = new CShipTable;
else if (strEquals(pDesc->GetTag(), SOUND_TAG))
pType = new CSoundResource;
else if (strEquals(pDesc->GetTag(), SYSTEM_FRAGMENT_TABLE_TAG))
pType = new CSystemTable;
else if (strEquals(pDesc->GetTag(), SYSTEM_MAP_TAG))
pType = new CSystemMap;
else if (strEquals(pDesc->GetTag(), IMAGE_TAG))
pType = new CObjectImage;
else if (strEquals(pDesc->GetTag(), ECONOMY_TYPE_TAG))
pType = new CEconomyType;
else if (strEquals(pDesc->GetTag(), TEMPLATE_TYPE_TAG))
pType = new CTemplateType;
else if (strEquals(pDesc->GetTag(), TYPE_TAG))
pType = new CGenericType;
else if (strEquals(pDesc->GetTag(), IMAGE_COMPOSITE_TAG))
pType = new CCompositeImageType;
else if (strEquals(pDesc->GetTag(), ADVENTURE_DESC_TAG))
{
// Only valid if we are inside an Adventure
if (Ctx.pExtension != NULL && !Ctx.pExtension->CanHaveAdventureDesc())
{
Ctx.sError = CONSTLIT("<AdventureDesc> element is only valid for Adventures");
return ERR_FAIL;
}
pType = new CAdventureDesc;
}
else if (strEquals(pDesc->GetTag(), EFFECT_TAG))
{
// Load UNID
DWORD dwUNID;
if (error = ::LoadUNID(Ctx, pDesc->GetAttribute(UNID_ATTRIB), &dwUNID))
return error;
// This is an old-style CEffectCreator for compatibility
if (error = CEffectCreator::CreateFromXML(Ctx, pDesc, NULL_STR, (CEffectCreator **)&pType))
return error;
pType->m_dwUNID = dwUNID;
if (!pDesc->FindAttribute(ATTRIBUTES_ATTRIB, &pType->m_sAttributes))
pType->m_sAttributes = pDesc->GetAttribute(MODIFIERS_ATTRIB);
if (retpType)
*retpType = pType;
// We skip the normal initialization for backwards compatibility
return NOERROR;
}
else if (strEquals(pDesc->GetTag(), EFFECT_TYPE_TAG))
{
// This is a full effect type. The actual class depends on the content
if (error = CEffectCreator::CreateTypeFromXML(Ctx, pDesc, (CEffectCreator **)&pType))
return error;
}
else if (strEquals(pDesc->GetTag(), SHIP_CLASS_OVERRIDE_TAG))
{
pType = new CShipClass;
bOverride = true;
}
else
{
Ctx.sError = strPatternSubst(CONSTLIT("Unknown design element: <%s>"), pDesc->GetTag());
return ERR_FAIL;
}
// Result
if (retpType)
*retpType = pType;
// Initialize
return pType->InitFromXML(Ctx, pDesc, bOverride);
}
catch (...)
{
Ctx.sError = strPatternSubst(CONSTLIT("Crash loading: %x"), pDesc->GetAttributeInteger(CONSTLIT("UNID")));
return ERR_FAIL;
}
}
ICCItem *CDesignType::FindBaseProperty (CCodeChainCtx &Ctx, const CString &sProperty) const
// FindBaseProperty
//
// Returns base property of all design types. We also check to see if we have
// an old-style data field.
//
// If we don't have the property, we return NULL.
{
int i;
CCodeChain &CC = g_pUniverse->GetCC();
CString sValue;
ICCItem *pResult;
ICCItemPtr pValue;
if (strEquals(sProperty, PROPERTY_API_VERSION))
return CC.CreateInteger(GetAPIVersion());
else if (strEquals(sProperty, PROPERTY_ATTRIBUTES))
{
TArray<CString> Attribs;
ParseAttributes(GetAttributes(), &Attribs);
if (Attribs.GetCount() == 0)
return CC.CreateNil();
pResult = CC.CreateLinkedList();
for (i = 0; i < Attribs.GetCount(); i++)
pResult->AppendString(CC, Attribs[i]);
return pResult;
}
else if (strEquals(sProperty, PROPERTY_CLASS))
return CC.CreateString(GetTypeClassName());
else if (strEquals(sProperty, PROPERTY_EXTENSION))
{
if (m_pExtension)
return CC.CreateInteger(m_pExtension->GetUNID());
else
return CC.CreateNil();
}
else if (strEquals(sProperty, PROPERTY_MAP_DESCRIPTION))
return CC.CreateString(GetMapDescription(SMapDescriptionCtx()));
else if (strEquals(sProperty, PROPERTY_MERGED))
return CC.CreateBool(m_bIsMerged);
else if (strEquals(sProperty, PROPERTY_NAME_PATTERN))
{
pResult = CC.CreateSymbolTable();
DWORD dwFlags;
pResult->SetStringAt(CC, CONSTLIT("pattern"), GetNamePattern(0, &dwFlags));
pResult->SetIntegerAt(CC, CONSTLIT("flags"), dwFlags);
return pResult;
}
else if (strEquals(sProperty, PROPERTY_OBSOLETE_VERSION))
return (m_dwObsoleteVersion > 0 ? CC.CreateInteger(m_dwObsoleteVersion) : CC.CreateNil());
else if (strEquals(sProperty, PROPERTY_REQUIRED_VERSION))
return (m_dwMinVersion > 0 ? CC.CreateInteger(m_dwMinVersion) : CC.CreateNil());
else if (strEquals(sProperty, PROPERTY_UNID))
return CC.CreateInteger(GetUNID());
// Otherwise, we see if there is a data field
else if (FindDataField(sProperty, &sValue))
return CreateResultFromDataField(CC, sValue);
// Lastly, see if there is static data
else if (m_pExtra && (m_pExtra->StaticData.FindDataAsItem(sProperty, pValue)))
return pValue->Reference();
// Not found
else
return NULL;
}
bool CDesignType::FindDataField (const CString &sField, CString *retsValue) const
// FindDataField
//
// Subclasses should call this method when they do not know the field.
// [Normally we would do an OnFindDataField pattern, but we're too lazy
// to change all the instances.]
{
int i;
if (strEquals(sField, FIELD_ENTITY))
*retsValue = GetEntityName();
else if (strEquals(sField, FIELD_EXTENSION_UNID))
*retsValue = strPatternSubst("0x%08x", (m_pExtension ? m_pExtension->GetUNID() : 0));
else if (strEquals(sField, FIELD_NAME))
*retsValue = strPatternSubst("%s 0x%08x", GetTypeClassName(), m_dwUNID);
else if (strEquals(sField, FIELD_UNID))
*retsValue = strPatternSubst("0x%08x", m_dwUNID);
else if (strEquals(sField, FIELD_VERSION))
{
// Starting in version 12 we start numbering api versions independently
// of release version.
if (m_dwVersion >= 12)
*retsValue = strFromInt(m_dwVersion);
else
*retsValue = strPatternSubst("%d", ExtensionVersionToInteger(m_dwVersion));
}
else if (strStartsWith(sField, FIELD_ATTRIB_PREFIX))
{
CString sParam = strSubString(sField, FIELD_ATTRIB_PREFIX.GetLength());
// Parse into a list of attributes
TArray<CString> Attribs;
char *pPos = sParam.GetASCIIZPointer();
char *pStart = pPos;
while (true)
{
if (*pPos == '\0' || *pPos == '-')
{
CString sAttrib = CString(pStart, (int)(pPos - pStart));
if (!sAttrib.IsBlank())
Attribs.Insert(sAttrib);
if (*pPos == '\0')
break;
else
pStart = pPos + 1;
}
pPos++;
}
// See which attribute we have
CString sValue;
for (i = 0; i < Attribs.GetCount(); i++)
if (HasAttribute(Attribs[i]))
{
if (sValue.IsBlank())
sValue = Attribs[i];
else
sValue = strPatternSubst(CONSTLIT("%s, %s"), sValue, Attribs[i]);
}
// If not found, return false
if (sValue.IsBlank())
return false;
*retsValue = sValue;
}
else
return false;
return true;
}
bool CDesignType::FindEventHandler (const CString &sEvent, SEventHandlerDesc *retEvent) const
// FindEventHandler
//
// Returns an event handler
{
// Ask subclasses
if (OnFindEventHandler(sEvent, retEvent))
return true;
// If we have it, great
ICCItem *pCode;
if (m_Events.FindEvent(sEvent, &pCode))
{
if (retEvent)
{
retEvent->pExtension = m_pExtension;
retEvent->pCode = pCode;
}
return true;
}
// Otherwise, see if we inherit
if (m_pInheritFrom)
return m_pInheritFrom->FindEventHandler(sEvent, retEvent);
// Otherwise, nothing
return false;
}
bool CDesignType::FindStaticData (const CString &sAttrib, ICCItemPtr &pData) const
// FindStaticData
//
// Returns static data
{
if (m_pExtra && m_pExtra->StaticData.FindDataAsItem(sAttrib, pData))
return true;
if (m_pInheritFrom)
return m_pInheritFrom->FindStaticData(sAttrib, pData);
return false;
}
void CDesignType::FireCustomEvent (const CString &sEvent, ECodeChainEvents iEvent, ICCItem *pData, ICCItem **retpResult)
// FireCustomEvent
//
// Fires a custom event
{
CCodeChainCtx Ctx;
SEventHandlerDesc Event;
if (FindEventHandler(sEvent, &Event))
{
Ctx.SetEvent(iEvent);
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineDataVar(pData);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(sEvent, pResult);
// Either return the event result or discard it
if (retpResult)
*retpResult = pResult;
else
Ctx.Discard(pResult);
}
else
{
if (retpResult)
*retpResult = Ctx.CreateNil();
}
}
bool CDesignType::FireGetCreatePos (CSpaceObject *pBase, CSpaceObject *pTarget, CSpaceObject **retpGate, CVector *retvPos)
// FireGetCreatePos
//
// Fire GetCreatePos event
{
DEBUG_TRY
SEventHandlerDesc Event;
if (FindEventHandler(GET_CREATE_POS_EVENT, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.DefineSpaceObject(CONSTLIT("aBaseObj"), pBase);
Ctx.DefineSpaceObject(CONSTLIT("aTargetObj"), pTarget);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(GET_CREATE_POS_EVENT, pResult);
bool bResult;
if (pResult->IsInteger())
{
*retpGate = Ctx.AsSpaceObject(pResult);
*retvPos = (*retpGate)->GetPos();
bResult = true;
}
else if (pResult->IsList())
{
*retpGate = NULL;
*retvPos = Ctx.AsVector(pResult);
bResult = true;
}
else
bResult = false;
Ctx.Discard(pResult);
return bResult;
}
return false;
DEBUG_CATCH
}
void CDesignType::FireGetGlobalAchievements (CGameStats &Stats)
// FireGetGlobalAchievements
//
// Fires GetGlobalAchievements event
{
int i;
SEventHandlerDesc Event;
if (FindEventHandler(GET_GLOBAL_ACHIEVEMENTS_EVENT, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
// Run code
ICCItemPtr pResult = Ctx.RunCode(Event);
// Interpret result
if (pResult->IsError())
ReportEventError(GET_GLOBAL_ACHIEVEMENTS_EVENT, pResult);
else if (pResult->IsNil())
;
else if (pResult->IsSymbolTable())
Stats.Insert(this, pResult);
else if (pResult->IsList() && pResult->GetCount() > 0)
{
// If we have a list of lists, then we have
// a list of achievements
if (pResult->GetElement(0)->IsList() || pResult->GetElement(0)->IsSymbolTable())
{
for (i = 0; i < pResult->GetCount(); i++)
Stats.Insert(this, pResult->GetElement(i));
}
// Otherwise, we have a single achievement
else
Stats.Insert(this, pResult);
}
}
}
bool CDesignType::FireGetGlobalDockScreen (const SEventHandlerDesc &Event, const CSpaceObject *pObj, CDockScreenSys::SSelector &Selector) const
// FireGetGlobalDockScreen
//
// Asks a type to see if it wants to override an object's
// dock screen.
//
// NOTE: If we return TRUE and *retpData is non-NULL then the caller is responsible
// for discarding data.
{
// Set up
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(pObj);
// Run
ICCItemPtr pResult = Ctx.RunCode(Event);
if (pResult->IsError())
{
ReportEventError(GET_GLOBAL_DOCK_SCREEN_EVENT, pResult);
return false;
}
return CTLispConvert::AsScreenSelector(pResult, &Selector);
}
bool CDesignType::FireGetGlobalPlayerPriceAdj (const SEventHandlerDesc &Event, STradeServiceCtx &ServiceCtx, ICCItem *pData, int *retiPriceAdj)
// FireGetGlobalPlayerPriceAdj
//
// Fires event to allow types to modify prices charged to the player.
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
// Set up
Ctx.SetEvent(eventGetGlobalPlayerPriceAdj);
if (ServiceCtx.pItem)
{
Ctx.SaveAndDefineItemVar(*ServiceCtx.pItem);
Ctx.SetItemType(ServiceCtx.pItem->GetType());
}
else if (ServiceCtx.pObj)
{
Ctx.DefineSpaceObject(CONSTLIT("aObj"), ServiceCtx.pObj);
}
Ctx.DefineString(CONSTLIT("aService"), CTradingDesc::ServiceToString(ServiceCtx.iService));
Ctx.DefineSpaceObject(CONSTLIT("aProviderObj"), ServiceCtx.pProvider);
if (pData)
Ctx.SaveAndDefineDataVar(pData);
// Run
ICCItem *pResult = Ctx.Run(Event);
int iPriceAdj = 100;
if (pResult->IsError())
ReportEventError(GET_GLOBAL_PLAYER_PRICE_ADJ_EVENT, pResult);
else if (pResult->IsNil())
;
else
iPriceAdj = pResult->GetIntegerValue();
// Done
Ctx.Discard(pResult);
if (retiPriceAdj)
*retiPriceAdj = iPriceAdj;
return (iPriceAdj != 100);
}
int CDesignType::FireGetGlobalResurrectPotential (void)
// FireGetGlobalResurrectPotential
//
// Returns the potential that this design type will resurrect the player
// (0 = no chance; 100 = greatest potential)
{
int iResult = 0;
SEventHandlerDesc Event;
if (FindEventHandler(GET_GLOBAL_RESURRECT_POTENTIAL_EVENT, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
// Run code
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(GET_GLOBAL_RESURRECT_POTENTIAL_EVENT, pResult);
else