Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with the RP-1 config validation feature #17

Merged
merged 1 commit into from
Jun 19, 2024
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
20 changes: 10 additions & 10 deletions GameData/RealAntennas/RealAntennasCommNetParams.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL0
name = commsTL0
Level = 0
Description = WW2-era
PowerEfficiency = 0.0555
Expand All @@ -116,7 +116,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL1
name = commsTL1
Level = 1
Description = Lunar Range Comms, 1956: 26m antenna 1958
PowerEfficiency = 0.0769
Expand All @@ -133,7 +133,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL2
name = commsTL2
Level = 2
Description = Digital Comms, 1959-1960
PowerEfficiency = 0.1
Expand All @@ -150,7 +150,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL3
name = commsTL3
Level = 3
Description = Interplanetary Comms, 1961-1963: Maser 1962, S/C noise reduction 1961 (300-3000K=10dB noise temp)
PowerEfficiency = 0.1304
Expand All @@ -167,7 +167,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL4
name = commsTL4
Level = 4
Description = Improved Comms, 1964-1966: 64m Antenna 1967
PowerEfficiency = 0.1667
Expand All @@ -184,7 +184,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL5
name = commsTL5
Level = 5
Description = Advanced Comms, 1967-1971: Noise reduction 1968, block coding 1969
PowerEfficiency = 0.2222
Expand All @@ -201,7 +201,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL6
name = commsTL6
Level = 6
Description = Deep Space Comms, 1971-1974: Antenna improvements 1971-1972, Convolutional coding ~1973
PowerEfficiency = 0.25
Expand All @@ -218,7 +218,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL7
name = commsTL7
Level = 7
Description = High Data Rate Comms, 1976-1980: X-Band ~1975, concatenated coding, MW noise reduction ~1980
PowerEfficiency = 0.3
Expand All @@ -235,7 +235,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL8
name = commsTL8
Level = 8
Description = Massive Scale Comms, 1986-1997: 70m antennas 1988
PowerEfficiency = 0.3724
Expand All @@ -252,7 +252,7 @@ RealAntennasCommNetParams
}
TechLevelInfo
{
name = TL9
name = commsTL9
Level = 9
Description = Efficient Comms, 1998-2008: Super-cooled maser & feed 1995, Ka-band 2004
PowerEfficiency = 0.4397
Expand Down
20 changes: 10 additions & 10 deletions GameData/RealAntennas/RealismOverhaul.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
!TechLevelInfo,* {}
TechLevelInfo
{
name = TL0
name = commsTL0
Level = 0
Description = WW2-era
PowerEfficiency = 0.0555
Expand All @@ -170,7 +170,7 @@
}
TechLevelInfo
{
name = TL1
name = commsTL1
Level = 1
Description = Lunar Range Comms, 1956: 26m antenna 1958
PowerEfficiency = 0.0769
Expand All @@ -187,7 +187,7 @@
}
TechLevelInfo
{
name = TL2
name = commsTL2
Level = 2
Description = Digital Comms, 1959-1960
PowerEfficiency = 0.1
Expand All @@ -204,7 +204,7 @@
}
TechLevelInfo
{
name = TL3
name = commsTL3
Level = 3
Description = Interplanetary Comms, 1961-1963: Maser 1962, S/C noise reduction 1961 (300-3000K=10dB noise temp)
PowerEfficiency = 0.1304
Expand All @@ -221,7 +221,7 @@
}
TechLevelInfo
{
name = TL4
name = commsTL4
Level = 4
Description = Improved Comms, 1964-1966: 64m Antenna 1967
PowerEfficiency = 0.1667
Expand All @@ -238,7 +238,7 @@
}
TechLevelInfo
{
name = TL5
name = commsTL5
Level = 5
Description = Advanced Comms, 1967-1971: Noise reduction 1968, block coding 1969
PowerEfficiency = 0.2222
Expand All @@ -255,7 +255,7 @@
}
TechLevelInfo
{
name = TL6
name = commsTL6
Level = 6
Description = Deep Space Comms, 1971-1974: Antenna improvements 1971-1972, Convolutional coding ~1973
PowerEfficiency = 0.25
Expand All @@ -272,7 +272,7 @@
}
TechLevelInfo
{
name = TL7
name = commsTL7
Level = 7
Description = High Data Rate Comms, 1976-1980: X-Band ~1975, concatenated coding, MW noise reduction ~1980
PowerEfficiency = 0.3
Expand All @@ -289,7 +289,7 @@
}
TechLevelInfo
{
name = TL8
name = commsTL8
Level = 8
Description = Massive Scale Comms, 1986-1997: 70m antennas 1988
PowerEfficiency = 0.3724
Expand All @@ -306,7 +306,7 @@
}
TechLevelInfo
{
name = TL9
name = commsTL9
Level = 9
Description = Efficient Comms, 1998-2008: Super-cooled maser & feed 1995, Ka-band 2004
PowerEfficiency = 0.4397
Expand Down
126 changes: 119 additions & 7 deletions src/RealAntennasProject/ModuleRealAntenna.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ModuleRealAntenna : ModuleDataTransmitter, IPartCostModifier, IPart
private const string PAWGroup = "RealAntennas";
private const string PAWGroupPlanner = "Antenna Planning";
[KSPField(guiActiveEditor = true, guiName = "Antenna", isPersistant = true, groupName = PAWGroup, groupDisplayName = PAWGroup),
UI_Toggle(disabledText = "<color=red><b>Disabled</b></color>", enabledText = "<color=green>Enabled</color>", scene =UI_Scene.Editor)]
UI_Toggle(disabledText = "<color=red><b>Disabled</b></color>", enabledText = "<color=green>Enabled</color>", scene = UI_Scene.Editor)]
public bool _enabled = true;

[KSPField(guiActive = true, guiActiveEditor = true, guiName = "Gain", guiUnits = " dBi", guiFormat = "F1", groupName = PAWGroup, groupDisplayName = PAWGroup)]
Expand Down Expand Up @@ -70,6 +70,7 @@ public class ModuleRealAntenna : ModuleDataTransmitter, IPartCostModifier, IPart
public static float InactivePowerConsumptionMult = 0.1f;
private float DefaultPacketInterval = 1.0f;
private bool scienceMonitorActive = false;
private int actualMaxTechLevel = 0;

public float PowerDraw => RATools.LogScale(PowerDrawLinear);
public float PowerDrawLinear => RATools.LinearScale(TxPower) / RAAntenna.PowerEfficiency;
Expand Down Expand Up @@ -117,6 +118,7 @@ public override void OnSave(ConfigNode node)
public void OnDestroy()
{
GameEvents.OnGameSettingsApplied.Remove(ApplyGameSettings);
GameEvents.OnPartUpgradePurchased.Remove(OnPartUpgradePurchased);
}

public void Configure(ConfigNode node)
Expand All @@ -134,14 +136,19 @@ public override void OnStart(StartState state)
Fields[nameof(_enabled)].uiControlEditor.onFieldChanged = OnAntennaEnableChange;
(Fields[nameof(TxPower)].uiControlEditor as UI_FloatRange).maxValue = MaxTxPower;

if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER) maxTechLevel = HighLogic.CurrentGame.Parameters.CustomParams<RAParameters>().MaxTechLevel;
if (Fields[nameof(TechLevel)].uiControlEditor is UI_FloatRange fr)
actualMaxTechLevel = maxTechLevel; // maxTechLevel value can come from applied PartUpgrades
int maxLvlFromParams = HighLogic.CurrentGame.Parameters.CustomParams<RAParameters>().MaxTechLevel;
if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER)
{
fr.maxValue = maxTechLevel;
if (fr.maxValue == fr.minValue)
fr.maxValue += 0.001f;
maxTechLevel = actualMaxTechLevel = maxLvlFromParams;
}
if (TechLevel < 0) TechLevel = maxTechLevel;
else if (RATools.RP1Found)
{
// With RP-1 present, always allow selecting all TLs but validate the user choice on vessel getting built
maxTechLevel = maxLvlFromParams;
}
UpdateMaxTechLevelInUI();
if (TechLevel < 0) TechLevel = actualMaxTechLevel;

