Skip to content

Commit 1b48ed9

Browse files
author
Sander Hoksbergen
committed
1 parent 721ba07 commit 1b48ed9

8 files changed

Lines changed: 97 additions & 31 deletions

File tree

src/RemoteTech2/FlightComputer/DelayedCommand.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@ public static DelayedCommand WithNode(ManeuverNode node)
6262
{
6363
ManeuverCommand = new ManeuverCommand()
6464
{
65-
Node = node,
65+
Node = new ManeuverNode()
66+
{
67+
DeltaV = node.DeltaV,
68+
patch = node.patch,
69+
solver = node.solver,
70+
scaledSpaceTarget = node.scaledSpaceTarget,
71+
nextPatch = node.nextPatch,
72+
UT = node.UT,
73+
nodeRotation = node.nodeRotation,
74+
}
6675
},
6776
TimeStamp = RTUtil.GameTime,
6877
};

src/RemoteTech2/FlightComputer/FlightComputer.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public double Delay
3131
}
3232
}
3333

34+
public Vector3d Maneuver { get { return mCurrentCommand.ManeuverCommand != null ? mCurrentCommand.ManeuverCommand.Node.GetBurnVector(mVessel.orbit) : mVessel.obt_velocity.normalized; } }
35+
public ITargetable Target { get { return mCurrentCommand.TargetCommand != null ? mCurrentCommand.TargetCommand.Target : mVessel.mainBody; } }
36+
3437
public double TotalDelay { get; set; }
3538

3639
public List<Action<FlightCtrlState>> SanctionedPilots { get; private set; }
@@ -42,6 +45,7 @@ public double Delay
4245
private FlightCtrlState mPreviousFcs = new FlightCtrlState();
4346
private readonly List<DelayedCommand> mCommandBuffer = new List<DelayedCommand>();
4447
private readonly PriorityQueue<DelayedFlightCtrlState> mFlightCtrlBuffer = new PriorityQueue<DelayedFlightCtrlState>();
48+
private readonly PriorityQueue<DelayedCommand> mManeuverBuffer = new PriorityQueue<DelayedCommand>();
4549

