Skip to content

Commit

Permalink
support activating RCS
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 1, 2023
1 parent 7f45d8b commit d32fc91
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 22 deletions.
33 changes: 17 additions & 16 deletions MechJeb2/MechJebLib/Simulations/Builder.cs
Expand Up @@ -224,26 +224,27 @@ private void BuildModules(SimVessel vessel, SimPart part, Part kspPart)
ModuleAnchoredDecoupler kspAnchoredDecoupler => BuildModuleAnchoredDecoupler(part, kspAnchoredDecoupler),
ModuleDecouple kspModuleDecouple => BuildModuleDecouple(part, kspModuleDecouple),
ModuleDockingNode kspModuleDockingNode => BuildDockingNode(part, kspModuleDockingNode),
ModuleRCS kspModuleRCS => BuildModuleRCS(part, kspModuleRCS),
ModuleRCS kspModuleRCS => BuildModuleRCS(vessel, part, kspModuleRCS),
_ => null
};


if (m != null)
return m;

return kspModule.moduleName switch
} ?? kspModule.moduleName switch
{
"ProceduralFairingDecoupler" => BuildProceduralFairingDecoupler(part, kspModule),
_ => null
};

if (m == null)
return null;

m.ModuleIsEnabled = kspModule.moduleIsEnabled;
m.StagingEnabled = kspModule.stagingEnabled;

return m;
}


