Skip to content

Commit

Permalink
Add update frequency attribute to parameters that rely on polling
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav committed Dec 9, 2023
1 parent 12d0f36 commit 8e83e86
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ public class CollectScienceCustom : VesselParameter
protected BodyLocation? location { get; set; }
protected List<string> experiment { get; set; }
protected ScienceRecoveryMethod recoveryMethod { get; set; }
protected float updateFrequency { get; set; }

private static Vessel.Situations[] landedSituations = new Vessel.Situations[] { Vessel.Situations.LANDED, Vessel.Situations.PRELAUNCH, Vessel.Situations.SPLASHED };

private Dictionary<string, ScienceSubject> matchingSubjects = new Dictionary<string, ScienceSubject>();
private Dictionary<string, bool> recoveryDone = new Dictionary<string, bool>();

private float lastUpdate = 0.0f;
private const float UPDATE_FREQUENCY = 0.25f;
private int updateTicks = 0;
private static Vessel lastVessel = null;
private static string lastBiome = null;
private static float nextOffset = 0.0f;
internal const float DEFAULT_UPDATE_FREQUENCY = 0.25f;
private const float OFFSET_INCREMENT = 0.7f;

public CollectScienceCustom()
Expand All @@ -74,7 +75,7 @@ public CollectScienceCustom()
}