RAAntenna.Name = part.partInfo.title;
if (!RAAntenna.CanTarget)
Expand All @@ -158,13 +165,18 @@ public override void OnStart(StartState state)
SetupIdlePower();
RecalculateFields();
SetFieldVisibility(_enabled);
ApplyTLColoring();

if (HighLogic.LoadedSceneIsFlight)
{
isEnabled = _enabled;
if (_enabled)
GameEvents.OnGameSettingsApplied.Add(ApplyGameSettings);
}
else if (HighLogic.LoadedSceneIsEditor)
{
GameEvents.OnPartUpgradePurchased.Add(OnPartUpgradePurchased);
}
}

private void SetupIdlePower()
Expand Down Expand Up @@ -240,24 +252,62 @@ private void SetupUICallbacks()
Fields[nameof(plannerActiveTxTime)].uiControlEditor.onFieldChanged += OnPlannerActiveTxTimeChanged;
}

private void UpdateMaxTechLevelInUI()
{
if (Fields[nameof(TechLevel)].uiControlEditor is UI_FloatRange fr)
{
fr.maxValue = maxTechLevel;
if (fr.maxValue == fr.minValue)
fr.maxValue += 0.001f;
}
}

private void OnPlannerActiveTxTimeChanged(BaseField field, object obj) => RecalculatePlannerECConsumption();
private void OnAntennaEnableChange(BaseField field, object obj) { SetFieldVisibility(_enabled); RecalculatePlannerECConsumption(); }
private void OnRFBandChange(BaseField f, object obj) => RecalculateFields();
private void OnTxPowerChange(BaseField f, object obj) => RecalculateFields();
private void OnTechLevelChange(BaseField f, object obj) // obj is the OLD value
{
ApplyTLColoring();
string oldBand = RFBand;
ConfigBandOptions();
RecalculateFields();
if (!oldBand.Equals(RFBand)) MonoUtilities.RefreshPartContextWindow(part);
}

