This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Character.cs
357 lines (306 loc) · 15.6 KB
/
Character.cs
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
using System;
namespace ShogunOptimizer
{
public class Character
{
public double Level = 90;
public int AttackLevel = 10;
public int SkillLevel = 10;
public int BurstLevel = 10;
public int Constellation = 0;
public double BaseHp;
public double BaseAtk;
public double BaseDef;
public double AscensionStat;
public StatType AscensionStatType;
public double[] Stats = new double[Enum.GetValues<StatType>().Length];
public Character()
{
Stats[(int)StatType.CritRate] = .05;
Stats[(int)StatType.CritDamage] = .50;
Stats[(int)StatType.EnergyRecharge] = 1.00;
}
public virtual double GetBaseAtk(Build build) => BaseAtk + build.Weapon.BaseAtk;
public virtual double GetAtk(Build build) => GetBaseAtk(build) * (1 + GetStat(StatType.AtkPercent, build)) + GetStat(StatType.AtkFlat, build);
public virtual double GetMaxHp(Build build) => BaseHp * (1 + GetStat(StatType.HpPercent, build)) + GetStat(StatType.HpFlat, build);
public virtual double GetDef(Build build) => BaseDef * (1 + GetStat(StatType.DefPercent, build)) + GetStat(StatType.DefFlat, build);
public virtual double CalculateDamage(Build build, double baseDamage, DamageType damageType, Element element, HitType hitType, Enemy enemy)
=> (baseDamage + GetExtraDamage(build, damageType, element)) * GetMultiplier(build, damageType, element, hitType, enemy);
public virtual double GetExtraDamage(Build build, DamageType damageType, Element element)
{
var extraDamage = GetStat(StatType.ExtraDmg, build);
switch (damageType)
{
case DamageType.Normal:
extraDamage += GetStat(StatType.AttackExtraDmg, build);
break;
case DamageType.Charged:
extraDamage += GetStat(StatType.ChargedExtraDmg, build);
break;
case DamageType.Plunge:
extraDamage += GetStat(StatType.PlungeExtraDmg, build);
break;
case DamageType.Skill:
extraDamage += GetStat(StatType.SkillExtraDmg, build);
break;
case DamageType.Burst:
extraDamage += GetStat(StatType.BurstExtraDmg, build);
break;
}
extraDamage += GetStat(ElementToExtraDmg(element), build);
return extraDamage;
}
public virtual double GetMultiplier(Build build, DamageType damageType, Element element, HitType hitType, Enemy enemy)
{
var rawMultiplier = GetDmgMultiplier(build, damageType, element) * GetCritMultiplier(build, damageType, hitType);
var reactionMultiplier = GetReactionMultiplier(GetReaction(element, enemy), build);
var resMultiplier = GetResistanceMultiplier(build, element, enemy);
var defMultiplier = GetDefenseMultiplier(build, enemy);
return rawMultiplier * reactionMultiplier * resMultiplier * defMultiplier;
}
public virtual double GetDmgMultiplier(Build build, DamageType damageType, Element element)
{
var multiplier = 1
+ GetStat(StatType.DmgBonus, build)
+ GetStat(ElementToDmgBonus(element), build);
switch (damageType)
{
case DamageType.Normal:
multiplier += GetStat(StatType.AttackDmgBonus, build);
break;
case DamageType.Charged:
multiplier += GetStat(StatType.ChargedDmgBonus, build);
break;
case DamageType.Plunge:
multiplier += GetStat(StatType.PlungeDmgBonus, build);
break;
case DamageType.Skill:
multiplier += GetStat(StatType.SkillDmgBonus, build);
break;
case DamageType.Burst:
multiplier += GetStat(StatType.BurstDmgBonus, build);
break;
}
return multiplier;
}
public virtual double GetCritMultiplier(Build build, DamageType damageType, HitType hitType)
{
if (hitType == HitType.Normal)
return 1;
var critRate = GetStat(StatType.CritRate, build);
var critDamage = GetStat(StatType.CritDamage, build);
switch (damageType)
{
case DamageType.Normal:
critRate += GetStat(StatType.AttackCritRateBonus, build);
critDamage += GetStat(StatType.AttackCritDamageBonus, build);
break;
case DamageType.Charged:
critRate += GetStat(StatType.ChargedCritRateBonus, build);
critDamage += GetStat(StatType.ChargedCritDamageBonus, build);
break;
case DamageType.Plunge:
critRate += GetStat(StatType.PlungeCritRateBonus, build);
critDamage += GetStat(StatType.PlungeCritDamageBonus, build);
break;
case DamageType.Skill:
critRate += GetStat(StatType.SkillCritRateBonus, build);
critDamage += GetStat(StatType.SkillCritDamageBonus, build);
break;
case DamageType.Burst:
critRate += GetStat(StatType.BurstCritRateBonus, build);
critDamage += GetStat(StatType.BurstCritDamageBonus, build);
break;
}
if (hitType == HitType.Critical)
return 1 + critDamage;
if (hitType == HitType.Averaged)
return 1 + Math.Max(0, Math.Min(1.0, critRate)) * critDamage;
throw new InvalidOperationException($"Unknown hit type {hitType}");
}
public double GetResistanceMultiplier(Build build, Element element, Enemy enemy)
{
var resistance = enemy.Resistances[(int)element] - (GetStat(ElementToResShred(element), build) + GetStat(StatType.ResShred, build));
return resistance < 0 ? 1 - resistance / 2 :
resistance < .75 ? 1 - resistance :
1 / (4 * resistance + 1);
}
public double GetDefenseMultiplier(Build build, Enemy enemy)
=> (100 + Level) / ((100 + Level) + (100 + enemy.Level) * (1 - Math.Min(.9, GetStat(StatType.DefShred, build))));
public double GetTransformativeReactionDamage(ElementalReaction reaction, Build build, Enemy enemy)
{
Element element;
double reactionMultiplier, reactionBonus;
switch (reaction)
{
case ElementalReaction.Overloaded:
element = Element.Pyro;
reactionMultiplier = 4;
reactionBonus = GetStat(StatType.OverloadedDmgBonus, build);
break;
case ElementalReaction.Shattered:
element = Element.Physical;
reactionMultiplier = 3;
reactionBonus = GetStat(StatType.ShatterDmgBonus, build);
break;
case ElementalReaction.ElectroCharged:
element = Element.Electro;
reactionMultiplier = 2.4;
reactionBonus = GetStat(StatType.ElectroChargedDmgBonus, build);
break;
case ElementalReaction.Swirl:
element = Element.Anemo;
reactionMultiplier = 1.2;
reactionBonus = GetStat(StatType.SwirlDmgBonus, build);
break;
case ElementalReaction.Superconduct:
element = Element.Cryo;
reactionMultiplier = 1;
reactionBonus = GetStat(StatType.SuperconductDmgBonus, build);
break;
default:
return 0;
}
var em = GetStat(StatType.ElementalMastery, build);
var emMultiplier = 1 + (16 * em) / (2000 + em) + reactionBonus;
var levelMultiplier = GetTransformativeReactionLevelMultiplier();
var resMultiplier = GetResistanceMultiplier(build, element, enemy);
return reactionMultiplier * emMultiplier * levelMultiplier * resMultiplier;
}
public double GetTransformativeReactionLevelMultiplier()
=> Level < 60 ? 0.0002325 * Level * Level * Level + 0.05547 * Level * Level - 0.2523 * Level + 14.47 :
0.00194 * Level * Level * Level - 0.319 * Level * Level + 30.7 * Level - 868;
public double GetReactionMultiplier(ElementalReaction reaction, Build build)
{
double multiplier, reactionBonus;
switch (reaction)
{
case ElementalReaction.Melt:
multiplier = 2.0;
reactionBonus = GetStat(StatType.MeltDmgBonus, build);
break;
case ElementalReaction.Vaporize:
multiplier = 2.0;
reactionBonus = GetStat(StatType.VaporizeDmgBonus, build);
break;
case ElementalReaction.ReverseMelt:
multiplier = 1.5;
reactionBonus = GetStat(StatType.MeltDmgBonus, build);
break;
case ElementalReaction.ReverseVaporize:
multiplier = 1.5;
reactionBonus = GetStat(StatType.VaporizeDmgBonus, build);
break;
default:
return 1.0;
}
var em = GetStat(StatType.ElementalMastery, build);
var elementalMasteryBonus = (2.78 * em) / (1400 + em);
return multiplier * (1 + elementalMasteryBonus + reactionBonus);
}
public static ElementalReaction GetReaction(Element source, Enemy enemy) => source switch
{
Element.Hydro when enemy.AffectedBy == Element.Pyro => ElementalReaction.Vaporize,
Element.Pyro when enemy.AffectedBy == Element.Hydro => ElementalReaction.ReverseVaporize,
Element.Pyro when enemy.AffectedBy == Element.Cryo => ElementalReaction.Melt,
Element.Cryo when enemy.AffectedBy == Element.Pyro => ElementalReaction.ReverseMelt,
Element.Pyro when enemy.AffectedBy == Element.Electro => ElementalReaction.Overloaded,
Element.Electro when enemy.AffectedBy == Element.Pyro => ElementalReaction.Overloaded,
Element.Hydro when enemy.AffectedBy == Element.Electro => ElementalReaction.ElectroCharged,
Element.Electro when enemy.AffectedBy == Element.Hydro => ElementalReaction.ElectroCharged,
Element.Cryo when enemy.AffectedBy == Element.Electro => ElementalReaction.Superconduct,
Element.Electro when enemy.AffectedBy == Element.Cryo => ElementalReaction.Superconduct,
Element.Pyro when enemy.AffectedBy == Element.Dendro => ElementalReaction.Burning,
Element.Anemo when
enemy.AffectedBy == Element.Cryo ||
enemy.AffectedBy == Element.Electro ||
enemy.AffectedBy == Element.Hydro ||
enemy.AffectedBy == Element.Pyro
=> ElementalReaction.Swirl,
_ => ElementalReaction.None,
};
public double GetStat(StatType statType, Build build)
=> build.GetCachedStat(statType, this);
public virtual double CalculateStat(StatType statType, Build build)
{
var stat = Stats[(int)statType];
if (AscensionStatType == statType)
stat += AscensionStat;
foreach ((var weaponStatType, var weaponStatValue) in build.Weapon.Stats)
if (weaponStatType == statType)
stat += weaponStatValue;
stat += build.Weapon.GetStat(statType, build, this);
var setTypes = new Type[5];
var setCount = new int[5];
foreach (var artifact in build.Artifacts)
{
var setType = artifact.Set.GetType();
for (var i = 0; i < setTypes.Length; i++)
if (setTypes[i] == null)
{
setTypes[i] = setType;
stat += artifact.Set.GetStat(statType, build, this, ++setCount[i]);
break;
}
else if (setTypes[i] == setType)
{
stat += artifact.Set.GetStat(statType, build, this, ++setCount[i]);
break;
}
foreach ((var artifactStatType, var artifactStatValue) in artifact.Stats)
if (artifactStatType == statType)
stat += artifactStatValue;
}
return stat;
}
private readonly double[] AttackScalings = new[] { 1.0, 1.08, 1.16, 1.275, 1.35, 1.45, 1.575, 1.7, 1.825, 1.975, 2.125 };
private readonly double[] PercentageScalings = new[] { 1.0, 1.075, 1.15, 1.25, 1.325, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.125 };
private readonly double[] FlatScalings = new[] { 1.0, 1.1, 1.2, 1.325, 1.45, 1.575, 1.725, 1.875, 202.5, 2.2, 2.375, 2.55, 2.75 };
public virtual double GetTalentAttackScaling(int level) => AttackScalings[level - 1];
public virtual double GetTalentPercentageScaling(int level) => PercentageScalings[level - 1];
public virtual double GetTalentFlatScaling(int level) => FlatScalings[level - 1];
public virtual double Calculate(string property, Build build, HitType hitType, Enemy enemy)
{
switch (property)
{
}
return 0;
}
public static StatType ElementToResShred(Element element) => element switch
{
Element.Physical => StatType.PhysicalResShred,
Element.Pyro => StatType.PyroResShred,
Element.Hydro => StatType.HydroResShred,
Element.Cryo => StatType.CryoResShred,
Element.Electro => StatType.ElectroResShred,
Element.Anemo => StatType.AnemoResShred,
Element.Geo => StatType.GeoResShred,
Element.Dendro => StatType.DendroResShred,
_ => throw new NotImplementedException(element.ToString()),
};
public static StatType ElementToDmgBonus(Element element) => element switch
{
Element.Physical => StatType.PhysicalDmgBonus,
Element.Pyro => StatType.PyroDmgBonus,
Element.Hydro => StatType.HydroDmgBonus,
Element.Cryo => StatType.CryoDmgBonus,
Element.Electro => StatType.ElectroDmgBonus,
Element.Anemo => StatType.AnemoDmgBonus,
Element.Geo => StatType.GeoDmgBonus,
Element.Dendro => StatType.DendroDmgBonus,
_ => throw new NotImplementedException(element.ToString()),
};
public static StatType ElementToExtraDmg(Element element) => element switch
{
Element.Physical => StatType.PhysicalExtraDmg,
Element.Pyro => StatType.PyroExtraDmg,
Element.Hydro => StatType.HydroExtraDmg,
Element.Cryo => StatType.CryoExtraDmg,
Element.Electro => StatType.ElectroExtraDmg,
Element.Anemo => StatType.AnemoExtraDmg,
Element.Geo => StatType.GeoExtraDmg,
Element.Dendro => StatType.DendroExtraDmg,
_ => throw new NotImplementedException(element.ToString()),
};
}
}