-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFissionRadiator.cs
290 lines (239 loc) · 8.28 KB
/
FissionRadiator.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
/// FissionGeneratorRadiator
/// ---------------------------------------------------
/// Handles animation and fx for radiators
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace NearFuture
{
class FissionRadiator: PartModule
{
// animation for deploying and retracting of radiators
[KSPField(isPersistant = false)]
public string DeployAnimation;
// animation for radiator heat
[KSPField(isPersistant = false)]
public string HeatAnimation;
// Current radiator state
[KSPField(isPersistant = true)]
public string SavedState;
// Temperature lost by radiators
[KSPField(isPersistant = false)]
public float HeatRadiated;
// Temperature lost by radiators when closed
[KSPField(isPersistant = false)]
public float HeatRadiatedClosed;
// Amount of power dissipated w/ pressure in ideal conditions
[KSPField(isPersistant = false)]
public FloatCurve PressureCurve;
// animations for retracting
private AnimationState[] deployStates;
// and for heating
private AnimationState[] heatStates;
private float requestedHeatRejection = 0f;
// STATUS STRINGS
///--------------------
// Fuel Status string
[KSPField(isPersistant = false, guiActive = true, guiName = "Current Heat Rejection")]
public string HeatRejectionGUI = "0K";
// ACTIONS
// -----------------
// Deploy radiator
[KSPEvent(guiActive = true, guiName = "Deploy Radiator", active = true)]
public void DeployRadiator()
{
Deploy();
}
// Retract radiator
[KSPEvent(guiActive = true, guiName = "Retract Radiator", active = false)]
public void RetractRadiator()
{
Retract();
}
// Toggle radiator
[KSPEvent(guiActive = true, guiName = "Toggle Radiator", active = true)]
public void ToggleRadiator()
{
Toggle();
}
[KSPAction("Deploy Radiators")]
public void DeployRadiatorsAction(KSPActionParam param)
{
DeployRadiator();
}
[KSPAction("Retract Radiators")]
public void RetractRadiatorsAction(KSPActionParam param)
{
RetractRadiator();
}
[KSPAction("Toggle Radiators")]
public void ToggleRadiatorsAction(KSPActionParam param)
{
ToggleRadiator();
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
this.moduleName = "Fission Reactor Radiator";
}
// Info for ui
public override string GetInfo()
{
return String.Format("Heat Rejection (Retracted): {0:F1} kW", HeatRadiatedClosed) + "\n" +
String.Format("Heat Rejection (Deployed): {0:F1} kW", HeatRadiated);
}
// Deploy Radiators
public void Deploy()
{
foreach (AnimationState deployState in deployStates)
{
deployState.speed = 1;
}
State = RadiatorState.Deploying;
}
// Retract Radiators
public void Retract()
{
foreach (AnimationState deployState in deployStates)
{
deployState.speed = -1;
}
State = RadiatorState.Retracting;
}
// Toggle Radiators
public void Toggle()
{
if (State == RadiatorState.Deployed)
Retract();
else if (State == RadiatorState.Retracted)
Deploy();
else
return;
}
private float availableHeatRejection = 0f;
public float HeatRejection(float request)
{
requestedHeatRejection = request;
return availableHeatRejection;
}
// Get the state
public RadiatorState State
{
get
{
try
{
return (RadiatorState)Enum.Parse(typeof(RadiatorState), SavedState);
}
catch
{
State = RadiatorState.Retracted;
return State;
}
}
set
{
SavedState = value.ToString();
}
}
public override void OnStart(PartModule.StartState state)
{
deployStates = Utils.SetUpAnimation(DeployAnimation, part);
heatStates = Utils.SetUpAnimation(HeatAnimation, part);
PressureCurve = new FloatCurve();
PressureCurve.Add(0f, 0f);
PressureCurve.Add(1f, 1f);
if (State == RadiatorState.Deployed || State == RadiatorState.Deploying)
{
foreach (AnimationState deployState in deployStates)
{
deployState.normalizedTime = 1f;
}
}
else if (State == RadiatorState.Retracted || State == RadiatorState.Retracting)
{
foreach (AnimationState deployState in deployStates)
{
deployState.normalizedTime = 0f;
}
}
else
{
// broken! none for you!
}
this.part.force_activate();
}
public override void OnUpdate()
{
foreach (AnimationState deployState in deployStates)
{
deployState.normalizedTime = Mathf.Clamp01(deployState.normalizedTime);
}
if (State == RadiatorState.Retracting)
{
if (EvalAnimationCompletionReversed(deployStates) == 0f)
State = RadiatorState.Retracted;
}
if (State == RadiatorState.Deploying)
{
if (EvalAnimationCompletion(deployStates) == 1f)
State = RadiatorState.Deployed;
}
if ((State == RadiatorState.Deployed && Events["DeployRadiator"].active) || (State == RadiatorState.Retracted && Events["RetractRadiator"].active))
{
Events["DeployRadiator"].active = !Events["DeployRadiator"].active;
Events["RetractRadiator"].active = !Events["RetractRadiator"].active;
}
}
public override void OnFixedUpdate()
{
// convect
if (Utils.VesselInAtmosphere(this.vessel))
{
double pressure = FlightGlobals.getStaticPressure(vessel.transform.position);
availableHeatRejection = PressureCurve.Evaluate((float)pressure);
}
else
{
availableHeatRejection = 0f;
}
if (State != RadiatorState.Deployed && State != RadiatorState.Broken)
{
availableHeatRejection += HeatRadiatedClosed;
}
else if (State == RadiatorState.Broken)
{
availableHeatRejection = 0f;
}
else
{
availableHeatRejection += HeatRadiated;
}
foreach (AnimationState state in heatStates)
{
state.normalizedTime = Mathf.MoveTowards(state.normalizedTime,Mathf.Clamp01(requestedHeatRejection/availableHeatRejection),0.1f*TimeWarp.fixedDeltaTime);
}
HeatRejectionGUI = String.Format("{0:F1} kW", availableHeatRejection);
}
private float EvalAnimationCompletion(AnimationState[] states)
{
float checker = 0f;
foreach (AnimationState state in states)
{
checker = Mathf.Max(state.normalizedTime, checker);
}
return checker;
}
private float EvalAnimationCompletionReversed(AnimationState[] states)
{
float checker = 1f;
foreach (AnimationState state in states)
{
checker = Mathf.Min(state.normalizedTime, checker);
}
return checker;
}
}
}