public CollectScienceCustom(CelestialBody targetBody, string biome, ExperimentSituations? situation, BodyLocation? location,
List<string> experiment, ScienceRecoveryMethod recoveryMethod, string title)
List<string> experiment, ScienceRecoveryMethod recoveryMethod, float updateFrequency, string title)
: base(title)
{
lastUpdate = UnityEngine.Time.fixedTime + nextOffset;
Expand All @@ -87,6 +88,7 @@ public CollectScienceCustom(CelestialBody targetBody, string biome, ExperimentSi
this.location = location;
this.experiment = experiment;
this.recoveryMethod = recoveryMethod;
this.updateFrequency = updateFrequency;

disableOnStateChange = true;

Expand Down Expand Up @@ -269,6 +271,7 @@ private bool CheckBiome(Vessel vessel)
protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);
node.AddValue("updateFrequency", updateFrequency);

if (targetBody != null)
{
Expand Down Expand Up @@ -311,6 +314,7 @@ protected override void OnParameterLoad(ConfigNode node)
try
{
base.OnParameterLoad(node);
updateFrequency = ConfigNodeUtil.ParseValue<float>(node, "updateFrequency", DEFAULT_UPDATE_FREQUENCY);
targetBody = ConfigNodeUtil.ParseValue<CelestialBody>(node, "targetBody", (CelestialBody)null);
biome = ConfigNodeUtil.ParseValue<string>(node, "biome", "").Replace(" ", "");
situation = ConfigNodeUtil.ParseValue<ExperimentSituations?>(node, "situation", (ExperimentSituations?)null);
Expand Down Expand Up @@ -342,7 +346,7 @@ protected override void OnUpdate()

// Need to do frequent checks to catch biome changes
base.OnUpdate();
if (UnityEngine.Time.fixedTime - lastUpdate > UPDATE_FREQUENCY)
if (UnityEngine.Time.fixedTime - lastUpdate > updateFrequency)
{
lastUpdate = UnityEngine.Time.fixedTime;
string biome;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ public enum AntennaType
protected double minAntennaPower { get; set; }
protected double maxAntennaPower { get; set; }
protected AntennaType antennaType { get; set; }
protected float updateFrequency { get; set; }

private float lastUpdate = 0.0f;
private const float UPDATE_FREQUENCY = 0.25f;
internal const float DEFAULT_UPDATE_FREQUENCY = 0.25f;

public HasAntenna()
: this(0.0)
: this(DEFAULT_UPDATE_FREQUENCY, 0.0)
{
}

public HasAntenna(double minAntennaPower = 0.0, double maxAntennaPower = double.MaxValue, AntennaType antennaType = AntennaType.TRANSMIT, string title = null)
public HasAntenna(float updateFrequency, double minAntennaPower = 0.0, double maxAntennaPower = double.MaxValue, AntennaType antennaType = AntennaType.TRANSMIT, string title = null)
: base(title)
{
this.minAntennaPower = minAntennaPower;
this.maxAntennaPower = maxAntennaPower;
this.antennaType = antennaType;
this.updateFrequency = updateFrequency;

if (title == null)
{
Expand Down Expand Up @@ -67,6 +69,7 @@ public HasAntenna(double minAntennaPower = 0.0, double maxAntennaPower = double.
protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);
node.AddValue("updateFrequency", updateFrequency);
node.AddValue("minAntennaPower", minAntennaPower);
if (maxAntennaPower != double.MaxValue)
{
Expand All @@ -78,6 +81,7 @@ protected override void OnParameterSave(ConfigNode node)
protected override void OnParameterLoad(ConfigNode node)
{
base.OnParameterLoad(node);
updateFrequency = ConfigNodeUtil.ParseValue<float>(node, "updateFrequency", DEFAULT_UPDATE_FREQUENCY);
minAntennaPower = Convert.ToDouble(node.GetValue("minAntennaPower"));
maxAntennaPower = node.HasValue("maxAntennaPower") ? Convert.ToDouble(node.GetValue("maxAntennaPower")) : double.MaxValue;
antennaType = ConfigNodeUtil.ParseValue<AntennaType>(node, "antennaType", AntennaType.TRANSMIT);
Expand All @@ -96,7 +100,7 @@ protected override void OnUnregister()
protected override void OnUpdate()
{
base.OnUpdate();
if (UnityEngine.Time.fixedTime - lastUpdate > UPDATE_FREQUENCY)
if (UnityEngine.Time.fixedTime - lastUpdate > updateFrequency)
{
lastUpdate = UnityEngine.Time.fixedTime;
CheckVessel(FlightGlobals.ActiveVessel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ public class Filter
public Filter() { }
}

protected float updateFrequency { get; set; }

protected List<Filter> filters = new List<Filter>();
protected bool capacity = false;

private float lastUpdate = 0.0f;
private const float UPDATE_FREQUENCY = 0.25f;
internal const float DEFAULT_UPDATE_FREQUENCY = 0.25f;

public HasResource()
: base(null)
{
}

public HasResource(List<Filter> filters, bool capacity, string title = null)
public HasResource(List<Filter> filters, bool capacity, float updateFrequency, string title = null)
: base(title)
{
this.filters = filters;
this.capacity = capacity;
this.updateFrequency = updateFrequency;

CreateDelegates();
}
Expand Down Expand Up @@ -132,6 +135,7 @@ protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);

node.AddValue("updateFrequency", updateFrequency);
node.AddValue("capacity", capacity);

foreach (Filter filter in filters)
Expand All @@ -154,6 +158,7 @@ protected override void OnParameterLoad(ConfigNode node)
{
base.OnParameterLoad(node);

updateFrequency = ConfigNodeUtil.ParseValue<float>(node, "updateFrequency", DEFAULT_UPDATE_FREQUENCY);
capacity = ConfigNodeUtil.ParseValue<bool?>(node, "capacity", (bool?)false).Value;

foreach (ConfigNode childNode in node.GetNodes("RESOURCE"))
Expand Down Expand Up @@ -200,7 +205,7 @@ protected override void OnUnregister()
protected override void OnUpdate()
{
base.OnUpdate();
if (UnityEngine.Time.fixedTime - lastUpdate > UPDATE_FREQUENCY)
if (UnityEngine.Time.fixedTime - lastUpdate > updateFrequency)
{
lastUpdate = UnityEngine.Time.fixedTime;
CheckVessel(FlightGlobals.ActiveVessel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public class OrbitParameter : VesselParameter
protected double maxArgumentOfPeriapsis { get; set; }
protected double minPeriod { get; set; }
protected double maxPeriod { get; set; }
protected float updateFrequency { get; set; }
protected Orbit orbit;
protected double deviationWindow;
protected bool displayNotes;

private float lastUpdate = 0.0f;
private const float UPDATE_FREQUENCY = 0.25f;
internal const float DEFAULT_UPDATE_FREQUENCY = 0.25f;

private SpecificOrbitParameter sop;

Expand All @@ -48,7 +49,7 @@ public OrbitParameter()

public OrbitParameter(Vessel.Situations situation, double minAltitude, double maxAltitude, double minApoapsis, double maxApoapsis, double minPeriapsis, double maxPeriapsis,
double minEccentricity, double maxEccentricity, double minInclination, double maxInclination, double minArgumentOfPeriapsis, double maxArgumentOfPeriapsis, double minPeriod, double maxPeriod,
CelestialBody targetBody, string title = null)
CelestialBody targetBody, float updateFrequency, string title = null)
: base(title)
{
this.situation = situation;
Expand All @@ -67,13 +68,14 @@ public OrbitParameter(Vessel.Situations situation, double minAltitude, double ma
this.maxArgumentOfPeriapsis = maxArgumentOfPeriapsis;
this.minPeriod = minPeriod;
this.maxPeriod = maxPeriod;
this.updateFrequency = updateFrequency;
this.displayNotes = false;
orbit = null;

CreateDelegates();
}

public OrbitParameter(Orbit orbit, double deviationWindow, bool displayNotes)
public OrbitParameter(Orbit orbit, double deviationWindow, bool displayNotes, float updateFrequency)
{
targetBody = orbit.referenceBody;
minAltitude = 0.0;
Expand All @@ -88,6 +90,7 @@ public OrbitParameter(Orbit orbit, double deviationWindow, bool displayNotes)
maxInclination = 180;
minPeriod = 0.0;
maxPeriod = double.MaxValue;
this.updateFrequency = updateFrequency;
this.orbit = orbit;
this.deviationWindow = deviationWindow;
this.displayNotes = displayNotes;
Expand Down Expand Up @@ -316,6 +319,7 @@ private bool CheckAoP(Vessel vessel)
protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);
node.AddValue("updateFrequency", updateFrequency);
node.AddValue("situation", situation);
if (targetBody != null)
{
Expand Down Expand Up @@ -368,6 +372,7 @@ protected override void OnParameterLoad(ConfigNode node)
try
{
base.OnParameterLoad(node);
updateFrequency = ConfigNodeUtil.ParseValue<float>(node, "updateFrequency", DEFAULT_UPDATE_FREQUENCY);
situation = ConfigNodeUtil.ParseValue<Vessel.Situations>(node, "situation", Vessel.Situations.ORBITING);
minAltitude = ConfigNodeUtil.ParseValue<double>(node, "minAltitude");
maxAltitude = ConfigNodeUtil.ParseValue<double>(node, "maxAltitude", double.MaxValue);
Expand Down Expand Up @@ -403,7 +408,7 @@ protected override void OnParameterLoad(ConfigNode node)
protected override void OnUpdate()
{
base.OnUpdate();
if (Time.fixedTime - lastUpdate > UPDATE_FREQUENCY)
if (Time.fixedTime - lastUpdate > updateFrequency)
{
lastUpdate = UnityEngine.Time.fixedTime;
Vessel trackedVessel = CurrentVessel() ?? FlightGlobals.ActiveVessel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Contracts.Parameters;
using KSP.Localization;
using static FlightGlobals;
using System.Security.Policy;

namespace ContractConfigurator.Parameters
{
Expand All @@ -34,9 +35,11 @@ public class ReachState : VesselParameter
protected double maxDeltaVeeActual { get; set; }
protected double minDeltaVeeVacuum { get; set; }
protected double maxDeltaVeeVacuum { get; set; }
protected float updateFrequency { get; set; }

private float lastUpdate = 0.0f;
private const float UPDATE_FREQUENCY = 0.1f;

internal const float DEFAULT_UPDATE_FREQUENCY = 0.1f;

private static Vessel.Situations[] landedSituations = new Vessel.Situations[] { Vessel.Situations.LANDED, Vessel.Situations.PRELAUNCH, Vessel.Situations.SPLASHED };

Expand All @@ -46,8 +49,9 @@ public ReachState()
}

public ReachState(List<CelestialBody> targetBodies, string biome, List<Vessel.Situations> situation, float minAltitude, float maxAltitude,
float minTerrainAltitude, float maxTerrainAltitude, double minSpeed, double maxSpeed, SpeedDisplayModes? speedMode, double minRateOfClimb, double maxRateOfClimb,
float minAcceleration, float maxAcceleration, double minDeltaVeeActual, double maxDeltaVeeActual, double minDeltaVeeVacuum, double maxDeltaVeeVacuum, string title)
float minTerrainAltitude, float maxTerrainAltitude, double minSpeed, double maxSpeed, SpeedDisplayModes? speedMode, double minRateOfClimb,
double maxRateOfClimb, float minAcceleration, float maxAcceleration, double minDeltaVeeActual, double maxDeltaVeeActual, double minDeltaVeeVacuum,
double maxDeltaVeeVacuum, string title, float updateFrequency)
: base(title)
{
this.targetBodies = targetBodies;
Expand All @@ -68,6 +72,7 @@ public ReachState(List<CelestialBody> targetBodies, string biome, List<Vessel.Si
this.maxDeltaVeeActual = maxDeltaVeeActual;
this.minDeltaVeeVacuum = minDeltaVeeVacuum;
this.maxDeltaVeeVacuum = maxDeltaVeeVacuum;
this.updateFrequency = updateFrequency;

CreateDelegates();
}
Expand Down Expand Up @@ -359,6 +364,8 @@ private bool CheckVesselAltitude(Vessel vessel)
protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);
node.AddValue("updateFrequency", updateFrequency);

if (targetBodies != null)
{
foreach (CelestialBody targetBody in targetBodies)
Expand All @@ -369,12 +376,12 @@ protected override void OnParameterSave(ConfigNode node)
}
}
}

if (!string.IsNullOrEmpty(biome))
{
node.AddValue("biome", biome);
}


foreach (Vessel.Situations sit in situation)
{
node.AddValue("situation", sit);
Expand Down Expand Up @@ -453,6 +460,7 @@ protected override void OnParameterLoad(ConfigNode node)
try
{
base.OnParameterLoad(node);
updateFrequency = ConfigNodeUtil.ParseValue<float>(node, "updateFrequency", DEFAULT_UPDATE_FREQUENCY);
targetBodies = ConfigNodeUtil.ParseValue<List<CelestialBody>>(node, "targetBody", null);
biome = ConfigNodeUtil.ParseValue<string>(node, "biome", "");
situation = ConfigNodeUtil.ParseValue<List<Vessel.Situations>>(node, "situation", new List<Vessel.Situations>());
Expand Down Expand Up @@ -483,7 +491,7 @@ protected override void OnParameterLoad(ConfigNode node)
protected override void OnUpdate()
{
base.OnUpdate();
if (UnityEngine.Time.fixedTime - lastUpdate > UPDATE_FREQUENCY)
if (UnityEngine.Time.fixedTime - lastUpdate > updateFrequency)
{
lastUpdate = UnityEngine.Time.fixedTime;
CheckVessel(FlightGlobals.ActiveVessel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ public class Rendezvous : VesselParameter
{
protected List<string> vessels { get; set; }
protected double distance { get; set; }
protected float updateFrequency { get; set; }

private Vessel[] dockedVessels = new Vessel[2];
private float lastUpdate = 0.0f;
private const float UPDATE_FREQUENCY = 0.50f;
internal const float DEFAULT_UPDATE_FREQUENCY = 0.50f;
private List<VesselWaypoint> vesselWaypoints = new List<VesselWaypoint>();

public Rendezvous()
: base(null)
{
}

public Rendezvous(IEnumerable<string> vessels, double distance, string title)
public Rendezvous(IEnumerable<string> vessels, double distance, string title, float updateFrequency)
: base(title)
{
this.vessels = vessels.ToList();
this.distance = distance;
this.title = title;
this.updateFrequency = updateFrequency;
disableOnStateChange = true;
}

Expand Down Expand Up @@ -84,6 +86,7 @@ protected override void OnUnregister()
protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);
node.AddValue("updateFrequency", updateFrequency);
node.AddValue("distance", distance);
foreach (string vessel in vessels)
{
Expand All @@ -94,14 +97,15 @@ protected override void OnParameterSave(ConfigNode node)
protected override void OnParameterLoad(ConfigNode node)
{
base.OnParameterLoad(node);
updateFrequency = ConfigNodeUtil.ParseValue<float>(node, "updateFrequency", DEFAULT_UPDATE_FREQUENCY);
vessels = ConfigNodeUtil.ParseValue<List<string>>(node, "vessel", new List<string>());
distance = ConfigNodeUtil.ParseValue<double>(node, "distance");
}

protected override void OnUpdate()
{
base.OnUpdate();
if (UnityEngine.Time.fixedTime - lastUpdate > UPDATE_FREQUENCY)
if (UnityEngine.Time.fixedTime - lastUpdate > updateFrequency)
{
lastUpdate = UnityEngine.Time.fixedTime;

Expand Down
Loading

0 comments on commit 8e83e86

Please sign in to comment.