forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleSPU.cs
More file actions
175 lines (156 loc) · 5.81 KB
/
Copy pathModuleSPU.cs
File metadata and controls
175 lines (156 loc) · 5.81 KB
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
using System;
using System.Linq;
using System.Text;
using UnityEngine;
namespace RemoteTech
{
[KSPModule("Signal Processor")]
public class ModuleSPU : PartModule, ISignalProcessor
{
public String Name { get { return String.Format("ModuleSPU({0})", VesselName); } }
public String VesselName { get { return vessel.vesselName; } set { vessel.vesselName = value; } }
public bool VesselLoaded { get { return vessel.loaded; } }
public Guid Guid { get { return mRegisteredId; } }
public Vector3 Position { get { return vessel.GetWorldPos3D(); } }
public CelestialBody Body { get { return vessel.mainBody; } }
public bool Visible { get { return MapViewFiltering.CheckAgainstFilter(vessel); } }
public bool Powered { get { return IsRTPowered; } }
public bool IsCommandStation { get { return IsRTPowered && IsRTCommandStation && vessel.GetVesselCrew().Count >= 6; } }
public FlightComputer FlightComputer { get; private set; }
public Vessel Vessel { get { return vessel; } }
public bool IsMaster { get { return Satellite != null && Satellite.SignalProcessor == (ISignalProcessor) this; } }
private VesselSatellite Satellite { get { return RTCore.Instance.Satellites[mRegisteredId]; } }
[KSPField(isPersistant = true)]
public bool
IsRTPowered = false,
IsRTSignalProcessor = true,
IsRTCommandStation = false;
[KSPField]
public bool
ShowGUI_Status = true,
ShowEditor_Type = true;
[KSPField(guiName = "SPU", guiActive = true)]
public String GUI_Status;
private enum State
{
Operational,
ParentDefect,
NoConnection
}
private Guid mRegisteredId;
public override String GetInfo()
{
if (!ShowEditor_Type) return String.Empty;
return IsRTCommandStation ? "Remote Command capable (6+ crew)" : "Remote Control capable";
}
public override void OnStart(StartState state)
{
if (state != StartState.Editor)
{
GameEvents.onVesselWasModified.Add(OnVesselModified);
GameEvents.onPartUndock.Add(OnPartUndock);
mRegisteredId = vessel.id;
RTCore.Instance.Satellites.Register(vessel, this);
FlightComputer = new FlightComputer(this);
}
Fields["GUI_Status"].guiActive = ShowGUI_Status;
}
public void OnDestroy()
{
RTLog.Notify("ModuleSPU: OnDestroy");
GameEvents.onVesselWasModified.Remove(OnVesselModified);
GameEvents.onPartUndock.Remove(OnPartUndock);
if (RTCore.Instance != null)
{
RTCore.Instance.Satellites.Unregister(mRegisteredId, this);
mRegisteredId = Guid.Empty;
}
if (FlightComputer != null) FlightComputer.Dispose();
}
private State UpdateControlState()
{
IsRTPowered = part.isControlSource;
if (!RTCore.Instance)
{
return State.Operational;
}
if (!IsRTPowered)
{
return State.ParentDefect;
}
if (Satellite == null || !RTCore.Instance.Network[Satellite].Any())
{
return State.NoConnection;
}
return State.Operational;
}
public void Update()
{
if (FlightComputer != null) FlightComputer.OnUpdate();
}
public void FixedUpdate()
{
if (FlightComputer != null) FlightComputer.OnFixedUpdate();
HookPartMenus();
switch (UpdateControlState())
{
case State.Operational:
GUI_Status = "Operational.";
break;
case State.ParentDefect:
case State.NoConnection:
GUI_Status = "No connection.";
break;
}
}
public void OnPartUndock(Part p)
{
OnVesselModified(p.vessel);
}
public void OnVesselModified(Vessel v)
{
if ((mRegisteredId != vessel.id))
{
RTCore.Instance.Satellites.Unregister(mRegisteredId, this);
mRegisteredId = vessel.id;
RTCore.Instance.Satellites.Register(vessel, this);
}
}
public void HookPartMenus()
{
UIPartActionMenuPatcher.Wrap(vessel, (e, ignore_delay) =>
{
var v = FlightGlobals.ActiveVessel;
if (v == null || v.isEVA || RTCore.Instance == null)
{
e.Invoke();
return;
}
var vs = RTCore.Instance.Satellites[v];
if (vs == null || vs.HasLocalControl)
{
e.Invoke();
}
else if (vs.FlightComputer != null && vs.FlightComputer.InputAllowed)
{
if (ignore_delay)
{
e.Invoke();
}
else
{
vs.SignalProcessor.FlightComputer.Enqueue(EventCommand.Event(e));
}
}
else
{
ScreenMessages.PostScreenMessage(new ScreenMessage("No connection to send command on.", 4.0f, ScreenMessageStyle.UPPER_LEFT));
}
});
}
public override string ToString()
{
return String.Format("ModuleSPU({0}, {1})", Vessel != null ? Vessel.vesselName : "null", mRegisteredId);
}
}
}