forked from AniruddhKSP/KSP-Beamed-Power-Standalone-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WirelessSource.cs
320 lines (277 loc) · 14.9 KB
/
WirelessSource.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
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;
namespace UniversalResourceTransfer
{
public class WirelessSource : PartModule
{
static string ManagedResource;
public int ResourceHash;
// creating things on part right click menu (flight)
[KSPField(guiName = "Power Transmitter", isPersistant = true, guiActive = true, guiActiveEditor = false), UI_Toggle(scene = UI_Scene.Flight)]
public bool Transmitting;
[KSPField(guiName = "Power to Beam", isPersistant = true, guiActive = true, guiActiveEditor = false, guiUnits = "EC/s"), UI_FloatRange(minValue = 0, maxValue = 100000, stepIncrement = 1, scene = UI_Scene.Flight)]
public float PowerBeamed;
[KSPField(guiName = "Beamed Power", isPersistant = true, guiActive = true, guiActiveEditor = false, guiUnits = "EC/s", guiActiveUnfocused = true, unfocusedRange = 10000000000000)]
public float Excess;
[KSPField(isPersistant = true)]
public float Constant;
[KSPField(guiName = "Transmitting To", isPersistant = true, guiActive = true, guiActiveEditor = false)]
public string TransmittingTo;
[KSPField(guiName = "Status", isPersistant = false, guiActive = true, guiActiveEditor = 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;
// 'dish_diameter', 'efficiency', and 'wavelength' are set in part.cfg file:
[KSPField(isPersistant = false)]
public float DishDiameter;
[KSPField(isPersistant = true)]
public string Wavelength;
[KSPField(isPersistant = true)]
public float Efficiency;
[KSPField(isPersistant = false)]
public float maxCoreTemp = 900f;
[KSPField(isPersistant = false)]
public float maxSkinTemp = 1200f;
List<ConfigNode> receiversList; int frames; int initFrames;
VesselFinder vesselFinder = new VesselFinder(); ModuleCoreHeat coreHeat;
string operational = Localizer.Format("#LOC_BeamedPower_status_Operational");
string ExceedTempLimit = Localizer.Format("#LOC_BeamedPower_status_ExceededTempLimit");
string VesselNone = Localizer.Format("#LOC_BeamedPower_Vessel_None");
string GUIResourceName;
public void Start()
{
string ConfigFilePath = KSPUtil.ApplicationRootPath + "GameData/UniversalResourceTransfer/Settings.cfg";
ConfigNode MainNode = ConfigNode.Load(ConfigFilePath);
ManagedResource = MainNode.GetNode("BPSettings").GetNode("ResourceSettings").GetValue("ManagedResource");
GUIResourceName = MainNode.GetNode("BPSettings").GetNode("ResourceSettings").GetValue("GUIUnitName");
if (string.IsNullOrEmpty(ManagedResource))
{
Debug.LogError("Universal Resource Transfer: ManagedResource is not set correctly. It has reverted to the default of ElectricCharge.");
ManagedResource = "ElectricCharge";
}
if (string.IsNullOrEmpty(GUIResourceName))
{
Debug.LogError("Universal Resource Transfer: GUIResourceName is not set correctly. It has reverted to the default of EC/s.");
GUIResourceName = "EC/s";
}
ResourceHash = PartResourceLibrary.Instance.GetDefinition(ManagedResource).id;
Debug.Log(Time.realtimeSinceStartup + ManagedResource);
frames = 20; initFrames = 0;
receiversList = new List<ConfigNode>();
Fields["CoreTemp"].guiUnits = "K/" + maxCoreTemp.ToString() + "K";
Fields["SkinTemp"].guiUnits = "K/" + maxSkinTemp.ToString() + "K";
SetHeatParams();
SetLocalization();
}
private void SetLocalization()
{
//flight
Fields["Transmitting"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessSource_PowerTransmitter");
Fields["PowerBeamed"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessSource_PowerToBeam");
Fields["Excess"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessSource_BeamedPower");
Fields["TransmittingTo"].guiName = Localizer.Format("#LOC_BeamedPower_WirelessSource_TransmittingTo");
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");
Events["VesselCounter"].guiName = Localizer.Format("#LOC_BeamedPower_Vessels_Cyclethrough");
Actions["ToggleBPTransmitter"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_ToggleSource");
Actions["ActivateBPTransmitter"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_ActivateSource");
Actions["DeactivateBPTransmitter"].guiName = Localizer.Format("#LOC_BeamedPower_Actions_DeactivateSource");
//editor
Fields["Distance"].guiName = Localizer.Format("#LOC_BeamedPower_CalcDistance");
Fields["RecvDiameter"].guiName = Localizer.Format("#LOC_BeamedPower_CalcRecvDiameter");
Fields["RecvEfficiency"].guiName = Localizer.Format("#LOC_BeamedPower_CalcRecvEfficiency");
Fields["PowerBeamed"].guiName = Localizer.Format("#LOC_BeamedPower_CalcPowerBeamed");
Fields["PowerReceived"].guiName = Localizer.Format("#LOC_BeamedPower_CalcResult");
Fields["PowerBeamed"].guiUnits = GUIResourceName;
Fields["Excess"].guiUnits = GUIResourceName;
Fields["PowerReceived"].guiUnits = GUIResourceName;
}
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;
}
[KSPField(isPersistant = true)]
public int counter;
[KSPEvent(guiName = "Cycle through vessels", guiActive = true, isPersistent = false, requireFullControl = true)]
public void VesselCounter()
{
counter = (counter < receiversList.Count - 1) ? counter += 1 : counter = 0;
}
// getting resource id of 'Electric Charge'
// setting action group capability
[KSPAction(guiName = "Toggle Power Transmitter")]
public void ToggleBPTransmitter(KSPActionParam param)
{
Transmitting = Transmitting ? false : true;
}
[KSPAction(guiName = "Activate Power Transmitter")]
public void ActivateBPTransmitter(KSPActionParam param)
{
Transmitting = Transmitting ? true : true;
}
[KSPAction(guiName = "Deactivate Power Transmitter")]
public void DeactivateBPTransmitter(KSPActionParam param)
{
Transmitting = Transmitting ? false : false;
}
// adding part info to part description tab in editor
public string GetModuleTitle()
{
return "WirelessSource";
}
public override string GetModuleDisplayName()
{
return Localizer.Format("#LOC_BeamedPower_WirelessSource_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_WirelessSource_ModuleInfo",
DishDiameter.ToString(),
wavelengthLocalized,
(Efficiency * 100).ToString(),
maxCoreTemp.ToString(),
maxSkinTemp.ToString());
}
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;
Transmitting = false;
}
if (State == ExceedTempLimit & (CoreTemp >= maxCoreTemp * 0.7 | SkinTemp >= maxSkinTemp * 0.7))
{
Transmitting = 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 - Efficiency) * (Excess / Efficiency) * heatModifier;
WasteHeat = (float)Math.Round(heatExcess, 1);
coreHeat.AddEnergyToCore(heatExcess * 0.7 * TimeWarp.fixedDeltaTime); // first converted to kJ
this.part.AddSkinThermalFlux(heatExcess * 0.3); // some heat added to skin
}
private void SyncAnimationState()
{
if (part.Modules.Contains<ModuleDeployableAntenna>())
{
if (part.Modules.GetModule<ModuleDeployableAntenna>().deployState != ModuleDeployableAntenna.DeployState.EXTENDED)
{
Transmitting = false;
}
}
}
// main block of code - runs every physics frame
public void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
if (initFrames < 60)
{
initFrames += 1;
}
else
{
AddHeatToCore();
}
SyncAnimationState();
this.vessel.GetConnectedResourceTotals(ResourceHash, out double amount, out double maxAmount);
if (amount/maxAmount < 0.2d)
{
PowerBeamed = 0f;
}
if (Transmitting)
{
frames += 1;
if (frames == 30)
{
try
{
vesselFinder.ReceiverData(this.vessel.GetDisplayName(), out receiversList);
}
catch
{
Debug.LogError("BeamedPowerStandalone.WirelessSource : Unable to load receiver vessel list.");
}
frames = 0;
}
counter = (counter < receiversList.Count) ? counter : counter = 0;
try
{
TransmittingTo = receiversList[counter].GetValue("name");
}
catch
{
TransmittingTo = VesselNone;
}
// a bunch of math
Excess = Convert.ToSingle(Math.Round((PowerBeamed * Efficiency), 1));
if (Wavelength == "Short") // short ultraviolet
{
Constant = Convert.ToSingle((1.44 * 5 * Math.Pow(10, -8)) / DishDiameter);
}
else if (Wavelength == "Long") // short microwave
{
Constant = Convert.ToSingle((1.44 * Math.Pow(10, -3)) / DishDiameter);
}
else
{
Debug.LogWarning("BeamedPowerStandalone.WirelessSource : Incorrect wavelength set in .cfg file of part- " + this.part.partName);
Constant = 1f;
}
if (HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().BackgroundProcessing == false)
{
// reducing amount of EC in craft in each frame (makes it look like continuous EC consumption)
this.part.RequestResource(ResourceHash, (double)(PowerBeamed * TimeWarp.fixedDeltaTime));
}
}
else
{
Excess = 0;
}
}
}
// adds received power calculator in transmitter part's right click menu in editor
[KSPField(guiName = "Distance", groupName = "calculator3", groupDisplayName = "Received Power Calculator", groupStartCollapsed = true, guiUnits = "Mm", guiActive = false, guiActiveEditor = true), UI_FloatRange(minValue = 0, maxValue = 10000000, stepIncrement = 0.001f, scene = UI_Scene.Editor)]
public float Distance;
[KSPField(guiName = "Receiver Diameter", groupName = "calculator3", guiUnits = "m", guiActive = false, guiActiveEditor = true), UI_FloatRange(minValue = 0, maxValue = 50, stepIncrement = 0.5f, scene = UI_Scene.Editor)]
public float RecvDiameter;
[KSPField(guiName = "Receiver Efficiency", groupName = "calculator3", guiActive = false, guiActiveEditor = true, guiUnits = "%"), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 1, scene = UI_Scene.Editor)]
public float RecvEfficiency;
[KSPField(guiName = "Power Beamed", groupName = "calculator3", guiUnits = "EC/s", guiActive = false, guiActiveEditor = true), UI_FloatRange(minValue = 0, maxValue = 100000, stepIncrement = 1, scene = UI_Scene.Editor)]
public float BeamedPower;
[KSPField(guiName = "Result", groupName = "calculator3", guiUnits = "EC/s", guiActive = false, guiActiveEditor = true)]
public float PowerReceived;
public void Update()
{
if (HighLogic.LoadedSceneIsEditor)
{
float wavelength_num = (float)((Wavelength == "Long") ? Math.Pow(10, -3) : 5 * Math.Pow(10, -8));
float spot_size = (float)(1.44 * wavelength_num * Distance * 1000000d / DishDiameter);
PowerReceived = (spot_size > RecvDiameter) ?
RecvDiameter / spot_size * BeamedPower * Efficiency * (RecvEfficiency / 100) : BeamedPower * Efficiency * (RecvEfficiency / 100);
PowerReceived = (float)Math.Round(PowerReceived, 1);
}
}
}
}