@@ -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 }
0 commit comments