forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkManager.cs
More file actions
315 lines (271 loc) · 12 KB
/
Copy pathNetworkManager.cs
File metadata and controls
315 lines (271 loc) · 12 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using RemoteTech.Modules;
using RemoteTech.RangeModel;
using RemoteTech.SimpleTypes;
using UnityEngine;
namespace RemoteTech
{
/// <summary>
/// Class managing the satellites network.
/// Acts as a list of vessels in one or more networks.
/// </summary>
public partial class NetworkManager : IEnumerable<ISatellite>
{
public event Action<ISatellite, NetworkLink<ISatellite>> OnLinkAdd = delegate { };
public event Action<ISatellite, NetworkLink<ISatellite>> OnLinkRemove = delegate { };
public Dictionary<Guid, CelestialBody> Planets { get; private set; }
public Dictionary<Guid, ISatellite> GroundStations { get; private set; }
public Dictionary<Guid, List<NetworkLink<ISatellite>>> Graph { get; private set; }
public int Count { get { return RTCore.Instance.Satellites.Count + GroundStations.Count; } }
public static Guid ActiveVesselGuid = new Guid(RTSettings.Instance.ActiveVesselGuid);
public ISatellite this[Guid guid]
{
get
{
Vessel activeVessel = (FlightGlobals.ActiveVessel == null && HighLogic.LoadedScene == GameScenes.TRACKSTATION
? MapView.MapCamera.target.vessel : FlightGlobals.ActiveVessel);
ISatellite vesselSatellite = RTCore.Instance.Satellites[guid];
ISatellite activeSatellite = (guid == ActiveVesselGuid ? RTCore.Instance.Satellites[activeVessel] : null);
ISatellite groundSatellite = (GroundStations.ContainsKey(guid) ? GroundStations[guid] : null);
return vesselSatellite ?? activeSatellite ?? groundSatellite;
}
}
public List<NetworkRoute<ISatellite>> this[ISatellite sat]
{
get
{
if (sat == null) return new List<NetworkRoute<ISatellite>>();
return mConnectionCache.ContainsKey(sat) ? mConnectionCache[sat] : new List<NetworkRoute<ISatellite>>();
}
}
private const int REFRESH_TICKS = 50;
private int mTick;
private int mTickIndex;
private Dictionary<ISatellite, List<NetworkRoute<ISatellite>>> mConnectionCache = new Dictionary<ISatellite, List<NetworkRoute<ISatellite>>>();
public NetworkManager()
{
Graph = new Dictionary<Guid, List<NetworkLink<ISatellite>>>();
// Load all planets into a dictionary;
Planets = new Dictionary<Guid, CelestialBody>();
foreach (CelestialBody cb in FlightGlobals.Bodies)
{
Planets[cb.Guid()] = cb;
}
// Load all ground stations into a dictionary;
GroundStations = new Dictionary<Guid, ISatellite>();
foreach (ISatellite sat in RTSettings.Instance.GroundStations)
{
try
{
GroundStations.Add(sat.Guid, sat);
OnSatelliteRegister(sat);
}
catch (Exception e) // Already exists.
{
RTLog.Notify("A ground station cannot be loaded: " + e.Message, RTLogLevel.LVL1);
}
}
RTCore.Instance.Satellites.OnRegister += OnSatelliteRegister;
RTCore.Instance.Satellites.OnUnregister += OnSatelliteUnregister;
RTCore.Instance.OnPhysicsUpdate += OnPhysicsUpdate;
}
public void Dispose()
{
if (RTCore.Instance != null)
{
RTCore.Instance.OnPhysicsUpdate -= OnPhysicsUpdate;
RTCore.Instance.Satellites.OnRegister -= OnSatelliteRegister;
RTCore.Instance.Satellites.OnUnregister -= OnSatelliteUnregister;
}
}
public void FindPath(ISatellite start, IEnumerable<ISatellite> commandStations)
{
var paths = new List<NetworkRoute<ISatellite>>();
foreach (ISatellite root in commandStations.Concat(GroundStations.Values).Where(r => r != start))
{
paths.Add(NetworkPathfinder.Solve(start, root, FindNeighbors, RangeModelExtensions.DistanceTo, RangeModelExtensions.DistanceTo));
}
mConnectionCache[start] = paths.Where(p => p.Exists).ToList();
mConnectionCache[start].Sort((a, b) => a.Length.CompareTo(b.Length));
start.OnConnectionRefresh(this[start]);
}
public IEnumerable<NetworkLink<ISatellite>> FindNeighbors(ISatellite s)
{
if (!s.Powered) return Enumerable.Empty<NetworkLink<ISatellite>>();
return Graph[s.Guid].Where(l => l.Target.Powered);
}
private void UpdateGraph(ISatellite a)
{
var result = this.Select(b => GetLink(a, b)).Where(link => link != null).ToList();
// Send events for removed edges
foreach (var link in Graph[a.Guid].Except(result))
{
OnLinkRemove(a, link);
}
Graph[a.Guid].Clear();
// Input new edges
foreach (var link in result)
{
Graph[a.Guid].Add(link);
OnLinkAdd(a, link);
}
}
public static NetworkLink<ISatellite> GetLink(ISatellite sat_a, ISatellite sat_b)
{
if (sat_a == null || sat_b == null || sat_a == sat_b) return null;
bool los = sat_a.HasLineOfSightWith(sat_b) || CheatOptions.InfinitePropellant;
if (!los) return null;
switch (RTSettings.Instance.RangeModelType)
{
case RangeModel.RangeModel.Additive: // NathanKell
return RangeModelRoot.GetLink(sat_a, sat_b);
default: // Stock range model
return RangeModelStandard.GetLink(sat_a, sat_b);
}
}
public void OnPhysicsUpdate()
{
var count = RTCore.Instance.Satellites.Count;
if (count == 0) return;
int baseline = (count / REFRESH_TICKS);
int takeCount = baseline + (((mTick++ % REFRESH_TICKS) < (count - baseline * REFRESH_TICKS)) ? 1 : 0);
IEnumerable<ISatellite> commandStations = RTCore.Instance.Satellites.FindCommandStations();
foreach (VesselSatellite s in RTCore.Instance.Satellites.Concat(RTCore.Instance.Satellites).Skip(mTickIndex).Take(takeCount))
{
UpdateGraph(s);
//("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
if (s.SignalProcessor.VesselLoaded || HighLogic.LoadedScene == GameScenes.TRACKSTATION)
{
FindPath(s, commandStations);
}
}
mTickIndex += takeCount;
mTickIndex = mTickIndex % RTCore.Instance.Satellites.Count;
}
private void OnSatelliteUnregister(ISatellite s)
{
RTLog.Notify("NetworkManager: SatelliteUnregister({0})", s);
Graph.Remove(s.Guid);
foreach (var list in Graph.Values)
{
list.RemoveAll(l => l.Target == s);
}
mConnectionCache.Remove(s);
}
private void OnSatelliteRegister(ISatellite s)
{
RTLog.Notify("NetworkManager: SatelliteRegister({0})", s);
Graph[s.Guid] = new List<NetworkLink<ISatellite>>();
}
public IEnumerator<ISatellite> GetEnumerator()
{
return RTCore.Instance.Satellites.Cast<ISatellite>().Concat(GroundStations.Values).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>Gets the position of a RemoteTech target from its id</summary>
/// <returns>The absolute position or null if <paramref name="targetable"/> is neither
/// a satellite nor a celestial body.</returns>
/// <param name="targetable">The id of the satellite or celestial body whose position is
/// desired. May be the active vessel Guid.</param>
///
/// <exceptsafe>The program state is unchanged in the event of an exception.</exceptsafe>
internal Vector3d? GetPositionFromGuid(Guid targetable)
{
ISatellite targetSat = this[targetable];
if (targetSat != null) {
return targetSat.Position;
}
if (Planets.ContainsKey(targetable)) {
return Planets[targetable].position;
}
return null;
}
}
public sealed class MissionControlSatellite : ISatellite, IPersistenceLoad
{
/* Config Node parameters */
[Persistent] private String Guid = new Guid("5105f5a9d62841c6ad4b21154e8fc488").ToString();
[Persistent] private String Name = "Mission Control";
[Persistent] private double Latitude = -0.1313315f;
[Persistent] private double Longitude = -74.59484f;
[Persistent] private double Height = 75.0f;
[Persistent] private int Body = 1;
[Persistent] private Color MarkColor = new Color(0.996078f, 0, 0, 1);
[Persistent(collectionIndex = "ANTENNA")] private MissionControlAntenna[] Antennas = { new MissionControlAntenna() };
private bool AntennaActivated = true;
bool ISatellite.Powered { get { return this.AntennaActivated; } }
bool ISatellite.Visible { get { return true; } }
String ISatellite.Name { get { return Name; } set { Name = value; } }
Guid ISatellite.Guid { get { return mGuid; } }
Vector3d ISatellite.Position { get { return FlightGlobals.Bodies[Body].GetWorldSurfacePosition(Latitude, Longitude, Height); } }
bool ISatellite.IsCommandStation { get { return true; } }
bool ISatellite.HasLocalControl { get { return false; } }
bool ISatellite.isVessel { get { return false; } }
Vessel ISatellite.parentVessel { get { return null; } }
CelestialBody ISatellite.Body { get { return FlightGlobals.Bodies[Body]; } }
Color ISatellite.MarkColor { get { return MarkColor; } }
IEnumerable<IAntenna> ISatellite.Antennas { get { return Antennas; } }
public Guid mGuid { get; private set; }
public IEnumerable<IAntenna> MissionControlAntennas { get { return Antennas; } }
void ISatellite.OnConnectionRefresh(List<NetworkRoute<ISatellite>> route) { }
public MissionControlSatellite()
{
this.mGuid = new Guid(Guid);
}
public void reloadUpgradeableAntennas(int techlvl = 0)
{
foreach (var antenna in this.Antennas)
{
antenna.reloadUpgradeableAntennas(techlvl);
}
}
/*
* Simple getter + setter.
* For being able to add groundstations.
*/
public void SetDetails(String name, double lat, double longi, double height, int body)
{
this.Name = name;
this.Latitude = lat;
this.Longitude = longi;
this.Height = height;
this.Body = body;
this.mGuid = System.Guid.NewGuid ();
this.Guid = this.mGuid.ToString ();
}
public String GetDetails()
{
return String.Format ("name:{0}, lat={1}, long={2}, height={3}, body={4}", this.Name, this.Latitude, this.Longitude, this.Height, this.Body);
}
public String GetName()
{
return this.Name;
}
void IPersistenceLoad.PersistenceLoad()
{
foreach (var antenna in Antennas)
{
antenna.Parent = this;
}
mGuid = new Guid(Guid);
}
public override String ToString()
{
return Name;
}
/// <summary>
/// Used currently for debug purposes only. This method can be used to shut down the mission control
/// </summary>
/// <param name="powerswitch">true=Missioncontrol on, false=MissionControl off</param>
public void togglePower(bool powerswitch)
{
this.AntennaActivated = powerswitch;
}
}
}