Skip to content

Commit

Permalink
New Condition: Resource
Browse files Browse the repository at this point in the history
NEW: Added deltaTime to Evaulate()
  • Loading branch information
jwvanderbeck committed Mar 28, 2015
1 parent b03af93 commit 6a503a7
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 24 deletions.
Binary file modified GameData/RealScience/Plugins/RealScience.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion source/Conditions/IScienceCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface IScienceCondition : IConfigNode
bool IsRestriction { get; }
string Exclusion { get; }

bool Evaluate(Part part);
bool Evaluate(Part part, float deltaTime);
}
}
2 changes: 1 addition & 1 deletion source/Conditions/RealScienceCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public virtual string Exclusion
}


public virtual bool Evaluate(Part part)
public virtual bool Evaluate(Part part, float deltaTime)
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion source/Conditions/RealScienceCondition_Altitude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override string Exclusion
{
get { return exclusion; }
}
public override bool Evaluate(Part part)
public override bool Evaluate(Part part, float deltaTime)
{
float altitude = FlightGlobals.getAltitudeAtPos(part.transform.position);
if (altitude >= altitudeMin && altitude <= altitudeMax)
Expand Down
2 changes: 1 addition & 1 deletion source/Conditions/RealScienceCondition_Biome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override string Exclusion
get { return exclusion; }
}

public override bool Evaluate(Part part)
public override bool Evaluate(Part part, float deltaTime)
{
string currentBiome = ScienceUtil.GetExperimentBiome(part.vessel.mainBody, part.vessel.latitude, part.vessel.longitude);
if (biome.ToLower() == currentBiome.ToLower())
Expand Down
2 changes: 1 addition & 1 deletion source/Conditions/RealScienceCondition_Body.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override string Exclusion
get { return exclusion; }
}

public override bool Evaluate(Part part)
public override bool Evaluate(Part part, float deltaTime)
{
if (part.vessel.mainBody.ToString().ToLower() == body)
return true;
Expand Down
2 changes: 1 addition & 1 deletion source/Conditions/RealScienceCondition_Orbit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override string Exclusion
get { return exclusion; }
}

public override bool Evaluate(Part part)
public override bool Evaluate(Part part, float deltaTime)
{
if (part.vessel.orbit.eccentricity < eccentricityMin)
return false;
Expand Down
114 changes: 114 additions & 0 deletions source/Conditions/RealScienceCondition_Resource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;

using UnityEngine;

namespace RealScience.Conditions
{
class RealScienceCondition_Resource : RealScienceCondition
{
// common properties
public string conditionType = "Resource";
public bool restriction = false;
public string exclusion = "";
public float dataRateModifier = 1f;
// specific properties
public string resourceName = "ElectricCharge";
public float initialConsumption = 1f;
public float usagePerSecond = 1f;
public bool initialOnly = false;
public bool validIfEmpty = false;
public bool initialUpdate = true;

public override float DataRateModifier
{
get { return dataRateModifier; }
}
public override bool IsRestriction
{
get { return restriction; }
}
public override string Exclusion
{
get { return exclusion; }
}

public override bool Evaluate(Part part, float deltaTime)
{
if (!initialUpdate && initialOnly)
return true;
float consumption;
if (initialUpdate)
{
initialUpdate = false;
consumption = part.RequestResource(resourceName, initialConsumption);
if (consumption != initialConsumption && !validIfEmpty)
return false;
return true;
}
else
{
consumption = part.RequestResource(resourceName, usagePerSecond * deltaTime);
if (consumption != (usagePerSecond * deltaTime) && !validIfEmpty)
return false;
return true;
}
}
public override void Load(ConfigNode node)
{
// Load common properties
if (node.HasValue("conditionType"))
conditionType = node.GetValue("conditionType");
if (node.HasValue("exclusion"))
exclusion = node.GetValue("exclusion");
if (node.HasValue("restriction"))
{
try
{
restriction = bool.Parse(node.GetValue("restriction"));
}
catch (FormatException)
{
restriction = false;
}
}
if (node.HasValue("dataRateModifier"))
{
try
{
dataRateModifier = float.Parse(node.GetValue("dataRateModifier"));
}
catch (FormatException)
{
dataRateModifier = 1f;
}
}
// Load specific properties
if (node.HasValue("resourceName"))
resourceName = node.GetValue("resourceName");
if (node.HasValue("initialConsumption"))
initialConsumption = float.Parse(node.GetValue("initialConsumption"));
if (node.HasValue("usagePerSecond"))
usagePerSecond = float.Parse(node.GetValue("usagePerSecond"));
if (node.HasValue("initialOnly"))
initialOnly = bool.Parse(node.GetValue("initialOnly"));
if (node.HasValue("validIfEmpty"))
validIfEmpty = bool.Parse(node.GetValue("validIfEmpty"));
if (node.HasValue("initialUpdate"))
initialUpdate = bool.Parse(node.GetValue("initialUpdate"));
}
public override void Save(ConfigNode node)
{
// Save common properties
node.AddValue("conditionType", conditionType);
node.AddValue("restriction", restriction);
node.AddValue("exclusion", exclusion);
node.AddValue("dataRateModifier", dataRateModifier);
node.AddValue("resourceName", resourceName);
node.AddValue("initialConsumption", initialConsumption);
node.AddValue("usagePerSecond", usagePerSecond);
node.AddValue("initialOnly", initialOnly);
node.AddValue("validIfEmpty", validIfEmpty);
node.AddValue("initialUpdate", initialUpdate);
}
}
}
2 changes: 1 addition & 1 deletion source/Conditions/RealScienceCondition_Situation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override string Exclusion
get { return exclusion; }
}

