Skip to content

Commit

Permalink
Improvements to unit handling/display in Proc Avi window
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanKell committed Oct 24, 2023
1 parent d356700 commit cb1f4e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
25 changes: 10 additions & 15 deletions Source/RP0/Avionics/ModuleProceduralAvionics.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using KSPAPIExtensions;
using RealFuels.Tanks;
using RealFuels.Tanks;
using System;
using System.Collections.Generic;
using UniLinq;
Expand Down Expand Up @@ -133,7 +132,7 @@ internal float GetShieldedAvionicsMass(float controllableMass)
private float GetShieldingMass(float avionicsMass) => Mathf.Pow(avionicsMass, 2f / 3) * CurrentProceduralAvionicsTechNode.shieldingMassFactor;

protected override float GetEnabledkW() => GetEnabledkW(CurrentProceduralAvionicsTechNode, GetInternalMassLimit());
private static float GetEnabledkW(ProceduralAvionicsTechNode techNode, float controllableMass) => GetPolynomial(controllableMass, techNode.powerExponent, techNode.powerConstant, techNode.powerFactor) / 1000f;
internal static float GetEnabledkW(ProceduralAvionicsTechNode techNode, float controllableMass) => GetPolynomial(controllableMass, techNode.powerExponent, techNode.powerConstant, techNode.powerFactor) / 1000f;
protected override float GetDisabledkW() => GetEnabledkW() * CurrentProceduralAvionicsTechNode.disabledPowerFactor;

private static float GetPolynomial(float value, float exponent, float constant, float factor) => (Mathf.Pow(value, exponent) + constant) * factor;
Expand Down Expand Up @@ -587,25 +586,21 @@ internal void RefreshDisplays()
_window?.RefreshDisplays();
}

private void RefreshPowerDisplay()
public static string BuildPowerString(float enabledkW, float disabledkW)
{
var powerConsumptionBuilder = StringBuilderCache.Acquire();
AppendPowerString(powerConsumptionBuilder, GetEnabledkW());
float dkW = GetDisabledkW();
if (dkW > 0)
powerConsumptionBuilder.Append(KERBALISM.Lib.HumanOrSIRate(enabledkW, KERBALISM.Lib.ECResID));
if (disabledkW > 0)
{
powerConsumptionBuilder.Append(" /");
AppendPowerString(powerConsumptionBuilder, dkW);
powerConsumptionBuilder.Append(" / ");
powerConsumptionBuilder.Append(KERBALISM.Lib.HumanOrSIRate(disabledkW, KERBALISM.Lib.ECResID));
}
powerRequirementsDisplay = powerConsumptionBuilder.ToStringAndRelease();
return powerConsumptionBuilder.ToStringAndRelease();
}

private void AppendPowerString(System.Text.StringBuilder builder, float val)
private void RefreshPowerDisplay()
{
if (val >= 1)
builder.AppendFormat(KwFormat, val).Append("\u2009kW");
else
builder.AppendFormat(WFormat, val * 1000).Append("\u2009W");
powerRequirementsDisplay = BuildPowerString(GetEnabledkW(), GetDisabledkW());
}

private void SetScienceContainer()
Expand Down
9 changes: 6 additions & 3 deletions Source/RP0/UI/ProceduralAvionicsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private void WindowFunction(int windowID)
if (float.TryParse(ControllableMass, out _newControlMass))
{
float avionicsMass = _module.GetShieldedAvionicsMass(_newControlMass);
GUILayout.Label($" ({avionicsMass * 1000:0.#} kg)", HighLogic.Skin.label, GUILayout.Width(150));
GUILayout.Label($" ({MathUtils.PrintMass(avionicsMass)})", HighLogic.Skin.label, GUILayout.Width(150));
}

if (oldControlMass != _newControlMass)
Expand All @@ -187,12 +187,15 @@ private void WindowFunction(int windowID)
GUILayout.BeginHorizontal(GUILayout.Width(250));
GUILayout.Label("EC amount: ", HighLogic.Skin.label, GUILayout.Width(150));
_sECAmount = GUILayout.TextField(_sECAmount, HighLogic.Skin.textField);
GUILayout.Label("J", HighLogic.Skin.label);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal(GUILayout.MaxWidth(50));
if (float.TryParse(_sECAmount, out float ecAmount))
{
GUILayout.Label($" ({_ecTank.mass * ecAmount:0.#} kg)", HighLogic.Skin.label, GUILayout.Width(150));
float ekW = ModuleProceduralAvionics.GetEnabledkW(_module.CurrentProceduralAvionicsTechNode, _newControlMass);
float dkW = _module.CurrentProceduralAvionicsTechNode.disabledPowerFactor * ekW;
GUILayout.Label($" ({MathUtils.PrintMass(_ecTank.mass * ecAmount / _ecTank.utilization)}, {ModuleProceduralAvionics.BuildPowerString(ekW, dkW)})", HighLogic.Skin.label, GUILayout.Width(150));
}
GUILayout.EndHorizontal();
GUILayout.EndHorizontal();
Expand All @@ -205,7 +208,7 @@ private void WindowFunction(int windowID)
GUI.enabled = false;
_sExtraVolume = GUILayout.TextField(_sExtraVolume, HighLogic.Skin.textField);
GUI.enabled = true;
GUILayout.Label("l", HighLogic.Skin.label);
GUILayout.Label("L", HighLogic.Skin.label);
GUILayout.EndHorizontal();

if (_showROTankSizeWarning)
Expand Down
9 changes: 7 additions & 2 deletions Source/RP0/Utilities/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;

// ReSharper disable once CheckNamespace
namespace KSPAPIExtensions
namespace RP0
{
public static class MathUtils
{
Expand Down Expand Up @@ -350,10 +350,15 @@ public static string FormatMass(float mass, int sigFigs = 4, int exponent = 0)
/// </summary>
public static string FormatMass(double mass, int sigFigs = 4, int exponent = 0)
{
return mass < 1.0f ?
return mass < 1.0d ?
mass.ToStringSI(sigFigs, exponent + 6, "g") :
mass.ToStringSI(sigFigs, exponent, "t");
}

public static string PrintMass(double mass, int sigFigs = 3, bool longPrefix = false)
{
return mass < 1d ? KSPUtil.PrintSI(mass * 1000d * 1000d, longPrefix ? "grams" : "g", sigFigs, longPrefix) : KSPUtil.PrintSI(mass, longPrefix ? "tons" : "t", sigFigs, longPrefix);
}
}

/// <summary>
Expand Down

0 comments on commit cb1f4e1

Please sign in to comment.