/
VesselHealthInfo.cs
154 lines (142 loc) · 7.9 KB
/
VesselHealthInfo.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KerbalHealth
{
public class VesselHealthInfo
{
public static Dictionary<Guid, VesselHealthInfo> Cache = new Dictionary<Guid, VesselHealthInfo>();
public static VesselHealthInfo GetVesselInfo(Vessel v)
{
if (Cache.ContainsKey(v.id)) return Cache[v.id];
return Cache[v.id] = new VesselHealthInfo(v);
}
public static VesselHealthInfo GetVesselInfo(ProtoCrewMember pcm)
{
if (Core.IsInEditor)
{
if (Cache.Count > 0)
{
Core.Log("In editor and VesselHealthInfo found in cache. Retrieving.");
return Cache.First().Value;
}
Core.Log("In editor and VesselHealthInfo not found in cache. Calculating and adding to cache.");
return Cache[Guid.Empty] = new VesselHealthInfo(EditorLogic.SortedShipList);
}
return GetVesselInfo(Core.KerbalVessel(pcm));
}
public double HPChange { get; set; }
public double Space { get; set; }
public double RecuperationPower { get; set; }
public double MaxRecuperaction { get; set; }
public double Recuperation => Math.Min(RecuperationPower, MaxRecuperaction);
public double Decay { get; set; }
public double Shielding { get; set; }
public double PartsRadiation { get; set; }
public Dictionary<string, double> BonusSums { get; set; } = new Dictionary<string, double>();
public Dictionary<string, double> FreeMultipliers { get; set; } = new Dictionary<string, double>();
public Dictionary<string, double> MinMultipliers { get; set; } = new Dictionary<string, double>();
public Dictionary<string, double> MaxMultipliers { get; set; } = new Dictionary<string, double>();
public double GetMultiplier(string factorId, int crewCount)
{
double res = 1 - BonusSums[factorId] / crewCount;
if (res < 1) res = Math.Max(res, MinMultipliers[factorId]); else res = Math.Min(res, MaxMultipliers[factorId]);
return res * FreeMultipliers[factorId];
}
/// <summary>
/// Checks a part for its effects on the kerbal
/// </summary>
/// <param name="part"></param>
/// <param name="crew"></param>
public void ProcessPart(Part part, bool crewInPart)
{
if (part == null)
{
Core.Log("VesselHealthInfo: 'part' is null. Unless the kerbal is on EVA, this is probably an error.", Core.LogLevel.Important);
return;
}
foreach (ModuleKerbalHealth mkh in part.FindModulesImplementing<ModuleKerbalHealth>())
if (mkh.IsModuleActive && (!mkh.partCrewOnly ^ crewInPart))
{
Core.Log("Processing " + mkh.Title + " Module in " + part.name + ".");
Core.Log("PartCrewOnly: " + mkh.partCrewOnly + "; CrewInPart: " + crewInPart + "; condition: " + (!mkh.partCrewOnly ^ crewInPart));
HPChange += mkh.hpChangePerDay;
Space += mkh.space;
if (mkh.recuperation != 0)
{
RecuperationPower += mkh.RecuperationPower;
Core.Log("Module's recuperation power = " + mkh.RecuperationPower);
MaxRecuperaction = Math.Max(MaxRecuperaction, mkh.recuperation);
}
Decay += mkh.DecayPower;
// Processing factor multiplier
if ((mkh.multiplier != 1) && (mkh.MultiplyFactor != null))
{
if (mkh.crewCap > 0) BonusSums[mkh.multiplyFactor] += (1 - mkh.multiplier) * Math.Min(mkh.crewCap, mkh.CappedAffectedCrewCount);
else FreeMultipliers[mkh.MultiplyFactor.Name] *= mkh.multiplier;
if (mkh.multiplier > 1) MaxMultipliers[mkh.MultiplyFactor.Name] = Math.Max(MaxMultipliers[mkh.MultiplyFactor.Name], mkh.multiplier);
else MinMultipliers[mkh.MultiplyFactor.Name] = Math.Min(MinMultipliers[mkh.MultiplyFactor.Name], mkh.multiplier);
}
Core.Log((HPChange != 0 ? "HP change after this module: " + HPChange + ". " : "") + (mkh.MultiplyFactor != null ? "Bonus to " + mkh.MultiplyFactor.Name + ": " + BonusSums[mkh.MultiplyFactor.Name] + ". Free multiplier: " + FreeMultipliers[mkh.MultiplyFactor.Name] + "." : ""));
Shielding += mkh.shielding;
if (mkh.shielding != 0) Core.Log("Shielding of this module is " + mkh.shielding + ".");
PartsRadiation += mkh.radioactivity;
if (mkh.radioactivity != 0) Core.Log("Radioactive emission of this module is " + mkh.radioactivity);
}
}
public void ProcessParts(List<Part> parts)
{
Core.Log("Processing " + parts.Count + " parts...");
foreach (Part p in parts)
{
ProcessPart(p, false);
foreach (KeyValuePair<int, double> res in Core.ResourceShielding)
{
p.GetConnectedResourceTotals(res.Key, ResourceFlowMode.NO_FLOW, out double amount, out double maxAmount);
if (amount != 0) Core.Log("Part " + p.name + " contains " + amount + " / " + maxAmount + " of shielding resource " + res.Key);
Shielding += res.Value * amount;
}
}
}
public override string ToString()
{
string res = "";
if (HPChange != 0) res += "\nHP change per day: " + HPChange.ToString("F2");
if (Space != 0) res += "\nSpace: " + Space.ToString("F1");
if (RecuperationPower != 0) res += "\nRecuperation Power: " + RecuperationPower.ToString("F1") + "% (max " + MaxRecuperaction.ToString("F1") + "%)";
if (Decay != 0) res += "\nDecay: " + Shielding.ToString("F1") + "%";
if (Shielding != 0) res += "\nShielding: " + Shielding.ToString("F1");
if (PartsRadiation != 0) res += "\nParts radiation: " + PartsRadiation.ToString("F0");
foreach (HealthFactor f in Core.Factors)
{
if (BonusSums[f.Name] != 0) res += "\n" + f.Name + " bonus sum: " + BonusSums[f.Name];
if (FreeMultipliers[f.Name] != 1) res += "\n" + f.Name + " free multiplier: " + FreeMultipliers[f.Name];
if (MinMultipliers[f.Name] != 1) res += "\n" + f.Name + " min multiplier: " + MinMultipliers[f.Name];
if (MaxMultipliers[f.Name] != 1) res += "\n" + f.Name + " max multiplier: " + MaxMultipliers[f.Name];
}
if (BonusSums["All"] != 0) res += "\nWildcard bonus sum: " + BonusSums["All"];
if (FreeMultipliers["All"] != 1) res += "\nWildcard free multiplier: " + FreeMultipliers["All"];
if (MinMultipliers["All"] != 1) res += "\nWildcard min multiplier: " + MinMultipliers["All"];
if (MaxMultipliers["All"] != 1) res += "\nWildcard max multiplier: " + MaxMultipliers["All"];
return res.Trim();
}
public VesselHealthInfo Clone() => (VesselHealthInfo)this.MemberwiseClone();
public VesselHealthInfo()
{
foreach (HealthFactor f in Core.Factors)
{
BonusSums[f.Name] = 0;
FreeMultipliers[f.Name] = 1;
MinMultipliers[f.Name] = 1;
MaxMultipliers[f.Name] = 1;
}
BonusSums["All"] = 0;
FreeMultipliers["All"] = 1;
MinMultipliers["All"] = 1;
MaxMultipliers["All"] = 1;
}
public VesselHealthInfo(Vessel v) : this() => ProcessParts(v.Parts);
public VesselHealthInfo(List<Part> parts) : this() => ProcessParts(parts);
}
}