Example #1
0
 public static StockAutopilotCommand WithNewMode(AutopilotMode newMode)
 {
     return(new StockAutopilotCommand()
     {
         AutopilotMode = newMode,
         TimeStamp = RTUtil.GameTime,
     });
 }
            public void TravelToPosition(Vector3D position, bool enableAvoidance, double maximumVelocity = 100, double safetyMargin = 1.25)
            {
                _targetLocation   = position;
                _enableAvoidance  = enableAvoidance;
                _maxVelocity      = maximumVelocity;
                _safetyMarginMult = safetyMargin;

                currentMode |= AutopilotMode.TravelToPosition;
            }
Example #3
0
 public override bool Pop(FlightComputer f)
 {
     if (f.Vessel.Autopilot.CanSetMode(AutopilotMode))
     {
         f.Vessel.Autopilot.SetMode(AutopilotMode);
         savedAutopilotMode = AutopilotMode; // be sure to update the saved mode after setting autpilot mode
         return(false);
     }
     return(true);
 }
Example #4
0
        /// <summary>
        /// Action to be called by a RT listener on KSP's autopilot buttons
        /// </summary>
        public static void AutopilotButtonClick(int index, FlightComputer flightCom)
        {
            var satellite = flightCom.Vessel;

            if (!satellite.HasLocalControl() && flightCom.InputAllowed)
            {
                //Note: the VesselAutopilotUI's OnClickButton is delayed by FlightComputer so no further action needed
                //Note: KSP bug #13199 (http://bugs.kerbalspaceprogram.com/issues/13199) on wrong-placed Radial In & Out buttons
                var currentMode = flightCom.Vessel.Autopilot.Mode;
                var nextMode    = (AutopilotMode)index;

                if (currentMode != nextMode)
                {
                    savedAutopilotMode = currentMode; // autopilot's stock actionlistener will set to new mode so we need to roll back to prev mode via IsAutoPilotEngaged()
                    var newCommand = WithNewMode(nextMode);
                    flightCom.Enqueue(newCommand);

                    //Note: Timer of returning to prev mode doesn't really work too well in Unity and KSP architecture
                }
            }
        }
            public void Main()
            {
                if ((currentMode & AutopilotMode.ThrustToVelocity) != 0)
                {
                    autopilotModule.ThrustToVelocity(_targetVelocity);
                }
                else if ((currentMode & AutopilotMode.TravelToPosition) != 0)
                {
                    if (Vector3D.DistanceSquared(autopilotModule.controller.GetPosition(), _targetLocation) < 0.1 && autopilotModule.controller.GetShipSpeed() < 0.01)
                    {
                        currentMode &= ~AutopilotMode.TravelToPosition;
                    }

                    autopilotModule.TravelToPosition(_targetLocation, _maxVelocity, _safetyMarginMult, (currentMode & AutopilotMode.AimInDirection) != 0);
                }

                if ((currentMode & AutopilotMode.AimInDirection) != 0)
                {
                    autopilotModule.AimInDirection(_targetDirection, _targetDirUp);
                }
            }
Example #6
0
        /// <summary>
        /// Check if KSP's autopilot is performing one SAS function in presence of long delay
        /// </summary>
        public static bool IsAutoPilotEngaged(FlightComputer flightCom)
        {
            if (!flightCom.Vessel.Autopilot.Enabled) // autopilot is off
            {
                if (savedAutopilotMode != AutopilotMode.StabilityAssist)
                {
                    savedAutopilotMode = AutopilotMode.StabilityAssist; // matched to KSP's default to SAS mode when turned on
                }
                return(false);
            }

            if (flightCom.Vessel.Autopilot.Mode != savedAutopilotMode && flightCom.Vessel.Autopilot.CanSetMode(savedAutopilotMode))
            {
                flightCom.Vessel.Autopilot.SetMode(savedAutopilotMode); // purpose: return to the pre-click mode
            }
            if (GameSettings.PITCH_DOWN.GetKey() || GameSettings.PITCH_UP.GetKey() ||
                GameSettings.ROLL_LEFT.GetKey() || GameSettings.ROLL_RIGHT.GetKey() ||
                GameSettings.YAW_LEFT.GetKey() || GameSettings.YAW_RIGHT.GetKey()) // player trying to manually rotate
            {
                return(false);
            }

            if (GameSettings.TRANSLATE_FWD.GetKey() || GameSettings.TRANSLATE_BACK.GetKey() ||
                GameSettings.TRANSLATE_LEFT.GetKey() || GameSettings.TRANSLATE_RIGHT.GetKey() ||
                GameSettings.TRANSLATE_UP.GetKey() || GameSettings.TRANSLATE_DOWN.GetKey()) // player trying to manually translate
            {
                return(false);
            }

            if (!GameSettings.AXIS_PITCH.IsNeutral() ||
                !GameSettings.AXIS_ROLL.IsNeutral() ||
                !GameSettings.AXIS_YAW.IsNeutral()) // player trying to joystick
            {
                return(false);
            }

            return(true);
        }
            public void ThrustToVelocity(Vector3D velocity)
            {
                _targetVelocity = velocity;

                currentMode |= AutopilotMode.ThrustToVelocity;
            }
 public void Reset()
 {
     currentMode = AutopilotMode.Idle;
 }