Skip to content

Commit a6fcaaf

Browse files
author
Sander Hoksbergen
committed
Stuff.
1 parent 3d595d1 commit a6fcaaf

29 files changed

+445
-334
lines changed

src/RemoteTech2/API/API.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static bool HasAnyConnection(Guid id)
4848
public static bool HasConnectionToKSC(Guid id)
4949
{
5050
var satellite = RTCore.Instance.Satellites[id];
51-
return RTCore.Instance.Network[satellite].Any(r => r.Goal.Guid == MissionControlSatellite.Guid);
51+
return RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
5252
}
5353

5454
public static double GetShortestSignalDelay(Guid id)
@@ -61,8 +61,8 @@ public static double GetShortestSignalDelay(Guid id)
6161
public static double GetSignalDelayToKSC(Guid id)
6262
{
6363
var satellite = RTCore.Instance.Satellites[id];
64-
if (!RTCore.Instance.Network[satellite].Any(r => r.Goal.Guid == MissionControlSatellite.Guid)) return Double.PositiveInfinity;
65-
return RTCore.Instance.Network[satellite].Where(r => r.Goal.Guid == MissionControlSatellite.Guid).Min().Delay;
64+
if (!RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid))) return Double.PositiveInfinity;
65+
return RTCore.Instance.Network[satellite].Where(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid)).Min().Delay;
6666
}
6767

6868
public static double GetSignalDelayToSatellite(Guid a, Guid b)

src/RemoteTech2/FlightComputer/Commands/ManeuverCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override string Description
2222
return "Executing maneuver: " + RemainingDelta.ToString("F2") + "m/s" + Environment.NewLine +
2323
"Remaining duration: " + RTUtil.FormatDuration(RemainingTime) + Environment.NewLine + base.Description;
2424
else
25-
return "Execute planned maneuver";
25+
return "Execute planned maneuver" + Environment.NewLine + base.Description;
2626
}
2727
}
2828

@@ -49,7 +49,7 @@ public override bool Execute(FlightComputer f, FlightCtrlState fcs)
4949
RemainingDelta -= (FlightCore.GetTotalThrust(f.Vessel) / f.Vessel.GetTotalMass()) * TimeWarp.deltaTime;
5050
return false;
5151
}
52-
f.Enqueue(AttitudeCommand.Off(), true);
52+
f.Enqueue(AttitudeCommand.Off(), true, true, true);
5353
return true;
5454
}
5555

src/RemoteTech2/FlightComputer/FlightComputer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ public FlightComputer(ISignalProcessor s)
7979
SignalProcessor = s;
8080
Vessel = s.Vessel;
8181
SanctionedPilots = new List<Action<FlightCtrlState>>();
82-
Enqueue(TargetCommand.WithTarget(FlightGlobals.fetch.VesselTarget), true);
83-
Enqueue(AttitudeCommand.Off(), true);
82+
var target = TargetCommand.WithTarget(FlightGlobals.fetch.VesselTarget);
83+
mActiveCommands[target.Priority] = target;
84+
var attitude = AttitudeCommand.Off();
85+
mActiveCommands[attitude.Priority] = attitude;
8486
}
8587

8688
public void Dispose()
@@ -97,17 +99,16 @@ public void Dispose()
9799
}
98100
}
99101

100-
public void Enqueue(ICommand cmd, bool ignore_delay = false)
102+
public void Enqueue(ICommand cmd, bool ignore_control = false, bool ignore_delay = false, bool ignore_extra = false)
101103
{
102-
if (!InputAllowed) return;
104+
if (!InputAllowed && !ignore_control) return;
103105

104106
if (!ignore_delay) cmd.TimeStamp += Delay;
105-
if (!ignore_delay) cmd.ExtraDelay += Math.Max(0, TotalDelay - Delay);
107+
if (!ignore_extra) cmd.ExtraDelay += Math.Max(0, TotalDelay - Delay);
106108

107109
int pos = mCommandQueue.BinarySearch(cmd);
108110
if (pos < 0)
109111
{
110-
RTLog.Debug("Inserted " + cmd.Description);
111112
mCommandQueue.Insert(~pos, cmd);
112113
}
113114
}
@@ -242,8 +243,7 @@ private void OnFlyByWirePost(FlightCtrlState fcs)
242243

