Skip to content

Commit

Permalink
Showing 4 changed files with 127 additions and 15 deletions.
Binary file modified GameData/RP-0/Plugins/RP0.dll
Binary file not shown.
3 changes: 3 additions & 0 deletions Source/RP0.csproj
Original file line number Diff line number Diff line change
@@ -52,6 +52,8 @@
<Compile Include="ModuleTag\DecreaseB.cs" />
<Compile Include="ModuleTag\EngineLiquidPF.cs" />
<Compile Include="ModuleTag\Decoupler.cs" />
<Compile Include="ModuleTag\TankServiceModule.cs" />
<Compile Include="ModuleTag\TankBalloon.cs" />
<Compile Include="ModuleTag\Habitable.cs" />
<Compile Include="ModuleTag\DecreaseA.cs" />
<Compile Include="ModuleTag\Toxic.cs" />
@@ -91,6 +93,7 @@
<Compile Include="ProceduralAvionics\ProceduralAvionicsUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tooling\ModuleTooling.cs" />
<Compile Include="Tooling\ModuleToolingGeneric.cs" />
<Compile Include="Tooling\ToolingDatabase.cs" />
<Compile Include="Tooling\ToolingScenario.cs" />
<Compile Include="Tooling\ModuleToolingPTank.cs" />
109 changes: 109 additions & 0 deletions Source/Tooling/ModuleToolingGeneric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KSP;
using UnityEngine;

namespace RP0
{
class ModuleToolingGeneric : ModuleTooling
{
[KSPField]
public string partModuleName = string.Empty;

[KSPField]
public string diamField = "diameter";

[KSPField]
public string lenField = "length";

[KSPField]
public bool useLength = true;

protected PartModule pm;

protected BaseField diameter, length;

public override void OnAwake()
{
base.OnAwake();

// Grab current link to module, *if* we've done a load already to get the field.
if (!string.IsNullOrEmpty(partModuleName))
pm = part.Modules[partModuleName];
}

public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);

if (!string.IsNullOrEmpty(partModuleName))
pm = part.Modules[partModuleName];
}
protected void GetDimensions(out float diam, out float len)
{
diam = 0f;
len = 0f;

if (pm == null)
{
Debug.LogError("[ModuleTooling]: Could not find module " + partModuleName + " to bind to");
return;
}

if (diameter == null)
{
diameter = pm.Fields[diamField];
if (diameter == null)
Debug.LogError("[ModuleTooling]: Could not bind to field: " + diamField + " on " + partModuleName);
return;
}

if(useLength && length == null)
{

length = pm.Fields[lenField];

if (length == null)
{
Debug.LogError("[ModuleTooling]: Could not bind to field: " + lenField + " on " + partModuleName);
return;
}
}

diam = diameter.GetValue<float>(pm);

if (useLength)
len = length.GetValue<float>(pm);
}

public override float GetToolingCost()
{
float d, l;
GetDimensions(out d, out l);
float cost = lengthToolingCost.x * d * d + lengthToolingCost.y * d + lengthToolingCost.z * l + lengthToolingCost.w;
if (ToolingDatabase.HasTooling(toolingType, d, l) == ToolingDatabase.ToolingLevel.None)
cost += diameterToolingCost.x * d * d + diameterToolingCost.y * d + diameterToolingCost.z;

return cost;
}

public override void PurchaseTooling()
{
float d, l;
GetDimensions(out d, out l);
ToolingDatabase.UnlockTooling(toolingType, d, l);
}

public override bool IsUnlocked()
{
float d, l;
GetDimensions(out d, out l);
if (d < minDiameter)
return true;

return ToolingDatabase.HasTooling(toolingType, d, l) == ToolingDatabase.ToolingLevel.Full;
}
}
}
30 changes: 15 additions & 15 deletions Source/Tooling/ToolingDatabase.cs
Original file line number Diff line number Diff line change
@@ -9,29 +9,29 @@ namespace RP0
{
public class ToolingDiameter
{
public double diameter;
public float diameter;

public List<double> lengths;
public List<float> lengths;

public ToolingDiameter(double d)
public ToolingDiameter(float d)
{
diameter = d;
lengths = new List<double>();
lengths = new List<float>();
}

public ToolingDiameter(double d, double l)
public ToolingDiameter(float d, float l)
{
diameter = d;
lengths = new List<double>();
lengths = new List<float>();
lengths.Add(l);
}
}
public class ToolingDatabase
{
protected static double comparisonEpsilonHigh = 1.04d;
protected static double comparisonEpsilonLow = 0.96d;
protected static float comparisonEpsilonHigh = 1.04f;
protected static float comparisonEpsilonLow = 0.96f;

protected static int EpsilonCompare(double a, double b)
protected static int EpsilonCompare(float a, float b)
{
if (a > b * comparisonEpsilonLow && a < b * comparisonEpsilonHigh)
return 0;
@@ -41,7 +41,7 @@ protected static int EpsilonCompare(double a, double b)

protected static Dictionary<string, List<ToolingDiameter>> toolings = new Dictionary<string, List<ToolingDiameter>>();

protected static int DiamIndex(double diam, List<ToolingDiameter> lst, out int min)
protected static int DiamIndex(float diam, List<ToolingDiameter> lst, out int min)
{
min = 0;
int max = lst.Count - 1;
@@ -68,7 +68,7 @@ protected static int DiamIndex(double diam, List<ToolingDiameter> lst, out int m
return -1;
}

protected static int LengthIndex(double length, List<double> lst, out int min)
protected static int LengthIndex(float length, List<float> lst, out int min)
{
min = 0;
int max = lst.Count - 1;
@@ -102,7 +102,7 @@ public enum ToolingLevel
Full = 2
};

public static ToolingLevel HasTooling(string type, double diam, double len)
public static ToolingLevel HasTooling(string type, float diam, float len)
{
//Debug.Log("[Tooling]: Looking for " + type + ", " + diam + " x " + len);

@@ -137,7 +137,7 @@ public static ToolingLevel HasTooling(string type, double diam, double len)
return ToolingLevel.None;
}

public static bool UnlockTooling(string type, double diam, double len)
public static bool UnlockTooling(string type, float diam, float len)
{
//Debug.Log("[Tooling]: Purchasing " + type + ", " + diam + " x " + len);

@@ -195,7 +195,7 @@ public static void Load(ConfigNode node)

foreach (ConfigNode n in c.GetNodes("DIAMETER"))
{
double tmp = 0d;
float tmp = 0f;
if (!n.TryGetValue("diameter", ref tmp))
continue;

@@ -204,7 +204,7 @@ public static void Load(ConfigNode node)
ConfigNode len = n.GetNode("LENGTHS");
if (len != null)
foreach (ConfigNode.Value v in len.values)
if (double.TryParse(v.value, out tmp))
if (float.TryParse(v.value, out tmp))
d.lengths.Add(tmp);

lst.Add(d);

0 comments on commit e840002

Please sign in to comment.