4650
private Vector3 mLastVelocity;
4751
private Quaternion mKillRot;
@@ -98,25 +102,27 @@ public void OnUpdate()
98102
public void OnFixedUpdate()
99103
{
100104
// Send updates for Target / Maneuver
101-
if (FlightGlobals.fetch.VesselTarget != null && (mCurrentCommand.TargetCommand == null || mCurrentCommand.TargetCommand.Target != FlightGlobals.fetch.VesselTarget))
105+
if ((mCurrentCommand.TargetCommand == null && FlightGlobals.fetch.VesselTarget != null) ||
106+
(mCurrentCommand.TargetCommand != null && mCurrentCommand.TargetCommand.Target != FlightGlobals.fetch.VesselTarget))
102107
{
103-
if (!mCommandBuffer.Any(dc => dc.TargetCommand != null && dc.TargetCommand.Target == FlightGlobals.fetch.VesselTarget))
108+
if (!mCommandBuffer.Any(dc => dc.TargetCommand.Target == FlightGlobals.fetch.VesselTarget))
104109
{
105110
Enqueue(TargetCommand.WithTarget(FlightGlobals.fetch.VesselTarget));
106111
}
107112
}
108-
if (mVessel.patchedConicSolver != null)
113+
if (mVessel.patchedConicSolver != null && mVessel.patchedConicSolver.maneuverNodes != null)
109114
{
110-
if (mVessel.patchedConicSolver.maneuverNodes.Count > 0 && (mCurrentCommand.ManeuverCommand == null || mCurrentCommand.ManeuverCommand.Node != mVessel.patchedConicSolver.maneuverNodes[0]))
115+
if (mVessel.patchedConicSolver.maneuverNodes.Count > 0 && (mCurrentCommand.ManeuverCommand == null || mCurrentCommand.ManeuverCommand.Node.DeltaV != mVessel.patchedConicSolver.maneuverNodes[0].DeltaV))
111116
{
112-
if (!mCommandBuffer.Any(dc => dc.TargetCommand != null && dc.ManeuverCommand.Node == mVessel.patchedConicSolver.maneuverNodes[0]))
117+
if (!mManeuverBuffer.Any(dc => dc.ManeuverCommand.Node.DeltaV == mVessel.patchedConicSolver.maneuverNodes[0].DeltaV))
113118
{
114-
Enqueue(ManeuverCommand.WithNode(mVessel.patchedConicSolver.maneuverNodes[0]));
119+
var command = ManeuverCommand.WithNode(mVessel.patchedConicSolver.maneuverNodes[0]);
120+
command.TimeStamp += Delay;
121+
mManeuverBuffer.Enqueue(command);
115122
}
116123
}
117124
}
118125

119-
120126
if (mVessel != mParent.Vessel)
121127
{
122128
mVessel.VesselSAS.LockHeading(mVessel.transform.rotation, false);
@@ -139,10 +145,12 @@ private void Enqueue(FlightCtrlState fs)
139145
mFlightCtrlBuffer.Enqueue(dfs);
140146
}
141147

142-
private void PopFlightCtrlState(FlightCtrlState fcs)
148+
private void PopFlightCtrlState(FlightCtrlState fcs, ISatellite sat)
143149
{
144150
FlightCtrlState delayed = mPreviousFcs;
145-
mPreviousFcs.Neutralize();
151+
float prev_throttle = delayed.mainThrottle;
152+
delayed.Neutralize();
153+
delayed.mainThrottle = InputAllowed ? prev_throttle : 0.0f;
146154
while (mFlightCtrlBuffer.Count > 0 && mFlightCtrlBuffer.Peek().TimeStamp <= RTUtil.GameTime)
147155
{
148156
delayed = mFlightCtrlBuffer.Dequeue().State;
@@ -153,12 +161,20 @@ private void PopFlightCtrlState(FlightCtrlState fcs)
153161

154162
private void PopCommand()
155163
{
164+
// Maneuvers
165+
while (mManeuverBuffer.Count > 0 && mManeuverBuffer.Peek().TimeStamp <= RTUtil.GameTime)
166+
{
167+
mCurrentCommand.ManeuverCommand = mManeuverBuffer.Dequeue().ManeuverCommand;
168+
}
169+
170+
// Commands
156171
if (mCommandBuffer.Count > 0)
157172
{
158173
var time = TimeWarp.deltaTime;
174+
var delete = new List<DelayedCommand>();
159175
for (int i = 0; i < mCommandBuffer.Count && mCommandBuffer[i].TimeStamp <= RTUtil.GameTime; i++)
160176
{
161-
DelayedCommand dc = mCommandBuffer[i];
177+
var dc = mCommandBuffer[i];
162178
if (dc.ExtraDelay > 0)
163179
{
164180
dc.ExtraDelay -= time;
@@ -214,12 +230,7 @@ private void PopCommand()
214230

215231
if (dc.TargetCommand != null)
216232
{
217-
mCurrentCommand.TargetCommand = dc.TargetCommand;
218-
}
219-
220-
if (dc.ManeuverCommand != null)
221-
{
222-
mCurrentCommand.ManeuverCommand = dc.ManeuverCommand;
233+
mCurrentCommand.TargetCommand = dc.TargetCommand.Target != null ? dc.TargetCommand : null;
223234
}
224235

225236
if (dc.CancelCommand != null)
@@ -234,7 +245,7 @@ private void PopCommand()
234245
}
235246
else if (!do_not_delete)
236247
{
237-
mCommandBuffer.RemoveAt(i);
248+
mCommandBuffer.RemoveAt(i--);
238249
}
239250

240251
}
@@ -292,6 +303,7 @@ private void Burn(FlightCtrlState fs)
292303
private void HoldOrientation(FlightCtrlState fs, Quaternion target)
293304
{
294305
//mVessel.VesselSAS.LockHeading(target * Quaternion.AngleAxis(90, Vector3.right), true);
306+
mVessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
295307
kOS.SteeringHelper.SteerShipToward(target, fs, mVessel);
296308
//FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.SAS, true);
297309
}
@@ -396,7 +408,7 @@ private void OnFlyByWirePre(FlightCtrlState fcs)
396408

397409
if (!satellite.HasLocalControl)
398410
{
399-
PopFlightCtrlState(fcs);
411+
PopFlightCtrlState(fcs, satellite);
400412
}
401413

402414
}

src/RemoteTech2/NetworkManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public void OnPhysicsUpdate()
349349
foreach (VesselSatellite s in RTCore.Instance.Satellites.Concat(RTCore.Instance.Satellites).Skip(mTickIndex).Take(takeCount))
350350
{
351351
UpdateGraph(s);
352-
RTLog.Debug("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
352+
//RTLog.Debug("{0} [ E: {1} ]", s.ToString(), Graph[s.Guid].ToDebugString());
353353
if (s.SignalProcessor.VesselLoaded || HighLogic.LoadedScene == GameScenes.TRACKSTATION)
354354
{
355355
FindPath(s, commandStations);

src/RemoteTech2/RTSettings.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ public static Settings Load()
4747
{
4848
ConfigNode load = ConfigNode.Load(File);
4949
Settings settings = new Settings();
50-
if (load == null) return settings;
50+
if (load == null)
51+
{
52+
settings.Save();
53+
return settings;
54+
}
5155
ConfigNode.LoadObjectFromConfig(settings, load);
52-
settings.Save();
56+
5357
return settings;
5458
}
5559
}

src/RemoteTech2/SimpleTypes/BinaryHeap.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34

45
namespace RemoteTech
56
{
67
// Sadly there is no way to enforce immutability C#.
78
// Do not modify sorting order externally! Increase/Decrease().
8-
public class BinaryHeap<T> where T : class
9+
public class BinaryHeap<T> : IEnumerable<T>
910
{
1011
public int Count { get { return mData.Count; } }
1112

@@ -109,5 +110,15 @@ public int IndexOf(T item)
109110
{
110111
return mData.IndexOf(item);
111112
}
113+
114+
public IEnumerator<T> GetEnumerator()
115+
{
116+
return mData.GetEnumerator();
117+
}
118+
119+
IEnumerator IEnumerable.GetEnumerator()
120+
{
121+
return GetEnumerator();
122+
}
112123
}
113124
}

src/RemoteTech2/SimpleTypes/PriorityQueue.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24

35
namespace RemoteTech
46
{
57
// PriorityQueue based on a minimum-BinaryHeap.
6-
public class PriorityQueue<T> where T : class
8+
public class PriorityQueue<T> : IEnumerable<T>
79
{
810
public int Count { get { return mHeap.Count; } }
911

@@ -33,5 +35,15 @@ public T Dequeue()
3335
{
3436
return mHeap.Remove();
3537
}
38+
39+
public IEnumerator<T> GetEnumerator()
40+
{
41+
return mHeap.GetEnumerator();
42+
}
43+
44+
IEnumerator IEnumerable.GetEnumerator()
45+
{
46+
return GetEnumerator();
47+
}
3648
}
3749
}

src/RemoteTech2/UI/AttitudeFragment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private FlightAttitude Attitude
105105
private float mThrottle;
106106

107107
private String mPitch = "90";
108-
private String mRoll = "0";
108+
private String mRoll = "90";
109109
private String mHeading = "90";
110110
private String mDuration = "0s";
111111

src/RemoteTech2/UI/QueueFragment.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text;
3+
using System.Collections;
34
using UnityEngine;
45

56
namespace RemoteTech
@@ -43,13 +44,17 @@ public void Draw()
4344
{
4445
foreach (DelayedCommand dc in mFlightComputer)
4546
{
46-
GUILayout.BeginHorizontal(GUI.skin.box);
47+
var text = Format(dc);
48+
if (!String.IsNullOrEmpty(text))
4749
{
48-
GUILayout.Label(Format(dc));
49-
GUILayout.FlexibleSpace();
50-
RTUtil.Button("x", () => { mFlightComputer.Enqueue(DelayedCommand.Cancel(dc)); }, GUILayout.Width(21), GUILayout.Height(21));
50+
GUILayout.BeginHorizontal(GUI.skin.box);
51+
{
52+
GUILayout.Label(text);
53+
GUILayout.FlexibleSpace();
54+
RTUtil.Button("x", () => { RTCore.Instance.StartCoroutine(OnClickCancel(dc)); }, GUILayout.Width(21), GUILayout.Height(21));
55+
}
56+
GUILayout.EndHorizontal();
5157
}
52-
GUILayout.EndHorizontal();
5358
}
5459
}
5560
GUILayout.EndScrollView();
@@ -66,6 +71,12 @@ public void Draw()
6671
GUILayout.EndVertical();
6772
}
6873

74+
public IEnumerator OnClickCancel(DelayedCommand dc)
75+
{
76+
yield return null;
77+
mFlightComputer.Enqueue(DelayedCommand.Cancel(dc));
78+
}
79+
6980
private String Format(DelayedCommand dc)
7081
{
7182
StringBuilder s = new StringBuilder();
@@ -122,7 +133,7 @@ private String Format(DelayedCommand dc)
122133
case FlightAttitude.Surface:
123134
s.Append(dc.AttitudeCommand.Orientation.eulerAngles.x.ToString("F1"));
124135
s.Append("°, ");
125-
s.Append(RTUtil.Format180To360(180 - dc.AttitudeCommand.Orientation.eulerAngles.y).ToString("F1"));
136+
s.Append((360 - dc.AttitudeCommand.Orientation.eulerAngles.y).ToString("F1"));
126137
s.Append("°, ");
127138
s.Append(RTUtil.Format360To180(180 - dc.AttitudeCommand.Orientation.eulerAngles.z).ToString("F1"));
128139
s.AppendLine("°");
@@ -168,6 +179,13 @@ private String Format(DelayedCommand dc)
168179
{
169180
s.AppendLine("Cancelling a command");
170181
}
182+
if (dc.TargetCommand != null)
183+
{
184+
s.Append("Target: ");
185+
s.AppendLine(dc.TargetCommand.Target != null ? dc.TargetCommand.Target.GetName() : "None");
186+
}
187+
188+
if (s.ToString().Equals("")) return "";
171189

172190
double delay = Math.Max(dc.TimeStamp - RTUtil.GameTime, 0);
173191
if (delay > 0 || dc.ExtraDelay > 0)

0 commit comments

Comments
 (0)