forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 23
/
ScaleType.cs
441 lines (389 loc) · 17.3 KB
/
ScaleType.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace TweakScale
{
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class TechUpdater : MonoBehaviour
{
public void Start()
{
Tech.Reload();
}
}
public static class Tech
{
private static HashSet<string> _unlockedTechs = new HashSet<string>();
public static void Reload()
{
if (HighLogic.CurrentGame == null)
return;
if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER && HighLogic.CurrentGame.Mode != Game.Modes.SCIENCE_SANDBOX)
return;
var persistentfile = KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/persistent.sfs";
var config = ConfigNode.Load(persistentfile);
var gameconf = config.GetNode("GAME");
var scenarios = gameconf.GetNodes("SCENARIO");
var thisScenario = scenarios.FirstOrDefault(a => a.GetValue("name") == "ResearchAndDevelopment");
if (thisScenario == null)
return;
var techs = thisScenario.GetNodes("Tech");
_unlockedTechs = techs.Select(a => a.GetValue("id")).ToHashSet();
_unlockedTechs.Add("");
}
public static bool IsUnlocked(string techId)
{
if (HighLogic.CurrentGame == null)
return true;
if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER && HighLogic.CurrentGame.Mode != Game.Modes.SCIENCE_SANDBOX)
return true;
return techId == "" || _unlockedTechs.Contains(techId);
}
}
/// <summary>
/// Configuration values for TweakScale.
/// </summary>
public class ScaleType
{
/// <summary>
/// Fetches the scale ScaleType with the specified name.
/// </summary>
/// <param name="name">The name of the ScaleType to fetch.</param>
/// <returns>The specified ScaleType or the default ScaleType if none exists by that name.</returns>
/*private static ScaleType GetScaleConfig(string name)
{
var config = GameDatabase.Instance.GetConfigs("SCALETYPE").FirstOrDefault(a => a.name == name);
if (config == null)
{
Tools.LogWf("No SCALETYPE with name {0}", name);
}
return config; // == null ? DefaultScaleType : new ScaleType(config.config);
}*/
public class NodeInfo
{
public readonly string Family;
public readonly float Scale;
private NodeInfo()
{
}
public NodeInfo(string family, float scale) : this()
{
Family = family;
Scale = scale;
if (Mathf.Abs(Scale) < 0.01)
{
Tools.LogWf("Invalid scale for family {0}: {1}", family, scale);
}
}
public NodeInfo(string s) : this()
{
var parts = s.Split(':');
if (parts.Length == 1)
{
if (!float.TryParse(parts[0], out Scale))
Tools.LogWf("Invalid attachment node string \"{0}\"", s);
return;
}
if (parts.Length == 0)
{
return;
}
if (!float.TryParse(parts[1], out Scale))
{
Tools.LogWf("Invalid attachment node string \"{0}\"", s);
return;
}
Family = parts[0];
if (Mathf.Abs(Scale) < 0.01)
{
Tools.LogWf("Invalid scale for family {0}: {1}", Family, Scale);
}
}
public override string ToString()
{
return string.Format("({0}, {1})", Family, Scale);
}
}
private static List<ScaleType> _scaleTypes;
public static List<ScaleType> AllScaleTypes
{
get {
return _scaleTypes = _scaleTypes ??
(GameDatabase.Instance.GetConfigs("SCALETYPE")
.Select(a => new ScaleType(a.config))
.ToList<ScaleType>());
}
}
//private static readonly ScaleType DefaultScaleType = new ScaleType();
private float[] _scaleFactors = {};
private readonly string[] _scaleNames = {};
public readonly Dictionary<string, ScaleExponents> Exponents = new Dictionary<string, ScaleExponents>();
public readonly bool IsFreeScale = true;
public readonly string[] TechRequired = {};
//public readonly Dictionary<string, NodeInfo> AttachNodes = new Dictionary<string, NodeInfo>();
//public readonly float MinValue = 0f;
//public readonly float MaxValue = 0f;
public float DefaultScale = -1;
public float[] IncrementSlide = {};
public string Suffix = null;
public readonly string Name = null;
public readonly string Family;
/*public float BaseScale {
get { return AttachNodes["base"].Scale; }
}*/
public float[] AllScaleFactors
{
get
{
return _scaleFactors;
}
}
public float[] ScaleFactors
{
get
{
if (TechRequired.Length == 0)
return _scaleFactors;
var result = _scaleFactors.ZipFilter(TechRequired, Tech.IsUnlocked).ToArray();
return result;
}
}
public string[] ScaleNames
{
get
{
if (TechRequired.Length == 0)
return _scaleNames;
var result = _scaleNames.ZipFilter(TechRequired, Tech.IsUnlocked).ToArray();
return result;
}
}
public int[] ScaleNodes { get; private set; }
private ScaleType()
{
ScaleNodes = new int[] {};
//AttachNodes = new Dictionary<string, NodeInfo>();
//AttachNodes["base"] = new NodeInfo("", 1);
}
// config is a part config
public ScaleType(ConfigNode partConfig)
{
ConfigNode scaleConfig = null;
if ((object)partConfig != null )
{
Name = Tools.ConfigValue(partConfig, "type", Name);
ScaleExponents.LoadGlobalExponents();
if (Name != null)
{
var tmp = GameDatabase.Instance.GetConfigs("SCALETYPE").FirstOrDefault(a => a.name == Name);
if (tmp != null) scaleConfig = tmp.config;
if (scaleConfig != null)
{
// search scaletype for values
IsFreeScale = Tools.ConfigValue(scaleConfig, "freeScale", IsFreeScale);
DefaultScale = Tools.ConfigValue(scaleConfig, "defaultScale", DefaultScale);
Suffix = Tools.ConfigValue(scaleConfig, "suffix", Suffix);
_scaleFactors = Tools.ConfigValue(scaleConfig, "scaleFactors", _scaleFactors);
ScaleNodes = Tools.ConfigValue(scaleConfig, "scaleNodes", ScaleNodes); // currently not used!
_scaleNames = Tools.ConfigValue(scaleConfig, "scaleNames", _scaleNames).Select(a => a.Trim()).ToArray();
TechRequired = Tools.ConfigValue(scaleConfig, "techRequired", TechRequired).Select(a => a.Trim()).ToArray();
Family = Tools.ConfigValue(scaleConfig, "family", "default");
//AttachNodes = GetNodeFactors(scaleConfig.GetNode("ATTACHNODES"), AttachNodes); // currently not used!
IncrementSlide = Tools.ConfigValue(scaleConfig, "incrementSlide", IncrementSlide); // deprecated!
Exponents = ScaleExponents.CreateExponentsForModule(scaleConfig, Exponents);
//Debug.Log("[TweakScale] scaleConfig:" + scaleConfig.ToString());
//Debug.Log("[TweakScale] scaleConfig:" + this.ToString());
//Debug.Log("[TweakScale]" + Exponents.ToString());
}
}
else
Name = "";
// search part config for overrides
IsFreeScale = Tools.ConfigValue(partConfig, "freeScale", IsFreeScale);
DefaultScale = Tools.ConfigValue(partConfig, "defaultScale", DefaultScale);
Suffix = Tools.ConfigValue(partConfig, "suffix", Suffix);
_scaleFactors = Tools.ConfigValue(partConfig, "scaleFactors", _scaleFactors);
ScaleNodes = Tools.ConfigValue(partConfig, "scaleNodes", ScaleNodes);
_scaleNames = Tools.ConfigValue(partConfig, "scaleNames", _scaleNames).Select(a => a.Trim()).ToArray();
TechRequired = Tools.ConfigValue(partConfig, "techRequired", TechRequired).Select(a=>a.Trim()).ToArray();
Family = Tools.ConfigValue(partConfig, "family", "default");
//AttachNodes = GetNodeFactors(partConfig.GetNode("ATTACHNODES"), AttachNodes);
IncrementSlide= Tools.ConfigValue(partConfig, "incrementSlide", IncrementSlide);
Exponents = ScaleExponents.CreateExponentsForModule(partConfig, Exponents);
ScaleExponents.treatMassAndCost(Exponents);
// string log = "finished ScaleExponents: ";
// foreach(var e in Exponents) { log += e.ToString() + ", \n"; }
// Debug.Log(log);
//Debug.Log("[TweakScale] partConfig:" + partConfig.ToString());
//Debug.Log("[TweakScale] partConfig:" + this.ToString());
//Debug.Log("[TweakScale]" + Exponents.ToString());
}
if (IsFreeScale && (_scaleFactors.Length > 1))
{
bool error = false;
for (int i=0; i<_scaleFactors.Length-1; i++)
if (_scaleFactors[i + 1] <= _scaleFactors[i])
error = true;
if (error)
{
Tools.LogWf("scaleFactors must be in ascending order! \n{0}", this.ToString());
_scaleFactors = new float[0];
}
}
// fill in missing values
if ((DefaultScale <= 0) || (_scaleFactors.Length == 0))
RepairScaletype(scaleConfig, partConfig);
if (!IsFreeScale && (_scaleFactors.Length != _scaleNames.Length))
{
if(_scaleNames.Length != 0)
Tools.LogWf("Wrong number of scaleFactors compared to scaleNames in scaleType \"{0}\": {1} scaleFactors vs {2} scaleNames\n{3}", Name, _scaleFactors.Length, _scaleNames.Length, this.ToString());
_scaleNames = new string[_scaleFactors.Length];
for (int i=0; i<_scaleFactors.Length; i++)
_scaleNames[i] = _scaleFactors[i].ToString();
}
if (!IsFreeScale)
{
DefaultScale = Tools.Closest(DefaultScale, AllScaleFactors);
}
DefaultScale = Tools.Clamp(DefaultScale, _scaleFactors.Min(), _scaleFactors.Max());
if (IncrementSlide.Length == 0)
{
IncrementSlide = new float[_scaleFactors.Length-1];
for (var i=0; i<_scaleFactors.Length-1; i++)
IncrementSlide[i] = (_scaleFactors[i+1]-_scaleFactors[i])/50f;
}
if (IsFreeScale)
{
// workaround for stock bug in tweakable UI_ScaleEdit:
// add a tiny dummy interval to the range because the highest one is bugged
var tmp = _scaleFactors;
_scaleFactors = new float[tmp.Length + 1];
for (int i = 0; i < tmp.Length; i++)
_scaleFactors[i] = tmp[i];
_scaleFactors[tmp.Length] = _scaleFactors[tmp.Length - 1] + 0.1f * IncrementSlide.Max();
}
var numTechs = TechRequired.Length;
if ((numTechs > 0) && (numTechs != _scaleFactors.Length))
{
//Tools.LogWf("Wrong number of techRequired compared to scaleFactors in scaleType \"{0}\": {1} scaleFactors vs {2} techRequired", Name, _scaleFactors.Length, TechRequired.Length);
if (numTechs < _scaleFactors.Length)
{
var lastTech = TechRequired[TechRequired.Length - 1];
TechRequired = TechRequired.Concat(lastTech.Repeat()).Take(_scaleFactors.Length).ToArray();
}
}
//Debug.Log("[TweakScale] finished config:" + this.ToString());
//Debug.Log("[TweakScale]" + Exponents.ToString());
}
private void RepairScaletype(ConfigNode scaleConfig, ConfigNode partConfig)
{
if ((DefaultScale <= 0) && (_scaleFactors.Length == 0))
{
DefaultScale = 100;
if (Suffix == null)
Suffix = "%";
if (IncrementSlide.Length == 0)
IncrementSlide = new float[] {1f, 1f, 1f, 2f, 5f};
}
if ((DefaultScale > 0) && (_scaleFactors.Length == 0))
{
_scaleFactors = new float[] { DefaultScale/10f, DefaultScale/4f, DefaultScale/2f, DefaultScale, DefaultScale*2f, DefaultScale*4f };
}
else if ((DefaultScale <= 0) && (_scaleFactors.Length > 0))
{
DefaultScale = _scaleFactors[0];
}
else
{
// Legacy support: min/maxValue
float minScale = -1;
float maxScale = -1;
if (scaleConfig != null)
{
minScale = Tools.ConfigValue(scaleConfig, "minScale", minScale); // deprecated!
maxScale = Tools.ConfigValue(scaleConfig, "maxScale", maxScale); // deprecated!
}
if (partConfig != null)
{
minScale = Tools.ConfigValue(partConfig, "minScale", minScale);
maxScale = Tools.ConfigValue(partConfig, "maxScale", maxScale);
}
if ((minScale > 0) && (maxScale > 0))
{
if (minScale > 0 && maxScale > 0)
{
if (DefaultScale > minScale && DefaultScale < maxScale)
_scaleFactors = new float[] { minScale, DefaultScale, maxScale };
else
_scaleFactors = new float[] { minScale, maxScale };
}
}
}
}
private Dictionary<string, NodeInfo> GetNodeFactors(ConfigNode node, Dictionary<string, NodeInfo> source)
{
var result = source.Clone();
if (node != null)
{
foreach (var v in node.values.Cast<ConfigNode.Value>())
{
result[v.name] = new NodeInfo(v.value);
}
}
if (!result.ContainsKey("base"))
{
result["base"] = new NodeInfo(Family, 1.0f);
}
return result;
}
public override string ToString()
{
var result = "ScaleType {";
result += "\n name = " + Name;
result += "\n isFreeScale = " + IsFreeScale;
result += "\n " + _scaleFactors.Length + " scaleFactors = ";
foreach (var s in _scaleFactors)
result += s + " ";
result += "\n " + _scaleNames.Length + " scaleNames = ";
foreach (var s in _scaleNames)
result += s + " ";
result += "\n " + IncrementSlide.Length + " incrementSlide = ";
foreach (var s in IncrementSlide)
result += s + " ";
result += "\n " + TechRequired.Length + " TechRequired = ";
foreach (var s in TechRequired)
result += s + " ";
result += "\n defaultScale = " + DefaultScale;
//result += " scaleNodes = " + ScaleNodes + "\n";
//result += " minValue = " + MinValue + "\n";
//result += " maxValue = " + MaxValue + "\n";
return result + "\n}";
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((ScaleType) obj);
}
public static bool operator ==(ScaleType a, ScaleType b)
{
if ((object)a == null)
return (object)b == null;
if ((object)b == null)
return false;
return a.Name == b.Name;
}
public static bool operator !=(ScaleType a, ScaleType b)
{
return !(a == b);
}
protected bool Equals(ScaleType other)
{
return string.Equals(Name, other.Name);
}
public override int GetHashCode()
{
return (Name != null ? Name.GetHashCode() : 0);
}
}
}