forked from KSPModStewards/SystemHeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleSystemHeatCometHarvester.cs
More file actions
221 lines (181 loc) · 6.12 KB
/
Copy pathModuleSystemHeatCometHarvester.cs
File metadata and controls
221 lines (181 loc) · 6.12 KB
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
using KSP.Localization;
using SystemHeat.Addons;
using Unity.Profiling;
using UnityEngine;
namespace SystemHeat
{
/// <summary>
/// The connection between a stock ModuleCometDrill and the SystemHeat system
/// </summary>
public class ModuleSystemHeatCometHarvester : ModuleCometDrill
{
// This should be unique on the part
[KSPField(isPersistant = false)]
public string moduleID = "harvester";
// This should correspond to the related ModuleSystemHeat
[KSPField(isPersistant = false)]
public string systemHeatModuleID;
// Map loop temperature to system efficiency (0-1.0)
[KSPField(isPersistant = false)]
public FloatCurve systemEfficiency = new FloatCurve();
// Map system outlet temperature (K) to heat generation (kW)
[KSPField(isPersistant = false)]
public float systemPower = 0f;
// The temperature at which the system shuts down to protect itself from overheating.
[KSPField(isPersistant = false)]
public float shutdownTemperature = 1000f;
// The temperature the system contributes to loops
[KSPField(isPersistant = false)]
public float systemOutletTemperature = 1000f;
// Current efficiency GUI string
[KSPField(isPersistant = false, guiActive = true, guiActiveEditor = true, guiName = "Harvester Efficiency")]
public string HarvesterEfficiency = "-1%";
protected ModuleSystemHeat heatModule;
// Stock ModuleCometDrill uses Physics.DefaultRaycastLayers (no mask).
private const int ImpactLayerMask = -5;
private static readonly ProfilerMarker BaseFixedUpdateMarker = new("ModuleCometDrill.FixedUpdate");
public override string GetInfo()
{
string info = base.GetInfo();
int pos = info.IndexOf("\n\n");
if (pos < 0)
return info;
var extraInfo = Localizer.Format("#LOC_SystemHeat_ModuleSystemHeatHarvester_PartInfoAdd",
Utils.ToSI(systemPower, "F0"),
systemOutletTemperature.ToString("F0"),
shutdownTemperature.ToString("F0")
);
return info.Substring(0, pos) + extraInfo + info.Substring(pos);
}
public void Start()
{
heatModule = ModuleUtils.FindHeatModule(part, systemHeatModuleID);
Utils.Log("[ModuleSystemHeatCometHarvester] Setup completed", LogType.Modules);
Fields["HarvesterEfficiency"].guiName = Localizer.Format("#LOC_SystemHeat_ModuleSystemHeatHarvester_Field_Efficiency", ConverterName);
RegisterImpactRaycast();
}
void OnEnable()
{
RegisterImpactRaycast();
}
public override void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
FixedUpdateFlight();
}
else
{
UpdateFlux();
}
}
void Update()
{
if (!part.IsPAWVisible())
return;
HarvesterEfficiency = Localizer.Format(
"#LOC_SystemHeat_ModuleSystemHeatHarvester_Field_Efficiency_Value",
(GetHeatThrottle() * 100f).ToString("F1")
);
}
void OnDisable()
{
heatModule?.AddFlux(moduleID, 0f, 0f, false);
HarvesterEfficiency = "-";
RaycastManager.Instance?.Unregister(this);
}
void RegisterImpactRaycast()
{
if (!HighLogic.LoadedSceneIsFlight || impactTransformCache == null)
return;
RaycastManager.Instance?.Register(this, impactTransformCache, ImpactRange, ImpactLayerMask);
}
protected override bool CheckForImpact()
{
if (string.IsNullOrEmpty(ImpactTransform) || impactTransformCache == null)
return true;
Collider collider;
var hit = RaycastManager.Instance?.GetRaycastHit(this);
if (hit != null)
{
collider = hit.Value.collider;
}
else
{
// If we're not registered for whatever reason then do the raycast ourselves.
var origin = impactTransformCache.position;
if (!Physics.Raycast(new Ray(origin, impactTransformCache.forward), out var fallback, ImpactRange, ImpactLayerMask))
return false;
collider = fallback.collider;
}
if (collider == null)
return false;
return collider.gameObject.GetComponentUpwards<ModuleComet>() != null;
}
void FixedUpdateFlight()
{
if (heatModule == null)
{
// This disables this module entirely, so it won't be called every frame.
enabled = false;
return;
}
CheckOverheat();
if (!IsActivated && !AlwaysActive)
enabled = false;
using (BaseFixedUpdateMarker.ConditionalAuto())
base.FixedUpdate();
}
void UpdateFlux() => UpdateFlux(lastTimeFactor);
void UpdateFlux(double timeFactor)
{
if (heatModule == null)
return;
if (ModuleIsActive())
{
float scale = timeFactor != 0.0 ? 1f : 0f;
if (HighLogic.LoadedSceneIsEditor)
scale = 1f;
heatModule.AddFlux(moduleID, systemOutletTemperature, systemPower * scale, true);
}
else
{
heatModule.AddFlux(moduleID, 0f, 0f, false);
}
}
void CheckOverheat()
{
if (!ModuleIsActive())
return;
if (heatModule.currentLoopTemperature <= shutdownTemperature)
return;
ScreenMessages.PostScreenMessage(
new ScreenMessage(
Localizer.Format(
"#LOC_SystemHeat_ModuleSystemHeatHarvester_Message_Shutdown",
part.partInfo.title),
3.0f,
ScreenMessageStyle.UPPER_CENTER));
StopResourceConverter();
Utils.Log("[ModuleSystemHeatCometHarvester]: Overheated, shutdown fired", LogType.Modules);
}
public override void StartResourceConverter()
{
enabled = true;
base.StartResourceConverter();
}
// In stock this would use the ModuleCoreHeat on the same part. We don't
// want that, and just override it to point to our own efficiency multiplier.
public override float GetHeatThrottle()
{
if (heatModule == null)
return 1f;
return systemEfficiency.Evaluate(heatModule.currentLoopTemperature);
}
protected override void PostProcess(ConverterResults result, double deltaTime)
{
base.PostProcess(result, deltaTime);
UpdateFlux(result.TimeFactor);
}
}
}