-
Notifications
You must be signed in to change notification settings - Fork 7
/
VolumeConfiguration.cs
281 lines (246 loc) · 9.26 KB
/
VolumeConfiguration.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// VolumeConfiguration.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2016 Allis Tauri
using System.Collections.Generic;
using System.Linq;
namespace AT_Utils
{
public class BaseVolume : ConfigNodeObject
{
public new const string NODE_NAME = "VOLUME";
[Persistent] public float Volume = 1;
public virtual string Info(float volume_conversion = 1)
{
return $"{Utils.formatVolume(Volume * volume_conversion)}";
}
public virtual float AddMass(float volume_conversion = 1)
{
return 0f;
}
public virtual float Cost(float volume_conversion = 1)
{
return 0f;
}
public virtual float ResourceCost(bool maxAmount = true, float volume_conversion = 1)
{
return 0f;
}
public virtual float ResourceMass(bool maxAmount = true, float volume_conversion = 1)
{
return 0f;
}
public virtual float ResourceAmount(bool maxAmount = true, float volume_conversion = 1)
{
return 0f;
}
public virtual bool ContainsType(string tank_type)
{
return false;
}
}
public class TankVolume : BaseVolume
{
public new const string NODE_NAME = "TANK";
[Persistent] public string CurrentResource;
[Persistent] public float InitialAmount;
[Persistent] public string TankType;
public SwitchableTankType Type => SwitchableTankType.GetTankType(TankType);
public override float AddMass(float volume_conversion = 1)
{
var t = Type;
return t?.AddMass(Volume * volume_conversion) ?? 0;
}
public override float Cost(float volume_conversion = 1)
{
var t = Type;
return t?.Cost(Volume * volume_conversion) ?? 0;
}
public override float ResourceCost(bool maxAmount = true, float volume_conversion = 1)
{
try
{
var t = Type;
var res = t.Resources[CurrentResource];
var cost = res.def.unitCost * res.UnitsPerLiter * t.UsefulVolume(Volume) * volume_conversion * 1000;
return maxAmount ? cost : cost * InitialAmount;
}
catch
{
return 0;
}
}
public override float ResourceMass(bool maxAmount = true, float volume_conversion = 1)
{
try
{
var t = Type;
var res = t.Resources[CurrentResource];
var mass = res.def.density * res.UnitsPerLiter * t.UsefulVolume(Volume) * volume_conversion * 1000;
return maxAmount ? mass : mass * InitialAmount;
}
catch
{
return 0;
}
}
public override float ResourceAmount(bool maxAmount = true, float volume_conversion = 1)
{
try
{
var t = Type;
var res = t.Resources[CurrentResource];
var amount = res.UnitsPerLiter * t.UsefulVolume(Volume) * volume_conversion * 1000;
return maxAmount ? amount : amount * InitialAmount;
}
catch
{
return 0;
}
}
public override string Info(float volume_conversion = 1)
{
var haveResource = !string.IsNullOrEmpty(CurrentResource);
var info = StringBuilderCache.Acquire();
info.Append($"- <b>{TankType}</b>");
if(haveResource)
info.Append($"\n <b>{CurrentResource}</b>");
info.Append($"\n <b>{Utils.formatVolume(Volume * volume_conversion)}</b>");
if(haveResource)
{
info.Append($"\n <b>{Utils.formatBigValue(ResourceAmount(true, volume_conversion), "u")}</b>");
info.Append($"\n {Utils.formatBigValue(ResourceMass(true, volume_conversion), "t")}");
}
info.Append($"\n {Cost(volume_conversion):F1}");
if(InitialAmount > 0)
info.Append($"+{ResourceCost(false, volume_conversion):F1}");
info.Append(" Cr");
return info.ToStringAndRelease().Trim();
}
public override bool ContainsType(string tank_type)
{
return TankType == tank_type;
}
public static TankVolume FromResource(PartResource res)
{
var tank = new TankVolume();
var tank_type = SwitchableTankType.FindTankType(res.resourceName);
if(tank_type == null)
return null;
tank.TankType = tank_type.name;
tank.CurrentResource = res.resourceName;
tank.Volume = (float)(res.maxAmount
/ tank_type.Resources[res.resourceName].UnitsPerLiter
/ 1000
/ tank_type.UsefulVolumeRatio);
tank.InitialAmount = (float)(res.amount / res.maxAmount);
return tank;
}
}
public class VolumeConfiguration : BaseVolume
{
public new const string NODE_NAME = "TANKCONF";
/// <summary>
/// The name of a configuration.
/// </summary>
[Persistent]
public string name = "";
public readonly List<BaseVolume> Volumes = new List<BaseVolume>();
public float TotalVolume { get { return Volumes.Aggregate(0f, (v, t) => v + t.Volume); } }
public bool Valid => Volumes.Count > 0 && TotalVolume > 0;
public override void Load(ConfigNode node)
{
Volumes.Clear();
base.Load(node);
var volumes = node.GetNodes();
for(var i = 0; i < volumes.Length; i++)
{
var v = volumes[i];
switch(v.name)
{
case TankVolume.NODE_NAME:
{
var preset = VolumeConfigsLibrary.GetConfig(v.GetValue("name"));
if(preset != null)
{
if(!float.TryParse(v.GetValue("Volume"), out var volume))
volume = 100f;
var cfg = preset.Clone<VolumeConfiguration>();
cfg.Volume = volume;
Volumes.Add(cfg);
}
else
{
Volumes.Add(FromConfig<TankVolume>(v));
}
break;
}
case NODE_NAME:
Volumes.Add(FromConfig<VolumeConfiguration>(v));
break;
}
}
}
public override void Save(ConfigNode node)
{
base.Save(node);
Volumes.ForEach(t => t.SaveInto(node));
}
public override string Info(float volume_conversion = 1)
{
if(!Valid)
return $"{name}: configuration invalid";
var info = StringBuilderCache.Acquire();
volume_conversion = Volume * volume_conversion / TotalVolume;
Volumes.ForEach(v =>
{
info.AppendLine(v.Info(volume_conversion));
info.AppendLine();
});
return info.ToStringAndRelease().Trim();
}
public override float AddMass(float volume_conversion = 1)
{
volume_conversion = Volume * volume_conversion / TotalVolume;
return Volumes.Aggregate(0f, (s, v) => s + v.AddMass(volume_conversion));
}
public override float Cost(float volume_conversion = 1)
{
volume_conversion = Volume * volume_conversion / TotalVolume;
return Volumes.Aggregate(0f, (s, v) => s + v.Cost(volume_conversion));
}
public override float ResourceCost(bool maxAmount = true, float volume_conversion = 1)
{
volume_conversion = Volume * volume_conversion / TotalVolume;
return Volumes.Aggregate(0f, (s, v) => s + v.ResourceCost(maxAmount, volume_conversion));
}
public override float ResourceMass(bool maxAmount = true, float volume_conversion = 1)
{
volume_conversion = Volume * volume_conversion / TotalVolume;
return Volumes.Aggregate(0f, (s, v) => s + v.ResourceMass(maxAmount, volume_conversion));
}
public override bool ContainsType(string tank_type)
{
return Volumes.Any(v => v.ContainsType(tank_type));
}
public bool ContainsTypes(IEnumerable<string> tank_types)
{
return tank_types.Any(ContainsType);
}
public static VolumeConfiguration FromResources(IEnumerable<PartResource> resources)
{
var volume = new VolumeConfiguration();
foreach(var res in resources)
{
var tank = TankVolume.FromResource(res);
if(tank == null)
return null;
volume.Volumes.Add(tank);
}
volume.Volume = volume.TotalVolume;
return volume;
}
}
}