private void OnTechLevelChangeSymmetry(BaseField f, object obj) => ConfigBandOptions();

private void ApplyGameSettings()
{
StockRateModifier = HighLogic.CurrentGame.Parameters.CustomParams<RAParameters>().StockRateModifier;
}

/// <summary>
/// Handles TL PartUpgrade getting purchased in Editor scene
/// </summary>
/// <param name="upgd"></param>
private void OnPartUpgradePurchased(PartUpgradeHandler.Upgrade upgd)
{
var tlInf = TechLevelInfo.GetTechLevel(upgd.name);
if (tlInf != null && tlInf.Level > actualMaxTechLevel)
{
actualMaxTechLevel = tlInf.Level;
if (!RATools.RP1Found) maxTechLevel = actualMaxTechLevel;
UpdateMaxTechLevelInUI();
ApplyTLColoring();
}
}

private void ApplyTLColoring()
{
if (HighLogic.LoadedSceneIsEditor)
{
BaseField f = Fields[nameof(TechLevel)];
f.guiFormat = techLevel > actualMaxTechLevel ? "'<color=orange>'#'</color>'" : "N0";
f.guiName = techLevel > actualMaxTechLevel ? "<color=orange>Tech Level</color>" : "Tech Level";
}
}

private void ConfigBandOptions()
{
List<string> availableBands = new List<string>();
Expand Down Expand Up @@ -389,5 +439,67 @@ private void RecalculatePlannerECConsumption()
double ec = _enabled ? RAAntenna.IdlePowerDraw + (RAAntenna.PowerDrawLinear * 1e-6 * plannerActiveTxTime) : 0;
plannerECConsumption = new KeyValuePair<string, double>("ElectricCharge", -ec);
}

#region RP-1 integration
/// <summary>
/// Called from RP-1 VesselBuildValidator
/// </summary>
/// <param name="validationError"></param>
/// <param name="canBeResolved"></param>
/// <param name="costToResolve"></param>
/// <returns></returns>
public virtual bool Validate(out string validationError, out bool canBeResolved, out float costToResolve, out string techToResolve)
{
validationError = null;
canBeResolved = false;
costToResolve = 0;
techToResolve = string.Empty;

if (!_enabled || techLevel <= actualMaxTechLevel) return true;

PartUpgradeHandler.Upgrade upgd = GetUpgradeForTL(techLevel);
if (PartUpgradeManager.Handler.IsAvailableToUnlock(upgd.name))
{
canBeResolved = true;
costToResolve = upgd.entryCost;
validationError = $"purchase {upgd.title}";
}
else
{
techToResolve = upgd.techRequired;
validationError = $"unlock tech {ResearchAndDevelopment.GetTechnologyTitle(upgd.techRequired)}";
}

return false;
}

/// <summary>
/// Called from RP-1 VesselBuildValidator
/// </summary>
/// <returns></returns>
public virtual bool ResolveValidationError()
{
PartUpgradeHandler.Upgrade upgd = GetUpgradeForTL(techLevel);
if (upgd == null)
return false;
return PurchaseConfig(upgd);
}

private static bool PurchaseConfig(PartUpgradeHandler.Upgrade upgd)
{
CurrencyModifierQuery cmq = CurrencyModifierQuery.RunQuery(TransactionReasons.RnDPartPurchase, -upgd.entryCost, 0, 0);
if (!cmq.CanAfford())
return false;
PartUpgradeManager.Handler.SetUnlocked(upgd.name, true);
GameEvents.OnPartUpgradePurchased.Fire(upgd);
return true;
}

private static PartUpgradeHandler.Upgrade GetUpgradeForTL(int techLevel)
{
TechLevelInfo tlInf = TechLevelInfo.GetTechLevel(techLevel);
return PartUpgradeManager.Handler.GetUpgrade(tlInf.name);
}
#endregion
}
}
Loading
Loading