-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCItemTable.cpp
2391 lines (1778 loc) · 52.4 KB
/
CItemTable.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
// CItemTable.cpp
//
// CItemTable object
//
// NOTES:
//
// The count attribute is held in the collection objects. For example, in a table of items, the table
// object keeps an item count for each item entry (instead of the item keeping its own count).
// At some point, it might be better to switch this around.
#include "PreComp.h"
#ifdef DEBUG_AVERAGE_VALUE
#include <stdio.h>
#endif
#define AVERAGE_VALUE_TAG CONSTLIT("AverageValue")
#define COMPONENTS_TAG CONSTLIT("Components")
#define GROUP_TAG CONSTLIT("Group")
#define ITEM_TAG CONSTLIT("Item")
#define ITEMS_TAG CONSTLIT("Items")
#define LEVEL_TABLE_TAG CONSTLIT("LevelTable")
#define LOCATION_CRITERIA_TABLE_TAG CONSTLIT("LocationCriteriaTable")
#define LOOKUP_TAG CONSTLIT("Lookup")
#define NULL_TAG CONSTLIT("Null")
#define RANDOM_ITEM_TAG CONSTLIT("RandomItem")
#define TABLE_TAG CONSTLIT("Table")
#define ATTRIBUTES_ATTRIB CONSTLIT("attributes")
#define CATEGORIES_ATTRIB CONSTLIT("categories")
#define CHANCE_ATTRIB CONSTLIT("chance")
#define COUNT_ATTRIB CONSTLIT("count")
#define CRITERIA_ATTRIB CONSTLIT("criteria")
#define DAMAGED_ATTRIB CONSTLIT("damaged")
#define DEBUG_ONLY_ATTRIB CONSTLIT("debugOnly")
#define ENHANCED_ATTRIB CONSTLIT("enhanced")
#define ENHANCEMENT_ATTRIB CONSTLIT("enhancement")
#define ITEM_ATTRIB CONSTLIT("item")
#define LEVEL_ATTRIB CONSTLIT("level")
#define LEVEL_CURVE_ATTRIB CONSTLIT("levelCurve")
#define LEVEL_FREQUENCY_ATTRIB CONSTLIT("levelFrequency")
#define LEVEL_VALUE_ATTRIB CONSTLIT("levelValue")
#define MODIFIERS_ATTRIB CONSTLIT("modifiers")
#define TABLE_ATTRIB CONSTLIT("table")
#define UNID_ATTRIB CONSTLIT("unid")
#define VALUE_ATTRIB CONSTLIT("value")
#define ATTRIB_NOT_FOR_SALE CONSTLIT("notForSale")
#define FIELD_TREASURE_VALUE CONSTLIT("treasureValue")
#define STR_G_ITEM CONSTLIT("gItem")
static const Metric NOT_FOR_SALE_DISCOUNT = 0.2;
class CRecursingCheck
{
public:
CRecursingCheck (bool &bRecurse) :
m_bRecurse(bRecurse),
m_bWorking(false)
{ }
~CRecursingCheck ()
{
if (m_bWorking)
m_bRecurse = false;
}
bool IsRecursing (void)
{
if (m_bRecurse)
return true;
m_bWorking = true;
m_bRecurse = true;
return false;
}
private:
bool &m_bRecurse;
bool m_bWorking;
};
class CGroupOfGenerators : public IItemGenerator
{
public:
CGroupOfGenerators (void) : m_bRecursing(false)
{ }
virtual ~CGroupOfGenerators (void);
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel);
virtual IItemGenerator *GetGenerator (int iIndex) { return m_Table[iIndex].pItem; }
virtual int GetGeneratorCount (void) { return m_Table.GetCount(); }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
private:
struct SEntry
{
IItemGenerator *pItem;
int iChance;
DiceRange Count;
};
void AddItemsInt (SItemAddCtx &Ctx) const;
void AddItemsScaled (SItemAddCtx &Ctx, Metric rAdj) const;
Metric GetCountAdj (int iLevel);
inline bool SetsAverageValue (void) const { return m_AverageValue.GetCount() > 0; }
TArray<SEntry> m_Table;
TArray<CCurrencyAndValue> m_AverageValue;
TArray<Metric> m_CountAdj;
mutable bool m_bRecursing;
};
class CLevelTableOfItemGenerators : public IItemGenerator
{
public:
CLevelTableOfItemGenerators (void) : m_bRecursing(false)
{ }
virtual ~CLevelTableOfItemGenerators (void);
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel);
virtual IItemGenerator *GetGenerator (int iIndex) { return m_Table[iIndex].pEntry; }
virtual int GetGeneratorCount (void) { return m_Table.GetCount(); }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
private:
struct SEntry
{
IItemGenerator *pEntry;
CString sLevelFrequency;
int iChance;
DiceRange Count;
};
TArray<SEntry> m_Table;
mutable int m_iTotalChance;
mutable int m_iComputedLevel;
mutable bool m_bRecursing;
};
class CLocationCriteriaTableOfItemGenerators : public IItemGenerator
{
public:
CLocationCriteriaTableOfItemGenerators (void) : m_bRecursing(false)
{ }
virtual ~CLocationCriteriaTableOfItemGenerators (void);
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel);
virtual IItemGenerator *GetGenerator (int iIndex) { return m_Table[iIndex].pEntry; }
virtual int GetGeneratorCount (void) { return m_Table.GetCount(); }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
private:
struct SEntry
{
IItemGenerator *pEntry;
CAttributeCriteria Criteria;
int iChance;
DiceRange Count;
};
TArray<SEntry> m_Table;
mutable int m_iTotalChance;
mutable CSystem *m_pComputedSystem;
mutable CString m_sComputedAttribs;
mutable bool m_bRecursing;
};
class CLookup : public IItemGenerator
{
public:
static ALERROR Create (DWORD dwUNID, IItemGenerator **retpGenerator, CString *retsError = NULL);
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel) { return m_pTable->GetAverageValue(iLevel); }
virtual IItemGenerator *GetGenerator (int iIndex) { return m_pTable->GetGenerator(); }
virtual int GetGeneratorCount (void) { return ((m_pTable && m_pTable->GetGenerator()) ? 1 : 0); }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
private:
CItemTableRef m_pTable;
};
class CNullItem : public IItemGenerator
{
};
class CRandomItems : public IItemGenerator
{
public:
CRandomItems (void) : m_Table(NULL) { }
static ALERROR Create (const CItemCriteria &Crit,
const CString &sLevelFrequency,
IItemGenerator **retpGenerator);
virtual ~CRandomItems (void);
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel);
virtual CItemType *GetItemType (int iIndex) { return m_Table[iIndex].pType; }
virtual int GetItemTypeCount (void) { return m_iCount; }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
private:
struct SEntry
{
CItemType *pType;
int iProbability;
};
void InitTable (const CString &sLevelFrequency) const;
mutable int m_iCount;
mutable SEntry *m_Table;
CItemCriteria m_Criteria;
CString m_sLevelFrequency;
int m_iLevel;
int m_iLevelCurve;
int m_iDamaged;
CRandomEnhancementGenerator m_Enhanced;
bool m_bDynamicLevelFrequency; // If TRUE, level frequency depends on system level
mutable int m_iDynamicLevel;
};
class CSingleItem : public IItemGenerator
{
public:
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel);
virtual CItemType *GetItemType (int iIndex) { return m_pItemType; }
virtual int GetItemTypeCount (void) { return 1; }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
static CurrencyValue CalcItemValue (CItemType *pType);
private:
CItemTypeRef m_pItemType;
int m_iDamaged;
CRandomEnhancementGenerator m_Enhanced;
bool m_bDebugOnly;
};
class CTableOfGenerators : public IItemGenerator
{
public:
CTableOfGenerators (void) : m_bRecursing(false)
{ }
virtual ~CTableOfGenerators (void);
virtual void AddItems (SItemAddCtx &Ctx);
virtual void AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed);
virtual CurrencyValue GetAverageValue (int iLevel);
virtual IItemGenerator *GetGenerator (int iIndex) { return m_Table[iIndex].pItem; }
virtual int GetGeneratorCount (void) { return m_Table.GetCount(); }
virtual CItemTypeProbabilityTable GetProbabilityTable (SItemAddCtx &Ctx) const override;
virtual bool HasItemAttribute (const CString &sAttrib) const override;
virtual ALERROR LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc);
virtual ALERROR OnDesignLoadComplete (SDesignLoadCtx &Ctx);
private:
struct SEntry
{
IItemGenerator *pItem;
int iChance;
DiceRange Count;
};
TArray<SEntry> m_Table;
int m_iTotalChance;
mutable bool m_bRecursing;
};
// IItemGenerator -------------------------------------------------------------
ALERROR IItemGenerator::CreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, IItemGenerator **retpGenerator)
// CreateFromXML
//
// Creates a new generator
{
ALERROR error;
IItemGenerator *pGenerator = NULL;
if (strEquals(pDesc->GetTag(), ITEM_TAG))
pGenerator = new CSingleItem;
else if (strEquals(pDesc->GetTag(), TABLE_TAG))
pGenerator = new CTableOfGenerators;
else if (strEquals(pDesc->GetTag(), RANDOM_ITEM_TAG))
pGenerator = new CRandomItems;
else if (strEquals(pDesc->GetTag(), GROUP_TAG)
|| strEquals(pDesc->GetTag(), COMPONENTS_TAG)
|| strEquals(pDesc->GetTag(), ITEMS_TAG)
|| strEquals(pDesc->GetTag(), AVERAGE_VALUE_TAG))
pGenerator = new CGroupOfGenerators;
else if (strEquals(pDesc->GetTag(), LOOKUP_TAG))
pGenerator = new CLookup;
else if (strEquals(pDesc->GetTag(), LEVEL_TABLE_TAG))
pGenerator = new CLevelTableOfItemGenerators;
else if (strEquals(pDesc->GetTag(), LOCATION_CRITERIA_TABLE_TAG))
pGenerator = new CLocationCriteriaTableOfItemGenerators;
else if (strEquals(pDesc->GetTag(), NULL_TAG))
pGenerator = new CNullItem;
else
{
Ctx.sError = strPatternSubst(CONSTLIT("Unknown item generator: %s"), pDesc->GetTag());
return ERR_FAIL;
}
if (error = pGenerator->LoadFromXML(Ctx, pDesc))
{
if (pGenerator)
delete pGenerator;
return error;
}
*retpGenerator = pGenerator;
return NOERROR;
}
ALERROR IItemGenerator::CreateLookupTable (SDesignLoadCtx &Ctx, DWORD dwUNID, IItemGenerator **retpGenerator)
// CreateLookupTable
//
// Creates a new lookup item table
{
return CLookup::Create(dwUNID, retpGenerator, &Ctx.sError);
}
ALERROR IItemGenerator::CreateRandomItemTable (const CItemCriteria &Crit,
const CString &sLevelFrequency,
IItemGenerator **retpGenerator)
// CreateRandomItemTable
//
// Creates a new random item table
{
return CRandomItems::Create(Crit, sLevelFrequency, retpGenerator);
}
CItemTable::CItemTable (void) :
m_pGenerator(NULL)
// CItemTable constructor
{
}
CItemTable::~CItemTable (void)
// CItemTable destructor
{
if (m_pGenerator)
delete m_pGenerator;
}
bool CItemTable::FindDataField (const CString &sField, CString *retsValue) const
// FindDataField
//
// Returns meta-data
{
// Deal with the meta-data that we know about
if (strEquals(sField, FIELD_TREASURE_VALUE))
*retsValue = strFromInt((int)GetAverageValue(1));
else
return CDesignType::FindDataField(sField, retsValue);
return true;
}
void CItemTable::OnAddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// OnAddTypesUsed
//
// Adds types used by this table
{
if (m_pGenerator)
m_pGenerator->AddTypesUsed(retTypesUsed);
}
ALERROR CItemTable::OnCreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// OnCreateFromXML
//
// Create from XML descriptor
{
ALERROR error;
CXMLElement *pElement = pDesc->GetContentElement(0);
if (pElement)
{
if (error = IItemGenerator::CreateFromXML(Ctx, pElement, &m_pGenerator))
return ComposeLoadError(Ctx, Ctx.sError);
}
return NOERROR;
}
ALERROR CItemTable::OnBindDesign (SDesignLoadCtx &Ctx)
// OnBindDesign
//
// Load design references
{
ALERROR error;
if (m_pGenerator)
{
if (error = m_pGenerator->OnDesignLoadComplete(Ctx))
{
Ctx.sError = strPatternSubst(CONSTLIT("ItemTable (%x): %s"), GetUNID(), Ctx.sError);
return error;
}
}
return NOERROR;
}
// CGroupOfGenerators --------------------------------------------------------
CGroupOfGenerators::~CGroupOfGenerators (void)
// CGroupOfGenerators destructor
{
int i;
for (i = 0; i < m_Table.GetCount(); i++)
if (m_Table[i].pItem)
delete m_Table[i].pItem;
}
void CGroupOfGenerators::AddItems (SItemAddCtx &Ctx)
// AddItems
//
// Add items
{
int i;
// If we need to adjust counts, then do a separate algorithm
if (SetsAverageValue())
{
// Figure out how many loops to do. The more loops we do, the more
// different items there will be. We want a small number of loops
// for less valuable treasure hordes.
Metric rUnitCost = Max(1.0, CWeaponClass::GetStdStats(Ctx.iLevel).rCost * 0.5);
int iLoopCount = Max(1, Min(mathRandom(9, 13), mathRound((Metric)m_AverageValue[Ctx.iLevel].GetCreditValue() / rUnitCost)));
// CountAdj is the number of times we need to loop through our table
// to get the required item value (on average). We convert this to the
// scaling factor to use for the given number of loops.
#ifdef DEBUG_AVERAGE_VALUE
bool bDebug = (Ctx.pDest && Ctx.pDest->GetType()->GetUNID() == 0x080200C0);
if (bDebug)
printf("[%d] Level %d\n", Ctx.pDest->GetID(), Ctx.iLevel);
#endif
Metric rCountAdj = GetCountAdj(Ctx.iLevel);
Metric rScale = rCountAdj / iLoopCount;
#ifdef DEBUG_AVERAGE_VALUE
if (bDebug)
printf("[%d] CountAdj: %.2f\n", Ctx.pDest->GetID(), rCountAdj);
#endif
for (i = 0; i < iLoopCount; i++)
AddItemsScaled(Ctx, rScale);
}
else
AddItemsInt(Ctx);
}
void CGroupOfGenerators::AddItemsInt (SItemAddCtx &Ctx) const
// AddItemsInt
//
// Adds items to the context.
{
int i, j;
for (i = 0; i < m_Table.GetCount(); i++)
{
if (mathRandom(1, 100) <= m_Table[i].iChance)
{
int iCount = m_Table[i].Count.Roll();
for (j = 0; j < iCount; j++)
m_Table[i].pItem->AddItems(Ctx);
}
}
}
void CGroupOfGenerators::AddItemsScaled (SItemAddCtx &Ctx, Metric rAdj) const
// AddItemsScaled
//
// Adds items and then scales by the given adjustment. E.g., if we add one
// item and rAdj is 0.5, then we add the item 50% of the time.
{
int i;
#ifdef DEBUG_AVERAGE_VALUE
bool bDebug = (Ctx.pDest && Ctx.pDest->GetType()->GetUNID() == 0x080200C0);
if (bDebug)
printf("[%d] Adjust: %.2f\n", Ctx.pDest->GetID(), rAdj);
#endif
// Add the items to a private list.
CItemList LocalList;
CItemListManipulator ItemList(LocalList);
SItemAddCtx LocalCtx(ItemList);
LocalCtx.iLevel = Ctx.iLevel;
AddItemsInt(LocalCtx);
// Now loop over the items and adjust the count appropriately.
for (i = 0; i < LocalList.GetCount(); i++)
{
const CItem &Item = LocalList.GetItem(i);
int iOriginalCount = Item.GetCount();
// Adjust the count
Metric rNewCount = iOriginalCount * rAdj;
Metric rNewCountInt = floor(rNewCount);
int iNewCount = (int)rNewCountInt;
Metric rExtra = rNewCount - rNewCountInt;
int iExtraChance = (int)(100000.0 * rExtra);
if (mathRandom(0, 100000) < iExtraChance)
iNewCount++;
#ifdef DEBUG_AVERAGE_VALUE
if (bDebug)
{
printf("%s: %d -> %.2f = %d\n", (LPSTR)Item.GetNounPhrase(CItemCtx()), iOriginalCount, rNewCount, iNewCount);
}
#endif
// Add the item with the new count
if (iNewCount == iOriginalCount)
{
Ctx.ItemList.AddItem(Item);
}
else if (iNewCount > 0)
{
CItem NewItem(Item);
NewItem.SetCount(iNewCount);
Ctx.ItemList.AddItem(NewItem);
}
}
}
void CGroupOfGenerators::AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// AddTypesUsed
//
// Adds types used by this generator
{
int i;
for (i = 0; i < m_Table.GetCount(); i++)
m_Table[i].pItem->AddTypesUsed(retTypesUsed);
}
CurrencyValue CGroupOfGenerators::GetAverageValue (int iLevel)
// GetAverageValue
//
// Returns the average value.
{
int i;
if (SetsAverageValue())
{
if (iLevel >= 0 && iLevel < m_AverageValue.GetCount())
return m_AverageValue[iLevel].GetCreditValue();
else
return 0;
}
else
{
// Make sure we don't recurse
CRecursingCheck Check(m_bRecursing);
if (Check.IsRecursing())
return 0;
// Average value is proportional to chances.
Metric rTotal = 0.0;
for (i = 0; i < m_Table.GetCount(); i++)
{
if (m_Table[i].iChance < 100)
rTotal += (m_Table[i].Count.GetAveValueFloat() * (Metric)m_Table[i].pItem->GetAverageValue(iLevel) * (Metric)m_Table[i].iChance / 100.0);
else
rTotal += m_Table[i].Count.GetAveValueFloat() * m_Table[i].pItem->GetAverageValue(iLevel);
}
return (CurrencyValue)(rTotal + 0.5);
}
}
Metric CGroupOfGenerators::GetCountAdj (int iLevel)
// GetCountAdj
//
// Returns the count adjusment for the given level.
{
int i;
if (iLevel >= 0 && iLevel < m_CountAdj.GetCount())
{
Metric rCountAdj = m_CountAdj[iLevel];
if (rCountAdj < 0.0)
{
// Loop over all our children and compute the average value.
Metric rTotal = 0.0;
for (i = 0; i < m_Table.GetCount(); i++)
rTotal += (Metric)m_Table[i].pItem->GetAverageValue(iLevel);
// Compute the factor that we have to multiply the total to get to
// the desired value.
rCountAdj = (rTotal > 0.0 ? (Metric)m_AverageValue[iLevel].GetCreditValue() / rTotal : 0.0);
// Remember so we don't have to compute it again.
m_CountAdj[iLevel] = rCountAdj;
}
return rCountAdj;
}
else
return 0.0;
}
CItemTypeProbabilityTable CGroupOfGenerators::GetProbabilityTable (SItemAddCtx &Ctx) const
// GetProbabilityTable
//
// Returns a table with the probability of each item type appearing.
{
int i;
// Make sure we don't recurse
CRecursingCheck Check(m_bRecursing);
if (Check.IsRecursing())
return CItemTypeProbabilityTable();
// We combine probabilities.
CItemTypeProbabilityTable Result;
for (i = 0; i < m_Table.GetCount(); i++)
Result.Union(m_Table[i].pItem->GetProbabilityTable(Ctx));
return Result;
}
bool CGroupOfGenerators::HasItemAttribute (const CString &sAttrib) const
// HasItemAttribute
//
// Returns TRUE if any item has the given attribute.
{
int i;
for (i = 0; i < m_Table.GetCount(); i++)
if (m_Table[i].pItem->HasItemAttribute(sAttrib))
return true;
return false;
}
ALERROR CGroupOfGenerators::LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// LoadFromXML
//
// Load from XML
{
int i;
ALERROR error;
// Load content elements
m_Table.InsertEmpty(pDesc->GetContentElementCount());
for (i = 0; i < m_Table.GetCount(); i++)
{
CXMLElement *pEntry = pDesc->GetContentElement(i);
m_Table[i].iChance = pEntry->GetAttributeInteger(CHANCE_ATTRIB);
if (m_Table[i].iChance == 0)
m_Table[i].iChance = 100;
CString sCount = pEntry->GetAttribute(COUNT_ATTRIB);
if (sCount.IsBlank())
m_Table[i].Count = DiceRange(0, 0, 1);
else
m_Table[i].Count.LoadFromXML(sCount);
if (error = IItemGenerator::CreateFromXML(Ctx, pEntry, &m_Table[i].pItem))
return error;
}
// See if we force an average value
CString sAttrib;
if (pDesc->FindAttribute(LEVEL_VALUE_ATTRIB, &sAttrib))
{
TArray<CString> Values;
ParseStringList(sAttrib, 0, &Values);
m_AverageValue.InsertEmpty(MAX_ITEM_LEVEL + 1);
for (i = 0; i < Values.GetCount(); i++)
if (error = m_AverageValue[i + 1].InitFromXML(Ctx, Values[i], i + 1))
return error;
}
else if (pDesc->FindAttribute(VALUE_ATTRIB, &sAttrib))
{
m_AverageValue.InsertEmpty(MAX_ITEM_LEVEL + 1);
for (i = 1; i <= MAX_ITEM_LEVEL; i++)
if (error = m_AverageValue[i].InitFromXML(Ctx, sAttrib, i))
return error;
}
return NOERROR;
}
ALERROR CGroupOfGenerators::OnDesignLoadComplete (SDesignLoadCtx &Ctx)
// OnDesignLoadComplete
//
// Resolve references
{
int i;
ALERROR error;
for (i = 0; i < m_Table.GetCount(); i++)
{
if (error = m_Table[i].pItem->OnDesignLoadComplete(Ctx))
return error;
}
// Bind average values
for (i = 0; i < m_AverageValue.GetCount(); i++)
if (error = m_AverageValue[i].Bind(Ctx))
return error;
// Initialize count adjustment
m_CountAdj.InsertEmpty(m_AverageValue.GetCount());
for (i = 0; i < m_AverageValue.GetCount(); i++)
m_CountAdj[i] = -1.0;
return NOERROR;
}
// CSingleItem ---------------------------------------------------------------
void CSingleItem::AddItems (SItemAddCtx &Ctx)
// AddItems
//
// Add items
{
// Ignore if not debugging
if (m_bDebugOnly && !g_pUniverse->InDebugMode())
return;
// Create item
CItem NewItem(m_pItemType, 1);
if (mathRandom(1, 100) <= m_iDamaged)
NewItem.SetDamaged();
else
m_Enhanced.EnhanceItem(NewItem);
Ctx.ItemList.AddItem(NewItem);
}
void CSingleItem::AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// AddTypesUsed
//
// Adds types used by this generator
{
retTypesUsed->SetAt(m_pItemType.GetUNID(), true);
}
CurrencyValue CSingleItem::CalcItemValue (CItemType *pType)
// CalcItemValue
//
// Computes the value (to the player) of this item.
{
CurrencyValue ItemValue = CEconomyType::ExchangeToCredits(pType->GetCurrencyType(), pType->GetValue(CItemCtx(), true));
if (pType->HasAttribute(ATTRIB_NOT_FOR_SALE))
ItemValue = Max((CurrencyValue)1, (CurrencyValue)(ItemValue * NOT_FOR_SALE_DISCOUNT));
return ItemValue;
}
CurrencyValue CSingleItem::GetAverageValue (int iLevel)
// GetAverageValue
//
// Returns the average value (in credits)
{
return CalcItemValue(m_pItemType);
}
CItemTypeProbabilityTable CSingleItem::GetProbabilityTable (SItemAddCtx &Ctx) const
// GetProbabilityTable
//
// Returns a table with the probability of each item type appearing.
{
CItemTypeProbabilityTable Result;
if (m_pItemType)
Result.Union(m_pItemType, 1.0);
return Result;
}
bool CSingleItem::HasItemAttribute (const CString &sAttrib) const
// HasItemAttribute
//
// Returns TRUE if any item has the given attribute
{
if (m_pItemType && m_pItemType->HasAttribute(sAttrib))
return true;
return false;
}
ALERROR CSingleItem::LoadFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// LoadFromXML
//
// Load from XML
{
ALERROR error;
if (error = m_pItemType.LoadUNID(Ctx, pDesc->GetAttribute(ITEM_ATTRIB)))
return error;
m_iDamaged = pDesc->GetAttributeInteger(DAMAGED_ATTRIB);
m_bDebugOnly = pDesc->GetAttributeBool(DEBUG_ONLY_ATTRIB);
if (m_pItemType.GetUNID() == 0)
{
CString sUNID = pDesc->GetAttribute(ITEM_ATTRIB);
if (sUNID.IsBlank())
Ctx.sError = strPatternSubst(CONSTLIT("<Item> element missing item attribute."));
else
Ctx.sError = strPatternSubst(CONSTLIT("Invalid item UNID: %s"), sUNID);
return ERR_FAIL;
}
if (error = m_Enhanced.InitFromXML(Ctx, pDesc))
return error;
return NOERROR;
}
ALERROR CSingleItem::OnDesignLoadComplete (SDesignLoadCtx &Ctx)
// OnDesignLoadComplete
//
// Resolve references
{
ALERROR error;
// Ignore if not debugging. We don't bind because sometimes we have a
// reference to an item type that only exists in debug mode.
if (m_bDebugOnly && !g_pUniverse->InDebugMode())
return NOERROR;
// Bind
if (error = m_pItemType.Bind(Ctx))
return error;
return NOERROR;
}
// CLevelTableOfItemGenerators -----------------------------------------------
CLevelTableOfItemGenerators::~CLevelTableOfItemGenerators (void)
// CLevelTableOfItemGenerators destructor
{
int i;
for (i = 0; i < m_Table.GetCount(); i++)
if (m_Table[i].pEntry)
delete m_Table[i].pEntry;
}
void CLevelTableOfItemGenerators::AddItems (SItemAddCtx &Ctx)
// AddItems
//
// Adds items
{
int i, j;
// Compute probabilities, if necessary
if (m_iComputedLevel != Ctx.iLevel)
{
// Create a table of probabilities
m_iTotalChance = 0;
for (i = 0; i < m_Table.GetCount(); i++)
{
m_Table[i].iChance = GetFrequencyByLevel(m_Table[i].sLevelFrequency, Ctx.iLevel);
m_iTotalChance += m_Table[i].iChance;
}
m_iComputedLevel = Ctx.iLevel;
}
// Generate