-
Notifications
You must be signed in to change notification settings - Fork 4
/
Updaters.cs
339 lines (301 loc) · 12.7 KB
/
Updaters.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
// PartUpdaters.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
// This code is based on Procedural Fairings plug-in by Alexey Volynskov, KzPartResizer class
// And on ideas drawn from the TweakScale plugin
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AT_Utils
{
public class DeprecatedPartModule : PartModule
{
public override void OnStart(StartState state)
{
base.OnStart(state);
this.EnableModule(false);
part.Modules.Remove(this);
Destroy(this);
}
}
public class NodesUpdater : DeprecatedPartModule { }
public class PropsUpdater : DeprecatedPartModule { }
/// <summary>
/// Emitter updater. Adapted from TweakScale.
/// </summary>
public class EmitterUpdater : PartUpdater
{
struct EmitterData
{
public readonly float MinSize, MaxSize, Shape1D;
public readonly Vector2 Shape2D;
public readonly Vector3 Shape3D, LocalVelocity, Force;
public EmitterData(KSPParticleEmitter pe)
{
MinSize = pe.minSize;
MaxSize = pe.maxSize;
Shape1D = pe.shape1D;
Shape2D = pe.shape2D;
Shape3D = pe.shape3D;
Force = pe.force;
LocalVelocity = pe.localVelocity;
}
}
Scale scale;
readonly Dictionary<KSPParticleEmitter, EmitterData> orig_scales = new Dictionary<KSPParticleEmitter, EmitterData>();
void UpdateParticleEmitter(KSPParticleEmitter pe)
{
if(pe == null) return;
if(!orig_scales.ContainsKey(pe))
orig_scales[pe] = new EmitterData(pe);
var ed = orig_scales[pe];
pe.minSize = ed.MinSize * scale;
pe.maxSize = ed.MaxSize * scale;
pe.shape1D = ed.Shape1D * scale;
pe.shape2D = ed.Shape2D * scale;
pe.shape3D = ed.Shape3D * scale;
pe.force = ed.Force * scale;
pe.localVelocity = ed.LocalVelocity * scale;
}
public override void OnUpdate()
{
if(scale == null) return;
var emitters = part.gameObject.GetComponentsInChildren<KSPParticleEmitter>();
if(emitters == null) return;
emitters.ForEach(UpdateParticleEmitter);
scale = null;
}
public override void OnRescale(Scale scale)
{
if(!enabled)
return;
if(part.FindModelComponent<KSPParticleEmitter>() != null ||
part.GetComponents<EffectBehaviour>()
.Any(e => e is ModelMultiParticleFX || e is ModelParticleFX))
this.scale = scale;
}
}
public class ResourcesUpdater : PartUpdater, IPartCostModifier
{
private readonly HashSet<int> baseResources = new HashSet<int>();
private float resourcesCost, baseResourcesCost;
protected override bool Init()
{
if(!base.Init())
return false;
resourcesCost = 0;
baseResourcesCost = 0;
baseResources.Clear();
foreach(var resource in base_part.Resources)
{
var cost = (float)resource.maxAmount * resource.info.unitCost;
baseResources.Add(resource.info.id);
baseResourcesCost += cost;
resourcesCost += cost;
}
return baseResources.Count > 0;
}
public override void OnRescale(Scale scale)
{
if(!enabled)
return;
//no need to update resources on start
//as they are persistent; less calculations
if(scale.FirstTime)
return;
resourcesCost = 0;
foreach(PartResource r in part.Resources)
{
if(!baseResources.Contains(r.info.id))
continue;
var surface = r.resourceName == "AblativeShielding" || r.resourceName == "Ablator";
var s = surface ? scale.relative.quad : scale.relative.volume;
r.maxAmount *= s;
r.amount *= s;
resourcesCost += (float)r.maxAmount * r.info.unitCost;
}
}
public float GetModuleCost(float defaultCost, ModifierStagingSituation sit) =>
resourcesCost - baseResourcesCost;
public ModifierChangeWhen GetModuleCostChangeWhen() => ModifierChangeWhen.CONSTANTLY;
}
public class RCS_Updater : ModuleUpdater<ModuleRCS>
{
[KSPField(isPersistant = false, guiActiveEditor = true, guiActive = true, guiName = "Thrust")]
public string thrustDisplay;
string all_thrusts()
{
return modules
.Aggregate("", (s, mp) => s + mp.module.thrusterPower + ", ")
.Trim(", ".ToCharArray());
}
public override void OnStart(StartState state)
{
base.OnStart(state);
thrustDisplay = all_thrusts();
}
public override void OnRescale(Scale scale)
{
if(!enabled)
return;
base.OnRescale(scale);
thrustDisplay = all_thrusts();
}
protected override void on_rescale(ModulePair<ModuleRCS> mp, Scale scale)
{ mp.module.thrusterPower = mp.base_module.thrusterPower * scale.absolute.quad; }
}
public class DockingNodeUpdater : ModuleUpdater<ModuleDockingNode>
{
protected override void on_rescale(ModulePair<ModuleDockingNode> mp, Scale scale)
{
AttachNode node = part.FindAttachNode(mp.module.referenceAttachNode);
if(node == null) return;
if(mp.module.nodeType.StartsWith("size"))
mp.module.nodeType = string.Format("size{0}", node.size);
}
}
public class ReactionWheelUpdater : ModuleUpdater<ModuleReactionWheel>
{
protected override void on_rescale(ModulePair<ModuleReactionWheel> mp, Scale scale)
{
mp.module.PitchTorque = mp.base_module.PitchTorque * scale.absolute.quad * scale.absolute.aspect;
mp.module.YawTorque = mp.base_module.YawTorque * scale.absolute.quad * scale.absolute.aspect;
mp.module.RollTorque = mp.base_module.RollTorque * scale.absolute.quad * scale.absolute.aspect;
var input_resources = mp.base_module.resHandler.inputResources.ToDictionary(r => r.name);
mp.module.resHandler.inputResources.ForEach(r => r.rate = input_resources[r.name].rate * scale.absolute.quad * scale.absolute.aspect);
}
}
public class GeneratorUpdater : ModuleUpdater<ModuleGenerator>
{
protected override void on_rescale(ModulePair<ModuleGenerator> mp, Scale scale)
{
var input_resources = mp.base_module.resHandler.inputResources.ToDictionary(r => r.name);
var output_resources = mp.base_module.resHandler.inputResources.ToDictionary(r => r.name);
mp.module.resHandler.inputResources.ForEach(r => r.rate = input_resources[r.name].rate * scale.absolute.volume);
mp.module.resHandler.inputResources.ForEach(r => r.rate = output_resources[r.name].rate * scale.absolute.volume);
}
}
public class AsteroidDrillUpdater : ModuleUpdater<ModuleAsteroidDrill>
{
protected override void on_rescale(ModulePair<ModuleAsteroidDrill> mp, Scale scale)
{
mp.module.PowerConsumption = mp.base_module.PowerConsumption * scale;
mp.module.Efficiency = mp.base_module.Efficiency * scale;
}
}
public class ResourceHarvesterUpdater : ModuleUpdater<ModuleResourceHarvester>
{
protected override void on_rescale(ModulePair<ModuleResourceHarvester> mp, Scale scale)
{
mp.module.Efficiency = mp.base_module.Efficiency * scale;
var def_ratios = mp.base_module.inputList.ToDictionary(r => r.ResourceName);
mp.module.inputList.ForEach(r => r.Ratio = def_ratios[r.ResourceName].Ratio * scale);
}
}
public class ResourceConverterUpdater : ModuleUpdater<ModuleResourceConverter>
{
static void scale_res_list(List<ResourceRatio> cur, List<ResourceRatio> def, float scale)
{
var def_ratios = def.ToDictionary(r => r.ResourceName);
cur.ForEach(r => r.Ratio = def_ratios[r.ResourceName].Ratio * scale);
}
protected override void on_rescale(ModulePair<ModuleResourceConverter> mp, Scale scale)
{
scale_res_list(mp.module.Recipe.Inputs, mp.base_module.Recipe.Inputs, scale);
scale_res_list(mp.module.Recipe.Outputs, mp.base_module.Recipe.Outputs, scale);
scale_res_list(mp.module.Recipe.Requirements, mp.base_module.Recipe.Requirements, scale);
}
}
public class SolarPanelUpdater : ModuleUpdater<ModuleDeployableSolarPanel>
{
protected override void on_rescale(ModulePair<ModuleDeployableSolarPanel> mp, Scale scale)
{
mp.module.chargeRate = mp.base_module.chargeRate * scale.absolute.quad * scale.absolute.aspect;
mp.module.flowRate = mp.base_module.flowRate * scale.absolute.quad * scale.absolute.aspect;
}
}
public class DecoupleUpdater : ModuleUpdater<ModuleDecouple>
{
protected override void on_rescale(ModulePair<ModuleDecouple> mp, Scale scale)
{ mp.module.ejectionForce = mp.base_module.ejectionForce * scale.absolute.cube; }
}
public class EngineUpdater : ModuleUpdater<ModuleEngines>
{
[KSPField(isPersistant = false, guiActiveEditor = true, guiActive = false, guiName = "Max. Thrust")]
public string thrustDisplay;
string all_thrusts()
{
return modules
.Aggregate("", (s, mp) => s + mp.module.maxThrust + ", ")
.Trim(", ".ToCharArray());
}
public override void OnStart(StartState state)
{
base.OnStart(state);
thrustDisplay = all_thrusts();
}
public override void OnRescale(Scale scale)
{
if(!enabled)
return;
base.OnRescale(scale);
thrustDisplay = all_thrusts();
}
protected override void on_rescale(ModulePair<ModuleEngines> mp, Scale scale)
{
mp.module.minThrust = mp.base_module.minThrust * scale.absolute.quad;
mp.module.maxThrust = mp.base_module.maxThrust * scale.absolute.quad;
// mp.module.heatProduction = mp.base_module.heatProduction * scale.absolute;
}
}
public class ResourceIntakeUpdater : ModuleUpdater<ModuleResourceIntake>
{
protected override void on_rescale(ModulePair<ModuleResourceIntake> mp, Scale scale)
{ mp.module.area = mp.base_module.area * scale.absolute.quad; }
}
public class JettisonUpdater : ModuleUpdater<ModuleJettison>
{
private void update_fairings(ModulePair<ModuleJettison> mp, Scale scale)
{
if(mp.module.jettisonTransform == null)
return;
var p = mp.module.jettisonTransform.parent.gameObject.GetComponent<Part>();
if(p == null || p == mp.module.part)
return;
if(!mp.orig_data.TryGetValue("local_scale", out var orig_scale)
|| !(orig_scale is Vector3))
{
orig_scale = mp.module.jettisonTransform.localScale;
mp.orig_data["local_scale"] = orig_scale;
}
mp.module.jettisonTransform.localScale = scale.ScaleVector((Vector3)orig_scale);
}
protected override void on_rescale(ModulePair<ModuleJettison> mp, Scale scale)
{
mp.module.jettisonedObjectMass =
mp.base_module.jettisonedObjectMass * scale.absolute.volume;
mp.module.jettisonForce = mp.base_module.jettisonForce * scale.absolute.volume;
update_fairings(mp, scale);
}
}
public class ControlSurfaceUpdater : ModuleUpdater<ModuleControlSurface>
{
protected override void on_rescale(ModulePair<ModuleControlSurface> mp, Scale scale)
{
mp.module.ctrlSurfaceArea = mp.base_module.ctrlSurfaceArea * scale.absolute.quad;
}
}
public class ATMagneticDamperUpdater : ModuleUpdater<ATMagneticDamper>
{
protected override void on_rescale(ModulePair<ATMagneticDamper> mp, Scale scale)
{
var s = scale.absolute.quad * scale.absolute.aspect;
mp.module.MaxForce = mp.base_module.MaxForce * s;
mp.module.MaxEnergyConsumption = mp.base_module.MaxEnergyConsumption * s;
mp.module.IdleEnergyConsumption = mp.base_module.IdleEnergyConsumption * s;
}
}
}