-
Notifications
You must be signed in to change notification settings - Fork 4
/
UsefulModules.cs
226 lines (213 loc) · 11.4 KB
/
UsefulModules.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
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;
using CommNet.Occluders;
namespace BeamedPowerStandalone
{
public class VesselFinder
{
// Loading all vessels that have WirelessSource module, and adding them to a list to use later
public void SourceData(string vesselName, out List<Vessel> vesselList, out List<double> excess, out List<double> constant, out List<string> target, out List<string> wave)
{
ConfigNode Node = ConfigNode.Load(KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/persistent.sfs");
ConfigNode FlightNode = Node.GetNode("GAME").GetNode("FLIGHTSTATE");
vesselList = new List<Vessel>(); excess = new List<double>();
constant = new List<double>(); target = new List<string>();
wave = new List<string>();
foreach (ConfigNode vesselnode in FlightNode.GetNodes("VESSEL"))
{
if (vesselnode.GetValue("name") != vesselName)
{
foreach (ConfigNode partnode in vesselnode.GetNodes("PART"))
{
if (partnode.HasNode("MODULE"))
{
foreach (ConfigNode module in partnode.GetNodes("MODULE"))
{
if (module.GetValue("name") == "WirelessSource")
{
foreach (Vessel vessel in FlightGlobals.Vessels)
{
if (vesselnode.GetValue("name") == vessel.GetDisplayName())
{
vesselList.Add(vessel);
if (vessel.loaded)
{
foreach (Part part in vessel.Parts)
{
if (part.Modules.Contains<WirelessSource>())
{
excess.Add(Convert.ToDouble(part.Modules.GetModule<WirelessSource>().Fields.GetValue("Excess")));
constant.Add(Convert.ToDouble(part.Modules.GetModule<WirelessSource>().Fields.GetValue("Constant")));
target.Add(Convert.ToString(part.Modules.GetModule<WirelessSource>().Fields.GetValue("TransmittingTo")));
wave.Add(Convert.ToString(part.Modules.GetModule<WirelessSource>().Fields.GetValue("Wavelength")));
break;
}
}
}
else
{
excess.Add(Convert.ToDouble(module.GetValue("Excess")));
constant.Add(Convert.ToDouble(module.GetValue("Constant")));
target.Add(module.GetValue("TransmittingTo"));
wave.Add(module.GetValue("Wavelength"));
}
break;
}
}
break;
}
else if (module.GetValue("name") == "WirelessReflector")
{
foreach (Vessel vessel in FlightGlobals.Vessels)
{
if (vesselnode.GetValue("name") == vessel.GetDisplayName())
{
vesselList.Add(vessel);
if (vessel.loaded)
{
foreach (Part part in vessel.Parts)
{
if (part.Modules.Contains<WirelessReflector>())
{
excess.Add(Convert.ToDouble(part.Modules.GetModule<WirelessReflector>().Fields.GetValue("Excess")));
constant.Add(Convert.ToDouble(part.Modules.GetModule<WirelessReflector>().Fields.GetValue("Constant")));
target.Add(Convert.ToString(part.Modules.GetModule<WirelessReflector>().Fields.GetValue("TransmittingTo")));
wave.Add(Convert.ToString(part.Modules.GetModule<WirelessReflector>().Fields.GetValue("Wavelength")));
break;
}
}
}
else
{
excess.Add(Convert.ToDouble(module.GetValue("Excess")));
constant.Add(Convert.ToDouble(module.GetValue("Constant")));
target.Add(module.GetValue("TransmittingTo"));
wave.Add(module.GetValue("Wavelength"));
}
}
}
}
}
}
}
}
}
}
// gets all receiver spacecraft's confignodes from savefile
public void ReceiverData(string vesselName, out List<ConfigNode> receiversList)
{
ConfigNode Node = ConfigNode.Load(KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/persistent.sfs");
ConfigNode FlightNode = Node.GetNode("GAME").GetNode("FLIGHTSTATE");
receiversList = new List<ConfigNode>();
foreach (ConfigNode vesselnode in FlightNode.GetNodes("VESSEL"))
{
if (vesselnode.GetValue("name") != vesselName)
{
foreach (ConfigNode partnode in vesselnode.GetNodes("PART"))
{
if (partnode.HasNode("MODULE"))
{
foreach (ConfigNode module in partnode.GetNodes("MODULE"))
{
if (module.GetValue("name") == "WirelessReceiver")
{
receiversList.Add(vesselnode);
break;
}
else if (module.GetValue("name") == "WirelessReflector" | module.GetValue("name") == "ThermalEngine")
{
receiversList.Add(vesselnode);
break;
}
else if (module.GetValue("name") == "AblativeEngine" | module.GetValue("name") == "PhotonSail")
{
receiversList.Add(vesselnode);
break;
}
}
}
}
}
}
}
}
public class PlanetOcclusion
{
// checks for occlusion by each celestial body
public void IsOccluded(Vector3d source, Vector3d dest, string wavelength, out CelestialBody celestialBody, out bool occluded)
{
bool planetocclusion = HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().planetOcclusion;
Transform transform2; double radius2; celestialBody = new CelestialBody(); occluded = new bool();
if (planetocclusion)
{
for (int x = 0; x < FlightGlobals.Bodies.Count; x++)
{
transform2 = FlightGlobals.Bodies[x].transform;
radius2 = FlightGlobals.Bodies[x].Radius;
celestialBody = FlightGlobals.Bodies[x];
radius2 *= (wavelength == "Long") ? 0.7 : 0.95;
OccluderHorizonCulling occlusion = new OccluderHorizonCulling(transform2, radius2, radius2, radius2);
occlusion.Update();
occluded = occlusion.Raycast(source, dest);
if (occluded == true)
{
break;
}
}
}
else
{
occluded = false;
}
}
}
public class RelativisticEffects
{
bool relativistic; const float c = 299792452; Vector3d prevPos = Vector3d.zero;
string exceeded_c = Localizer.Format("#LOC_BeamedPower_ExceededC");
public double RedOrBlueShift(Vessel source, Vessel dest, string state, out string status)
{
relativistic = HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().relativistic; double powerMult;
double v = Vector3d.Magnitude(source.orbit.GetWorldSpaceVel() - dest.orbit.GetWorldSpaceVel());
v *= Math.Cos(Vector3d.Angle((source.GetWorldPos3D() - dest.GetWorldPos3D()),
(source.orbit.GetWorldSpaceVel() - dest.orbit.GetWorldSpaceVel())));
if (relativistic)
{
if (v >= c - 1)
{
powerMult = 0;
status = exceeded_c;
}
else
{
powerMult = Math.Sqrt((1 + v / c) / (1 - v / c));
status = state;
}
}
else
{
powerMult = 1;
status = state;
}
return powerMult;
}
public bool WarpDriveEngaged(Part part)
{
relativistic = HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().relativistic; bool warping = false;
if (relativistic)
{
Vector3d position = part.vessel.GetWorldPos3D();
double displacement = Vector3d.Distance(position, prevPos);
double v = displacement / TimeWarp.fixedDeltaTime;
if (v > part.vessel.orbit.GetWorldSpaceVel().magnitude * 2d)
{
warping = true;
}
prevPos = position;
}
return warping;
}
}
}