Skip to content

Commit

Permalink
Failure_Engine Override Patterns
Browse files Browse the repository at this point in the history
Don't duplicate code found in base methods when calling those base methods.  Updated OnStart, OnAwake
Don't declare overrides that do nothing but call the base method
Start caching TestFlightCore in FailureEngine, rather than having to rediscover it every time we check if TF is enabled (multiple times per frame!)
  • Loading branch information
DRVeyl committed Jan 26, 2022
1 parent bb5e6e4 commit ed2423a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 110 deletions.
2 changes: 1 addition & 1 deletion TestFlightCore/TestFlightCore/TestFlightCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ public List<string> GetTestFlightInfo()
{
ITestFlightFailure failureModule = failureModules[i];
List<string> infoColl = failureModule.GetTestFlightInfo();
if (infoColl != null)
if (infoColl != null && infoColl.Count > 0)
{
// Don't indent header string
infoStrings.Add(infoColl[0]);
Expand Down
51 changes: 11 additions & 40 deletions TestFlightFailure_Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,36 @@ protected class EngineHandler
public bool failEngine;
}

protected List<EngineHandler> engines = null;
protected List<EngineHandler> engines = new List<EngineHandler>();
protected ITestFlightCore core = null;

public new bool TestFlightEnabled
{
get
{
ITestFlightCore core = TestFlightUtil.GetCore (this.part, Configuration);
if (core != null && engines.Count > 0)
return core.TestFlightEnabled;

core = TestFlightUtil.GetCore(part, Configuration);
if (core == null)
{
Log ("EngineBase: No TestFlight core found");
return false;
}
// Make sure we have valid engines
if (engines == null)
{
else if (engines.Count == 0)
Log("EngineBase: No valid engines found");
return false;
}
return core.TestFlightEnabled;

return false;
}
}

public override void OnStart(StartState state)
{
base.OnStart(state);
StartCoroutine("Attach");
}

IEnumerator Attach()
{
while (this.part == null || this.part.partInfo == null || this.part.partInfo.partPrefab == null || this.part.Modules == null)
yield return null;

core = TestFlightUtil.GetCore(part, Configuration);
Startup();
}

public virtual void Startup()
{
engines = new List<EngineHandler>();
engines.Clear();
if (!String.IsNullOrEmpty(engineID))
{
if (engineID.ToLower() == "all")
Expand Down Expand Up @@ -130,16 +121,6 @@ public override void SetActiveConfig(string alias)
currentConfig.TryGetValue("engineID", ref engineID);
}

public override void OnAwake()
{
base.OnAwake();
if (!string.IsNullOrEmpty(configNodeData))
{
var node = ConfigNode.Parse(configNodeData);
OnLoad(node);
}
}

public IEnumerator UpdateEngineStatus()
{
while (true)
Expand All @@ -156,16 +137,6 @@ public IEnumerator UpdateEngineStatus()
}
}
}

// Failure methods
public override void DoFailure()
{
base.DoFailure();
}
public override float DoRepair()
{
return base.DoRepair();
}
}
}

22 changes: 2 additions & 20 deletions TestFlightFailure_EnginePerformanceLoss.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
using TestFlightAPI;

namespace TestFlight
namespace TestFlight
{
public class TestFlightFailure_EnginePerformanceLoss : TestFlightFailure_Engine
{
Expand All @@ -17,7 +10,6 @@ public class TestFlightFailure_EnginePerformanceLoss : TestFlightFailure_Engine
public override void OnStart(StartState state)
{
base.OnStart(state);
base.Startup();
if (Failed)
DoFailure();
}
Expand All @@ -30,7 +22,7 @@ public override void DoFailure()
// for each engine change its specific impulse
foreach (EngineHandler engine in engines)
{
float jitter = ispMultiplierJitter - ((float)TestFlightUtil.GetCore(this.part, Configuration).RandomGenerator.NextDouble() * (ispMultiplierJitter * 2));
float jitter = ispMultiplierJitter - ((float)core.RandomGenerator.NextDouble() * (ispMultiplierJitter * 2));
float actualMultiplier = ispMultiplier + jitter;
engine.engine.SetFuelIspMult(actualMultiplier);
engine.engine.failMessage = failureTitle;
Expand Down Expand Up @@ -60,16 +52,6 @@ public override void SetActiveConfig(string alias)
currentConfig.TryGetValue("ispMultiplier", ref ispMultiplier);
currentConfig.TryGetValue("ispMultiplierJitter", ref ispMultiplierJitter);
}

public override void OnAwake()
{
base.OnAwake();
if (!string.IsNullOrEmpty(configNodeData))
{
var node = ConfigNode.Parse(configNodeData);
OnLoad(node);
}
}
}
}

