-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRadStorm.cs
139 lines (119 loc) · 4.06 KB
/
RadStorm.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
using System;
namespace KerbalHealth
{
public enum RadStormTargetType
{
None = 0,
Body,
Vessel
};
/// <summary>
/// Represents a (potential) solar radiation storm (CME)
/// </summary>
public class RadStorm : IConfigNode
{
public const string ConfigNodeName = "RADSTORM";
string name;
public RadStormTargetType Target { get; set; }
public string Name
{
get => Target == RadStormTargetType.Vessel ? Vessel?.vesselName : name;
set => name = value;
}
public string VesselId { get; set; }
public double Magnitutde { get; set; }
public double Time { get; set; }
/// <summary>
/// Must be a planet (i.e. orbiting the sun)
/// </summary>
public CelestialBody CelestialBody
{
get => Target == RadStormTargetType.Body ? FlightGlobals.GetBodyByName(Name) : null;
set
{
Target = RadStormTargetType.Body;
Name = value?.name;
}
}
public Vessel Vessel
{
get => Target == RadStormTargetType.Vessel ? FlightGlobals.FindVessel(new Guid(VesselId)) : null;
set
{
Target = RadStormTargetType.Vessel;
VesselId = value.id.ToString();
}
}
public double DistanceFromSun
{
get
{
switch (Target)
{
case RadStormTargetType.Body:
return CelestialBody.orbit.altitude + Sun.Instance.sun.Radius;
case RadStormTargetType.Vessel:
return Vessel.GetDistanceToSun();
}
return 0;
}
}
public RadStorm(CelestialBody body) => CelestialBody = body;
public RadStorm(Vessel vessel) => Vessel = vessel;
public RadStorm(ConfigNode node) => Load(node);
public void Save(ConfigNode node)
{
if (Target == RadStormTargetType.None)
{
Core.Log("Trying to save RadStormTarget of type None.", LogLevel.Important);
return;
}
node.AddValue("target", Target.ToString());
if (Target == RadStormTargetType.Vessel)
node.AddValue("id", VesselId);
else node.AddValue("body", Name);
if (Magnitutde > 0)
node.AddValue("magnitude", Magnitutde);
if (Time > 0)
node.AddValue("time", Time);
}
public void Load(ConfigNode node)
{
if (Enum.TryParse(node.GetValue("target"), true, out RadStormTargetType radStormTargetType))
Target = radStormTargetType;
else
{
Core.Log($"No valid 'target' node found in RadStorm ConfigNode:\r\n{node}", LogLevel.Error);
Target = RadStormTargetType.None;
return;
}
if (Target == RadStormTargetType.Vessel)
{
VesselId = node.GetString("id");
if (FlightGlobals.FindVessel(new Guid(VesselId)) == null)
{
Core.Log($"Vessel id {VesselId} from RadStorm ConfigNode not found.", LogLevel.Error);
Target = RadStormTargetType.None;
return;
}
}
else Name = node.GetString("body");
Magnitutde = node.GetDouble("magnitude");
Time = node.GetDouble("time");
}
public bool Affects(ProtoCrewMember pcm)
{
Vessel v = pcm.GetVessel();
if (v == null)
return false;
switch (Target)
{
case RadStormTargetType.Body:
return v.mainBody.GetPlanet()?.name == Name;
case RadStormTargetType.Vessel:
return v.id.ToString() == VesselId;
}
return false;
}
}
}