243244
if (SignalProcessor.Powered)
244245
{
245-
foreach (var dc in mActiveCommands.Values.TakeWhile(c => c.TimeStamp <= RTUtil.GameTime)
246-
.Where(c => c.ExtraDelay <= 0).ToList())
246+
foreach (var dc in mActiveCommands.Values.ToList())
247247
{
248248
if (dc.Execute(this, fcs)) mActiveCommands.Remove(dc.Priority);
249249
}

src/RemoteTech2/FlightComputer/UIPartActionMenuPatcher.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public static BaseEvent CreateWrapper(BaseEvent original, Action<BaseEvent, bool
6060
ConfigNode cn = new ConfigNode();
6161
original.OnSave(cn);
6262
Wrapper wrapper = new Wrapper(original, passthrough, ignore_delay);
63-
BaseEvent new_event = new BaseEvent(original.listParent, original.name,
64-
wrapper.Invoke);
63+
BaseEvent new_event = new BaseEvent(original.listParent, original.name, wrapper.Invoke);
6564
new_event.OnLoad(cn);
6665

6766
return new_event;

src/RemoteTech2/Modules/MissionControlAntenna.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ namespace RemoteTech
66
public sealed class MissionControlAntenna : IAntenna
77
{
88
[Persistent] public float Omni = 75000000;
9+
public ISatellite Parent { get; set; }
910

1011
float IAntenna.Omni { get { return Omni; } }
11-
Guid IAntenna.Guid { get { return MissionControlSatellite.Guid; } }
12+
Guid IAntenna.Guid { get { return Parent.Guid; } }
1213
String IAntenna.Name { get { return "Dummy Antenna"; } }
1314
bool IAntenna.Powered { get { return true; } }
1415
bool IAntenna.Activated { get { return true; } set { return; } }

src/RemoteTech2/Modules/ModuleRTAntenna.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace RemoteTech
99
{
10-
[KSPModule("RemoteTech Antenna")]
10+
[KSPModule("Antenna")]
1111
public class ModuleRTAntenna : PartModule, IAntenna
1212
{
1313
public String Name { get { return part.partInfo.title; } }
@@ -160,15 +160,12 @@ public virtual void SetState(bool state)
160160
Events["OverrideClose"].guiActiveUnfocused = IsRTActive && !IsRTBroken;
161161

162162
UpdateContext();
163-
if (IsRTActive != prev_state)
164-
{
165-
StartCoroutine(SetFXModules_Coroutine(mDeployFxModules, IsRTActive ? 1.0f : 0.0f));
166-
}
163+
StartCoroutine(SetFXModules_Coroutine(mDeployFxModules, IsRTActive ? 1.0f : 0.0f));
167164

168165
if (RTCore.Instance != null)
169166
{
170167
var satellite = RTCore.Instance.Network[Guid];
171-
bool route_home = RTCore.Instance.Network[satellite].Any(r => r.Links[0].Interfaces.Contains(this) && r.Goal.Guid == MissionControlSatellite.Guid);
168+
bool route_home = RTCore.Instance.Network[satellite].Any(r => r.Links[0].Interfaces.Contains(this) && RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
172169
if (mTransmitter == null && route_home)
173170
{
174171
AddTransmitter();

src/RemoteTech2/Modules/ModuleRTAntennaPassive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public virtual void SetState(bool state)
8585
{
8686
IsRTActive = state;
8787
var satellite = RTCore.Instance.Network[Guid];
88-
bool route_home = RTCore.Instance.Network[satellite].Any(r => r.Links[0].Interfaces.Contains(this) && r.Goal.Guid == MissionControlSatellite.Guid);
88+
bool route_home = RTCore.Instance.Network[satellite].Any(r => r.Links[0].Interfaces.Contains(this) && RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
8989
if (mTransmitter == null && route_home)
9090
{
9191
AddTransmitter();

src/RemoteTech2/Modules/ModuleSPU.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace RemoteTech
77
{
8+
[KSPModule("Signal Processor")]
89
public class ModuleSPU : PartModule, ISignalProcessor
910
{
1011
public String Name { get { return String.Format("ModuleSPU({0})", VesselName); } }
@@ -141,6 +142,7 @@ public void HookPartMenus()
141142
if (v == null || v.isEVA || RTCore.Instance == null)
142143
{
143144
e.Invoke();
145+
return;
144146
}
145147
var vs = RTCore.Instance.Satellites[v];
146148
if (vs == null || vs.HasLocalControl)

src/RemoteTech2/NetworkManager.cs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ public partial class NetworkManager : IEnumerable<ISatellite>
2121
public event Action<ISatellite, NetworkLink<ISatellite>> OnLinkRemove = delegate { };
2222

2323
public Dictionary<Guid, CelestialBody> Planets { get; private set; }
24-
public ISatellite[] GroundStations { get { return RTSettings.Instance.GroundStations; } }
24+
public Dictionary<Guid, ISatellite> GroundStations { get; private set; }
2525
public Dictionary<Guid, List<NetworkLink<ISatellite>>> Graph { get; private set; }
2626

27-
public int Count { get { return RTCore.Instance.Satellites.Count + GroundStations.Length; } }
27+
public int Count { get { return RTCore.Instance.Satellites.Count + GroundStations.Count; } }
2828

2929
public static Guid ActiveVesselGuid = new Guid(RTSettings.Instance.ActiveVesselGuid);
3030

3131
public ISatellite this[Guid guid]
3232
{
3333
get
3434
{
35-
if (guid == ActiveVesselGuid)
36-
return RTCore.Instance.Satellites[FlightGlobals.ActiveVessel];
37-
return RTCore.Instance.Satellites[guid];
35+
return RTCore.Instance.Satellites[guid] ??
36+
((guid == ActiveVesselGuid) ? RTCore.Instance.Satellites[FlightGlobals.ActiveVessel] : null) ??
37+
(GroundStations.ContainsKey(guid) ? GroundStations[guid] : null);
3838
}
3939
}
4040

@@ -56,16 +56,27 @@ public List<NetworkRoute<ISatellite>> this[ISatellite sat]
5656
public NetworkManager()
5757
{
5858
Graph = new Dictionary<Guid, List<NetworkLink<ISatellite>>>();
59-
Planets = new Dictionary<Guid, CelestialBody>();
6059

60+
// Load all planets into a dictionary;
61+
Planets = new Dictionary<Guid, CelestialBody>();
6162
foreach (CelestialBody cb in FlightGlobals.Bodies)
6263
{
6364
Planets[cb.Guid()] = cb;
6465
}
6566

66-
foreach (var sat in GroundStations)
67+
// Load all ground stations into a dictionary;
68+
GroundStations = new Dictionary<Guid, ISatellite>();
69+
foreach (ISatellite sat in RTSettings.Instance.GroundStations)
6770
{
68-
OnSatelliteRegister(sat);
71+
try
72+
{
73+
GroundStations.Add(sat.Guid, sat);
74+
OnSatelliteRegister(sat);
75+
}
76+
catch (Exception e) // Already exists.
77+
{
78+
RTLog.Notify("A ground station cannot be loaded: " + e.Message);
79+
}
6980
}
7081

7182
RTCore.Instance.Satellites.OnRegister += OnSatelliteRegister;
@@ -86,7 +97,7 @@ public void Dispose()
8697
public void FindPath(ISatellite start, IEnumerable<ISatellite> commandStations)
8798
{
8899
var paths = new List<NetworkRoute<ISatellite>>();
89-
foreach (ISatellite root in commandStations.Concat(GroundStations).Where(r => r != start))
100+
foreach (ISatellite root in commandStations.Concat(GroundStations.Values).Where(r => r != start))
90101
{
91102
paths.Add(NetworkPathfinder.Solve(start, root, FindNeighbors, RangeModelExtensions.DistanceTo, RangeModelExtensions.DistanceTo));
92103
}
@@ -154,7 +165,7 @@ public void OnPhysicsUpdate()
154165
foreach (VesselSatellite s in RTCore.Instance.Satellites.Concat(RTCore.Instance.Satellites).Skip(mTickIndex).Take(takeCount))
155166
{
156167
UpdateGraph(s);
157-
//RTLog.Debug("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
168+
//("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
158169
if (s.SignalProcessor.VesselLoaded || HighLogic.LoadedScene == GameScenes.TRACKSTATION)
159170
{
160171
FindPath(s, commandStations);
@@ -183,7 +194,7 @@ private void OnSatelliteRegister(ISatellite s)
183194

184195
public IEnumerator<ISatellite> GetEnumerator()
185196
{
186-
return RTCore.Instance.Satellites.Cast<ISatellite>().Concat(GroundStations).GetEnumerator();
197+
return RTCore.Instance.Satellites.Cast<ISatellite>().Concat(GroundStations.Values).GetEnumerator();
187198
}
188199

189200
IEnumerator IEnumerable.GetEnumerator()
@@ -192,23 +203,21 @@ IEnumerator IEnumerable.GetEnumerator()
192203
}
193204
}
194205

195-
public sealed class MissionControlSatellite : ISatellite
206+
public sealed class MissionControlSatellite : ISatellite, IPersistenceLoad
196207
{
197-
public static Guid Guid = new Guid("5105f5a9d62841c6ad4b21154e8fc488");
198-
199208
/* Config Node parameters */
209+
[Persistent] private String Guid = new Guid("5105f5a9d62841c6ad4b21154e8fc488").ToString();
200210
[Persistent] private String Name = "Mission Control";
201211
[Persistent] private double Latitude = -0.1313315f;
202212
[Persistent] private double Longitude = -74.59484f;
203213
[Persistent] private double Height = 75.0f;
204214
[Persistent] private int Body = 1;
205-
[Persistent(collectionIndex = "ANTENNA")]
206-
private MissionControlAntenna[] Antennas = new MissionControlAntenna[] { new MissionControlAntenna() };
215+
[Persistent(collectionIndex = "ANTENNA")] private MissionControlAntenna[] Antennas = new MissionControlAntenna[] { new MissionControlAntenna() };
207216

208217
bool ISatellite.Powered { get { return true; } }
209218
bool ISatellite.Visible { get { return true; } }
210219
String ISatellite.Name { get { return Name; } set { Name = value; } }
211-
Guid ISatellite.Guid { get { return Guid; } }
220+
Guid ISatellite.Guid { get { return mGuid; } }
212221
Vector3d ISatellite.Position { get { return FlightGlobals.Bodies[Body].GetWorldSurfacePosition(Latitude, Longitude, Height); } }
213222
bool ISatellite.IsCommandStation { get { return true; } }
214223
bool ISatellite.HasLocalControl { get { return false; } }
@@ -217,6 +226,17 @@ public sealed class MissionControlSatellite : ISatellite
217226

218227
void ISatellite.OnConnectionRefresh(List<NetworkRoute<ISatellite>> route) { }
219228

229+
private Guid mGuid;
230+
231+
void IPersistenceLoad.PersistenceLoad()
232+
{
233+
foreach (var antenna in Antennas)
234+
{
235+
antenna.Parent = this;
236+
}
237+
mGuid = new Guid(Guid);
238+
}
239+
220240
public override String ToString()
221241
{
222242
return Name;

src/RemoteTech2/NetworkRenderer.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static NetworkRenderer()
4444
RTUtil.LoadImage(out mTexMark, "mark.png");
4545
}
4646

47-
public static NetworkRenderer AttachToMapView()
47+
public static NetworkRenderer CreateAndAttach()
4848
{
4949
var renderer = MapView.MapCamera.gameObject.GetComponent<NetworkRenderer>();
5050
if (renderer)
@@ -72,7 +72,7 @@ public void OnGUI()
7272
{
7373
if (Event.current.type == EventType.Repaint && MapView.MapIsEnabled)
7474
{
75-
foreach (ISatellite s in RTCore.Instance.Satellites.FindCommandStations().Concat(RTCore.Instance.Network.GroundStations))
75+
foreach (ISatellite s in RTCore.Instance.Satellites.FindCommandStations().Concat(RTCore.Instance.Network.GroundStations.Values))
7676
{
7777
var world_pos = ScaledSpace.LocalToScaledSpace(s.Position);
7878
if (MapView.MapCamera.transform.InverseTransformPoint(world_pos).z < 0f) continue;
@@ -85,10 +85,9 @@ public void OnGUI()
8585

8686
private void UpdateNetworkCones()
8787
{
88-
var antennas = RTCore.Instance.Antennas.Where(a => ShowPlanet)
89-
.Where(a => a.Powered && a.CanTarget && RTCore.Instance.Satellites[a.Guid] != null
90-
&& RTCore.Instance.Network.Planets.ContainsKey(a.Target))
91-
.ToList();
88+
var antennas = (ShowPlanet ? RTCore.Instance.Antennas.Where(a => a.Powered && a.CanTarget && RTCore.Instance.Satellites[a.Guid] != null
89+
&& RTCore.Instance.Network.Planets.ContainsKey(a.Target))
90+
: Enumerable.Empty<IAntenna>()).ToList();
9291
int oldLength = mCones.Count;
9392
int newLength = antennas.Count;
9493

@@ -98,7 +97,7 @@ private void UpdateNetworkCones()
9897
GameObject.Destroy(mCones[i]);
9998
}
10099
mCones.RemoveRange(Math.Min(oldLength, newLength), Math.Max(oldLength - newLength, 0));
101-
mCones.AddRange(Enumerable.Repeat<NetworkCone>(null, Math.Max(newLength - oldLength, 0)));
100+
mCones.AddRange(Enumerable.Repeat((NetworkCone) null, Math.Max(newLength - oldLength, 0)));
102101

103102
for (int i = 0; i < newLength; i++)
104103
{

0 commit comments

Comments
 (0)