Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
use a dummy RCS module for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Jun 15, 2015
1 parent 1390e22 commit a751ee6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 329 deletions.
25 changes: 22 additions & 3 deletions GameData/RW Saturatable/RW.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,27 @@
key = 0 1 -1 -1 // with 0% saturation, full torque
key = 1 0 -1 -1 // with 100% saturation, no torque
}

resources = MonoPropellant,0.1; ElectricCharge,0.3 // [resourceName,Rate];...
recoveryRate = 0.05 // fixed pct recovery per second when using resources
}

// Uncomment this module to enable discharging momentum through resource use
// MODULE
// {
// name = MomentumDischargeThruster
// thrusterPower = 0.05 // pct of momentum limit to discharge per second
// thrusterTransformName = delete
// atmosphereCurve
// {
// key = 0 240
// }
// PROPELLANT
// {
// name = ElectricCharge
// ratio = 10
// }
// PROPELLANT
// {
// name = MonoPropellant
// ratio = 1
// }
// }
}
Binary file modified GameData/RW Saturatable/SaturatableRW.dll
Binary file not shown.
37 changes: 37 additions & 0 deletions SaturatableRW/MomentumDischargeThruster.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace SaturatableRW
{
public class MomentumDischargeThruster : ModuleRCS
{
public override string GetInfo()
{
string baseInfo = base.GetInfo();
int index = baseInfo.IndexOf("<color=#99ff00ff><b>Requires:</b></color>");
string resourceRates = baseInfo.Substring(index);
return string.Format("Thruster used to remove accumulated momentum from a RW\r\n<b>Discharge Rate:</b> {0}% / s\r\n\r\n{1}", (thrusterPower * 100).ToString("0.0"), resourceRates);
}

public override void OnAwake()
{
if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight)
{
foreach (BaseField f in Fields) // hide "RCS ISP" field
{
f.guiActive = false;
f.guiActiveEditor = false;
}
foreach (BaseEvent e in Events) // hide "disable RCS port" button
{
e.guiActive = false;
e.guiActiveEditor = false;
e.guiActiveUnfocused = false;
}
}
base.OnAwake();
}
}
}
49 changes: 19 additions & 30 deletions SaturatableRW/RWSaturatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,8 @@ public class RWSaturatable : ModuleReactionWheel
/// </summary>
public bool bConsumeResource = false;

/// <summary>
/// string detailing resource usage for momentum discharge
/// syntax will be: "resourceName1","units/s";"resourceName2","units/s";...
/// </summary>
[KSPField]
public string resources;

public List<ResourceConsumer> dischargeResources;
public MomentumDischargeThruster dummyRCS;
public class ResourceConsumer
{
public int ID { get; set; }
Expand All @@ -114,13 +108,15 @@ public ResourceConsumer(int id, double rate, ResourceFlowMode flowMode)
FlowMode = flowMode;
}
}
/// <summary>
/// if false, resource consumption is disallowed
/// </summary>
public bool canForceDischarge = false;

/// <summary>
/// The fixed % of saturation to recover per second of discharge
/// The momentum to recover per second of discharge
/// </summary>
[KSPField]
public float recoveryRate;
public float dischargeRate;

public bool drawWheel = false;

Expand All @@ -146,25 +142,19 @@ public override void OnAwake()
if (HighLogic.LoadedSceneIsFlight)
this.part.force_activate();

if (!string.IsNullOrEmpty(resources))
dischargeResources = new List<ResourceConsumer>();
dummyRCS = part.Modules["MomentumDischargeThruster"] as MomentumDischargeThruster;
if (dummyRCS != null)
{
dischargeResources = new List<ResourceConsumer>();
foreach (string pair in resources.Split(';'))
dischargeRate = dummyRCS.thrusterPower;
double ISP = dummyRCS.atmosphereCurve.Evaluate(0);
double totalPropellantMassRatio = dummyRCS.propellants.Sum(r => r.ratio * PartResourceLibrary.Instance.resourceDefinitions[r.id].density);
double totalMassRate = dummyRCS.thrusterPower * saturationLimit / (ISP * dummyRCS.G);
foreach (Propellant p in dummyRCS.propellants)
{
if (!string.IsNullOrEmpty(pair))
{
string[] nameAndRate = pair.Split(',');
if (nameAndRate.Length == 2)
{
PartResourceDefinition res = PartResourceLibrary.Instance.resourceDefinitions.FirstOrDefault(prd => prd.name == nameAndRate[0].Trim());
if (res != null)
{
double rate = 0;
double.TryParse(nameAndRate[1].Trim(), out rate);
dischargeResources.Add(new ResourceConsumer(res.id, rate, res.resourceFlowMode));
}
}
}
PartResourceDefinition res = PartResourceLibrary.Instance.resourceDefinitions[p.id];
double propellantRate = p.ratio * totalMassRate / totalPropellantMassRatio;
dischargeResources.Add(new ResourceConsumer(res.id, propellantRate, res.resourceFlowMode));
}
if (dischargeResources.Any(rc => rc.Rate > 0))
canForceDischarge = true;
Expand All @@ -180,7 +170,7 @@ public void OnDestroy()
if (Window.Instance.Vessels.ContainsKey(vessel.vesselName))
Window.Instance.Vessels.Remove(vessel.vesselName);
}
catch (Exception ex)
catch //(Exception ex)
{
//Debug.Log(ex.StackTrace);
}
Expand Down Expand Up @@ -291,7 +281,7 @@ private void useResourcesToRecover()
if (!bConsumeResource || !canForceDischarge)
return;

float momentumToRemove = TimeWarp.fixedDeltaTime * recoveryRate * saturationLimit;
float momentumToRemove = TimeWarp.fixedDeltaTime * dischargeRate * saturationLimit;
float x_momentToRemove = Mathf.Clamp(x_Moment, -momentumToRemove, momentumToRemove);
float y_momentToRemove = Mathf.Clamp(y_Moment, -momentumToRemove, momentumToRemove);
float z_momentToRemove = Mathf.Clamp(z_Moment, -momentumToRemove, momentumToRemove);
Expand All @@ -303,7 +293,6 @@ private void useResourcesToRecover()
double requestedAmount = rc.Rate * resourcePctToRequest * TimeWarp.fixedDeltaTime;
pctRequestable = Math.Min(total / requestedAmount, pctRequestable);
}
Debug.Log(pctRequestable);
if (pctRequestable < 0.01 || resourcePctToRequest < 0.01)
bConsumeResource = false;
else
Expand Down
Loading

0 comments on commit a751ee6

Please sign in to comment.