-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFactorMultiplierList.cs
82 lines (71 loc) · 2.73 KB
/
FactorMultiplierList.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
using System;
using System.Collections.Generic;
using System.Text;
namespace KerbalHealth
{
public class FactorMultiplierList : List<FactorMultiplier>
{
public FactorMultiplier this[HealthFactor factor]
{
get => Find(factor) ?? new FactorMultiplier(factor);
set
{
if (factor != value.Factor)
{
Core.Log($"Failed assignment operation for factor multiplier for {value.FactorName}. Index factor is {factor.Name}.", LogLevel.Error);
throw new IndexOutOfRangeException();
}
for (int i = 0; i < Count; i++)
if (this[i].Factor == value.Factor)
{
this[i] = value;
return;
}
base.Add(value);
}
}
public FactorMultiplierList()
{
for (int i = 0; i < Core.Factors.Count; i++)
base.Add(new FactorMultiplier(Core.Factors[i]));
base.Add(new FactorMultiplier());
}
public FactorMultiplierList(FactorMultiplierList list)
: base(list)
{ }
public new void Add(FactorMultiplier factorMultiplier)
{
for (int i = 0; i < Count; i++)
if (this[i].Factor == factorMultiplier.Factor)
{
this[i].CombineWith(factorMultiplier);
return;
}
base.Add(factorMultiplier);
}
public FactorMultiplier Find(HealthFactor factor) => Find(fm => fm.Factor == factor);
public double GetMultiplier(HealthFactor healthFactor) => this[healthFactor].Multiplier * this[null].Multiplier;
public Dictionary<HealthFactor, double> ApplyToFactors(Dictionary<HealthFactor, double> factors)
{
Dictionary<HealthFactor, double> res = new Dictionary<HealthFactor, double>(factors);
double allMultiplier = this[null].Multiplier;
foreach (KeyValuePair<HealthFactor, double> kvp in factors)
res[kvp.Key] *= this[kvp.Key].Multiplier * allMultiplier;
return res;
}
public FactorMultiplierList CombineWith(FactorMultiplierList list)
{
for (int i = 0; i < list.Count; i++)
this[list[i].Factor]?.CombineWith(list[i]);
return this;
}
public override string ToString()
{
StringBuilder res = new StringBuilder();
for (int i = 0; i < Count; i++)
if (!this[i].IsTrivial)
res.AppendLine(this[i].ToString());
return res.ToStringAndRelease();
}
}
}