Skip to content

Commit

Permalink
Merge branch 'hotfix-0.7.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
Majiir committed Aug 12, 2013
2 parents 7ed38ea + b06d2bd commit 739244e
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 100 deletions.
Binary file added Flags/Kethane 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 29 additions & 25 deletions Parts/kethane_1m_converter/part.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,44 @@ fuelCrossFeed = True
MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = LiquidFuel
ConversionEfficiency = 0.97
SourceConsumption = 2.25
PowerConsumption = 6
Label = Rocket Fuel
InputRates
{
Kethane = 2.25
ElectricCharge = 6
}
OutputRatios
{
LiquidFuel = 0.99
Oxidizer = 0.99
}
}

MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = Oxidizer
ConversionEfficiency = 1.01
SourceConsumption = 2.75
PowerConsumption = 4
InputRates
{
Kethane = 1.5
ElectricCharge = 8
}
OutputRatios
{
MonoPropellant = 0.3
}
}

MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = MonoPropellant
ConversionEfficiency = 0.3
SourceConsumption = 1.5
PowerConsumption = 8
}

MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = XenonGas
ConversionEfficiency = 0.4
SourceConsumption = 1.5
PowerConsumption = 3
InputRates
{
Kethane = 1.5
ElectricCharge = 3
}
OutputRatios
{
XenonGas = 0.4
}
}
}
56 changes: 36 additions & 20 deletions Parts/kethane_2m_converter/part.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,60 @@ MODULE
MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = LiquidFuel
ConversionEfficiency = 1.03
SourceConsumption = 6.75
PowerConsumption = 12
HeatProduction = 600
InputRates
{
Kethane = 6.75
ElectricCharge = 12
}
OutputRatios
{
LiquidFuel = 1.03
}
}
MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = Oxidizer
ConversionEfficiency = 0.99
SourceConsumption = 8.25
PowerConsumption = 8
HeatProduction = 800
InputRates
{
Kethane = 8.25
ElectricCharge = 8
}
OutputRatios
{
Oxidizer = 0.99
}
}
MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = MonoPropellant
ConversionEfficiency = 0.85
SourceConsumption = 3
PowerConsumption = 10
HeatProduction = 1200
InputRates
{
Kethane = 3
ElectricCharge = 10
}
OutputRatios
{
MonoPropellant = 0.85
}
}
MODULE
{
name = KethaneConverter
SourceResource = Kethane
TargetResource = XenonGas
ConversionEfficiency = 0.25
SourceConsumption = 2
PowerConsumption = 8
HeatProduction = 300
InputRates
{
Kethane = 2
ElectricCharge = 8
}
OutputRatios
{
XenonGas = 0.25
}
}
}
4 changes: 4 additions & 0 deletions Parts/kethane_highGain/part.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ MODULE
DetectingPeriod = 1.5
DetectingHeight = 250000
PowerConsumption = 0.8
Resource
{
Name = Kethane
}
}

MODULE
Expand Down
4 changes: 4 additions & 0 deletions Parts/kethane_sensor_1m/part.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ MODULE {
DetectingPeriod = 0.9
DetectingHeight = 1200000
PowerConsumption = 2.5
Resource
{
Name = Kethane
}
}

