|
| 1 | +using System; |
| 2 | +using static VesselAutopilot; |
| 3 | + |
| 4 | +namespace RemoteTech.FlightComputer.Commands |
| 5 | +{ |
| 6 | + public class StockAutopilotCommand : AbstractCommand |
| 7 | + { |
| 8 | + [Persistent] public AutopilotMode AutopilotMode; |
| 9 | + public static VesselAutopilotUI UIreference = null; |
| 10 | + private static AutopilotMode savedAutopilotMode = AutopilotMode.StabilityAssist; |
| 11 | + |
| 12 | + public override string ShortName |
| 13 | + { |
| 14 | + get |
| 15 | + { |
| 16 | + return AutopilotMode.ToString(); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public override string Description |
| 21 | + { |
| 22 | + get |
| 23 | + { |
| 24 | + return "Autopilot: " + ShortName + Environment.NewLine + base.Description; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public override bool Pop(FlightComputer f) |
| 29 | + { |
| 30 | + if (f.Vessel.Autopilot.CanSetMode(AutopilotMode)) |
| 31 | + { |
| 32 | + f.Vessel.Autopilot.SetMode(AutopilotMode); |
| 33 | + savedAutopilotMode = AutopilotMode; // be sure to update the saved mode after setting autpilot mode |
| 34 | + return false; |
| 35 | + } |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + public static StockAutopilotCommand WithNewMode(AutopilotMode newMode) |
| 40 | + { |
| 41 | + return new StockAutopilotCommand() |
| 42 | + { |
| 43 | + AutopilotMode = newMode, |
| 44 | + TimeStamp = RTUtil.GameTime, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Action to be called by a RT listener on KSP's autopilot buttons |
| 50 | + /// </summary> |
| 51 | + public static void AutopilotButtonClick(int index, FlightComputer flightCom) |
| 52 | + { |
| 53 | + var satellite = flightCom.Vessel; |
| 54 | + if (!satellite.HasLocalControl() && flightCom.InputAllowed) |
| 55 | + { |
| 56 | + //Note: the VesselAutopilotUI's OnClickButton is delayed by FlightComputer so no further action needed |
| 57 | + //Note: KSP bug #13199 (http://bugs.kerbalspaceprogram.com/issues/13199) on wrong-placed Radial In & Out buttons |
| 58 | + var currentMode = flightCom.Vessel.Autopilot.Mode; |
| 59 | + var nextMode = (AutopilotMode)index; |
| 60 | + |
| 61 | + if (currentMode != nextMode) |
| 62 | + { |
| 63 | + savedAutopilotMode = currentMode; // autopilot's stock actionlistener will set to new mode so we need to roll back to prev mode via IsAutoPilotEngaged() |
| 64 | + var newCommand = WithNewMode(nextMode); |
| 65 | + flightCom.Enqueue(newCommand); |
| 66 | + |
| 67 | + //Note: Timer of returning to prev mode doesn't really work too well in Unity and KSP architecture |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Check if KSP's autopilot is performing one SAS function in presence of long delay |
| 74 | + /// </summary> |
| 75 | + public static bool IsAutoPilotEngaged(FlightComputer flightCom) |
| 76 | + { |
| 77 | + if (!flightCom.Vessel.Autopilot.Enabled) // autopilot is off |
| 78 | + return false; |
| 79 | + |
| 80 | + if (flightCom.Vessel.Autopilot.Mode != savedAutopilotMode && flightCom.Vessel.Autopilot.CanSetMode(savedAutopilotMode)) |
| 81 | + flightCom.Vessel.Autopilot.SetMode(savedAutopilotMode); // purpose: return to the pre-click mode |
| 82 | + |
| 83 | + if (GameSettings.PITCH_DOWN.GetKey() || GameSettings.PITCH_UP.GetKey() || |
| 84 | + GameSettings.ROLL_LEFT.GetKey() || GameSettings.ROLL_RIGHT.GetKey() || |
| 85 | + GameSettings.YAW_LEFT.GetKey() || GameSettings.YAW_RIGHT.GetKey()) // player trying to manually rotate |
| 86 | + return false; |
| 87 | + |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments