Skip to content

Commit 1a50e5e

Browse files
authored
Various cleanups to ModuleSystemHeat (#161)
* Simplify the Loop property * Immediately initialize fluxes, temperatures, and loopIDs * Initialize loop IDs in OnAwake * Add a helper for checking whether a log is enabled + cleanup * Clean up ModuleSystemHeat * Initialize fluxes, temperatures, and loopIDs in the ctor * Switch to using guard clauses * A bunch of minor formatting fixes * Avoid calling string.Format if the relevant LogType is not enabled * Delete ChangeAllLoops since it is dead code * Make fluxes, temperatures, loopIDs readonly * Remove now-unnecessary null guards * Add a ConditionalAuto helper that only traces on ENABLE_PROFILER
1 parent 98f790b commit 1a50e5e

2 files changed

Lines changed: 127 additions & 189 deletions

File tree

Source/Modules/ModuleSystemHeat.cs

Lines changed: 74 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,7 @@ public class ModuleSystemHeat : PartModule
8282
[KSPField(isPersistant = false, guiActive = true, guiActiveEditor = true, guiName = "#LOC_SystemHeat_ModuleSystemHeat_Field_LoopTemperature", groupName = "sysheatinfo", groupDisplayName = "#LOC_SystemHeat_ModuleSystemHeat_GroupName", groupStartCollapsed = false)]
8383
public string LoopTemperatureUI = "-";
8484

85-
public HeatLoop Loop
86-
{
87-
get
88-
{
89-
if (simulator != null)
90-
{
91-
return simulator.Loop(currentLoopID);
92-
}
93-
else return null;
94-
}
95-
}
85+
public HeatLoop Loop => simulator?.Loop(currentLoopID);
9686

9787
public int LoopID
9888
{
@@ -113,9 +103,8 @@ public float LoopFlux
113103
}
114104

115105
protected SystemHeatSimulator simulator;
116-
protected Dictionary<string, float> fluxes;
117-
protected Dictionary<string, float> temperatures;
118-
protected List<int> loopIDs;
106+
protected readonly Dictionary<string, float> fluxes = [];
107+
protected readonly Dictionary<string, float> temperatures = [];
119108

120109
public override string GetModuleDisplayName()
121110
{
@@ -129,14 +118,6 @@ public override string GetInfo()
129118

130119
public void Start()
131120
{
132-
133-
loopIDs = new List<int>();
134-
fluxes = new Dictionary<string, float>();
135-
temperatures = new Dictionary<string, float>();
136-
137-
for (int i = 0; i < SystemHeatSettings.maxLoopCount; i++)
138-
loopIDs.Add(i);
139-
140121
SetupUI();
141122

142123
Fields["totalSystemTemperature"].guiActive = SystemHeatSettings.DebugPartUI;
@@ -157,90 +138,23 @@ void SetupUI()
157138
{
158139
BaseField chooseField = Fields["currentLoopID"];
159140
UI_ChooseOption chooseOption = HighLogic.LoadedSceneIsFlight ? chooseField.uiControlFlight as UI_ChooseOption : chooseField.uiControlEditor as UI_ChooseOption;
160-
chooseOption.options = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
141+
chooseOption.options = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
161142
chooseOption.onFieldChanged = ChangeLoop;
162143
}
163144

164145
private void ChangeLoop(BaseField field, object oldFieldValueObj)
165146
{
166-
if (HighLogic.LoadedSceneIsFlight)
167-
{
168-
Utils.Log($"[ModuleSystemHeat] Changing part from loop {(int)oldFieldValueObj} to loop {currentLoopID}", LogType.Modules);
169-
simulator.RemoveHeatModuleFromLoop((int)oldFieldValueObj, this);
170-
simulator.AddHeatModuleToLoop(currentLoopID, this);
171-
}
172-
}
173-
174-
void ChangeAllLoops(object oldFieldValueObj)
175-
{
176-
if (HighLogic.LoadedSceneIsFlight)
177-
{
178-
Utils.Log($"[ModuleSystemHeat] Changing all loop {(int)oldFieldValueObj} modules to loop {currentLoopID}", LogType.Modules);
179-
List<ModuleSystemHeat> allHeatModules = new List<ModuleSystemHeat>();
180-
for (int i = 0; i < part.vessel.Parts.Count; i++)
181-
{
182-
if (part.vessel.Parts[i].GetComponent<ModuleSystemHeat>())
183-
{
184-
allHeatModules.Add(part.vessel.Parts[i].GetComponent<ModuleSystemHeat>());
185-
}
186-
}
187-
188-
// Find list of used heat modules
189-
List<int> usedModules = new List<int>();
190-
for (int i = 0; i < allHeatModules.Count; i++)
191-
{
192-
if (allHeatModules[i] != this)
193-
{
194-
if (!usedModules.Contains(allHeatModules[i].currentLoopID))
195-
{
196-
usedModules.Add(allHeatModules[i].currentLoopID);
197-
Utils.Log($"[ModuleSystemHeat] {allHeatModules[i].currentLoopID} is in use", LogType.Modules);
198-
}
199-
}
200-
}
201-
202-
bool unused = false;
203-
204-
int newID = currentLoopID;
205-
while (!unused)
206-
{
207-
208-
if (usedModules.Contains(newID))
209-
{
210-
Utils.Log($"[ModuleSystemHeat] {newID} is in use and cannot be used", LogType.Modules);
211-
newID++;
212-
}
213-
else
214-
{
215-
unused = true;
216-
Utils.Log($"[ModuleSystemHeat] {newID} will be the new ID", LogType.Modules);
217-
}
218-
}
219-
220-
for (int i = 0; i < allHeatModules.Count; i++)
221-
{
222-
if (allHeatModules[i] == this)
223-
{
224-
allHeatModules[i].currentLoopID = newID;
225-
226-
UIPartActionWindow window = UIPartActionController.Instance?.GetItem(part, false);
227-
if (window == null) return;
228-
window.displayDirty = true;
229-
}
230-
if (allHeatModules[i].currentLoopID == (int)oldFieldValueObj)
231-
{
232-
233-
234-
allHeatModules[i].currentLoopID = newID;
235-
236-
}
237-
}
238-
Utils.Log($"[ModuleSystemHeat] finished changing loop IDs to new {newID}", LogType.Modules);
239-
simulator.ChangeLoopID((int)oldFieldValueObj, newID);
240-
}
147+
if (!HighLogic.LoadedSceneIsFlight)
148+
return;
149+
150+
var oldLoopID = (int)oldFieldValueObj;
151+
if (Utils.IsLogEnabled(LogType.Modules))
152+
Utils.Log($"[ModuleSystemHeat] Changing part from loop {oldLoopID} to loop {currentLoopID}", LogType.Modules);
153+
simulator.RemoveHeatModuleFromLoop(oldLoopID, this);
154+
simulator.AddHeatModuleToLoop(currentLoopID, this);
241155
}
242156

243-
static ProfilerMarker x_AddFluxMarker = new ProfilerMarker("ModuleSystemHeat.AddFlux");
157+
static readonly ProfilerMarker x_AddFluxMarker = new("ModuleSystemHeat.AddFlux");
244158

245159
/// <summary>
246160
/// Add heat flux at a given temperature to system
@@ -254,56 +168,51 @@ void ChangeAllLoops(object oldFieldValueObj)
254168
/// </param>
255169
public void AddFlux(string id, float sourceTemperature, float flux, bool useForNominal)
256170
{
257-
x_AddFluxMarker.Begin();
171+
using var scope = x_AddFluxMarker.ConditionalAuto();
258172

259-
if (fluxes != null && temperatures != null)
260-
{
261-
fluxes[id] = flux;
173+
fluxes[id] = flux;
262174

263-
// Add if used for nominal
264-
if (useForNominal)
265-
{
266-
temperatures[id] = sourceTemperature;
267-
}
268-
else
269-
{
270-
temperatures[id] = 0f;
271-
}
175+
// Add if used for nominal
176+
if (useForNominal)
177+
{
178+
temperatures[id] = sourceTemperature;
179+
}
180+
else
181+
{
182+
temperatures[id] = 0f;
183+
}
272184

273-
totalSystemFlux = 0;
274-
foreach (var f in fluxes.Values)
275-
{
276-
totalSystemFlux += f;
277-
}
278-
totalSystemFlux *= (float)(PhysicsGlobals.InternalHeatProductionFactor / 0.025d);
279-
totalSystemTemperature = 0;
280-
float denom = 0;
281-
foreach (var temp in temperatures.Values)
185+
totalSystemFlux = 0;
186+
foreach (var f in fluxes.Values)
187+
{
188+
totalSystemFlux += f;
189+
}
190+
totalSystemFlux *= (float)(PhysicsGlobals.InternalHeatProductionFactor / 0.025d);
191+
totalSystemTemperature = 0;
192+
float denom = 0;
193+
foreach (var temp in temperatures.Values)
194+
{
195+
if (temp > 0f)
282196
{
283-
if (temp > 0f)
284-
{
285-
totalSystemTemperature += temp;
286-
denom++;
287-
}
197+
totalSystemTemperature += temp;
198+
denom++;
288199
}
200+
}
289201

290-
if (denom > 0)
291-
systemNominalTemperature = totalSystemTemperature / denom;
292-
else
293-
systemNominalTemperature = 0f;
202+
if (denom > 0)
203+
systemNominalTemperature = totalSystemTemperature / denom;
204+
else
205+
systemNominalTemperature = 0f;
294206

295-
totalSystemTemperature = systemNominalTemperature;
296-
if (totalSystemTemperature == 0f)
297-
{
298-
ignoreTemperature = true;
299-
}
300-
else
301-
{
302-
ignoreTemperature = false;
303-
}
207+
totalSystemTemperature = systemNominalTemperature;
208+
if (totalSystemTemperature == 0f)
209+
{
210+
ignoreTemperature = true;
211+
}
212+
else
213+
{
214+
ignoreTemperature = false;
304215
}
305-
306-
x_AddFluxMarker.End();
307216
}
308217

309218
public float GetFlux(string id)
@@ -329,13 +238,12 @@ public void SetSystemHeatModuleEnabled(bool enabled)
329238
if (simulator == null)
330239
FindSimulator();
331240

332-
333241
if (enabled && !moduleUsed)
334242
{
335-
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
243+
if (Utils.IsLogEnabled(LogType.Modules))
244+
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
336245
moduleUsed = enabled;
337-
if (simulator != null)
338-
simulator.AddHeatModule(this);
246+
simulator?.AddHeatModule(this);
339247

340248
// turn things on
341249
Fields["SystemTemperatureUI"].guiActive = true;
@@ -347,12 +255,13 @@ public void SetSystemHeatModuleEnabled(bool enabled)
347255
Fields["currentLoopID"].guiActive = true;
348256
Fields["currentLoopID"].guiActiveEditor = true;
349257
}
258+
350259
if (!enabled && moduleUsed)
351260
{
352-
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
261+
if (Utils.IsLogEnabled(LogType.Modules))
262+
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
353263
moduleUsed = enabled;
354-
if (simulator != null)
355-
simulator.RemoveHeatModule(this);
264+
simulator?.RemoveHeatModule(this);
356265

357266
// turn things off
358267
Fields["SystemTemperatureUI"].guiActive = false;
@@ -376,24 +285,24 @@ protected void FixedUpdate()
376285

377286
protected void Update()
378287
{
379-
if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
288+
if (!HighLogic.LoadedSceneIsFlight && !HighLogic.LoadedSceneIsEditor)
289+
return;
290+
291+
if (!moduleUsed || !part.IsPAWVisible())
292+
return;
293+
294+
SystemFluxUI = String.Format("{0}W", Utils.ToSI(totalSystemFlux, "F0"));
295+
LoopTemperatureUI = String.Format("{0:F0} / {1:F0} K", currentLoopTemperature, nominalLoopTemperature);
296+
if (totalSystemFlux > 0f)
380297
{
381-
if (part.IsPAWVisible() && moduleUsed)
382-
{
383-
SystemFluxUI = String.Format("{0}W", Utils.ToSI(totalSystemFlux, "F0"));
384-
LoopTemperatureUI = String.Format("{0:F0} / {1:F0} K", currentLoopTemperature, nominalLoopTemperature);
385-
if (totalSystemFlux > 0f)
386-
{
387-
Fields["SystemTemperatureUI"].guiActive = true;
388-
Fields["SystemTemperatureUI"].guiActiveEditor = true;
389-
SystemTemperatureUI = String.Format("{0:F0} K", totalSystemTemperature);
390-
}
391-
else
392-
{
393-
Fields["SystemTemperatureUI"].guiActive = false;
394-
Fields["SystemTemperatureUI"].guiActiveEditor = false;
395-
}
396-
}
298+
Fields["SystemTemperatureUI"].guiActive = true;
299+
Fields["SystemTemperatureUI"].guiActiveEditor = true;
300+
SystemTemperatureUI = String.Format("{0:F0} K", totalSystemTemperature);
301+
}
302+
else
303+
{
304+
Fields["SystemTemperatureUI"].guiActive = false;
305+
Fields["SystemTemperatureUI"].guiActiveEditor = false;
397306
}
398307
}
399308

0 commit comments

Comments
 (0)