-
Notifications
You must be signed in to change notification settings - Fork 4
/
BeamedPowerReceiver.cs
274 lines (231 loc) · 13 KB
/
BeamedPowerReceiver.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
using System;
using UnityEngine;
using KSP.Localization;
namespace BeamedPowerStandalone
{
public class WirelessReceiver : PartModule
{
// UI-right click menu in flight
[KSPField(guiName = "Power Receiver", isPersistant = true, guiActive = true, guiActiveEditor = false), UI_Toggle(scene = UI_Scene.Flight)]
public bool Listening;
[KSPField(guiName = "Received Power Limiter", isPersistant = true, guiActive = true, guiActiveEditor = false, guiUnits = "%"), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 1, requireFullControl = true, scene = UI_Scene.Flight)]
public float PowerLimiter;
[KSPField(guiName = "Received Power", isPersistant = true, guiActive = true, guiActiveEditor = false, guiUnits = "EC/s")]
public float ReceivedPower;
[KSPField(guiName = "Receiving from", isPersistant = false, guiActive = true, guiActiveEditor = false)]
public string ReceivingFrom;
[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;
// parameters set in part.cfg file
[KSPField(isPersistant = false)]
public string recvType;
[KSPField(isPersistant = false)]
public float recvDiameter;
[KSPField(isPersistant = false)]
public float recvEfficiency;
[KSPField(isPersistant = false)]
public float maxCoreTemp = 900f;
[KSPField(isPersistant = false)]
public float maxSkinTemp = 1200f;
int initFrames; ModuleCoreHeat coreHeat; ReceivedPower receiver;
readonly int EChash = PartResourceLibrary.Instance.GetDefinition("ElectricCharge").id;
string ExceedTempLimit = Localizer.Format("#LOC_BeamedPower_status_ExceededTempLimit");
string operational = Localizer.Format("#LOC_BeamedPower_status_Operational");
[KSPField(isPersistant = true)]
public int counter;
public void Start()
{
initFrames = 0;
receiver = new ReceivedPower();
Fields["CoreTemp"].guiUnits = "K/" + maxCoreTemp.ToString() + "K";
Fields["SkinTemp"].guiUnits = "K/" + maxSkinTemp.ToString() + "K";
if (recvType == "Spherical")
{
Fields["ReceivingFrom"].guiActive = false;
Fields["counter"].isPersistant = false;
Events["VesselCounter"].guiActive = false;
}
SetHeatParams();
SetLocalization();
}
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 *= 3d;
coreHeat.HeatRadiantMultiplier *= 2d;
}
private void SetLocalization()
{
//flight
Fields["Listening"].guiName = Localizer.Format("#LOC_BeamedPower_Receiver_PowerReceiver");
Fields["PowerLimiter"].guiName = Localizer.Format("#LOC_BeamedPower_RecvPowerLimiter");
Fields["ReceivedPower"].guiName = Localizer.Format("#LOC_BeamedPower_RecvPower");
Fields["State"].guiName = Localizer.Format("#LOC_BeamedPower_Status");
Fields["CoreTemp"].guiName = Localizer.Format("#LOC_BeamedPower_CoreTemp");
Fields["CoreTemp"].group.displayName = Localizer.Format("#LOC_BeamedPower_HeatInfo");
Fields["SkinTemp"].guiName = Localizer.Format("#LOC_BeamedPower_SkinTemp");
Fields["WasteHeat"].guiName = Localizer.Format("#LOC_BeamedPower_WasteHeat");
Actions["ToggleBPReceiver"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_ToggleReceiver");
Actions["ActivateBPReceiver"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_ActivateReceiver");
Actions["DeactivateBPReceiver"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_DeactivateReceiver");
Events["VesselCounter"].guiName = Localizer.Format("#LOC_BeamedPower_Vessels_Cyclethrough");
//editor
Fields["Distance"].guiName = Localizer.Format("#LOC_BeamedPower_CalcDistance");
Fields["Distance"].group.displayName = Localizer.Format("#LOC_BeamedPower_RecvPowerCalcName");
Fields["SourceDishDia"].guiName = Localizer.Format("#LOC_BeamedPower_CalcSourceDishDia");
Fields["CalcEfficiency"].guiName = Localizer.Format("#LOC_BeamedPower_CalcSourceEfficiency");
Fields["BeamedPower"].guiName = Localizer.Format("#LOC_BeamedPower_CalcPowerBeamed");
Fields["PowerReceived"].guiName = Localizer.Format("#LOC_BeamedPower_CalcResult");
Fields["CalcWavelength"].guiName = Localizer.Format("#LOC_BeamedPower_CalcWavelength");
Events["ToggleWavelength"].guiName = Localizer.Format("#LOC_BeamedPower_CalcToggleWavelength");
}
[KSPEvent(guiName = "Cycle through vessels", guiActive = true, isPersistent = false, requireFullControl = true)]
public void VesselCounter()
{
counter += 1;
}
// setting action group capability
[KSPAction(guiName = "Toggle Power Receiver")]
public void ToggleBPReceiver(KSPActionParam param)
{
Listening = Listening ? false : true;
}
[KSPAction(guiName = "Activate Power Receiver")]
public void ActivateBPReceiver(KSPActionParam param)
{
Listening = Listening ? true : true;
}
[KSPAction(guiName = "Deactivate Power Receiver")]
public void DeactivateBPReceiver(KSPActionParam param)
{
Listening = Listening ? false : false;
}
// adding part info to part description tab in editor
public string GetModuleTitle()
{
return "WirelessReceiver";
}
public override string GetModuleDisplayName()
{
return Localizer.Format("#LOC_BeamedPower_Receiver_ModuleName");
}
public override string GetInfo()
{
return Localizer.Format("#LOC_BeamedPower_Receiver_ModuleInfo",
recvType,
recvDiameter.ToString(),
(recvEfficiency * 100).ToString(),
maxCoreTemp.ToString(),
maxSkinTemp.ToString());
}
// adds heat mechanics to this receiver
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;
Listening = false;
}
if (State == ExceedTempLimit & (CoreTemp >= maxCoreTemp * 0.7 | SkinTemp >= maxSkinTemp * 0.7))
{
Listening = false;
}
else if (CoreTemp < maxCoreTemp * 0.7 & SkinTemp < maxSkinTemp * 0.7)
{
State = operational;
}
double heatModifier = (double)HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().PercentHeat / 100;
double heatExcess = (1 - recvEfficiency) * (ReceivedPower / recvEfficiency) * heatModifier;
WasteHeat = (float)Math.Round(heatExcess, 1);
coreHeat.AddEnergyToCore(heatExcess * 0.3 * TimeWarp.fixedDeltaTime); // first converted to kJ
this.part.AddSkinThermalFlux(heatExcess * 0.7); // some heat added to skin
}
private void SyncAnimationState()
{
if (part.Modules.Contains<ModuleDeployableAntenna>())
{
if (part.Modules.GetModule<ModuleDeployableAntenna>().deployState != ModuleDeployableAntenna.DeployState.EXTENDED)
{
Listening = false;
}
}
}
public void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
if (initFrames < 60)
{
initFrames += 1;
}
else
{
AddHeatToCore();
}
SyncAnimationState();
double recvPower;
if (recvType == "Directional")
{
receiver.Directional(this.part, counter, Listening, PowerLimiter, recvDiameter, recvEfficiency,
false, false, State, out State, out ReceivingFrom, out recvPower, out counter);
}
else if (recvType == "Spherical")
{
receiver.Spherical(this.part, Listening, PowerLimiter, recvDiameter, recvEfficiency, false,
false, State, out State, out recvPower);
}
else
{
counter = 0; recvPower = 0d;
Debug.LogWarning("BeamedPowerStandalone.WirelessReceiver : Invalid recvType set in part.cfg of " + this.part.partName);
}
ReceivedPower = (float)Math.Round(recvPower, 1);
if (HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().BackgroundProcessing == false)
{
this.part.RequestResource(EChash, -(double)ReceivedPower * TimeWarp.fixedDeltaTime);
}
}
}
// adds received power calculator to receivers right-click menu in editor
[KSPField(guiName = "Distance", groupName = "calculator1", groupDisplayName = "Received Power Calculator", groupStartCollapsed = true, guiUnits = "Mm", guiActive = false, guiActiveEditor = true, isPersistant = false), UI_FloatRange(minValue = 0, maxValue = 10000000, stepIncrement = 0.001f, scene = UI_Scene.Editor)]
public float Distance;
[KSPField(guiName = "Source Dish Diameter", groupName = "calculator1", guiUnits = "m", guiActive = false, guiActiveEditor = true, isPersistant = false), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 0.5f, scene = UI_Scene.Editor)]
public float SourceDishDia;
[KSPField(guiName = "Source Efficiency", groupName = "calculator1", guiActive = false, guiActiveEditor = true, guiUnits = "%", isPersistant = false), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 1, scene = UI_Scene.Editor)]
public float CalcEfficiency;
[KSPField(guiName = "Power Beamed", groupName = "calculator1", guiUnits = "EC/s", guiActive = false, guiActiveEditor = true, isPersistant = false), UI_FloatRange(minValue = 0, maxValue = 100000, stepIncrement = 1, scene = UI_Scene.Editor)]
public float BeamedPower;
[KSPField(guiName = "Result", groupName = "calculator1", guiUnits = "EC/s", guiActive = false, guiActiveEditor = true, isPersistant = false)]
public float PowerReceived;
[KSPField(guiName = "Beamed Wavelength", groupName = "calculator1", guiActiveEditor = true, guiActive = false, isPersistant = false)]
public string CalcWavelength = Localizer.Format("#LOC_BeamedPower_Wavelength_long");
[KSPEvent(guiName = "Toggle Wavelength", guiActive = false, guiActiveEditor = true, groupName = "calculator1", isPersistent = false)]
public void ToggleWavelength()
{
CalcWavelength = (CalcWavelength == Long) ? Short : Long;
}
string Long = Localizer.Format("#LOC_BeamedPower_Wavelength_long"); string Short = Localizer.Format("#LOC_BeamedPower_Wavelength_short");
public void Update()
{
if (HighLogic.LoadedSceneIsEditor)
{
float wavelength_num = (float)((CalcWavelength == Long) ? Math.Pow(10, -3) : 5 * Math.Pow(10, -8));
float spot_size = (float)(1.44 * wavelength_num * Distance * 1000000d / SourceDishDia);
PowerReceived = (spot_size > recvDiameter) ?
recvDiameter / spot_size * BeamedPower * (CalcEfficiency / 100) * recvEfficiency :
BeamedPower * (CalcEfficiency / 100) * recvEfficiency;
PowerReceived = (float)Math.Round(PowerReceived, 1);
}
}
}
}