-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleIgnitionTankController.cs
More file actions
133 lines (108 loc) · 4.97 KB
/
ModuleIgnitionTankController.cs
File metadata and controls
133 lines (108 loc) · 4.97 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
using System;
using System.Collections.Generic;
namespace Ignition
{
class ModuleIgnitionTankController : ModuleIgnitionController, IPartMassModifier, IPartCostModifier
{
[KSPField(isPersistant = true)]
public double volume = 0;
[KSPField(isPersistant = true)]
private double PreviousVolume = 0;
private double VolumeScaled => GetScale(VolumeScaleExponent) * volume;
private double VolumeResolution => 1e-5 * VolumeScaled;
[KSPField(isPersistant = true)]
public double addedMass = 0;
[KSPField(isPersistant = true)]
public double currentAddedMass = 0;
public float GetModuleMass(float baseMass, ModifierStagingSituation situation) => (float)currentAddedMass;
public ModifierChangeWhen GetModuleMassChangeWhen() => ModifierChangeWhen.CONSTANTLY;
[KSPField(isPersistant = true)]
public double addedCost = 0;
[KSPField(isPersistant = true)]
public double currentAddedCost = 0;
public float GetModuleCost(float baseCost, ModifierStagingSituation situation) => (float)currentAddedCost;
public ModifierChangeWhen GetModuleCostChangeWhen() => ModifierChangeWhen.CONSTANTLY;
[KSPField(guiName = "<b>Resources</b>")]
[UI_Label(scene = UI_Scene.All)]
public string ResourcesString = "";
public override void OnStart(StartState state)
{
UpdatePropellantConfigs();
RemoveZeroResources();
}
protected override void SetInfoStrings()
{
bool isActive = DisplayGuiStrings();
Fields["ResourcesString"].guiActiveEditor = isActive;
Fields["ResourcesString"].guiActive = isActive;
if (!isActive) return;
ResourcesString = PropellantConfigUtils.GetPropellantRatiosString(PropellantConfigCurrent.Propellants);
}
private void RemoveZeroResources()
{
var resourcesToRemove = new List<string>();
foreach (var resource in part.Resources)
{
if (resource.maxAmount < VolumeResolution) resourcesToRemove.Add(resource.resourceName);
}
foreach (var resourceName in resourcesToRemove) part.RemoveResource(resourceName);
}
private void AddOrRemoveResource(string resourceName, double volumeFraction, double tankDensity, bool addNotRemove)
{
var resourceDefinition = PartResourceLibrary.Instance.GetDefinition(resourceName);
var addedVolume = addNotRemove ? volume : -PreviousVolume;
addedVolume *= volumeFraction * GetScale(VolumeScaleExponent);
var addedAmount = addedVolume / PropellantConfigUtils.GetUnitVolume(resourceName);
if (addNotRemove)
{
// Set unscaled values because TweakScale will handle them afterwards
currentAddedMass += addedVolume * tankDensity / GetScale(MassScaleExponent);
currentAddedCost += addedAmount * resourceDefinition.unitCost / GetScale(CostScaleExponent);
PreviousVolume = volume;
}
var totalAmount = addedAmount;
if (part.Resources.Contains(resourceName)) totalAmount += part.Resources.Get(resourceName).maxAmount;
if (totalAmount < 0) totalAmount = 0;
var resourceNode = new ConfigNode();
resourceNode.name = "RESOURCE";
resourceNode.AddValue("name", resourceName);
resourceNode.AddValue("amount", totalAmount);
resourceNode.AddValue("maxAmount", totalAmount);
part.SetResource(resourceNode);
}
private void AddOrRemoveConfiguredPropellant(bool addNotRemove)
{
currentAddedMass = addedMass;
currentAddedCost = addedCost;
if (PropellantConfigCurrent is null || PropellantConfigCurrent.Propellants.Count == 0)
{
currentAddedMass = 0;
return;
}
var totalRatio = 0.0;
foreach (var propellant in PropellantConfigCurrent.Propellants) totalRatio += propellant.ratio;
foreach (var propellant in PropellantConfigCurrent.Propellants) AddOrRemoveResource(propellant.name, propellant.ratio / totalRatio, PropellantConfigCurrent.TankDensity, addNotRemove);
}
public override void UnapplyPropellantConfig()
{
AddOrRemoveConfiguredPropellant(false);
}
public override void ApplyPropellantConfig()
{
AddOrRemoveConfiguredPropellant(true);
RemoveZeroResources();
}
public override bool ShouldUpdateAndApply()
{
return HighLogic.LoadedSceneIsEditor;
}
public override string GetInfo()
{
return "<b>Volume: </b>" + KSPUtil.LocalizeNumber(volume, "F1") + " L";
}
public override string GetModuleDisplayName()
{
return "Propellant tank";
}
}
}