-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEngineCommander.cs
80 lines (69 loc) · 2.59 KB
/
EngineCommander.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace MajiirKerbalLib
{
internal class EngineCommander
{
#region Static utility methods
public static float UpdateThrust<T>(float mainThrottle, T engine) where T : global::Part, IEngine
{
if (engine.vessel == null)
{
MonoBehaviour.print(String.Format("[MajiirKerbalLib] Null vessel for {0}", engine.name));
return mainThrottle;
}
var commander = VesselCommander.GetInstance(engine.vessel).EngineCommander;
if (!commander.IsActive)
{
return mainThrottle;
}
return commander.Update(mainThrottle, engine);
}
#endregion
public bool IsActive { get; set; }
private int lastFrame = -1;
private Dictionary<IEngine, float> throttleValues;
public EngineCommander()
{
IsActive = true;
}
private float Update<T>(float mainThrottle, T targetEngine) where T : global::Part, IEngine
{
if (targetEngine.State != PartStates.ACTIVE) { return mainThrottle; }
if (Time.frameCount != lastFrame)
{
lastFrame = Time.frameCount;
ComputeThrottles(mainThrottle, targetEngine.vessel);
}
if (throttleValues.ContainsKey(targetEngine))
{
return throttleValues[targetEngine];
}
else
{
MonoBehaviour.print(String.Format("[MajiirKerbalLib] Couldn't find throttle level for {0}", targetEngine.name));
return mainThrottle;
}
}
private void ComputeThrottles(float mainThrottle, Vessel vessel)
{
var engines = vessel.parts.Where(p => p.State == PartStates.ACTIVE).OfType<IEngine>();
var thrust = engines.Sum(e => e.MaxThrust);
thrust *= mainThrottle;
throttleValues = new Dictionary<IEngine, float>();
foreach (var group in engines.ToLookup(e => e.RealIsp).OrderByDescending(g => g.Key))
{
var availableThrust = group.Sum(e => e.MaxThrust);
var groupThrust = Math.Min(availableThrust, thrust);
thrust -= groupThrust;
var throttle = availableThrust > 0 ? groupThrust / availableThrust : 0;
foreach (var engine in group)
{
throttleValues[engine] = throttle;
}
}
}
}
}