-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoverCruiseControl.cs
262 lines (225 loc) · 6.76 KB
/
RoverCruiseControl.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
/*
* Created by SharpDevelop.
* User: Bernhard
* Date: 21.03.2013
* Time: 01:34
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using UnityEngine;
public class RCCPartlessLoader : KSP.Testing.UnitTest {
public RCCPartlessLoader() : base() {
//Called at the first loading screen
//When you start the game.
RCCLoader.Initialize();
}
}
public static class RCCLoader {
private static UnityEngine.GameObject MyMonobehaviourObject;
public static void Initialize() {
MyMonobehaviourObject = new UnityEngine.GameObject("ModuleAttacherLoader", new Type[] {typeof(RoverCruiseControlAttacher)});
UnityEngine.GameObject.DontDestroyOnLoad(MyMonobehaviourObject);
}
}
public class RoverCruiseControlAttacher : UnityEngine.MonoBehaviour {
private Vessel last;
public void Update() {
if (!HighLogic.LoadedSceneIsFlight || FlightGlobals.fetch == null) {
return;
}
var ship = FlightGlobals.ActiveVessel;
// try {
if (ship != null && ship.state == Vessel.State.ACTIVE && last != ship) {
foreach (Part p in ship.Parts) {
if (p.Modules.Contains("ModuleCommand") && !p.Modules.Contains("RoverCruiseControl")) {
p.AddModule("RoverCruiseControl");
RoverCruiseControl.print("attached to " + p.name);
}
}
last = ship;
}
// }
// catch (Exception ex) {
// print(ex.Message);
// }
}
}
public class RoverCruiseControl : PartModule {
public static void print(string message) {
MonoBehaviour.print("RCC: " + message);
}
public bool Active = false;
public bool Wheels = false;
public float upperSpeedLimit = 0;
public float lowerSpeedLimit = 1000;
public float upperTorqueLimit = 0;
public float lowerTorqueLimit = 1000;
private int parts;
private List<RoverCruiseControl> controllers = new List<RoverCruiseControl>();
private List<ModuleEngines> engines = new List<ModuleEngines>();
[KSPEvent(guiActive = false, guiName = "Activate Cruise Control", active = true)]
public void Activate() {
ChangeState(true);
UpdateOthers();
}
[KSPEvent(guiActive = false, guiName = "Deactivate Cruise Control", active = false)]
public void Deactivate() {
ChangeState(false);
UpdateOthers();
}
[KSPAction("Toggle Cruise Control")]
public void Toggle(KSPActionParam param) {
ChangeState(!Active);
UpdateOthers();
}
[KSPAction("Activate Cruise Control")]
public void Activate(KSPActionParam param) {
Activate();
}
[KSPAction("Deactivate Cruise Control")]
public void Deactivate(KSPActionParam param) {
Deactivate();
}
public void ChangeState(bool nowActive) {
Active = nowActive;
Events["Activate"].active = !Active;
Events["Deactivate"].active = Active;
}
private void UpdateOthers() {
controllers.ForEach(c => c.ChangeState(Active));
}
private float ActiveThrust() {
float thrust = 0;
foreach (ModuleEngines e in engines) {
if (e.getIgnitionState && !e.getFlameoutState) {
thrust += e.maxThrust;
}
}
return thrust;
}
private void ThrottleEngine(ModuleEngines engine, float throttle) {
if (engine.useEngineResponseTime)
{
if (engine.currentThrottle < throttle)
{
engine.currentThrottle = Mathf.Lerp(engine.currentThrottle, throttle, engine.engineAccelerationSpeed * TimeWarp.fixedDeltaTime);
}
else
{
engine.currentThrottle = Mathf.Lerp(engine.currentThrottle, throttle, engine.engineDecelerationSpeed * TimeWarp.fixedDeltaTime);
}
}
else
{
engine.currentThrottle = throttle;
}
}
private void ScanVessel() {
controllers.Clear();
engines.Clear();
upperTorqueLimit = upperSpeedLimit = 0;
lowerTorqueLimit = lowerSpeedLimit = 1000;
parts = vessel.Parts.Count;
Wheels = false;
foreach (Part p in vessel.Parts) {
foreach (PartModule pm in p.Modules) {
if (pm is ModuleWheel) {
var wheel = (ModuleWheel)pm;
float speed = FindTorque(wheel);
// print("Speed: " + speed);
upperTorqueLimit = Mathf.Max(upperTorqueLimit, speed);
lowerTorqueLimit = Mathf.Min(lowerTorqueLimit, speed);
upperSpeedLimit = Mathf.Max(upperSpeedLimit, wheel.overSpeedDamage);
lowerSpeedLimit = Mathf.Min(lowerSpeedLimit, wheel.overSpeedDamage);
Wheels = true;
}
else if (pm is RoverCruiseControl) {
controllers.Add((RoverCruiseControl)pm);
}
else if (pm is ModuleEngines) {
engines.Add((ModuleEngines)pm);
}
}
}
if (controllers.Count > 0 && this == controllers[0]) {
print("Controllers: " + controllers.Count);
}
Events["Activate"].guiActive = Events["Deactivate"].guiActive = Wheels;
}
public float FindTorque(ModuleWheel w) {
var fc = w.torqueCurve;
var t = 0f;
var res = fc.Evaluate(t);
while (res > 0) {
t += 10;
res = fc.Evaluate(t);
}
while (res == 0) {
t--;
res = fc.Evaluate(t);
}
while (res > 0) {
t += 0.1f;
res = fc.Evaluate(t);
}
return t;
}
private void Init() {
if (vessel != null) {
vessel.OnFlyByWire -= OnFlyByWire;
vessel.OnFlyByWire += OnFlyByWire;
ScanVessel();
}
}
// public override void OnAwake() {
// Init();
// base.OnAwake();
// }
//
// public override void OnActive() {
// Init();
// }
//
// public override void OnInactive() {
// Init();
// }
//
public override void OnStart(PartModule.StartState state) {
print("Start: " + vessel.ToString());
Init();
base.OnStart(state);
}
public void OnDestroy() {
if (vessel != null) {
vessel.OnFlyByWire -= OnFlyByWire;
}
}
public void Update() {
if (vessel.Parts.Count != parts || controllers.Count == 0) { Init(); }
if (!Wheels || controllers.Count == 0 || this != controllers[0]) { return; }
if (Input.GetKeyUp(KeyCode.O)) {
Toggle(null);
if (Active) {
var speed = (float)vessel.horizontalSrfSpeed;
FlightInputHandler.state.mainThrottle = speed / (ActiveThrust() > 0 ? upperSpeedLimit : upperTorqueLimit);
}
}
}
public void OnFlyByWire(FlightCtrlState fs) {
if (!Wheels || !Active || this != controllers[0]) { return; }
var speed = (float)vessel.horizontalSrfSpeed;
if (fs.wheelThrottle == 0) {
var diff = (((ActiveThrust() > 0 ? upperSpeedLimit : upperTorqueLimit) * fs.mainThrottle) - speed);
var throttle = Mathf.Clamp(diff / 125, 0, 1) + (fs.mainThrottle > 0 ? 0.01f : 0);
foreach(var e in engines) {
ThrottleEngine(e, throttle);
}
fs.wheelThrottle = Mathf.Clamp(diff * 5, 0, 1);
vessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, diff <= 0 || GameSettings.BRAKES.GetKey());
} else {
vessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, GameSettings.BRAKES.GetKey());
}
}
}