forked from KSPModStewards/SystemHeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleSystemHeatAsteroidHarvester.cs
More file actions
176 lines (144 loc) · 4.75 KB
/
Copy pathModuleSystemHeatAsteroidHarvester.cs
File metadata and controls
176 lines (144 loc) · 4.75 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
using UnityEngine;
using KSP.Localization;
using Unity.Profiling;
namespace SystemHeat
{
/// <summary>
/// The connection between a stock ModuleAsteroidDrill and the SystemHeat system
/// </summary>
public class ModuleSystemHeatAsteroidHarvester : ModuleAsteroidDrill
{
// 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;
//
[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;
private static readonly ProfilerMarker BaseFixedUpdateMarker = new("ModuleAsteroidDrill.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("[ModuleSystemHeatAsteroidHarvester] Setup completed", LogType.Modules);
Fields["HarvesterEfficiency"].guiName = Localizer.Format("#LOC_SystemHeat_ModuleSystemHeatHarvester_Field_Efficiency", ConverterName);
}
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 = "-";
}
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("[ModuleSystemHeatConverter]: 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);
}
}
}