-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCDamageAdjDesc.cpp
523 lines (406 loc) · 12.5 KB
/
CDamageAdjDesc.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
// CDamageAdjDesc.cpp
//
// CDamageAdjDesc class
// Copyright (c) 2012 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define DAMAGE_ADJ_ATTRIB CONSTLIT("damageAdj")
#define HP_BONUS_ATTRIB CONSTLIT("hpBonus")
static Metric DAMAGE_TYPE_FRACTION[MAX_ITEM_LEVEL][damageCount] =
{
// las kin par bls ion the pos plm ant nan grv sin dac dst dlg dfr
{ 0.50, 0.50, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.40, 0.40, 0.10, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.25, 0.25, 0.25, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.15, 0.15, 0.35, 0.35, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.05, 0.05, 0.35, 0.35, 0.10, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.25, 0.25, 0.25, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.15, 0.15, 0.35, 0.35, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.05, 0.05, 0.35, 0.35, 0.10, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.25, 0.25, 0.25, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.15, 0.15, 0.35, 0.35, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.05, 0.05, 0.35, 0.35, 0.10, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.25, 0.25, 0.25, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.15, 0.15, 0.35, 0.35, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.05, 0.05, 0.35, 0.35, 0.10, 0.10, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.25, 0.25, 0.25, 0.25, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.15, 0.15, 0.35, 0.35, 0.00, 0.00, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.05, 0.05, 0.35, 0.35, 0.10, 0.10, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.25, 0.25, 0.25, 0.25, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.15, 0.15, 0.35, 0.35, 0.00, 0.00 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.05, 0.05, 0.35, 0.35, 0.10, 0.10 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.25, 0.25, 0.25, 0.25 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.15, 0.25, 0.25, 0.35 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.15, 0.25, 0.25, 0.35 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.10, 0.20, 0.30, 0.40 },
{ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.10, 0.20, 0.30, 0.40 },
};
ALERROR CDamageAdjDesc::Bind (SDesignLoadCtx &Ctx, const CDamageAdjDesc *pDefault)
// Bind
//
// Bind the design
{
Compute(pDefault);
return NOERROR;
}
void CDamageAdjDesc::Compute (const CDamageAdjDesc *pDefault)
// Compute
//
// Compute the m_iDamageAdj table based on the descriptors
{
int i;
m_pDefault = pDefault;
for (i = 0; i < damageCount; i++)
{
switch (m_Desc[i].dwAdjType)
{
case adjDefault:
if (pDefault)
m_iDamageAdj[i] = pDefault->GetAdj((DamageTypes)i);
else
m_iDamageAdj[i] = 100;
break;
case adjAbsolute:
m_iDamageAdj[i] = (int)(DWORD)m_Desc[i].dwAdjValue;
break;
case adjRelative:
if (pDefault)
{
int iValue = (int)(short)m_Desc[i].dwAdjValue;
int iInc = iValue + (10000 / pDefault->GetAdj((DamageTypes)i)) - 100;
m_iDamageAdj[i] = GetDamageAdjFromHPBonus(iInc);
}
else
m_iDamageAdj[i] = 100;
break;
default:
ASSERT(false);
}
}
}
int CDamageAdjDesc::GetAbsorbAdj (DamageTypes iDamageType) const
// GetAbsorbAdj
//
// Returns an absolute adjustment, without consulting default values. If
// adjustment is not defined, we return 0. This is specifically useful when
// defining absorb values (which default to 0).
{
if (m_Desc[iDamageType].dwAdjType == adjAbsolute)
return (int)(short)(m_Desc[iDamageType].dwAdjValue);
else
return 0;
}
void CDamageAdjDesc::GetAdjAndDefault (DamageTypes iDamageType, int *retiAdj, int *retiDefault) const
// GetAdjAndDefault
//
// Returns the adjustment and the default
{
if (retiAdj)
*retiAdj = GetAdj(iDamageType);
if (retiDefault)
*retiDefault = (m_pDefault ? m_pDefault->GetAdj(iDamageType) : 100);
}
int CDamageAdjDesc::GetBonusFromAdj (int iDamageAdj, int iDefault)
// GetBonusFromAdj
//
// Converts a damage adjustment to a bonus
{
if (iDamageAdj == 0)
return -100;
int iBonus = (int)((100.0 * (iDefault - iDamageAdj) / iDamageAdj) + 0.5);
// Prettify. Because of rounding-error, sometimes a bonus of +25 or -25 comes out as
// +24 or -24. This is because we store a damage adjustment not the bonus.
if (((iBonus + 1) % 25) == 0)
iBonus++;
else if (((iBonus - 1) % 25) == 0)
iBonus--;
else if (iBonus == 48)
iBonus = 50;
return iBonus;
}
int CDamageAdjDesc::GetDamageAdjFromHPBonus (int iBonus)
// GetDamageAdjFromHPBonus
//
// Converts a bonus to a damage adjustment. +50% bonus = 67 damage adj; -50% =
// 200 damage adj.
{
if (iBonus <= -100)
return MAX_DAMAGE_ADJ;
else if (iBonus > 20000)
return 0;
else
return 10000 / (100 + iBonus);
}
Metric CDamageAdjDesc::GetDamageTypeFraction (int iLevel, DamageTypes iDamageType)
// GetDamageTypePercent
//
// Returns the % of damage (0-1) that will come from the given type at the given
// level. The sum of all damage types at a given level should add up to 1.0
{
if (iLevel < 1 || iLevel > MAX_ITEM_LEVEL || iDamageType < 0 || iDamageType >= damageCount)
return 0.0;
return DAMAGE_TYPE_FRACTION[iLevel - 1][iDamageType];
}
int CDamageAdjDesc::GetHPBonus (DamageTypes iDamageType) const
// GetHPBonus
//
// Returns the HP bonus
{
if (m_Desc[iDamageType].dwAdjType == adjRelative)
return (int)(short)(m_Desc[iDamageType].dwAdjValue);
else if (m_Desc[iDamageType].dwAdjType == adjDefault)
return 0;
else
{
int iDefault;
int iDamageAdj;
GetAdjAndDefault(iDamageType, &iDamageAdj, &iDefault);
return GetBonusFromAdj(iDamageAdj, iDefault);
}
}
ICCItem *CDamageAdjDesc::GetDamageAdjProperty (const CItemEnhancementStack *pEnhancements) const
// GetDamageAdjProperty
//
// Returns a struct of damage adjustments, one for each damage type.
{
int i;
CCodeChain &CC = g_pUniverse->GetCC();
ICCItem *pResult = CC.CreateSymbolTable();
for (i = 0; i < damageCount; i++)
{
DamageTypes iDamageType = (DamageTypes)i;
DamageDesc Damage(iDamageType, DiceRange(0, 0, 1));
int iDefault;
int iDamageAdj;
GetAdjAndDefault(iDamageType, &iDamageAdj, &iDefault);
if (pEnhancements)
iDamageAdj = iDamageAdj * pEnhancements->GetDamageAdj(Damage) / 100;
pResult->SetIntegerAt(CC, ::GetDamageType(iDamageType), iDamageAdj);
}
return pResult;
}
ICCItem *CDamageAdjDesc::GetHPBonusProperty (const CItemEnhancementStack *pEnhancements) const
// GetHPBonusProperty
//
// Returns an array of hp bonuses, one for each damage type
{
int i;
CCodeChain &CC = g_pUniverse->GetCC();
ICCItem *pResult = CC.CreateSymbolTable();
for (i = 0; i < damageCount; i++)
{
DamageTypes iDamageType = (DamageTypes)i;
DamageDesc Damage(iDamageType, DiceRange(0, 0, 1));
int iDefault;
int iDamageAdj;
GetAdjAndDefault(iDamageType, &iDamageAdj, &iDefault);
if (pEnhancements)
iDamageAdj = iDamageAdj * pEnhancements->GetDamageAdj(Damage) / 100;
int iBonus = GetBonusFromAdj(iDamageAdj, iDefault);
if (iBonus == -100)
pResult->SetStringAt(CC, ::GetDamageType(iDamageType), CONSTLIT("immune"));
else
pResult->SetIntegerAt(CC, ::GetDamageType(iDamageType), iBonus);
}
return pResult;
}
ALERROR CDamageAdjDesc::InitFromArray (int *pTable)
// InitFromArray
//
// Initializes from an array. The array must be at least damageCount entries
// long.
//
// In this path there is no need for Bind.
{
int i;
for (i = 0; i < damageCount; i++)
{
m_Desc[i].dwAdjType = adjAbsolute;
m_Desc[i].dwAdjValue = (DWORD)pTable[i];
m_iDamageAdj[i] = pTable[i];
}
return NOERROR;
}
ALERROR CDamageAdjDesc::InitFromDamageAdj (SDesignLoadCtx &Ctx, const CString &sAttrib, bool bNoDefault)
// InitFromDamageAdj
//
// Loads a damage adjustment descriptor as follows:
//
// 100,100,...
// laser:100; kinetic:100; ...
{
ALERROR error;
int i;
// Short-circuit
if (sAttrib.IsBlank())
{
for (i = 0; i < damageCount; i++)
{
m_Desc[i].dwAdjType = (bNoDefault ? adjAbsolute : adjDefault);
m_Desc[i].dwAdjValue = (bNoDefault ? 100 : 0);
}
return NOERROR;
}
// We expect a list of damageAdj percent values, either with a damageType
// label or ordered by damageType.
TArray<CString> DamageAdj;
if (error = ParseDamageTypeList(sAttrib, &DamageAdj))
{
Ctx.sError = CONSTLIT("Invalid damageAdj definition.");
return error;
}
// Apply damage adj
for (i = 0; i < damageCount; i++)
{
if (DamageAdj[i].IsBlank())
{
m_Desc[i].dwAdjType = (bNoDefault ? adjAbsolute : adjDefault);
m_Desc[i].dwAdjValue = (bNoDefault ? 100 : 0);
}
else
{
int iValue = strToInt(DamageAdj[i], 0);
if (iValue < 0)
{
Ctx.sError = CONSTLIT("damageAdj values cannot be negative.");
return ERR_FAIL;
}
else if (iValue > 32767)
{
Ctx.sError = strPatternSubst(CONSTLIT("damageAdj value is out of range: %d."), iValue);
return ERR_FAIL;
}
m_Desc[i].dwAdjType = adjAbsolute;
m_Desc[i].dwAdjValue = (WORD)iValue;
// If this table doesn't have a default, then it is the default table.
// Initialize m_iDamageAdj so we don't have to bind.
if (bNoDefault)
m_iDamageAdj[i] = iValue;
}
}
// Done
return NOERROR;
}
ALERROR CDamageAdjDesc::InitFromHPBonus (SDesignLoadCtx &Ctx, const CString &sAttrib)
// InitFromHPBonus
//
// Loads an HP bonus descriptor as follows:
//
// +25, -25, ...
// laser:+25; kinetic:-25; ...
{
ALERROR error;
int i;
// Short-circuit null values
if (sAttrib.IsBlank())
{
for (i = 0; i < damageCount; i++)
{
m_Desc[i].dwAdjType = adjDefault;
m_Desc[i].dwAdjValue = 0;
}
return NOERROR;
}
// We expect a list of percent adjustments
TArray<CString> DamageAdj;
if (error = ParseDamageTypeList(sAttrib, &DamageAdj))
{
Ctx.sError = CONSTLIT("Invalid hpBonus definition.");
return error;
}
// Apply damage adj
for (i = 0; i < damageCount; i++)
{
// An omitted value means default
if (DamageAdj[i].IsBlank())
{
m_Desc[i].dwAdjType = adjDefault;
m_Desc[i].dwAdjValue = 0;
}
// A star means no damage
else if (*DamageAdj[i].GetASCIIZPointer() == '*')
{
m_Desc[i].dwAdjType = adjAbsolute;
m_Desc[i].dwAdjValue = 0;
}
// Otherwise, this is a relative value
else
{
bool bNull;
int iValue;
iValue = strToInt(DamageAdj[i], 0, &bNull);
if (bNull)
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid hpBonus value: %s."), DamageAdj[i]);
return ERR_FAIL;
}
if (iValue == 0)
{
m_Desc[i].dwAdjType = adjDefault;
m_Desc[i].dwAdjValue = 0;
}
else
{
if (iValue > 32767 || iValue < -32768)
{
Ctx.sError = strPatternSubst(CONSTLIT("hpBonus is out of range: %d."), iValue);
return ERR_FAIL;
}
m_Desc[i].dwAdjType = adjRelative;
m_Desc[i].dwAdjValue = (WORD)(DWORD)iValue;
}
}
}
return NOERROR;
}
ALERROR CDamageAdjDesc::InitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, bool bIsDefault)
// InitFromXML
//
// Initialize from XML.
//
// If bIsDefault is TRUE then we don't need to bind.
{
ALERROR error;
int i;
CString sValue;
if (pDesc->FindAttribute(HP_BONUS_ATTRIB, &sValue))
{
if (bIsDefault)
{
Ctx.sError = CONSTLIT("Default damageAdj tables must have absolute values.");
return ERR_FAIL;
}
if (error = InitFromHPBonus(Ctx, sValue))
return error;
}
else if (pDesc->FindAttribute(DAMAGE_ADJ_ATTRIB, &sValue))
{
if (error = InitFromDamageAdj(Ctx, sValue, bIsDefault))
return error;
}
else
{
if (bIsDefault)
{
Ctx.sError = CONSTLIT("Default damageAdj tables must have absolute values.");
return ERR_FAIL;
}
for (i = 0; i < damageCount; i++)
{
m_Desc[i].dwAdjType = adjDefault;
m_Desc[i].dwAdjValue = 0;
}
}
// Done
return NOERROR;
}
bool CDamageAdjDesc::IsEmpty (void) const
// IsEmpty
//
// Returns TRUE if all values are default.
{
int i;
for (i = 0; i < damageCount; i++)
if (m_Desc[i].dwAdjType != adjDefault)
return false;
return true;
}