-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathFNRadiator.cs
468 lines (381 loc) · 18.3 KB
/
FNRadiator.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace FNPlugin {
[KSPModule("Radiator")]
class FNRadiator : FNResourceSuppliableModule {
[KSPField(isPersistant = true)]
public bool radiatorIsEnabled;
[KSPField(isPersistant = true)]
public bool isupgraded;
[KSPField(isPersistant = true)]
public bool radiatorInit;
[KSPField(isPersistant = false)]
public string upgradeTechReq;
[KSPField(isPersistant = false)]
public bool isDeployable = true;
[KSPField(isPersistant = false)]
public float convectiveBonus = 1.0f;
[KSPField(isPersistant = false)]
public string animName;
[KSPField(isPersistant = false)]
public float radiatorTemp;
[KSPField(isPersistant = false)]
public float radiatorArea;
[KSPField(isPersistant = false)]
public string originalName;
[KSPField(isPersistant = false)]
public float upgradeCost = 100;
[KSPField(isPersistant = false)]
public string upgradedName;
[KSPField(isPersistant = false)]
public float upgradedRadiatorTemp;
[KSPField(isPersistant = false, guiActive = true, guiName = "Type")]
public string radiatorType;
[KSPField(isPersistant = false, guiActive = true, guiName = "Temperature")]
public string radiatorTempStr;
[KSPField(isPersistant = false, guiActive = true, guiName = "Power Radiated")]
public string thermalPowerDissipStr;
[KSPField(isPersistant = false, guiActive = true, guiName = "Power Convected")]
public string thermalPowerConvStr;
[KSPField(isPersistant = false, guiActive = true, guiName = "Upgrade")]
public string upgradeCostStr;
//public static double stefan_const = 5.6704e-8;
protected static float rad_const_h = 1000;
protected static double alpha = 0.001998001998001998001998001998;
protected Animation anim;
protected float radiatedThermalPower;
protected float convectedThermalPower;
protected double current_rad_temp;
protected float myScience = 0;
protected float directionrotate = 1;
protected float oldangle = 0;
protected Vector3 original_eulers;
protected Transform pivot;
protected long last_draw_update = 0;
protected long update_count = 0;
protected bool hasrequiredupgrade;
protected int explode_counter = 0;
protected static List<FNRadiator> list_of_radiators = new List<FNRadiator>();
public static List<FNRadiator> getRadiatorsForVessel(Vessel vess) {
List<FNRadiator> list_of_radiators_for_vessel = new List<FNRadiator>();
list_of_radiators.RemoveAll(item => item == null);
foreach (FNRadiator radiator in list_of_radiators) {
if (radiator.vessel == vess) {
list_of_radiators_for_vessel.Add (radiator);
}
}
return list_of_radiators_for_vessel;
}
public static bool hasRadiatorsForVessel(Vessel vess) {
list_of_radiators.RemoveAll(item => item == null);
bool has_radiators = false;
foreach (FNRadiator radiator in list_of_radiators) {
if (radiator.vessel == vess) {
has_radiators = true;
}
}
return has_radiators;
}
public static double getAverageRadiatorTemperatureForVessel(Vessel vess) {
list_of_radiators.RemoveAll(item => item == null);
double average_temp = 0;
double n_radiators = 0;
foreach (FNRadiator radiator in list_of_radiators) {
if (radiator.vessel == vess) {
average_temp += radiator.getRadiatorTemperature ();
n_radiators+=1.0f;
}
}
if (n_radiators > 0) {
average_temp = average_temp / n_radiators;
} else {
average_temp = 0;
}
return average_temp;
}
public static double getAverageMaximumRadiatorTemperatureForVessel(Vessel vess) {
list_of_radiators.RemoveAll(item => item == null);
double average_temp = 0;
double n_radiators = 0;
foreach (FNRadiator radiator in list_of_radiators) {
if (radiator.vessel == vess) {
average_temp += radiator.radiatorTemp;
n_radiators += 1.0f;
}
}
if (n_radiators > 0) {
average_temp = average_temp / n_radiators;
} else {
average_temp = 0;
}
return average_temp;
}
[KSPEvent(guiActive = true, guiName = "Deploy Radiator", active = true)]
public void DeployRadiator() {
if (!isDeployable) {
return;
}
anim [animName].speed = 1f;
anim [animName].normalizedTime = 0f;
anim.Blend (animName, 2f);
radiatorIsEnabled = true;
}
[KSPEvent(guiActive = true, guiName = "Retract Radiator", active = false)]
public void RetractRadiator() {
if (!isDeployable) {
return;
}
anim [animName].speed = -1f;
anim [animName].normalizedTime = 1f;
anim.Blend (animName, 2f);
radiatorIsEnabled = false;
}
[KSPEvent(guiActive = true, guiName = "Retrofit", active = true)]
public void RetrofitRadiator() {
if (ResearchAndDevelopment.Instance == null) { return;}
if (isupgraded || ResearchAndDevelopment.Instance.Science < upgradeCost) { return; }
isupgraded = true;
radiatorType = upgradedName;
radiatorTemp = upgradedRadiatorTemp;
radiatorTempStr = radiatorTemp + "K";
ResearchAndDevelopment.Instance.AddScience(-upgradeCost, TransactionReasons.RnDPartPurchase);
}
[KSPAction("Deploy Radiator")]
public void DeployRadiatorAction(KSPActionParam param) {
DeployRadiator();
}
[KSPAction("Retract Radiator")]
public void RetractRadiatorAction(KSPActionParam param) {
RetractRadiator();
}
[KSPAction("Toggle Radiator")]
public void ToggleRadiatorAction(KSPActionParam param) {
if (radiatorIsEnabled) {
RetractRadiator();
} else {
DeployRadiator();
}
}
public override void OnStart(PartModule.StartState state) {
Actions["DeployRadiatorAction"].guiName = Events["DeployRadiator"].guiName = String.Format("Deploy Radiator");
Actions["RetractRadiatorAction"].guiName = Events["RetractRadiator"].guiName = String.Format("Retract Radiator");
Actions["ToggleRadiatorAction"].guiName = String.Format("Toggle Radiator");
if (state == StartState.Editor) {
if (hasTechsRequiredToUpgrade()) {
isupgraded = true;
hasrequiredupgrade = true;
isupgraded = true;
}
return;
}
FNRadiator.list_of_radiators.Add (this);
anim = part.FindModelAnimators (animName).FirstOrDefault ();
//orig_emissive_colour = part.renderer.material.GetTexture (emissive_property_name);
if (anim != null) {
anim [animName].layer = 1;
if (radiatorIsEnabled) {
anim.Blend (animName, 1, 0);
} else {
//anim.Blend (animName, 0, 0);
}
//anim.Play ();
}
if (isDeployable) {
pivot = part.FindModelTransform ("suntransform");
original_eulers = pivot.transform.localEulerAngles;
} else {
radiatorIsEnabled = true;
}
if(HighLogic.CurrentGame.Mode == Game.Modes.CAREER) {
if(PluginHelper.hasTech(upgradeTechReq)) {
hasrequiredupgrade = true;
}
}else{
hasrequiredupgrade = true;
}
if (radiatorInit == false) {
radiatorInit = true;
}
if (!isupgraded) {
radiatorType = originalName;
} else {
radiatorType = upgradedName;
radiatorTemp = upgradedRadiatorTemp;
}
radiatorTempStr = radiatorTemp + "K";
this.part.force_activate();
}
public override void OnUpdate() {
Events["DeployRadiator"].active = !radiatorIsEnabled && isDeployable;
Events["RetractRadiator"].active = radiatorIsEnabled && isDeployable;
if (ResearchAndDevelopment.Instance != null) {
Events ["RetrofitRadiator"].active = !isupgraded && ResearchAndDevelopment.Instance.Science >= upgradeCost && hasrequiredupgrade;
} else {
Events ["RetrofitRadiator"].active = false;
}
Fields["upgradeCostStr"].guiActive = !isupgraded && hasrequiredupgrade;
if (ResearchAndDevelopment.Instance != null) {
upgradeCostStr = ResearchAndDevelopment.Instance.Science + "/" + upgradeCost.ToString ("0") + " Science";
}
if (update_count - last_draw_update > 8) {
thermalPowerDissipStr = radiatedThermalPower.ToString("0.000") + "MW";
thermalPowerConvStr = convectedThermalPower.ToString("0.000") + "MW";
radiatorTempStr = current_rad_temp.ToString("0.0") + "K / " + radiatorTemp.ToString("0.0") + "K";
last_draw_update = update_count;
}
colorHeat();
update_count++;
}
public override void OnFixedUpdate() {
float atmosphere_height = vessel.mainBody.maxAtmosphereAltitude;
float vessel_height = (float) vessel.mainBody.GetAltitude (vessel.transform.position);
float conv_power_dissip = 0;
if (vessel.altitude <= PluginHelper.getMaxAtmosphericAltitude(vessel.mainBody)) {
float pressure = (float) FlightGlobals.getStaticPressure (vessel.transform.position);
float dynamic_pressure = (float) (0.5*pressure*1.2041*vessel.srf_velocity.sqrMagnitude/101325.0);
pressure += dynamic_pressure;
float low_temp = FlightGlobals.getExternalTemperature (vessel.transform.position);
float delta_temp = Mathf.Max(0,radiatorTemp - low_temp);
conv_power_dissip = pressure * delta_temp * radiatorArea * rad_const_h/1e6f * TimeWarp.fixedDeltaTime*convectiveBonus;
if (!radiatorIsEnabled) {
conv_power_dissip = conv_power_dissip / 2.0f;
}
convectedThermalPower = consumeFNResource (conv_power_dissip, FNResourceManager.FNRESOURCE_WASTEHEAT) / TimeWarp.fixedDeltaTime;
if (radiatorIsEnabled && dynamic_pressure > 1.4854428818159388107574636072046e-3 && isDeployable) {
part.deactivate();
//part.breakingForce = 1;
//part.breakingTorque = 1;
part.decouple (1);
}
}
if (radiatorIsEnabled) {
if (getResourceBarRatio (FNResourceManager.FNRESOURCE_WASTEHEAT) >= 1 && current_rad_temp >= radiatorTemp) {
explode_counter ++;
if (explode_counter > 25) {
part.explode ();
}
} else {
explode_counter = 0;
}
double radiator_temperature_temp_val = radiatorTemp * Math.Pow(getResourceBarRatio(FNResourceManager.FNRESOURCE_WASTEHEAT), 0.25);
if (vessel.HasAnyActiveThermalSources()) radiator_temperature_temp_val = Math.Min(vessel.GetTemperatureofColdestThermalSource() / 1.01, radiator_temperature_temp_val);
double thermal_power_dissip = (GameConstants.stefan_const * radiatorArea * Math.Pow(radiator_temperature_temp_val, 4) / 1e6) * TimeWarp.fixedDeltaTime;
radiatedThermalPower = consumeFNResource (thermal_power_dissip, FNResourceManager.FNRESOURCE_WASTEHEAT) / TimeWarp.fixedDeltaTime;
double instantaneous_rad_temp = (Math.Min(Math.Pow(radiatedThermalPower * 1e6 / (GameConstants.stefan_const * radiatorArea), 0.25), radiatorTemp));
instantaneous_rad_temp = Math.Max(instantaneous_rad_temp, Math.Max(FlightGlobals.getExternalTemperature((float)vessel.altitude, vessel.mainBody) + 273.16, 2.7));
if (current_rad_temp <= 0) {
current_rad_temp = instantaneous_rad_temp;
} else {
current_rad_temp = instantaneous_rad_temp * alpha + (1.0 - alpha) * instantaneous_rad_temp;
}
if (isDeployable) {
Vector3 pivrot = pivot.rotation.eulerAngles;
pivot.Rotate (Vector3.up * 5f * TimeWarp.fixedDeltaTime * directionrotate);
Vector3 sunpos = FlightGlobals.Bodies [0].transform.position;
Vector3 flatVectorToTarget = sunpos - transform.position;
flatVectorToTarget = flatVectorToTarget.normalized;
float dot = Mathf.Asin (Vector3.Dot (pivot.transform.right, flatVectorToTarget)) / Mathf.PI * 180.0f;
float anglediff = -dot;
oldangle = dot;
//print (dot);
directionrotate = anglediff / 5 / TimeWarp.fixedDeltaTime;
directionrotate = Mathf.Min (3, directionrotate);
directionrotate = Mathf.Max (-3, directionrotate);
part.maximum_drag = 0.8f;
part.minimum_drag = 0.8f;
}
} else {
if (isDeployable) {
pivot.transform.localEulerAngles = original_eulers;
}
double radiator_temperature_temp_val = radiatorTemp * Math.Pow(getResourceBarRatio(FNResourceManager.FNRESOURCE_WASTEHEAT), 0.25);
if (vessel.HasAnyActiveThermalSources()) radiator_temperature_temp_val = Math.Min (vessel.GetTemperatureofColdestThermalSource()/1.01, radiator_temperature_temp_val);
double thermal_power_dissip = (GameConstants.stefan_const * radiatorArea * Math.Pow(radiator_temperature_temp_val, 4) / 1e7) * TimeWarp.fixedDeltaTime;
radiatedThermalPower = consumeFNResource (thermal_power_dissip, FNResourceManager.FNRESOURCE_WASTEHEAT) / TimeWarp.fixedDeltaTime;
double instantaneous_rad_temp = (Math.Min(Math.Pow(radiatedThermalPower * 1e7 / (GameConstants.stefan_const * radiatorArea), 0.25), radiatorTemp));
instantaneous_rad_temp = Math.Max(instantaneous_rad_temp, Math.Max(FlightGlobals.getExternalTemperature((float)vessel.altitude, vessel.mainBody) + 273.16, 2.7));
if (current_rad_temp <= 0) {
current_rad_temp = instantaneous_rad_temp;
} else {
current_rad_temp = instantaneous_rad_temp * alpha + (1.0 - alpha) * instantaneous_rad_temp;
}
part.maximum_drag = 0.2f;
part.minimum_drag = 0.2f;
}
}
public bool hasTechsRequiredToUpgrade() {
if (HighLogic.CurrentGame != null) {
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER) {
if (upgradeTechReq != null) {
if (PluginHelper.hasTech(upgradeTechReq)) {
return true;
}
}
} else {
return true;
}
}
return false;
}
public float getRadiatorTemperature() {
return (float)current_rad_temp;
}
public override string GetInfo() {
float thermal_power_dissip = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(radiatorTemp, 4) / 1e6);
float thermal_power_dissip2 = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(upgradedRadiatorTemp, 4) / 1e6);
float thermal_power_dissip3 = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(600, 4) / 1e6);
float thermal_power_dissip4 = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(1200, 4) / 1e6);
float thermal_power_dissip5 = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(1800, 4) / 1e6);
float thermal_power_dissip6 = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(2400, 4) / 1e6);
float thermal_power_dissip7 = (float)(GameConstants.stefan_const * radiatorArea * Math.Pow(3000, 4) / 1e6);
return String.Format("Maximum Waste Heat Radiated\n Base: {0} MW\n Upgraded: {1} MW\n-----\nRadiator Performance at:\n600K: {2} MW\n1200K: {3} MW\n1800K: {4} MW\n2400K: {5} MW\n3000K: {6} MW\n", thermal_power_dissip, thermal_power_dissip2, thermal_power_dissip3, thermal_power_dissip4, thermal_power_dissip5, thermal_power_dissip6, thermal_power_dissip7);
}
public override int getPowerPriority() {
return 3;
}
private void colorHeat()
{
const String KSPShader = "KSP/Emissive/Bumped Specular";
float currentTemperature = getRadiatorTemperature();
float maxTemperature = part.maxTemp;
double temperatureRatio = currentTemperature / maxTemperature;
Color emissiveColor = new Color((float)(Math.Pow(temperatureRatio, 3)), 0.0f, 0.0f, 1.0f);
Renderer[] array = part.FindModelComponents<Renderer>();
foreach (Renderer renderer in array)
{
if (renderer.material.shader.name != KSPShader)
renderer.material.shader = Shader.Find(KSPShader);
if (part.name.StartsWith("circradiator"))
{
if (renderer.material.GetTexture("_Emissive") == null)
renderer.material.SetTexture("_Emissive", GameDatabase.Instance.GetTexture("WarpPlugin/Parts/Electrical/circradiatorKT/texture1_e", false));
if (renderer.material.GetTexture("_BumpMap") == null)
renderer.material.SetTexture("_BumpMap", GameDatabase.Instance.GetTexture("WarpPlugin/Parts/Electrical/circradiatorKT/texture1_n", false));
} else if (part.name.StartsWith("RadialRadiator"))
{
if (renderer.material.GetTexture("_Emissive") == null)
renderer.material.SetTexture("_Emissive", GameDatabase.Instance.GetTexture("WarpPlugin/Parts/Electrical/RadialHeatRadiator/d_glow", false));
Debug.Log("rd _Emissive: " + renderer.material.GetTexture("_Emissive"));
} else if (part.name.StartsWith("LargeFlatRadiator"))
{
if (renderer.material.shader.name != KSPShader)
renderer.material.shader = Shader.Find(KSPShader);
if (renderer.material.GetTexture("_Emissive") == null)
renderer.material.SetTexture("_Emissive", GameDatabase.Instance.GetTexture("WarpPlugin/Parts/Electrical/LargeFlatRadiator/glow", false));
if (renderer.material.GetTexture("_BumpMap") == null)
renderer.material.SetTexture("_BumpMap", GameDatabase.Instance.GetTexture("WarpPlugin/Parts/Electrical/LargeFlatRadiator/radtex_n", false));
} else if (part.name.StartsWith("radiator"))
{
// radiators have already everything set up
} else // uknown raidator
{
return;
}
renderer.material.SetColor("_EmissiveColor", emissiveColor);
}
}
}
}