public override bool Evaluate(Part part)
public override bool Evaluate(Part part, float deltaTime)
{
if (part.vessel.situation.ToString().ToLower() == situation)
return true;
Expand Down
1 change: 1 addition & 0 deletions source/RealScience.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="Conditions\RealScienceCondition_Biome.cs" />
<Compile Include="Conditions\RealScienceCondition_Body.cs" />
<Compile Include="Conditions\RealScienceCondition_Orbit.cs" />
<Compile Include="Conditions\RealScienceCondition_Resource.cs" />
<Compile Include="Conditions\RealScienceCondition_Situation.cs" />
<Compile Include="FrameworkExt\ClassDefinitions.cs" />
<Compile Include="FrameworkExt\Extensions.cs" />
Expand Down
36 changes: 19 additions & 17 deletions source/RealScienceExperiment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public override void OnUpdate()
{
base.OnUpdate();
float currentMET = (float)this.vessel.missionTime;
float deltaTime = currentMET - lastMET;

switch (state.CurrentState)
{
Expand Down Expand Up @@ -226,7 +227,6 @@ public override void OnUpdate()
state.CurrentState = ExperimentState.StateEnum.TRANSMIT_COMPLETE;
break;
}
float deltaTime = currentMET - lastMET;
quedPackets += transmissionDataRate * deltaTime;
LogFormatted_DebugOnly(String.Format("RealScience: OnUpdate: TRANSMITTING: deltaTime={0:F2}, quedPackets={1:F2}", deltaTime, quedPackets));
// This just ensures we don't transmit too much
Expand All @@ -242,12 +242,14 @@ public override void OnUpdate()
}
transmittedPackets += quedPackets;
// Use up charge
part.RequestResource("ElectricCharge", transmissionDataResourceCost * deltaTime);
ResearchAndDevelopment.Instance.AddScience(sciencePerPacket * quedPackets, TransactionReasons.ScienceTransmission);
LogFormatted_DebugOnly(String.Format("RealScience: OnUpdate: TRANSMITTING: transmittedPackets={0:F2}, add {1:F2} science", transmittedPackets, sciencePerPacket * quedPackets));
ScreenMessage statusMessage = new ScreenMessage(String.Format("{0:F2}/{1:F2} Packets Transmitted...", transmittedPackets, dataSize), 5.0f, ScreenMessageStyle.UPPER_LEFT);
ScreenMessages.PostScreenMessage(statusMessage, true);
quedPackets = 0f;
if (part.RequestResource("ElectricCharge", transmissionDataResourceCost * deltaTime) == transmissionDataResourceCost * deltaTime)
{
ResearchAndDevelopment.Instance.AddScience(sciencePerPacket * quedPackets, TransactionReasons.ScienceTransmission);
LogFormatted_DebugOnly(String.Format("RealScience: OnUpdate: TRANSMITTING: transmittedPackets={0:F2}, add {1:F2} science", transmittedPackets, sciencePerPacket * quedPackets));
ScreenMessage statusMessage = new ScreenMessage(String.Format("{0:F2}/{1:F2} Packets Transmitted...", transmittedPackets, dataSize), 5.0f, ScreenMessageStyle.UPPER_LEFT);
ScreenMessages.PostScreenMessage(statusMessage, true);
quedPackets = 0f;
}
break;
case ExperimentState.StateEnum.TRANSMIT_COMPLETE:
// science is awarded by the transmission, so we don't need to do it here
Expand All @@ -265,7 +267,7 @@ public override void OnUpdate()
case ExperimentState.StateEnum.PAUSED_CONNECTION:
break;
case ExperimentState.StateEnum.CONDITIONS_NOT_MET:
ValidateConditions();
ValidateConditions(deltaTime);
break;
case ExperimentState.StateEnum.RESEARCHING:
// check if research data >= required data and change state to RESEARCH_COMPLETE if so
Expand All @@ -277,7 +279,7 @@ public override void OnUpdate()
// Evaluate each group or condition and if they are all true, add research data
else
{
if (ValidateConditions())
if (ValidateConditions(deltaTime))
{
float currentDataRate = researchDataRate * totalDataRateModifier;
currentData = currentData + (currentDataRate * (currentMET - lastMET));
Expand All @@ -294,7 +296,7 @@ public override void OnUpdate()
lastMET = currentMET;
}

public bool ValidateConditions()
public bool ValidateConditions(float deltaTime)
{
bool conditionsValid = true;
totalDataRateModifier = 1f;
Expand All @@ -303,7 +305,7 @@ public bool ValidateConditions()
// No valid groups so we evaluate each condition instead
foreach (IScienceCondition condition in conditions)
{
if (condition.Evaluate(this.part) && condition.IsRestriction)
if (condition.Evaluate(this.part, deltaTime) && condition.IsRestriction)
{
if (condition.Exclusion.ToLower() == "fail")
state.CurrentState = ExperimentState.StateEnum.FAILED;
Expand All @@ -316,7 +318,7 @@ public bool ValidateConditions()
state.CurrentState = ExperimentState.StateEnum.CONDITIONS_NOT_MET;
return false;
}
if (!condition.Evaluate(this.part) && !condition.IsRestriction)
if (!condition.Evaluate(this.part, deltaTime) && !condition.IsRestriction)
{
state.CurrentState = ExperimentState.StateEnum.CONDITIONS_NOT_MET;
return false;
Expand All @@ -329,7 +331,7 @@ public bool ValidateConditions()
// We have groups, so instead of evaluating the conditions, we evaluate the groups
foreach (RealScienceConditionGroup group in conditionGroups)
{
if (group.Evaluate(this.part) && group.IsRestriction)
if (group.Evaluate(this.part, deltaTime) && group.IsRestriction)
{
if (group.Exclusion.ToLower() == "fail")
state.CurrentState = ExperimentState.StateEnum.FAILED;
Expand All @@ -342,7 +344,7 @@ public bool ValidateConditions()
state.CurrentState = ExperimentState.StateEnum.CONDITIONS_NOT_MET;
return false;
}
if (!group.Evaluate(this.part) && !group.IsRestriction)
if (!group.Evaluate(this.part, deltaTime) && !group.IsRestriction)
{
state.CurrentState = ExperimentState.StateEnum.CONDITIONS_NOT_MET;
return false;
Expand Down Expand Up @@ -449,13 +451,13 @@ public string Exclusion
get { return exclusion; }
}

public bool Evaluate(Part part)
public bool Evaluate(Part part, float deltaTime)
{
if (groupType.ToLower() == "or")
{
foreach (IScienceCondition condition in conditions)
{
if (condition.Evaluate(part))
if (condition.Evaluate(part, deltaTime))
{
dataRateModifier *= condition.DataRateModifier;
return true;
Expand All @@ -468,7 +470,7 @@ public bool Evaluate(Part part)
bool conditionsValid = true;
foreach (IScienceCondition condition in conditions)
{
if (!condition.Evaluate(part))
if (!condition.Evaluate(part, deltaTime))
conditionsValid = false;
dataRateModifier *= condition.DataRateModifier;
}
Expand Down

0 comments on commit 6a503a7

Please sign in to comment.