-
Notifications
You must be signed in to change notification settings - Fork 4
/
BeamedPowerReflector.cs
364 lines (316 loc) · 16.5 KB
/
BeamedPowerReflector.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
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;
namespace BeamedPowerStandalone
{
public class WirelessReflector : PartModule
{
// parameters set in part.cfg file
[KSPField(isPersistant = false)]
public float Reflectivity;
[KSPField(isPersistant = false)]
public float ReflectorDiameter;
// optional parameters also set in part.cfg, only needed if the reflector can amplify
[KSPField(isPersistant = false)]
public string CanAmplify = "False";
[KSPField(isPersistant = false)]
public float Efficiency = 0f;
[KSPField(isPersistant = false)]
public string wavelength = "None";
[KSPField(isPersistant = false)]
public float maxCoreTemp = 900f;
[KSPField(isPersistant = false)]
public float maxSkinTemp = 1200f;
// counter variables used to cycle through transmitter and receiver lists respectively
[KSPField(isPersistant = true)]
public int transmitterCounter;
[KSPField(isPersistant = true)]
public int receiverCounter;
// variables on transmitter
[KSPField(isPersistant = true)]
public float Excess;
[KSPField(isPersistant = true)]
public float Constant;
[KSPField(isPersistant = true)]
public string Wavelength;
[KSPField(isPersistant = true)]
public float resourceConsumption;
// reflector specific variables
[KSPField(guiName = "Beam Reflector", isPersistant = true, guiActive = true, guiActiveEditor = false), UI_Toggle(scene = UI_Scene.Flight)]
public bool IsEnabled;
[KSPField(guiName = "Power Reflected", guiActive = true, guiActiveEditor = false, isPersistant = false, guiUnits = "kW")]
public float PowerReflected;
[KSPField(guiName = "Amplify power", guiActive = true, guiActiveEditor = false, isPersistant = false, guiUnits = "x"), UI_FloatRange(minValue = 1, maxValue = 5, stepIncrement = 0.05f, scene = UI_Scene.Flight)]
public float AmplifyMult = 1f;
[KSPField(guiName = "Status", guiActive = true, guiActiveEditor = false, isPersistant = false)]
public string State;
[KSPField(guiName = "Core Temperature", groupName = "HeatInfo", groupDisplayName = "Heat Info", groupStartCollapsed = false, guiActive = true, guiActiveEditor = false, isPersistant = false)]
public float CoreTemp;
[KSPField(guiName = "Skin Temperature", groupName = "HeatInfo", guiActive = true, guiActiveEditor = false, isPersistant = false)]
public float SkinTemp;
[KSPField(guiName = "Waste Heat", groupName = "HeatInfo", guiActive = true, guiActiveEditor = false, isPersistant = false, guiUnits = "kW")]
public float WasteHeat;
// adding vessel names for 'from' and 'to' to part right-click menu in flight
[KSPField(guiName = "From", guiActive = true, guiActiveEditor = false, isPersistant = false)]
public string TransmitterName = Localizer.Format("#LOC_BeamedPower_Vessel_None");
[KSPField(guiName = "To", guiActive = true, guiActiveEditor = false, isPersistant = true)]
public string TransmittingTo = Localizer.Format("#LOC_BeamedPower_Vessel_None");
// declaring frequently used variables
VesselFinder vesselFinder = new VesselFinder(); int frames;
readonly int EChash = PartResourceLibrary.Instance.GetDefinition("ElectricCharge").id; int initFrames;
ModuleCoreHeat coreHeat; ReceivedPower receiver = new ReceivedPower(); double heatModifier;
string operational = Localizer.Format("#LOC_BeamedPower_status_Operational");
string ExceedTempLimit = Localizer.Format("#LOC_BeamedPower_status_ExceededTempLimit");
// KSPEvent buttons to cycle through vessels lists
[KSPEvent(guiName = "Cycle through transmitter vessels", guiActive = true, guiActiveEditor = false, requireFullControl = true)]
public void TransmitterCounter()
{
transmitterCounter += 1;
}
[KSPEvent(guiName = "Cycle through receiver vessels", guiActive = true, guiActiveEditor = false, requireFullControl = true)]
public void ReceiverCounter()
{
receiverCounter += 1;
}
// initialise variables
public void Start()
{
initFrames = 0; frames = 0;
Fields["CoreTemp"].guiUnits = "K/" + maxCoreTemp.ToString() + "K";
Fields["SkinTemp"].guiUnits = "K/" + maxSkinTemp.ToString() + "K";
if (CanAmplify == "False")
{
Fields["AmplifyMult"].guiActive = false;
}
SetHeatParams();
SetLocalization();
}
private void SetLocalization()
{
//flight
Fields["IsEnabled"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_BeamReflector");
Fields["PowerReflected"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_PowerReflected");
Fields["AmplifyMult"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_AmplifyPower");
Fields["State"].guiName = Localizer.Format("#LOC_BeamedPower_Status");
Fields["CoreTemp"].guiName = Localizer.Format("#LOC_BeamedPower_CoreTemp");
Fields["SkinTemp"].guiName = Localizer.Format("#LOC_BeamedPower_SkinTemp");
Fields["WasteHeat"].guiName = Localizer.Format("#LOC_BeamedPower_WasteHeat");
Fields["CoreTemp"].group.displayName = Localizer.Format("#LOC_BeamedPower_HeatInfo");
Fields["TransmitterName"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_From");
Fields["TransmittingTo"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_To");
Events["TransmitterCounter"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_CycleTransmitters");
Events["ReceiverCounter"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_CycleReceivers");
Actions["ToggleReflector"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_ToggleReflector");
Actions["ActivateReflector"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_ActivateReflector");
Actions["DeactivateReflector"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_DeactivateReflector");
//editor
Fields["ReceivedPower"].guiName = Localizer.Format("#LOC_BeamedPower_RecvPower");
Fields["Amplify"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessReflector_AmplifyPower");
Fields["ReflectedPower"].guiName = Localizer.Format("#LOC_BeamedPower_CalcResult");
}
private void SetHeatParams()
{
this.part.AddModule("ModuleCoreHeat");
coreHeat = this.part.Modules.GetModule<ModuleCoreHeat>();
coreHeat.CoreTempGoal = maxCoreTemp * 1.4; // placeholder value, there is no optimum temperature
coreHeat.CoolantTransferMultiplier *= 2d;
coreHeat.HeatRadiantMultiplier *= 2d;
}
private void AddHeatToCore()
{
CoreTemp = (float)(Math.Round(coreHeat.CoreTemperature, 1));
SkinTemp = (float)(Math.Round(this.part.skinTemperature, 1));
if (CoreTemp > maxCoreTemp | SkinTemp > maxSkinTemp)
{
State = ExceedTempLimit;
IsEnabled = false;
}
if (State == ExceedTempLimit & (CoreTemp >= maxCoreTemp * 0.7 | SkinTemp >= maxSkinTemp * 0.7))
{
IsEnabled = false;
}
else if (CoreTemp < maxCoreTemp * 0.7 & SkinTemp < maxSkinTemp * 0.7)
{
State = operational;
}
heatModifier = (double)HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().PercentHeat / 100;
double heatExcess = (1 - Efficiency) * Mathf.Clamp(resourceConsumption, 0f, 50000f/Efficiency) * heatModifier;
WasteHeat = (float)Math.Round(heatExcess, 1);
coreHeat.AddEnergyToCore(heatExcess * 0.7 * TimeWarp.fixedDeltaTime); // first converted to kJ
this.part.AddSkinThermalFlux(heatExcess * 0.2); // add some heat to skin
}
[KSPAction(guiName = "Toggle Power Reflector")]
public void ToggleReflector(KSPActionParam param)
{
IsEnabled = (IsEnabled) ? false : true;
}
[KSPAction(guiName = "Activate Power Reflector")]
public void ActivateReflector(KSPActionParam param)
{
IsEnabled = (IsEnabled) ? true : true;
}
[KSPAction(guiName = "Deactivate Power Reflector")]
public void DeactivateReflector(KSPActionParam param)
{
IsEnabled = (IsEnabled) ? false : false;
}
private void SyncAnimationState()
{
if (this.part.Modules.Contains<ModuleDeployableAntenna>() &&
this.part.Modules.GetModule<ModuleDeployableAntenna>().deployState != ModuleDeployableAntenna.DeployState.EXTENDED)
{
IsEnabled = false;
}
else if (this.part.Modules.Contains<ModuleDeployablePart>() &&
this.part.Modules.GetModule<ModuleDeployablePart>().deployState != ModuleDeployablePart.DeployState.EXTENDED)
{
IsEnabled = false;
}
}
// adding part info to part description tab in editor
public string GetModuleTitle()
{
return "BeamedPowerReflector";
}
public override string GetModuleDisplayName()
{
return Localizer.Format("#LOC_BeamedPower_WirelessReflector_ModuleName");
}
public override string GetInfo()
{
string Long = Localizer.Format("#LOC_BeamedPower_Wavelength_long");
string Short = Localizer.Format("#LOC_BeamedPower_Wavelength_short");
string wavelengthLocalized = (wavelength == "Long") ? Long : Short;
return Localizer.Format("#LOC_BeamedPower_WirelessReflector_ModuleInfo",
ReflectorDiameter.ToString(),
(Reflectivity * 100).ToString(),
CanAmplify,
(Efficiency * 100).ToString(),
wavelengthLocalized);
}
// main block of code- runs every physics frame
public void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
if (IsEnabled)
{
// we wait ~60 frames before starting to add heat, to stop overheating bugs
if (initFrames < 60)
{
initFrames += 1;
}
else
{
AddHeatToCore();
}
SyncAnimationState();
// fail-safe mechanism (ie if amplify setting is set too high and craft runs out of power, amplifier will be shutdown)
this.vessel.GetConnectedResourceTotals(EChash, out double amount, out double maxAmount);
if (amount / maxAmount < 0.2d)
{
IsEnabled = false;
}
receiver.Directional(this.part, transmitterCounter, IsEnabled, 100f, ReflectorDiameter, Reflectivity, false, false,
State, out State, out TransmitterName, out double recvPower, out transmitterCounter);
//try
//{
if (receiver.wavelengthList.Count > 0)
{
Wavelength = receiver.wavelengthList[transmitterCounter];
}
//}
//catch
//{
//Wavelength = "Long";
//}
Excess = (float)Math.Round(recvPower, 1);
this.part.AddSkinThermalFlux((Excess / Reflectivity) * (1 - Reflectivity) * (heatModifier / 100));
if (CanAmplify == "True" && wavelength == Wavelength)
{
bool background = HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().BackgroundProcessing;
resourceConsumption = (float)(recvPower * (AmplifyMult - 1));
if (background == false)
{
this.part.RequestResource(EChash, (double)resourceConsumption * Time.fixedDeltaTime);
}
Excess += Mathf.Clamp((resourceConsumption * Efficiency), 0f, 50000f);
}
else
{
AmplifyMult = 1f;
}
PowerReflected = (float)Math.Round(Excess, 1);
Constant = (float)((Wavelength == "Long") ? 1.44 * Math.Pow(10, -3) / ReflectorDiameter :
1.44 * 5 * Math.Pow(10, -8) / ReflectorDiameter);
frames += 1;
List<ConfigNode> receiverConfigList = new List<ConfigNode>();
if (frames == 40)
{
try
{
vesselFinder.ReceiverData(this.vessel.GetDisplayName(), out receiverConfigList);
}
catch
{
Debug.LogError("BeamedPowerStandalone.WirelessReflector : Unable to load receiver vessel list.");
}
frames = 0;
}
if (receiverCounter >= receiverConfigList.Count)
{
receiverCounter = 0;
}
if (receiverConfigList.Count > 0)
{
TransmittingTo = receiverConfigList[receiverCounter].GetValue("name");
}
else
{
TransmittingTo = Localizer.Format("#LOC_BeamedPower_Vessel_None");
}
}
else
{
Excess = 0f;
Constant = 0f;
Wavelength = "Long";
TransmittingTo = Localizer.Format("#LOC_BeamedPower_Vessel_None");
TransmitterName = Localizer.Format("#LOC_BeamedPower_Vessel_None");
State = Localizer.Format("#LOC_BeamedPower_Status_Offline");
}
}
}
// reflected power calculator in part right-click menu
[KSPField(guiName = "Received Power", guiActive = false, guiActiveEditor = true, groupName = "reflectedpowerCalc", groupDisplayName = "Reflected Power Calculator", groupStartCollapsed = true, isPersistant = false, guiUnits = "kW"), UI_FloatRange(scene = UI_Scene.Editor, minValue = 0, maxValue = 100000, stepIncrement = 1)]
public float ReceivedPower;
[KSPField(guiName = "Amplify", guiActive = false, guiActiveEditor = true, groupName = "reflectedpowerCalc", isPersistant = false, guiUnits = "x"), UI_FloatRange(scene = UI_Scene.Editor, minValue = 1, maxValue = 5, stepIncrement = 0.05f)]
public float Amplify = 1f;
[KSPField(guiName = "Result", guiActive = false, guiActiveEditor = true, groupName = "reflectedpowerCalc", isPersistant = false, guiUnits = "kW")]
public float ReflectedPower;
public void Update()
{
if (HighLogic.LoadedSceneIsEditor)
{
ReflectedPower = ReceivedPower * Reflectivity;
if (CanAmplify == "True")
{
ReflectedPower += Mathf.Clamp((ReceivedPower * (Amplify - 1) * Efficiency), 0f, 50000f);
}
else
{
Amplify = 1f;
}
ReflectedPower = (float)Math.Round(ReflectedPower, 1);
}
else if (HighLogic.LoadedSceneIsFlight)
{
if (CanAmplify != "True" | Wavelength != wavelength)
{
AmplifyMult = 1f;
}
}
}
}
}