21 changes: 0 additions & 21 deletions TestFlightFailure_IgnitionFail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ public class TestFlightFailure_IgnitionFail : TestFlightFailure_Engine

private readonly Dictionary<uint, EngineRunData> engineRunData = new Dictionary<uint, EngineRunData>(8);

private ITestFlightCore core = null;
private bool preLaunchFailures;
private bool dynPressurePenalties;
private bool verboseDebugging;
private float restartTimeMin;
private float restartTimeMax = float.MaxValue;

[KSPField(isPersistant=true)]
private double previousTime;
Expand Down Expand Up @@ -123,13 +120,7 @@ public List<string> RestartCurveDescription()
public override void OnStart(StartState state)
{
base.OnStart(state);
core = TestFlightUtil.GetCore(this.part, Configuration);
if (core != null)
Startup();

verboseDebugging = core.DebugEnabled;

// Get the in-game settings
preLaunchFailures = HighLogic.CurrentGame.Parameters.CustomParams<TestFlightGameSettings>().preLaunchFailures;
dynPressurePenalties = HighLogic.CurrentGame.Parameters.CustomParams<TestFlightGameSettings>().dynPressurePenalties;
}
Expand Down Expand Up @@ -158,20 +149,10 @@ public override void OnSave(ConfigNode node)
public override void Startup()
{
base.Startup();
if (core == null)
return;
// We don't want this getting triggered as a random failure
core.DisableFailure("TestFlightFailure_IgnitionFail");
}

public void OnEnable()
{
if (core == null)
core = TestFlightUtil.GetCore(this.part, Configuration);
if (core != null)
Startup();
}

public EngineRunData GetEngineRunDataForID(uint id)
{
EngineRunData data;
Expand Down Expand Up @@ -438,8 +419,6 @@ public override void SetActiveConfig(string alias)
else
{
restartWindowPenalty.Add(0f, 1f);
restartTimeMin = 0;
restartTimeMax = float.MaxValue;
hasRestartWindow = false;
}

Expand Down
19 changes: 1 addition & 18 deletions TestFlightFailure_ReducedMaxThrust.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
using TestFlightAPI;

namespace TestFlight
namespace TestFlight
{
public class TestFlightFailure_ReducedMaxThrust : TestFlightFailure_Engine
{
Expand All @@ -15,7 +8,6 @@ public class TestFlightFailure_ReducedMaxThrust : TestFlightFailure_Engine
public override void OnStart(StartState state)
{
base.OnStart(state);
base.Startup();
if (Failed)
DoFailure();
}
Expand All @@ -29,15 +21,6 @@ public override void SetActiveConfig(string alias)
currentConfig.TryGetValue("thrustReduction", ref thrustReduction);
}

public override void OnAwake()
{
base.OnAwake();
if (!string.IsNullOrEmpty(configNodeData))
{
var node = ConfigNode.Parse(configNodeData);
OnLoad(node);
}
}
/// <summary>
/// Triggers the failure controlled by the failure module
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions TestFlightFailure_ShutdownEngine.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
using TestFlightAPI;

namespace TestFlight
{
Expand All @@ -17,11 +12,6 @@ protected struct CachedEngineState

Dictionary<int, CachedEngineState> engineStates;

public override void OnStart(StartState state)
{
base.OnStart(state);
base.Startup();
}
/// <summary>
/// Triggers the failure controlled by the failure module
/// </summary>
Expand Down

0 comments on commit ed2423a

Please sign in to comment.