Skip to content

Commit

Permalink
Merge branch 'Feature_71' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jwvanderbeck committed May 16, 2015
2 parents a8ece71 + 4f569a9 commit 5c440dc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
Binary file modified GameData/TestFlight/Plugins/TestFlight.dll
Binary file not shown.
Binary file modified GameData/TestFlight/Plugins/TestFlightAPI.dll
Binary file not shown.
Binary file modified GameData/TestFlight/Plugins/TestFlightContracts.dll
Binary file not shown.
Binary file modified GameData/TestFlight/Plugins/TestFlightCore.dll
Binary file not shown.
90 changes: 81 additions & 9 deletions TestFlightFailure_ResourceLeak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ public class TestFlightFailure_ResourceLeak : TestFlightFailureBase
[KSPField(isPersistant = true)]
public string resourceToLeak = "random";
[KSPField(isPersistant = true)]
public float initialAmount = 10f;
public string initialAmount = "10";
[KSPField(isPersistant = true)]
public float perSecondAmount = 0.1f;
public string perSecondAmount = "0.1";
[KSPField(isPersistant = true)]
public bool calculatePerTick = false;

private string leakingResource;
private string leakingResource = "";
private int leakingResourceID = 0;
private float _initialAmount = 10f;
private float _perSecondAmount = 0.1f;

/// <summary>
/// Triggers the failure controlled by the failure module
Expand All @@ -31,19 +36,37 @@ public override void DoFailure()
List<PartResource> allResources = this.part.Resources.list;
int randomResource = UnityEngine.Random.Range(0, allResources.Count());
leakingResource = allResources[randomResource].resourceName;
leakingResourceID = allResources[randomResource].info.id;
}
else
leakingResource = resourceToLeak;
{
List<PartResource> resources = this.part.Resources.list.Where(n => n.resourceName == resourceToLeak).ToList();
if (resources != null && resources.Count > 0)
{
PartResource resource = resources[0];
leakingResource = resourceToLeak;
leakingResourceID = resource.info.id;
}
}

this.part.RequestResource(leakingResource, initialAmount);
StartCoroutine("LeakResource");
if (!String.IsNullOrEmpty(leakingResource))
{
ParseResourceValues();
this.part.RequestResource(leakingResource, _initialAmount, ResourceFlowMode.NO_FLOW);
StartCoroutine("LeakResource");
}
}

internal IEnumerator LeakResource()
{
while (true)
{
this.part.RequestResource(leakingResource, perSecondAmount);
if (!String.IsNullOrEmpty(leakingResource))
{
if (calculatePerTick)
ParseResourceValues();
this.part.RequestResource(leakingResource, _perSecondAmount, ResourceFlowMode.NO_FLOW);
}
yield return new WaitForSeconds(1f);
}
}
Expand All @@ -55,6 +78,53 @@ public override float DoRepair()

return 0;
}

private float ParseValue(string rawValue)
{
float parsedValue = 0f;
int index = rawValue.IndexOf("%");
string trimmedValue = "";
double calculateFromAmount = 0;

rawValue = rawValue.ToLowerInvariant();

if (index > 0)
{
if (rawValue.EndsWith("%t"))
{
trimmedValue = rawValue.Substring(0, index);
if (!float.TryParse(trimmedValue, out parsedValue))
parsedValue = 0f;
// Calculate the % value based on the total capacity of the tank
calculateFromAmount = this.part.Resources.Get(leakingResourceID).maxAmount;
}
else if (rawValue.EndsWith("%c"))
{
trimmedValue = rawValue.Substring(0, index);
if (!float.TryParse(trimmedValue, out parsedValue))
parsedValue = 0f;

// Calculate the % value based on the current resource level of the tank
calculateFromAmount = this.part.Resources.Get(leakingResourceID).amount;
}

parsedValue = parsedValue * (float)calculateFromAmount;
}
else
{
if (!float.TryParse(rawValue, out parsedValue))
parsedValue = 0f;
}

return parsedValue;
}

private void ParseResourceValues()
{
_initialAmount = ParseValue(initialAmount);
_perSecondAmount = ParseValue(perSecondAmount);
}

public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
Expand All @@ -63,9 +133,11 @@ public override void OnLoad(ConfigNode node)
else
resourceToLeak = "random";
if (node.HasValue("initialAmount"))
initialAmount = float.Parse(node.GetValue("initialAmount"));
initialAmount = node.GetValue("initialAmount");
if (node.HasValue("perSecondAmount"))
perSecondAmount = float.Parse(node.GetValue("perSecondAmount"));
perSecondAmount = node.GetValue("perSecondAmount");
if (node.HasValue("calculatePerTick"))
calculatePerTick = bool.Parse(node.GetValue("calculatePerTick"));
}
}
}
Expand Down

0 comments on commit 5c440dc

Please sign in to comment.