private SimPartModule BuildDockingNode(SimPart part, ModuleDockingNode kspModuleDockingNode)
{
var decoupler = SimModuleDockingNode.Borrow(part);
decoupler.StagingEnabled = kspModuleDockingNode.stagingEnabled;
decoupler.Staged = kspModuleDockingNode.staged;

if (!(kspModuleDockingNode.referenceNode.attachedPart is null))
Expand All @@ -255,7 +256,6 @@ private SimPartModule BuildModuleDecouple(SimPart part, ModuleDecouple kspModule
{
var decoupler = SimModuleDecouple.Borrow(part);
decoupler.IsDecoupled = kspModuleDecouple.isDecoupled;
decoupler.StagingEnabled = kspModuleDecouple.stagingEnabled;
decoupler.IsOmniDecoupler = kspModuleDecouple.isOmniDecoupler;
decoupler.Staged = kspModuleDecouple.staged;

Expand Down Expand Up @@ -283,7 +283,6 @@ private SimPartModule BuildModuleAnchoredDecoupler(SimPart part, ModuleAnchoredD
{
var decoupler = SimModuleAnchoredDecoupler.Borrow(part);
decoupler.IsDecoupled = kspAnchoredDecoupler.isDecoupled;
decoupler.StagingEnabled = kspAnchoredDecoupler.stagingEnabled;
decoupler.Staged = kspAnchoredDecoupler.staged;

Part kspPart = kspAnchoredDecoupler.part;
Expand Down Expand Up @@ -401,24 +400,26 @@ private SimModuleEngines BuildModuleEngines(SimVessel vessel, SimPart part, Modu
return engine;
}

private SimPartModule BuildModuleRCS(SimPart part, ModuleRCS kspModuleRCS)
private SimPartModule BuildModuleRCS(SimVessel vessel, SimPart part, ModuleRCS kspModuleRCS)
{
var rcs = SimModuleRCS.Borrow(part);
rcs.Isp = kspModuleRCS.atmosphereCurve.Evaluate(0) * kspModuleRCS.ispMult;
rcs.G = kspModuleRCS.G;
rcs.Thrust = kspModuleRCS.flowMult * kspModuleRCS.maxFuelFlow * rcs.Isp * rcs.G;
rcs.Isp = kspModuleRCS.atmosphereCurve.Evaluate(0) * kspModuleRCS.ispMult;
rcs.G = kspModuleRCS.G;
rcs.Thrust = kspModuleRCS.flowMult * kspModuleRCS.maxFuelFlow * rcs.Isp * rcs.G;
rcs.rcsEnabled = kspModuleRCS.rcsEnabled;

foreach (Propellant p in kspModuleRCS.propellants)
rcs.Propellants.Add(new SimPropellant(p.id, p.ignoreForIsp, p.ratio, (SimFlowMode)p.GetFlowMode(),
PartResourceLibrary.Instance.GetDefinition(p.id).density));

vessel.RCSActivatedInStage[kspModuleRCS.part.inverseStage].Add(rcs);

return rcs;
}

private SimProceduralFairingDecoupler BuildProceduralFairingDecoupler(SimPart part, PartModule kspPartModule)
{
var decoupler = SimProceduralFairingDecoupler.Borrow(part);
decoupler.StagingEnabled = kspPartModule.stagingEnabled;
decoupler.IsDecoupled = kspPartModule.Fields["decoupled"].GetValue<bool>(kspPartModule);
return decoupler;
}
Expand Down
Expand Up @@ -9,7 +9,6 @@ public class SimModuleAnchoredDecoupler : SimPartModule
private static readonly ObjectPool<SimModuleAnchoredDecoupler> _pool = new ObjectPool<SimModuleAnchoredDecoupler>(New, Clear);

public bool IsDecoupled;
public bool StagingEnabled;
public bool Staged;
public SimPart? AttachedPart;

Expand Down
Expand Up @@ -9,7 +9,6 @@ public class SimModuleDecouple : SimPartModule
private static readonly ObjectPool<SimModuleDecouple> _pool = new ObjectPool<SimModuleDecouple>(New, Clear);

public bool IsDecoupled;
public bool StagingEnabled;
public bool IsOmniDecoupler;
public bool Staged;
public SimPart? AttachedPart;
Expand Down
Expand Up @@ -8,7 +8,6 @@ public class SimModuleDockingNode : SimPartModule
{
private static readonly ObjectPool<SimModuleDockingNode> _pool = new ObjectPool<SimModuleDockingNode>(New, Clear);

public bool StagingEnabled;
public bool Staged;
public SimPart? AttachedPart;

Expand Down
Expand Up @@ -73,7 +73,7 @@ public class SimModuleEngines : SimPartModule
private double _atmDensity => Part.Vessel.ATMDensity;
private double _machNumber => Part.Vessel.MachNumber;

public void Ignite()
public void Activate()
{
Log("igniting engine");
IsOperational = true;
Expand Down
10 changes: 10 additions & 0 deletions MechJeb2/MechJebLib/Simulations/PartModules/SimModuleRCS.cs
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using MechJebLib.Utils;
using static MechJebLib.Utils.Statics;

namespace MechJebLib.Simulations.PartModules
{
Expand All @@ -15,6 +16,7 @@ public class SimModuleRCS : SimPartModule
public double G;
public double Isp;
public double Thrust;
public bool rcsEnabled;

public override void Dispose()
{
Expand All @@ -33,6 +35,14 @@ private static SimModuleRCS New()
return new SimModuleRCS();
}

public void Activate()
{
if (StagingEnabled && ModuleIsEnabled)
{
rcsEnabled = true;
}
}

private static void Clear(SimModuleRCS m)
{
}
Expand Down
Expand Up @@ -9,7 +9,6 @@ public class SimProceduralFairingDecoupler : SimPartModule
private static readonly ObjectPool<SimProceduralFairingDecoupler> _pool = new ObjectPool<SimProceduralFairingDecoupler>(New, Clear);

public bool IsDecoupled;
public bool StagingEnabled;

public override void Dispose()
{
Expand Down
1 change: 1 addition & 0 deletions MechJeb2/MechJebLib/Simulations/SimPart.cs
Expand Up @@ -27,6 +27,7 @@ public class SimPart
public bool IsThrottleLocked;
public int ResourcePriority;
public double ResourceRequestRemainingThreshold;
public bool IsEnabled;

public double Mass;
public double DryMass;
Expand Down
2 changes: 2 additions & 0 deletions MechJeb2/MechJebLib/Simulations/SimPartModule.cs
Expand Up @@ -8,6 +8,8 @@ public abstract class SimPartModule : IDisposable
{
public bool IsEnabled;
public SimPart Part = null!;
public bool ModuleIsEnabled;
public bool StagingEnabled;

public abstract void Dispose();
}
Expand Down
8 changes: 7 additions & 1 deletion MechJeb2/MechJebLib/Simulations/SimVessel.cs
Expand Up @@ -18,6 +18,7 @@ public class SimVessel : IDisposable
public readonly DictOfLists<int, SimPart> PartsDroppedInStage = new DictOfLists<int, SimPart>(10);
public readonly DictOfLists<int, SimModuleEngines> EnginesDroppedInStage = new DictOfLists<int, SimModuleEngines>(10);
public readonly DictOfLists<int, SimModuleEngines> EnginesActivatedInStage = new DictOfLists<int, SimModuleEngines>(10);
public readonly DictOfLists<int, SimModuleRCS> RCSActivatedInStage = new DictOfLists<int, SimModuleRCS>(10);
public readonly List<SimModuleEngines> ActiveEngines = new List<SimModuleEngines>(10);

public int CurrentStage; // FIXME: restorable
Expand Down Expand Up @@ -62,7 +63,11 @@ public void Stage()

foreach (SimModuleEngines e in EnginesActivatedInStage[CurrentStage])
if (e.IsEnabled)
e.Ignite();
e.Activate();

foreach (SimModuleRCS r in RCSActivatedInStage[CurrentStage])
if (r.IsEnabled)
r.Activate();

UpdateMass();
}
Expand Down Expand Up @@ -149,6 +154,7 @@ private static void Clear(SimVessel v)
v.Parts.Clear();
v.PartsDroppedInStage.Clear();
v.EnginesActivatedInStage.Clear();
v.RCSActivatedInStage.Clear();
v.ActiveEngines.Clear();
}

Expand Down

0 comments on commit d32fc91

Please sign in to comment.