MODULE
Expand Down
2 changes: 1 addition & 1 deletion Plugin/InstallChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Kethane
{
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
[KSPAddonFixed(KSPAddon.Startup.MainMenu, true, typeof(InstallChecker))]
internal class InstallChecker : MonoBehaviour
{
protected void Start()
Expand Down
37 changes: 37 additions & 0 deletions Plugin/KSPAddonFixed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace Kethane
{
/// <summary>
/// KSPAddon with equality checking using an additional type parameter. Fixes the issue where AddonLoader prevents multiple start-once addons with the same start scene.
/// </summary>
public class KSPAddonFixed : KSPAddon, IEquatable<KSPAddonFixed>
{
private readonly Type type;

public KSPAddonFixed(KSPAddon.Startup startup, bool once, Type type)
: base(startup, once)
{
this.type = type;
}

public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType()) { return false; }
return Equals((KSPAddonFixed)obj);
}

public bool Equals(KSPAddonFixed other)
{
if (this.once != other.once) { return false; }
if (this.startup != other.startup) { return false; }
if (this.type != other.type) { return false; }
return true;
}

public override int GetHashCode()
{
return this.startup.GetHashCode() ^ this.once.GetHashCode() ^ this.type.GetHashCode();
}
}
}
1 change: 1 addition & 0 deletions Plugin/Kethane.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="KethaneParticleEmitter.cs" />
<Compile Include="KethaneParticleDynamics.cs" />
<Compile Include="KethaneWetMassIndicator.cs" />
<Compile Include="KSPAddonFixed.cs" />
<Compile Include="MapOverlay.cs" />
<Compile Include="Misc.cs" />
<Compile Include="OrthogonalIntake.cs" />
Expand Down
110 changes: 65 additions & 45 deletions Plugin/KethaneConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,38 @@ namespace Kethane
{
public class KethaneConverter : PartModule
{
[KSPField(isPersistant = false)]
public string SourceResource;

[KSPField(isPersistant = false)]
public string TargetResource;
private struct ResourceRate
{
public String Resource { get; private set; }
public double Rate { get; private set; }

[KSPField(isPersistant = false)]
public float ConversionEfficiency;
public ResourceRate(String resource, double rate)
: this()
{
Resource = resource;
Rate = rate;
}

[KSPField(isPersistant = false)]
public float SourceConsumption;
public static ResourceRate operator *(ResourceRate rate, double multiplier)
{
return new ResourceRate(rate.Resource, rate.Rate * multiplier);
}
}

[KSPField(isPersistant = false)]
public float PowerConsumption;
public String Label;

[KSPField(isPersistant = false)]
public float HeatProduction;

[KSPField(isPersistant = true)]
public bool IsEnabled;

public ConfigNode config;

private ResourceRate[] inputRates;
private ResourceRate[] outputRates;

[KSPEvent(guiActive = true, guiName = "Activate Converter", active = true)]
public void ActivateConverter()
{
Expand Down Expand Up @@ -61,14 +72,48 @@ public void ToggleConverterAction(KSPActionParam param)

public override string GetInfo()
{
return String.Format("{0}:\n- Conversion Efficiency: {1:P0}\n- {4} Consumption: {2:F1}L/s\n- Power Consumption: {3:F1}/s", TargetResource, ConversionEfficiency, SourceConsumption, PowerConsumption, SourceResource);
return String.Format("{0} Converter:\n> Inputs:\n", Label) + String.Join("\n", inputRates.Select(r => String.Format(" - {0}: {1:N2}/s", r.Resource, r.Rate)).ToArray()) + "\n> Outputs:\n" + String.Join("\n", outputRates.Select(r => String.Format(" - {0}: {1:N2}/s", r.Resource, r.Rate)).ToArray()) + "\n";
}

public override void OnLoad(ConfigNode config)
{
if (this.config == null)
{
this.config = new ConfigNode();
config.CopyTo(this.config);
}

loadConfig();
}

private void loadConfig()
{
var definitions = PartResourceLibrary.Instance.resourceDefinitions;

inputRates = loadRates(config.GetNode("InputRates")).ToArray();
var inputMassRate = inputRates.Sum(p => p.Rate * definitions[p.Resource].density);

outputRates = loadRates(config.GetNode("OutputRatios")).Select(r => r * (inputMassRate / definitions[r.Resource].density)).GroupBy(r => r.Resource).Select(g => new ResourceRate(g.Key, g.Sum(r => r.Rate))).Concat(loadRates(config.GetNode("OutputRates"))).ToArray();

if (Label == null)
{
Label = String.Join("/", outputRates.Select(r => r.Resource).ToArray());
}
}

private static IEnumerable<ResourceRate> loadRates(ConfigNode config)
{
return (config ?? new ConfigNode()).values.Cast<ConfigNode.Value>().Where(v => PartResourceLibrary.Instance.resourceDefinitions.Any(d => d.name == v.name)).Select(v => new ResourceRate(v.name, Misc.Parse(v.value, 0.0))).Where(r => r.Rate > 0);
}

public override void OnStart(PartModule.StartState state)
{
Actions["ActivateConverterAction"].guiName = Events["ActivateConverter"].guiName = String.Format("Activate {0} Converter", TargetResource);
Actions["DeactivateConverterAction"].guiName = Events["DeactivateConverter"].guiName = String.Format("Deactivate {0} Converter", TargetResource);
Actions["ToggleConverterAction"].guiName = String.Format("Toggle {0} Converter", TargetResource);
loadConfig();

Actions["ActivateConverterAction"].guiName = Events["ActivateConverter"].guiName = String.Format("Activate {0} Converter", Label);
Actions["DeactivateConverterAction"].guiName = Events["DeactivateConverter"].guiName = String.Format("Deactivate {0} Converter", Label);
Actions["ToggleConverterAction"].guiName = String.Format("Toggle {0} Converter", Label);

if (state == StartState.Editor) { return; }
this.part.force_activate();
}
Expand All @@ -83,24 +128,8 @@ public override void OnFixedUpdate()
{
if (!IsEnabled) { return; }

var conversionRatio = PartResourceLibrary.Instance.GetDefinition(SourceResource).density / PartResourceLibrary.Instance.GetDefinition(TargetResource).density;

double requestedSpace = SourceConsumption * conversionRatio * ConversionEfficiency * TimeWarp.fixedDeltaTime;
double requestedSource = SourceConsumption * TimeWarp.fixedDeltaTime;
double requestedEnergy = PowerConsumption * TimeWarp.fixedDeltaTime;

double availableSpace = 0;
var listPartResource = Misc.GetConnectedResources(this.part, TargetResource);
if (listPartResource.Count > 0)
availableSpace = listPartResource.Max(r => r.maxAmount - r.amount);
var availableSource = Misc.GetConnectedResources(this.part, SourceResource).Max(r => r.amount);
var availableEnergy = Misc.GetConnectedResources(this.part, "ElectricCharge").Max(r => r.amount);

var spaceRatio = availableSpace / requestedSpace;
var sourceRatio = availableSource / requestedSource;
var energyRatio = availableEnergy / requestedEnergy;

var ratio = Math.Min(Math.Min(Math.Min(spaceRatio, sourceRatio), energyRatio), 1);
var rates = outputRates.Select(r => r * -1).Concat(inputRates).Select(r => r * TimeWarp.fixedDeltaTime).ToArray();
var ratio = rates.Select(r => Misc.GetConnectedResources(this.part, r.Resource).Select(c => r.Rate > 0 ? c.amount : c.maxAmount - c.amount).DefaultIfEmpty().Max() / Math.Abs(r.Rate)).Prepend(1).Min();

var heatsink = this.part.Modules.OfType<HeatSinkAnimator>().SingleOrDefault();
if (heatsink != null)
Expand All @@ -109,19 +138,10 @@ public override void OnFixedUpdate()
ratio *= heatsink.AddHeat(heatRequest) / heatRequest;
}

requestedSource *= ratio;

var drawnSource = this.part.RequestResource(SourceResource, requestedSource);

ratio *= drawnSource / requestedSource;
requestedEnergy *= ratio;

var drawnEnergy = this.part.RequestResource("ElectricCharge", requestedEnergy);

ratio *= drawnEnergy / requestedEnergy;
requestedSpace *= ratio;

this.part.RequestResource(TargetResource, -requestedSpace);
foreach (var rate in rates)
{
this.part.RequestResource(rate.Resource, rate.Rate * ratio);
}
}
}
}
4 changes: 4 additions & 0 deletions Plugin/KethaneData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static KethaneData Current
if (!game.scenarios.Any(p => p.moduleName == typeof(KethaneData).Name))
{
var proto = game.AddProtoScenarioModule(typeof(KethaneData), GameScenes.FLIGHT, GameScenes.TRACKSTATION);
if (proto.targetScenes.Contains(HighLogic.LoadedScene))
{
proto.Load(ScenarioRunner.fetch);
}
}

return game.scenarios.Select(s => s.moduleRef).OfType<KethaneData>().SingleOrDefault();
Expand Down
1 change: 0 additions & 1 deletion Plugin/KethaneExtractorAnimatorLanded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class KethaneExtractorAnimatorLanded : PartModule, IExtractorAnimator
public ExtractorState CurrentState { get; private set; }
public void Deploy() { CurrentState = ExtractorState.Deployed; }
public void Retract() { CurrentState = ExtractorState.Retracted; }
public bool CanExtract { get { return vessel.LandedOrSplashed; } }

public KethaneExtractorAnimatorLanded()
{
Expand Down
Loading

0 comments on commit 739244e

Please sign in to comment.