-
Notifications
You must be signed in to change notification settings - Fork 7
/
AbstractResourceTank.cs
183 lines (158 loc) · 6.09 KB
/
AbstractResourceTank.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// AbstractResourceTank.cs
//
// Author:
// Allis Tauri <allista@gmail.com>
//
// Copyright (c) 2016 Allis Tauri
using JetBrains.Annotations;
using UnityEngine;
namespace AT_Utils
{
public abstract class AbstractResourceTank : SerializableFiledsPartModule, IPartCostModifier, IPartMassModifier,
IModuleInfo
{
/// <summary>
/// The difference between the Part.cost and the initial value of the GetModuleCost.
/// Used when the Patch flag is set.
/// </summary>
[KSPField(isPersistant = true)]
public float CostPatch;
/// <summary>
/// If set, this flag causes the Module to save the initial difference between the
/// Part.cost and GetModuleCost value so that the total part cost is unchanged.
/// </summary>
[KSPField(isPersistant = true)]
public bool DoCostPatch;
/// <summary>
/// If set, this flag causes the Module to save the initial difference between the
/// Part.mass and GetModuleMass value so that the total part cost is unchanged.
/// </summary>
[KSPField(isPersistant = true)]
public bool DoMassPatch;
/// <summary>
/// The difference between the Part.mass and the initial value of the GetModuleMass.
/// Used when the Patch flag is set.
/// </summary>
[KSPField(isPersistant = true)]
public float MassPatch;
/// <summary>
/// The config node provided to OnLoad.
/// </summary>
[SerializeField]
public ConfigNode ModuleSave;
/// <summary>
/// If true, the module save was received not in the flight scene.
/// </summary>
[KSPField(isPersistant = true)]
public bool ModuleSaveFromPrefab;
/// <summary>
/// The volume of a tank in m^3. It is defined in a config or calculated from the part volume in editor.
/// Cannot be changed in flight.
/// </summary>
[KSPField(isPersistant = true)]
public float Volume = -1f;
/// <summary>
/// This is called within the GetModuleCost to calculate the cost of the tank.
/// </summary>
protected abstract float TankCost(float defaultCost);
/// <summary>
/// This is called within the GetModuleCost to calculate the cost of tank resources.
/// </summary>
/// <param name="maxAmount">If true, returns the cost of maxAmount of resources; of current amount otherwise.</param>
protected abstract float ResourcesCost(bool maxAmount = true);
/// <summary>
/// This is called within the GetModuleMass to calculate the mass of the tank.
/// </summary>
protected abstract float TankMass(float defaultMass);
/// <summary>
/// This is called within the GetModuleMass to calculate the mass of tank resources.
/// </summary>
/// <param name="maxAmount">If true, returns the mass of maxAmount of resources; of current amount otherwise.</param>
protected abstract float ResourcesMass(bool maxAmount = true);
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
ModuleSaveFromPrefab = HighLogic.LoadedScene == GameScenes.LOADING;
}
public override void OnStart(StartState state)
{
base.OnStart(state);
//this means the module was added by MM patch to an existing part
if(HighLogic.LoadedSceneIsFlight && ModuleSaveFromPrefab)
init_from_part();
}
protected abstract void init_from_part();
#region IPart*Modifiers
public virtual float GetModuleCost(float defaultCost, ModifierStagingSituation sit)
{
var cost = TankCost(defaultCost);
if(DoCostPatch)
{
CostPatch = -Mathf.Min(cost + ResourcesCost(false), defaultCost);
DoCostPatch = false;
}
var res = part.partInfo != null && part.partInfo.partPrefab == part
? ResourcesCost(false)
: ResourcesCost();
return cost + res + CostPatch;
}
public virtual ModifierChangeWhen GetModuleCostChangeWhen()
{
return ModifierChangeWhen.CONSTANTLY;
}
//this is not called by PartListTooltip.GetPartStats
public virtual float GetModuleMass(float defaultMass, ModifierStagingSituation sit)
{
var mass = TankMass(defaultMass);
// ReSharper disable once InvertIf
if(DoMassPatch)
{
MassPatch = -Mathf.Min(mass, defaultMass);
DoMassPatch = false;
}
return mass + MassPatch;
}
public virtual ModifierChangeWhen GetModuleMassChangeWhen()
{
return ModifierChangeWhen.CONSTANTLY;
}
#endregion
#region IModInfo
public override string GetInfo()
{
return "";
}
public virtual string GetModuleTitle()
{
return KSPUtil.PrintModuleName(moduleName);
}
public virtual string GetPrimaryField()
{
var info = "<b>Additional Mass:</b>\n";
var tank = DoMassPatch ? 0 : TankMass(part.mass);
var res = ResourcesMass(false);
if(tank > 0)
info += Utils.formatMass(tank) + " internals";
if(res > 0)
info += (tank > 0 ? "+" : "") + Utils.formatMass(res) + " resources";
return tank > 0 || res > 0 ? info : "";
}
public Callback<Rect> GetDrawModulePanelCallback()
{
return null;
}
#endregion
#if DEBUG
[UsedImplicitly]
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "Module")]
public string ThisModule = "";
public override void OnAwake()
{
base.OnAwake();
{
ThisModule = GetType().Name;
}
}
#endif
}
}