forked from allista/ThrottleControlledAvionics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVesselWrapper.cs
369 lines (335 loc) · 11.3 KB
/
VesselWrapper.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
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
// or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using KSP.UI.Screens;
using AT_Utils;
namespace ThrottleControlledAvionics
{
public class VesselWrapper
{
public ModuleTCA TCA { get; private set; }
public VesselConfig CFG { get; private set; }
internal Globals GLB { get { return Globals.Instance; } }
public TCAState State;
public void SetState(TCAState state) { State |= state; }
public bool IsStateSet(TCAState state) { return (State & state) == state; }
public Vessel vessel { get; private set; }
public Transform refT; //transform of the controller-part
public PhysicalProps Physics;
public AltitudeProps Altitude;
public VerticalSpeedProps VerticalSpeed;
public HorizontalSpeedProps HorizontalSpeed;
public EnginesProps Engines;
public ControlProps Controls;
public InfoProps Info;
public OnPlanetProps OnPlanetParams;
public TorqueProps Torque;
public GeometryProps Geometry;
List<VesselProps> AllPros = new List<VesselProps>();
static List<FieldInfo> AllPropFields = typeof(VesselWrapper)
.GetFields(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public)
.Where(fi => fi.FieldType.IsSubclassOf(typeof(VesselProps))).ToList();
//state
public CelestialBody Body { get { return vessel.mainBody; } }
public Orbit orbit { get { return vessel.orbit; } }
public bool OnPlanet { get; private set; }
public bool InOrbit { get; private set; }
public bool IsActiveVessel { get; private set; }
public bool LandedOrSplashed { get { return vessel.LandedOrSplashed; } }
public ITargetable Target { get { return vessel.targetObject; } set { vessel.targetObject = value; } }
public Vessel TargetVessel { get { return vessel.targetObject == null? null : vessel.targetObject.GetVessel(); } }
public bool HasTarget { get { return vessel.targetObject != null && !(vessel.targetObject is CelestialBody); } }
public bool HasManeuverNode
{
get
{
return vessel.patchedConicSolver != null &&
vessel.patchedConicSolver.maneuverNodes.Count > 0 &&
vessel.patchedConicSolver.maneuverNodes[0] != null;
}
}
public ManeuverNode FirstManeuverNode { get { return vessel.patchedConicSolver.maneuverNodes[0]; } }
public Vessel.Situations Situation { get { return vessel.situation; } }
public void SetTarget(WayPoint wp = null)
{
CFG.Target = wp;
var t = wp == null? null : wp.GetTarget();
if(t != null && IsActiveVessel)
Utils.Message("Target: {0}", t.GetName());
Target = t;
}
#if DEBUG
public void LogF(string msg, params object[] args)
{ vessel.Log("{0}", Utils.Format(msg, args)); }
public void LogFST(string msg, params object[] args)
{ vessel.Log("{0}\n{1}", Utils.Format(msg, args), DebugUtils.getStacktrace(1)); }
#endif
#region Utils
public void Log(string msg, params object[] args) { vessel.Log(msg, args); }
public Vector3 LocalDir(Vector3 worldV) { return refT.InverseTransformDirection(worldV); }
public Vector3 WorldDir(Vector3 localV) { return refT.TransformDirection(localV); }
public Vector3d PredictedSrfVelocity(float time) { return vessel.srf_velocity+vessel.acceleration*time; }
#endregion
#region VesselRanges
static List<FieldInfo> situation_ranges = typeof(VesselRanges)
.GetFields(BindingFlags.Public|BindingFlags.Instance)
.Where(fi => fi.FieldType.Equals(typeof(VesselRanges.Situation))).ToList();
VesselRanges saved_ranges;
public void SetUnpackDistance(float distance)
{
if(saved_ranges == null)
saved_ranges = new VesselRanges(vessel.vesselRanges);
foreach(var fi in situation_ranges)
{
var sit = fi.GetValue(vessel.vesselRanges) as VesselRanges.Situation;
if(sit == null) continue;
sit.pack = distance*1.5f;
sit.unpack = distance;
sit.unload = distance*2.5f;
sit.load = distance*2f;
}
}
public void RestoreUnpackDistance()
{
if(saved_ranges == null) return;
vessel.vesselRanges = new VesselRanges(saved_ranges);
saved_ranges = null;
}
#endregion
void create_props()
{
AllPros.Clear();
foreach(var fi in AllPropFields)
{
var constructor = fi.FieldType.GetConstructor(new [] {typeof(VesselWrapper)});
if(constructor == null)
throw new MissingMemberException(string.Format("No suitable constructor found for {0}", fi.FieldType.Name));
var prop = constructor.Invoke(new [] {this}) as VesselProps;
if(prop != null) AllPros.Add(prop);
fi.SetValue(this, prop);
}
}
public VesselWrapper(ModuleTCA tca)
{
TCA = tca;
CFG = tca.CFG;
vessel = TCA.vessel;
create_props();
OnPlanet = _OnPlanet();
InOrbit = _InOrbit();
UpdateState();
UpdatePhysics();
UpdateParts();
}
public void Init()
{
Controls.UpdateRefTransform();
UpdateCommons();
OnPlanetParams.Update();
Geometry.Update();
vessel.OnAutopilotUpdate += UpdateAutopilotInfo;
}
public void ConnectAutopilotOutput()
{ vessel.OnAutopilotUpdate += ApplyAutopilotSteering; }
public void Reset()
{
vessel.OnAutopilotUpdate -= UpdateAutopilotInfo;
vessel.OnAutopilotUpdate -= ApplyAutopilotSteering;
RestoreUnpackDistance();
}
bool _OnPlanet()
{
return (vessel.situation != Vessel.Situations.DOCKED &&
vessel.situation != Vessel.Situations.ORBITING &&
vessel.situation != Vessel.Situations.ESCAPING);
}
bool _InOrbit()
{
return vessel.situation == Vessel.Situations.ORBITING ||
vessel.situation == Vessel.Situations.SUB_ORBITAL;
}
public bool AutopilotDisabled;
public bool HasUserInput;
public void UpdateAutopilotInfo(FlightCtrlState s)
{
if(!CFG.Enabled) return;
Controls.GimbalLimit = 100;
HasUserInput =
!Mathfx.Approx(s.pitch, s.pitchTrim, 0.01f) ||
!Mathfx.Approx(s.roll, s.rollTrim, 0.01f) ||
!Mathfx.Approx(s.yaw, s.yawTrim, 0.01f);
AutopilotDisabled = HasUserInput;
}
public void ApplyAutopilotSteering(FlightCtrlState s)
{
if(!CFG.Enabled || Controls.AutopilotSteering.IsZero()) return;
s.pitch = Utils.Clamp(Controls.AutopilotSteering.x, -1, 1);
s.roll = Utils.Clamp(Controls.AutopilotSteering.y, -1, 1);
s.yaw = Utils.Clamp(Controls.AutopilotSteering.z, -1, 1);
Controls.AutopilotSteering = Vector3.zero;
}
public void UpdateState()
{
//update onPlanet state
var on_planet = _OnPlanet();
var in_orbit = _InOrbit();
if(on_planet != OnPlanet)
{
CFG.EnginesProfiles.OnPlanetChanged(on_planet);
if(!on_planet)
{
if(CFG.BlockThrottle)
{
var THR = TCA.GetModule<ThrottleControl>();
if(THR != null) THR.Throttle = 0f;
}
CFG.DisableVSC();
CFG.Nav.Off();
CFG.HF.Off();
}
}
OnPlanet = on_planet;
InOrbit = in_orbit;
IsActiveVessel = vessel != null && vessel == FlightGlobals.ActiveVessel;
}
public void UpdatePhysics()
{
Physics.Update();
Altitude.Update();
if(CFG.Target != null)
CFG.Target.Update(this);
}
public void UpdateCommons()
{
Engines.Sort();
Engines.Update();
Torque.Update();
Controls.Update();
}
public void ClearFrameState()
{
AllPros.ForEach(p => p.ClearFrameState());
}
public void UpdateOnPlanetStats()
{
if(!OnPlanet) return;
VerticalSpeed.Update();
HorizontalSpeed.Update();
OnPlanetParams.Update();
}
public static bool AddModule<M>(PartModule m, List<M> db)
where M : PartModule
{
var module = m as M;
if(module == null) return false;
db.Add(module);
return true;
}
public void UpdateParts()
{
EngineWrapper.ThrustPI.setMaster(CFG.Engines);
Engines.Clear(); Torque.Wheels.Clear();
OnPlanetParams.Parachutes.Clear();
Physics.AngularDrag = 0;
var drag_parts = 0;
var parts_count = vessel.Parts.Count;
for(int i = 0; i < parts_count; i++)
{
Part p = vessel.Parts[i];
if(p.angularDragByFI) { Physics.AngularDrag += p.angularDrag; drag_parts += 1; }
for(int j = 0, pModulesCount = p.Modules.Count; j < pModulesCount; j++)
{
var module = p.Modules[j];
if(Engines.Add(module as ModuleEngines)) continue;
if(Engines.Add(module as ModuleRCS)) continue;
if(AddModule(module, Torque.Wheels)) continue;
if(AddModule(module, OnPlanetParams.Parachutes)) continue;
}
}
Physics.AngularDrag /= drag_parts;
if(CFG.EnginesProfiles.Empty) CFG.EnginesProfiles.AddProfile(Engines.All);
else if(CFG.Enabled && TCA.ProfileSyncAllowed) CFG.ActiveProfile.Update(Engines.All);
}
bool stage_is_empty(int stage)
{
var cur_parts = vessel.parts.Where(p => p.hasStagingIcon && p.inverseStage == stage).ToList();
// LogF("{} stagable parts at stage {}", cur_parts.Count, stage);//debug
return cur_parts.Count == 0 || cur_parts.All(p => p.State == PartStates.ACTIVE);
}
void activate_next_stage()
{
var next_stage = vessel.currentStage;
while(next_stage >= 0 && stage_is_empty(next_stage)) next_stage--;
if(next_stage < 0) return;
// Log(vessel.parts.Aggregate("\n", (s, p) => s+Utils.Format("{}: {}, stage {}\n", p.Title(), p.State, p.inverseStage)));//debug
// LogF("current stage {}, next stage {}, next engines {}", vessel.currentStage, next_stage, Engines.NearestEnginedStage);//debug
if(IsActiveVessel)
{
StageManager.ActivateStage(next_stage);
vessel.ActionGroups.ToggleGroup(KSPActionGroup.Stage);
if(ResourceDisplay.Instance != null)
ResourceDisplay.Instance.isDirty = true;
}
else
{
GameEvents.onStageActivate.Fire(next_stage);
vessel.parts.ForEach(p => p.activate(next_stage, vessel));
vessel.currentStage = next_stage;
vessel.ActionGroups.ToggleGroup(KSPActionGroup.Stage);
}
}
readonly ActionDamper next_cooldown = new ActionDamper(0.5);
public void ActivateNextStage() { next_cooldown.Run(activate_next_stage); }
public void GearOn(bool enable = true)
{
if(!CFG.AutoGear) return;
if(vessel.ActionGroups[KSPActionGroup.Gear] != enable)
vessel.ActionGroups.SetGroup(KSPActionGroup.Gear, enable);
}
public void BrakesOn(bool enable = true)
{
if(!CFG.AutoBrakes) return;
if(vessel.ActionGroups[KSPActionGroup.Brakes] != enable)
vessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, enable);
}
}
/// <summary>
/// Binary flags of TCA state.
/// They should to be checked in this particular order, as they are set sequentially:
/// If a previous flag is not set, the next ones are not either.
/// </summary>
[Flags] public enum TCAState
{
//basic state
Disabled = 0,
Enabled = 1 << 0,
HaveEC = 1 << 1,
HaveActiveEngines = 1 << 2,
Unoptimized = 1 << 3,
//vertical flight
VerticalSpeedControl = 1 << 4,
AltitudeControl = 1 << 5,
LoosingAltitude = 1 << 6,
//cruise radar
ObstacleAhead = 1 << 7,
GroundCollision = 1 << 8,
Ascending = 1 << 9,
//autopilot
VTOLAssist = 1 << 10,
StabilizeFlight = 1 << 11,
//composite
Nominal = Enabled | HaveEC | HaveActiveEngines,
NoActiveEngines = Enabled | HaveEC,
NoEC = Enabled,
}
}