Skip to content

Commit 6b48041

Browse files
committed
PersistentEngine now a PartModule, not ModuleEngines. It finds those and uses them on the same part.
1 parent 5f76f95 commit 6b48041

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

Patches/ionEngine.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
{
33
@MODULE[ModuleEngines]
44
{
5-
%name = PersistentEngine
65
%maxThrust = 0.00025 // 0.25 N like NASA's NEXT. Originally 2 kN.
7-
%resourceDeltaV = XenonGas // Resource used for deltaV
86
@PROPELLANT[ElectricCharge]
97
{
108
%ratio = 14400 // Originally 1.8. x8000 to make up for maxThrust reduction.
119
}
1210
}
11+
MODULE
12+
{
13+
name = PersistentEngine
14+
}
1315
}

TODO.org

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@
6969
** TODO Apply a reduced deltaV
7070
** TODO Flag depletion
7171
* TODO Do we want to calculate PersistentThrust as a function of PersistentThrottle, minThrust, and maxThrust? :FEATURE:
72-
* TODO Make PersistentEngine a separate addon that uses existing ModuleEngines* module [0/5] :FEATURE:
73-
** TODO Add "engine" field to PersistentEngine class
74-
** TODO Make class inherit PartModule
75-
** TODO Use "engine" field instead of direct fields
76-
** TODO Make module search vessel for ModuleEngine* module
77-
** TODO Update ion engine config to add PersistentEngine instead of replace ModuleEnginesFX
72+
* TODO Make PersistentEngine a separate addon that uses existing ModuleEngines* module [5/6] :FEATURE:
73+
** DONE Add "engine" field to PersistentEngine class
74+
** DONE Make class inherit PartModule
75+
** DONE Use "engine" field instead of direct fields
76+
** DONE Make module search vessel for ModuleEngine* module
77+
** DONE Update ion engine config to add PersistentEngine instead of replace ModuleEnginesFX
78+
** TODO Disable persistent features when ModuleEngine not found

src/PersistentEngine.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace PersistentThrust {
88

9-
public class PersistentEngine : ModuleEnginesFX {
9+
public class PersistentEngine : PartModule {
1010

1111
// Flag to activate force if it isn't to allow overriding stage activation
1212
[KSPField(isPersistant = true)]
@@ -26,6 +26,10 @@ public class PersistentEngine : ModuleEnginesFX {
2626
// Throttle
2727
[KSPField(guiActive = true, guiName = "Throttle")]
2828
protected string Throttle = "";
29+
30+
// Engine module on the same part
31+
public ModuleEngines engine;
32+
ModuleEnginesFX engineFX;
2933

3034
// Numeric display values
3135
protected double thrust_d = 0;
@@ -45,6 +49,20 @@ public class PersistentEngine : ModuleEnginesFX {
4549
// Average density of propellants
4650
public double densityAverage;
4751

52+
// Make "engine" and "engineFX" fields refer to the ModuleEngines and ModuleEnginesFX modules in part.Modules
53+
void FindModuleEngines () {
54+
foreach (PartModule pm in part.Modules) {
55+
if (pm is ModuleEngines) {
56+
engine = pm as ModuleEngines;
57+
} else {
58+
Debug.Log("No ModuleEngine found.");
59+
}
60+
if (pm is ModuleEnginesFX) {
61+
engineFX = pm as ModuleEnginesFX;
62+
}
63+
}
64+
}
65+
4866
// Update
4967
public override void OnUpdate() {
5068

@@ -65,7 +83,7 @@ public override void OnUpdate() {
6583
Throttle = Math.Round(throttle_d * 100).ToString() + "%";
6684

6785
// Activate force if engine is enabled and operational
68-
if (!IsForceActivated && isEnabled && isOperational)
86+
if (!IsForceActivated && engine.isEnabled && engine.isOperational)
6987
{
7088
IsForceActivated = true;
7189
part.force_activate();
@@ -78,8 +96,11 @@ public override void OnLoad(ConfigNode node) {
7896
// Run base OnLoad method
7997
base.OnLoad(node);
8098

99+
// Populate engine and engineFX fields
100+
FindModuleEngines();
101+
81102
// Initialize PersistentPropellant list
82-
pplist = PersistentPropellant.MakeList(propellants);
103+
pplist = PersistentPropellant.MakeList(engine.propellants);
83104

84105
// Initialize density of propellant used in deltaV and mass calculations
85106
densityAverage = pplist.AverageDensity();
@@ -88,13 +109,13 @@ public override void OnLoad(ConfigNode node) {
88109
void UpdatePersistentParameters () {
89110
// Update values to use during timewarp
90111
// Update thrust calculation
91-
this.CalculateThrust();
112+
engine.CalculateThrust();
92113
// Get Isp
93-
IspPersistent = realIsp;
114+
IspPersistent = engine.realIsp;
94115
// Get throttle
95116
ThrottlePersistent = vessel.ctrlState.mainThrottle;
96117
// Get final thrust
97-
ThrustPersistent = this.finalThrust;
118+
ThrustPersistent = engine.finalThrust;
98119
}
99120

100121
// Calculate demands of each resource

0 commit comments

Comments
 (0)