Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 74 additions & 165 deletions Source/Modules/ModuleSystemHeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,7 @@ public class ModuleSystemHeat : PartModule
[KSPField(isPersistant = false, guiActive = true, guiActiveEditor = true, guiName = "#LOC_SystemHeat_ModuleSystemHeat_Field_LoopTemperature", groupName = "sysheatinfo", groupDisplayName = "#LOC_SystemHeat_ModuleSystemHeat_GroupName", groupStartCollapsed = false)]
public string LoopTemperatureUI = "-";

public HeatLoop Loop
{
get
{
if (simulator != null)
{
return simulator.Loop(currentLoopID);
}
else return null;
}
}
public HeatLoop Loop => simulator?.Loop(currentLoopID);

public int LoopID
{
Expand All @@ -113,9 +103,8 @@ public float LoopFlux
}

protected SystemHeatSimulator simulator;
protected Dictionary<string, float> fluxes;
protected Dictionary<string, float> temperatures;
protected List<int> loopIDs;
protected readonly Dictionary<string, float> fluxes = [];
protected readonly Dictionary<string, float> temperatures = [];

public override string GetModuleDisplayName()
{
Expand All @@ -129,14 +118,6 @@ public override string GetInfo()

public void Start()
{

loopIDs = new List<int>();
fluxes = new Dictionary<string, float>();
temperatures = new Dictionary<string, float>();

for (int i = 0; i < SystemHeatSettings.maxLoopCount; i++)
loopIDs.Add(i);

SetupUI();

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

private void ChangeLoop(BaseField field, object oldFieldValueObj)
{
if (HighLogic.LoadedSceneIsFlight)
{
Utils.Log($"[ModuleSystemHeat] Changing part from loop {(int)oldFieldValueObj} to loop {currentLoopID}", LogType.Modules);
simulator.RemoveHeatModuleFromLoop((int)oldFieldValueObj, this);
simulator.AddHeatModuleToLoop(currentLoopID, this);
}
}

void ChangeAllLoops(object oldFieldValueObj)
{
if (HighLogic.LoadedSceneIsFlight)
{
Utils.Log($"[ModuleSystemHeat] Changing all loop {(int)oldFieldValueObj} modules to loop {currentLoopID}", LogType.Modules);
List<ModuleSystemHeat> allHeatModules = new List<ModuleSystemHeat>();
for (int i = 0; i < part.vessel.Parts.Count; i++)
{
if (part.vessel.Parts[i].GetComponent<ModuleSystemHeat>())
{
allHeatModules.Add(part.vessel.Parts[i].GetComponent<ModuleSystemHeat>());
}
}

// Find list of used heat modules
List<int> usedModules = new List<int>();
for (int i = 0; i < allHeatModules.Count; i++)
{
if (allHeatModules[i] != this)
{
if (!usedModules.Contains(allHeatModules[i].currentLoopID))
{
usedModules.Add(allHeatModules[i].currentLoopID);
Utils.Log($"[ModuleSystemHeat] {allHeatModules[i].currentLoopID} is in use", LogType.Modules);
}
}
}

bool unused = false;

int newID = currentLoopID;
while (!unused)
{

if (usedModules.Contains(newID))
{
Utils.Log($"[ModuleSystemHeat] {newID} is in use and cannot be used", LogType.Modules);
newID++;
}
else
{
unused = true;
Utils.Log($"[ModuleSystemHeat] {newID} will be the new ID", LogType.Modules);
}
}

for (int i = 0; i < allHeatModules.Count; i++)
{
if (allHeatModules[i] == this)
{
allHeatModules[i].currentLoopID = newID;

UIPartActionWindow window = UIPartActionController.Instance?.GetItem(part, false);
if (window == null) return;
window.displayDirty = true;
}
if (allHeatModules[i].currentLoopID == (int)oldFieldValueObj)
{


allHeatModules[i].currentLoopID = newID;

}
}
Utils.Log($"[ModuleSystemHeat] finished changing loop IDs to new {newID}", LogType.Modules);
simulator.ChangeLoopID((int)oldFieldValueObj, newID);
}
if (!HighLogic.LoadedSceneIsFlight)
return;

var oldLoopID = (int)oldFieldValueObj;
if (Utils.IsLogEnabled(LogType.Modules))
Utils.Log($"[ModuleSystemHeat] Changing part from loop {oldLoopID} to loop {currentLoopID}", LogType.Modules);
simulator.RemoveHeatModuleFromLoop(oldLoopID, this);
simulator.AddHeatModuleToLoop(currentLoopID, this);
}

static ProfilerMarker x_AddFluxMarker = new ProfilerMarker("ModuleSystemHeat.AddFlux");
static readonly ProfilerMarker x_AddFluxMarker = new("ModuleSystemHeat.AddFlux");

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

if (fluxes != null && temperatures != null)
{
fluxes[id] = flux;
fluxes[id] = flux;

// Add if used for nominal
if (useForNominal)
{
temperatures[id] = sourceTemperature;
}
else
{
temperatures[id] = 0f;
}
// Add if used for nominal
if (useForNominal)
{
temperatures[id] = sourceTemperature;
}
else
{
temperatures[id] = 0f;
}

totalSystemFlux = 0;
foreach (var f in fluxes.Values)
{
totalSystemFlux += f;
}
totalSystemFlux *= (float)(PhysicsGlobals.InternalHeatProductionFactor / 0.025d);
totalSystemTemperature = 0;
float denom = 0;
foreach (var temp in temperatures.Values)
totalSystemFlux = 0;
foreach (var f in fluxes.Values)
{
totalSystemFlux += f;
}
totalSystemFlux *= (float)(PhysicsGlobals.InternalHeatProductionFactor / 0.025d);
totalSystemTemperature = 0;
float denom = 0;
foreach (var temp in temperatures.Values)
{
if (temp > 0f)
{
if (temp > 0f)
{
totalSystemTemperature += temp;
denom++;
}
totalSystemTemperature += temp;
denom++;
}
}

if (denom > 0)
systemNominalTemperature = totalSystemTemperature / denom;
else
systemNominalTemperature = 0f;
if (denom > 0)
systemNominalTemperature = totalSystemTemperature / denom;
else
systemNominalTemperature = 0f;

totalSystemTemperature = systemNominalTemperature;
if (totalSystemTemperature == 0f)
{
ignoreTemperature = true;
}
else
{
ignoreTemperature = false;
}
totalSystemTemperature = systemNominalTemperature;
if (totalSystemTemperature == 0f)
{
ignoreTemperature = true;
}
else
{
ignoreTemperature = false;
}

x_AddFluxMarker.End();
}

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


if (enabled && !moduleUsed)
{
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
if (Utils.IsLogEnabled(LogType.Modules))
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
moduleUsed = enabled;
if (simulator != null)
simulator.AddHeatModule(this);
simulator?.AddHeatModule(this);

// turn things on
Fields["SystemTemperatureUI"].guiActive = true;
Expand All @@ -347,12 +255,13 @@ public void SetSystemHeatModuleEnabled(bool enabled)
Fields["currentLoopID"].guiActive = true;
Fields["currentLoopID"].guiActiveEditor = true;
}

if (!enabled && moduleUsed)
{
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
if (Utils.IsLogEnabled(LogType.Modules))
Utils.Log($"[ModuleSystemHeat] seting module {moduleID} system state from {moduleUsed} to {enabled}", LogType.Modules);
moduleUsed = enabled;
if (simulator != null)
simulator.RemoveHeatModule(this);
simulator?.RemoveHeatModule(this);

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

protected void Update()
{
if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
if (!HighLogic.LoadedSceneIsFlight && !HighLogic.LoadedSceneIsEditor)
return;

if (!moduleUsed || !part.IsPAWVisible())
return;

SystemFluxUI = String.Format("{0}W", Utils.ToSI(totalSystemFlux, "F0"));
LoopTemperatureUI = String.Format("{0:F0} / {1:F0} K", currentLoopTemperature, nominalLoopTemperature);
if (totalSystemFlux > 0f)
{
if (part.IsPAWVisible() && moduleUsed)
{
SystemFluxUI = String.Format("{0}W", Utils.ToSI(totalSystemFlux, "F0"));
LoopTemperatureUI = String.Format("{0:F0} / {1:F0} K", currentLoopTemperature, nominalLoopTemperature);
if (totalSystemFlux > 0f)
{
Fields["SystemTemperatureUI"].guiActive = true;
Fields["SystemTemperatureUI"].guiActiveEditor = true;
SystemTemperatureUI = String.Format("{0:F0} K", totalSystemTemperature);
}
else
{
Fields["SystemTemperatureUI"].guiActive = false;
Fields["SystemTemperatureUI"].guiActiveEditor = false;
}
}
Fields["SystemTemperatureUI"].guiActive = true;
Fields["SystemTemperatureUI"].guiActiveEditor = true;
SystemTemperatureUI = String.Format("{0:F0} K", totalSystemTemperature);
}
else
{
Fields["SystemTemperatureUI"].guiActive = false;
Fields["SystemTemperatureUI"].guiActiveEditor = false;
}
}

Expand Down
Loading
Loading