-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleIgnitionController.cs
More file actions
163 lines (140 loc) · 6.22 KB
/
ModuleIgnitionController.cs
File metadata and controls
163 lines (140 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using static Ignition.PropellantConfigUtils;
namespace Ignition
{
abstract class ModuleIgnitionController : PartModule
{
[KSPField(isPersistant = true)]
public string moduleID;
[KSPField(isPersistant = true)]
public string PropellantModuleIDs = "";
protected PropellantConfigBase PropellantConfigOriginal = null;
protected PropellantConfigBase PropellantConfigCurrent = null;
protected double ScaleFactorPrevious = 1;
public double ScaleFactor = 1;
private Dictionary<string, double> ScaleExponents = new Dictionary<string, double>();
protected const string MassScaleExponent = "mass";
protected const string CostScaleExponent = "cost";
protected const string VolumeScaleExponent = "tank";
protected const string EngineThrustScaleExponent = "engine";
protected const string RCSThrustScaleExponent = "rcs";
public virtual void UnapplyPropellantConfig() {}
public abstract void ApplyPropellantConfig();
public virtual void SetupData() { }
protected abstract void SetInfoStrings();
public ModuleIgnitionController()
{
foreach (var node in GameDatabase.Instance.GetConfigNodes("TWEAKSCALEEXPONENTS"))
{
if (node.HasValue("name"))
{
switch (node.GetValue("name"))
{
case "TweakScale":
if (node.HasValue("MassScale")) ScaleExponents[MassScaleExponent] = double.Parse(node.GetValue("MassScale"));
if (node.HasValue("DryCost")) ScaleExponents[CostScaleExponent] = double.Parse(node.GetValue("DryCost"));
break;
case "Part":
if (node.HasNode("Resources") && node.GetNode("Resources").HasValue("!amount")) ScaleExponents[VolumeScaleExponent] = double.Parse(node.GetNode("Resources").GetValue("!amount"));
break;
case "ModuleEngines":
if (node.HasValue("maxFuelFlow")) ScaleExponents[EngineThrustScaleExponent] = double.Parse(node.GetValue("maxFuelFlow"));
break;
case "ModuleRCS":
if (node.HasValue("thrusterPower")) ScaleExponents[RCSThrustScaleExponent] = double.Parse(node.GetValue("thrusterPower"));
break;
default:
break;
}
}
}
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
if (PropellantModuleIDs == "")
{
var propellantIDs = node.GetValues("propellantModuleID");
for (int i = 0; i < propellantIDs.Length; i++)
{
PropellantModuleIDs += propellantIDs[i];
if (i != propellantIDs.Length - 1) PropellantModuleIDs += ";";
}
}
UpdateAndApply(false);
}
public override void OnStart(StartState state)
{
base.OnStart(state);
UpdateAndApply(true);
}
protected virtual bool DisplayGuiStrings()
{
return !(PropellantConfigCurrent is null) && PropellantConfigCurrent.Propellants.Count > 0;
}
protected void UpdateScaleFactor()
{
foreach (var module in part.Modules)
{
if (module.ClassName == "TweakScale" && module.Fields.GetValue<bool>("moduleIsEnabled"))
{
ScaleFactor = module.Fields.GetValue<float>("currentScaleFactor");
break;
}
}
}
public void UpdatePropellantConfigs()
{
PropellantConfigOriginal = GetOrCreatePropellantConfig(GetConnectedPropellantModules(true, true));
PropellantConfigCurrent = GetOrCreatePropellantConfig(GetConnectedPropellantModules(true, false));
}
public virtual bool ShouldUpdateAndApply()
{
return true;
}
public void UpdateAndApply(bool initialSetup)
{
if (!ShouldUpdateAndApply()) return;
UpdateScaleFactor();
UnapplyPropellantConfig();
UpdatePropellantConfigs();
if (initialSetup) SetupData();
ApplyPropellantConfig();
SetInfoStrings();
}
public void Update()
{
if (!HighLogic.LoadedSceneIsEditor) return;
UpdateScaleFactor();
if (ScaleFactor != ScaleFactorPrevious)
{
// Only really need to update the stats for display purposes here since TweakScale will have already scaled the volume/thrust,
// but just to be safe, reapply the config
UpdateAndApply(false);
ScaleFactorPrevious = ScaleFactor;
}
}
public double GetScale(string key)
{
return ScaleExponents.ContainsKey(key) ? Math.Pow(ScaleFactor, ScaleExponents[key]) : 1;
}
public bool IsConnectedToPropellantModule(string moduleID)
{
return PropellantModuleIDs.Contains(moduleID);
}
protected List<ModuleIgnitionPropellantWrapper> GetConnectedPropellantModules(bool requireGoodResource, bool useOriginalResourceNames)
{
var propellantModules = new List<ModuleIgnitionPropellantWrapper>();
foreach (var module in part.GetComponents<ModuleIgnitionPropellant>())
{
var propellantModule = new ModuleIgnitionPropellantWrapper(module, useOriginalResourceNames);
if (!IsConnectedToPropellantModule(propellantModule.moduleID)) continue;
var resource = propellantModule.GetResourceName();
if (requireGoodResource && (resource is null || PartResourceLibrary.Instance.GetDefinition(resource) is null)) continue;
propellantModules.Add(propellantModule);
}
return propellantModules;
}
}
}