-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathScaleType.cs
295 lines (256 loc) · 10.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
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 && name != "default")
{
Tools.LogWf("No SCALETYPE with name {0}", name);
}
return (object)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 readonly float[] _scaleFactors = { 0.625f, 1.25f, 2.5f, 3.75f, 5f };
private readonly string[] _scaleNames = { "62.5cm", "1.25m", "2.5m", "3.75m", "5m" };
public readonly Dictionary<string, ScaleExponents> Exponents = new Dictionary<string, ScaleExponents>();
public readonly bool IsFreeScale = false;
public readonly string[] TechRequired = { "", "", "", "", "" };
public readonly Dictionary<string, NodeInfo> AttachNodes = new Dictionary<string, NodeInfo>();
public readonly float MinValue = 0.625f;
public readonly float MaxValue = 5.0f;
public readonly float DefaultScale = 1.25f;
public readonly string Suffix = "m";
public readonly string Name;
public readonly string Family;
public float BaseScale {
get { return AttachNodes["base"].Scale; }
}
public float[] AllScaleFactors
{
get
{
return _scaleFactors;
}
}
public float[] ScaleFactors
{
get
{
var result = _scaleFactors.ZipFilter(TechRequired, Tech.IsUnlocked).ToArray();
return result;
}
}
public string[] ScaleNames
{
get
{
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);
}
public ScaleType(ConfigNode config)
{
ScaleNodes = new int[] {};
if ((object)config == null || Tools.ConfigValue(config, "name", "default") == "default")
{
return; // Default values.
}
var type = Tools.ConfigValue(config, "type", "default");
var source = GetScaleConfig(type);
IsFreeScale = Tools.ConfigValue(config, "freeScale", source.IsFreeScale);
MinValue = Tools.ConfigValue(config, "minScale", source.MinValue);
MaxValue = Tools.ConfigValue(config, "maxScale", source.MaxValue);
Suffix = Tools.ConfigValue(config, "suffix", source.Suffix);
_scaleFactors = Tools.ConfigValue(config, "scaleFactors", source._scaleFactors);
ScaleNodes = Tools.ConfigValue(config, "scaleNodes", source.ScaleNodes);
_scaleNames = Tools.ConfigValue(config, "scaleNames", source._scaleNames).Select(a => a.Trim()).ToArray();
TechRequired = Tools.ConfigValue(config, "techRequired", source.TechRequired).Select(a=>a.Trim()).ToArray();
Name = Tools.ConfigValue(config, "name", "unnamed scaletype");
Family = Tools.ConfigValue(config, "family", "default");
AttachNodes = GetNodeFactors(config.GetNode("ATTACHNODES"), source.AttachNodes);
if (Name == "TweakScale")
{
Name = source.Name;
}
if (_scaleFactors.Length != _scaleNames.Length)
{
Tools.LogWf("Wrong number of scaleFactors compared to scaleNames: {0} scaleFactors vs {1} scaleNames", _scaleFactors.Length, _scaleNames.Length);
}
if (TechRequired.Length < _scaleFactors.Length)
{
TechRequired = TechRequired.Concat("".Repeat()).Take(_scaleFactors.Length).ToArray();
}
var tmpScale = Tools.ConfigValue(config, "defaultScale", source.DefaultScale);
if (!IsFreeScale)
{
tmpScale = Tools.Closest(tmpScale, AllScaleFactors);
}
DefaultScale = Tools.Clamp(tmpScale, MinValue, MaxValue);
Exponents = ScaleExponents.CreateExponentsForModule(config, source.Exponents);
}
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 {\n";
result += " isFreeScale = " + IsFreeScale + "\n";
result += " scaleFactors = " + ScaleFactors + "\n";
result += " scaleNodes = " + ScaleNodes + "\n";
result += " minValue = " + MinValue + "\n";
result += " maxValue = " + MaxValue + "\n";
return result + "}";
}
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);
}
}
}