forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.cs
More file actions
271 lines (220 loc) · 10.4 KB
/
API.cs
File metadata and controls
271 lines (220 loc) · 10.4 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
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
using RemoteTech.RangeModel;
using RemoteTech.Modules;
using RemoteTech.SimpleTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using WrappedEvent = RemoteTech.FlightComputer.UIPartActionMenuPatcher.WrappedEvent;
namespace RemoteTech.API
{
public static class API
{
public static bool IsRemoteTechEnabled()
{
if (RTCore.Instance != null) return true;
return false;
}
public static bool HasLocalControl(Guid id)
{
var vessel = RTUtil.GetVesselById(id);
if (vessel == null) return false;
RTLog.Verbose("Flight: {0} HasLocalControl: {1}", RTLogLevel.API, id, vessel.HasLocalControl());
return vessel.HasLocalControl();
}
public static bool HasFlightComputer(Guid id)
{
if (RTCore.Instance == null) return false;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null) return false;
var hasFlightComputer = satellite.FlightComputer != null;
RTLog.Verbose("Flight: {0} HasFlightComputer: {1}", RTLogLevel.API, id, hasFlightComputer);
return hasFlightComputer;
}
public static void AddSanctionedPilot(Guid id, Action<FlightCtrlState> autopilot)
{
if (RTCore.Instance == null) return;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null || satellite.SignalProcessor == null) return;
foreach (var spu in satellite.SignalProcessors)
{
if (spu.FlightComputer == null || spu.FlightComputer.SanctionedPilots == null) continue;
if (spu.FlightComputer.SanctionedPilots.Contains(autopilot)) continue;
RTLog.Verbose("Flight: {0} Adding Sanctioned Pilot", RTLogLevel.API, id);
spu.FlightComputer.SanctionedPilots.Add(autopilot);
}
}
public static void RemoveSanctionedPilot(Guid id, Action<FlightCtrlState> autopilot)
{
if (RTCore.Instance == null) return;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null || satellite.SignalProcessor == null) return;
foreach (var spu in satellite.SignalProcessors)
{
if (spu.FlightComputer == null || spu.FlightComputer.SanctionedPilots == null) continue;
RTLog.Verbose("Flight: {0} Removing Sanctioned Pilot", RTLogLevel.API, id);
spu.FlightComputer.SanctionedPilots.Remove(autopilot);
}
}
public static bool HasAnyConnection(Guid id)
{
if (RTCore.Instance == null) return false;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null) return false;
var hasConnection = RTCore.Instance.Network[satellite].Any();
RTLog.Verbose("Flight: {0} Has Connection: {1}", RTLogLevel.API, id, hasConnection);
return hasConnection;
}
public static bool HasConnectionToKSC(Guid id)
{
if (RTCore.Instance == null) return false;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null) return false;
var connectedToKerbin = RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
RTLog.Verbose("Flight: {0} Has Connection to Kerbin: {1}", RTLogLevel.API, id, connectedToKerbin);
return connectedToKerbin;
}
public static bool AntennaHasConnection(Part part)
{
if (RTCore.Instance == null) return false;
var antennaModules = part.Modules.OfType<IAntenna>();
return antennaModules.Any(m => m.Connected);
}
public static Guid GetAntennaTarget(Part part) {
ModuleRTAntenna module = part.Modules.OfType<ModuleRTAntenna>().First();
if (module == null)
{
throw new ArgumentException();
}
return module.Target;
}
public static void SetAntennaTarget(Part part, Guid id) {
ModuleRTAntenna module = part.Modules.OfType<ModuleRTAntenna>().First();
if (module == null)
{
throw new ArgumentException();
}
module.Target = id;
}
public static IEnumerable<string> GetGroundStations()
{
return RTSettings.Instance.GroundStations.Select(s => ((ISatellite)s).Name);
}
public static Guid GetGroundStationGuid(String name)
{
MissionControlSatellite groundStation = RTSettings.Instance.GroundStations.Where(station => station.GetName().Equals(name)).FirstOrDefault();
if (groundStation == null)
{
return Guid.Empty;
}
return groundStation.mGuid;
}
public static Guid GetCelestialBodyGuid(CelestialBody celestialBody)
{
return RTUtil.Guid(celestialBody);
}
public static Guid GetNoTargetGuid() {
return new Guid(RTSettings.Instance.NoTargetGuid);
}
public static Guid GetActiveVesselGuid() {
return new Guid(RTSettings.Instance.ActiveVesselGuid);
}
public static double GetShortestSignalDelay(Guid id)
{
if (RTCore.Instance == null) return double.PositiveInfinity;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null) return double.PositiveInfinity;
if (!RTCore.Instance.Network[satellite].Any()) return double.PositiveInfinity;
var shortestDelay = RTCore.Instance.Network[satellite].Min().Delay;
RTLog.Verbose("Flight: Shortest signal delay from {0} to {1}", RTLogLevel.API, id, shortestDelay);
return shortestDelay;
}
public static double GetSignalDelayToKSC(Guid id)
{
if (RTCore.Instance == null) return double.PositiveInfinity;
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null) return double.PositiveInfinity;
if (!RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid))) return double.PositiveInfinity;
var signalDelaytoKerbin = RTCore.Instance.Network[satellite].Where(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid)).Min().Delay;
RTLog.Verbose("Connection from {0} to Kerbin Delay: {1}", RTLogLevel.API, id, signalDelaytoKerbin);
return signalDelaytoKerbin;
}
public static double GetSignalDelayToSatellite(Guid a, Guid b)
{
if (RTCore.Instance == null) return double.PositiveInfinity;
var satelliteA = RTCore.Instance.Satellites[a];
var satelliteB = RTCore.Instance.Satellites[b];
if (satelliteA == null || satelliteB == null) return double.PositiveInfinity;
Func<ISatellite, IEnumerable<NetworkLink<ISatellite>>> neighbors = RTCore.Instance.Network.FindNeighbors;
Func<ISatellite, NetworkLink<ISatellite>, double> cost = RangeModelExtensions.DistanceTo;
Func<ISatellite, ISatellite, double> heuristic = RangeModelExtensions.DistanceTo;
var path = NetworkPathfinder.Solve(satelliteA, satelliteB, neighbors, cost, heuristic);
var delayBetween = path.Delay;
RTLog.Verbose("Connection from {0} to {1} Delay: {2}", RTLogLevel.API, a, b, delayBetween);
return delayBetween;
}
//exposed method called by other mods, passing a ConfigNode to RemoteTech
public static bool QueueCommandToFlightComputer(ConfigNode externalData)
{
if (RTCore.Instance == null) return false;
//check we were actually passed a config node
if (externalData == null) return false;
// check our min values
if (!externalData.HasValue("GUIDString") && !externalData.HasValue("Executor") && !externalData.HasValue("ReflectionType"))
{
return false;
}
try
{
Guid externalVesselId = new Guid(externalData.GetValue("GUIDString"));
// you can only push a new external command if the vessel guid is the current active vessel
if (FlightGlobals.ActiveVessel.id != externalVesselId)
{
RTLog.Verbose("Passed Guid is not the active Vessels guid", RTLogLevel.API);
return false;
}
// maybe we should look if this vessel hasLocal controll or not. If so, we can execute the command
// immediately
// get the flightcomputer
FlightComputer.FlightComputer computer = RTCore.Instance.Satellites[externalVesselId].FlightComputer;
var extCmd = FlightComputer.Commands.ExternalAPICommand.FromExternal(externalData);
computer.Enqueue(extCmd);
return true;
}
catch(Exception ex)
{
RTLog.Verbose(ex.Message, RTLogLevel.API);
}
return false;
}
// this method provides a workaround for issue #437, it may be possible to remove it in the future
public static void InvokeOriginalEvent(BaseEvent e)
{
if (e is WrappedEvent)
{
WrappedEvent wrappedEvent = e as WrappedEvent;
wrappedEvent.InvokeOriginalEvent();
}
else
{
e.Invoke();
}
}
public static Guid AddGroundStation(string name, double latitude, double longitude, double height, int body)
{
RTLog.Notify ("Trying to add groundstation {0}", RTLogLevel.API, name);
Guid newStationId = RTSettings.Instance.AddGroundStation(name, latitude, longitude, height, body);
return newStationId;
}
public static bool RemoveGroundStation(Guid stationid)
{
RTLog.Notify ("Trying to remove groundstation {0}", RTLogLevel.API, stationid);
// do not allow to remove the default mission control
if (stationid.ToString ("N").Equals ("5105f5a9d62841c6ad4b21154e8fc488"))
{
RTLog.Notify ("Cannot remove KSC Mission Control!", RTLogLevel.API);
return false;
}
return RTSettings.Instance.RemoveGroundStation(stationid);
}
}
}