From cbea6b9d58e6c4fbfa1481fd5f3833c00c7765f9 Mon Sep 17 00:00:00 2001 From: dylan madisetti Date: Sun, 26 Mar 2023 20:05:25 -0400 Subject: [PATCH 1/3] Adds Flight Controls (partially addresses #15) --- service/spacecenter/src/Control.cs | 344 +++++++++++++++++++++++++++++ service/spacecenter/src/Vessel.cs | 12 + tools/install.sh | 2 +- 3 files changed, 357 insertions(+), 1 deletion(-) create mode 100644 service/spacecenter/src/Control.cs diff --git a/service/spacecenter/src/Control.cs b/service/spacecenter/src/Control.cs new file mode 100644 index 0000000..d647872 --- /dev/null +++ b/service/spacecenter/src/Control.cs @@ -0,0 +1,344 @@ +using System; +using KSP.Sim.impl; +using KSP.Sim.State; + +using KRPC.Service; +using KRPC.Service.Attributes; +using KRPC.Utils; + +// NOTE: This is basically just a reimplementation of KSP.Sim.impl/VesselVehicle with KRPC +// bindings. Implemented for consistency with krpc1. Also note that SpaceWarp +// VesselVehicleExtensions also provides something similar. +namespace KRPC2.SpaceCenter +{ + /// + /// Used to manipulate the controls of a vessel. This includes adjusting the + /// throttle, enabling/disabling systems such as SAS and RCS, or altering the + /// direction in which the vessel is pointing. + /// Obtained by calling . + /// + /// + /// Control inputs (such as pitch, yaw and roll) are zeroed when all clients + /// that have set one or more of these inputs are no longer connected. + /// + [KRPCClass(Service = "SpaceCenter2", GameScene = GameScene.Flight)] + public class Control : Equatable + { + readonly Guid vesselId; + + internal Control(VesselComponent vessel) + { + // TODO: Don't hold on to vessel, object. Instead lookup from scope + // by ID. See the setter for this property. + InternalVessel = vessel; + vesselId = vessel.GlobalId; + } + + /// + /// Returns true if the vesselIds are equal. + /// + public override bool Equals(Control other) + { + return !ReferenceEquals(other, null) && vesselId == other.vesselId; + } + + /// + /// Hash code for the object. + /// + public override int GetHashCode() + { + return vesselId.GetHashCode(); + } + + /// + /// The KSP vessel object. + /// + public VesselComponent InternalVessel { get; private set; } + + private void AtomicSet(FlightCtrlStateIncremental flightControlUpdate) + { + InternalVessel.ApplyFlightCtrlState(flightControlUpdate); + } + + /// + /// The state of the throttle. A value between 0 and 1. + /// + [KRPCProperty] + public float Throttle + { + get + { + return InternalVessel.flightCtrlState.mainThrottle; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + mainThrottle = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the roll control. A value between -1 and 1. + /// + [KRPCProperty] + public float Roll + { + get + { + return InternalVessel.flightCtrlState.roll; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + roll = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the yaw control. A value between -1 and 1. + /// + [KRPCProperty] + public float Yaw + { + get + { + return InternalVessel.flightCtrlState.yaw; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + yaw = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the pitch control. A value between -1 and 1. + /// + [KRPCProperty] + public float Pitch + { + get + { + return InternalVessel.flightCtrlState.pitch; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + pitch = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the roll trim control. A value between -1 and 1. + /// + [KRPCProperty] + public float RollTrim + { + get + { + return InternalVessel.flightCtrlState.rollTrim; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + rollTrim = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the yaw trim control. A value between -1 and 1. + /// + [KRPCProperty] + public float YawTrim + { + get + { + return InternalVessel.flightCtrlState.yawTrim; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + yawTrim = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the pitch trim control. A value between -1 and 1. + /// + [KRPCProperty] + public float PitchTrim + { + get + { + return InternalVessel.flightCtrlState.pitchTrim; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + pitchTrim = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the wheel steer control. A value between -1 and 1. + /// + [KRPCProperty] + public float WheelSteer + { + get + { + return InternalVessel.flightCtrlState.wheelSteer; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + wheelSteer = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the wheel steer trim control. A value between -1 and 1. + /// + [KRPCProperty] + public float WheelSteerTrim + { + get + { + return InternalVessel.flightCtrlState.wheelSteerTrim; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + wheelSteerTrim = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the wheel throttle control. A value between -1 and 1. + /// + [KRPCProperty] + public float WheelThrottle + { + get + { + return InternalVessel.flightCtrlState.wheelThrottle; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + wheelThrottle = value + }; + AtomicSet(incremental); + } + } + + /// + /// The state of the wheel throttle trim control. A value between -1 and 1. + /// + [KRPCProperty] + public float WheelThrottleTrim + { + get + { + return InternalVessel.flightCtrlState.wheelThrottleTrim; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + wheelThrottleTrim = value + }; + AtomicSet(incremental); + } + } + + /// + /// Whether the gear up control is active. + /// + [KRPCProperty] + public bool Gear + { + get + { + return InternalVessel.flightCtrlState.gearUp; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + gearUp = value, + gearDown = !value + }; + AtomicSet(incremental); + } + } + + /// + /// Whether the light control is active. + /// + [KRPCProperty] + public bool Lights + { + get + { + return InternalVessel.flightCtrlState.headlight; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + headlight = value + }; + AtomicSet(incremental); + } + } + + /// + /// Whether the brakes control is active. + /// + [KRPCProperty] + public bool Brakes + { + get + { + return InternalVessel.flightCtrlState.brakes; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + brakes = value + }; + AtomicSet(incremental); + } + } + } +} diff --git a/service/spacecenter/src/Vessel.cs b/service/spacecenter/src/Vessel.cs index 2ac8d66..f40f003 100644 --- a/service/spacecenter/src/Vessel.cs +++ b/service/spacecenter/src/Vessel.cs @@ -1,5 +1,6 @@ using System; using KSP.Sim.impl; +using KRPC.Service; using KRPC.Service.Attributes; using KRPC.Utils; @@ -59,5 +60,16 @@ public Orbit Orbit { get { return new Orbit(InternalVessel); } } + + /// + /// Returns a object that can be used to manipulate + /// the vessel's control inputs. For example, its pitch/yaw/roll controls, + /// RCS and thrust. + /// + [KRPCProperty(GameScene = GameScene.Flight)] + public Control Control + { + get { return new Control(InternalVessel); } + } } } diff --git a/tools/install.sh b/tools/install.sh index c641a7b..a53e2e5 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -ev -bazel build --verbose_failures //:krpc2 +bazel build --verbose_failures //:plugin_files rm -rf lib/ksp2/BepInEx/plugins/kRPC2 rm -f lib/ksp2/BepInEx/config/krpc2.cfg From 391173246c9747a7fc6d113f1917a47c2754e073 Mon Sep 17 00:00:00 2001 From: dylan madisetti Date: Tue, 28 Mar 2023 00:56:02 -0400 Subject: [PATCH 2/3] Clarified wheel is for Rovers + comment update --- service/spacecenter/src/Control.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/service/spacecenter/src/Control.cs b/service/spacecenter/src/Control.cs index d647872..468319d 100644 --- a/service/spacecenter/src/Control.cs +++ b/service/spacecenter/src/Control.cs @@ -18,8 +18,8 @@ namespace KRPC2.SpaceCenter /// Obtained by calling . /// /// - /// Control inputs (such as pitch, yaw and roll) are zeroed when all clients - /// that have set one or more of these inputs are no longer connected. + /// Control inputs (such as pitch, yaw and roll) are instantaneous (the same + /// as tapping a button) /// [KRPCClass(Service = "SpaceCenter2", GameScene = GameScene.Flight)] public class Control : Equatable @@ -201,7 +201,7 @@ public float PitchTrim } /// - /// The state of the wheel steer control. A value between -1 and 1. + /// The state of the wheel steer control for rovers. A value between -1 and 1. /// [KRPCProperty] public float WheelSteer @@ -221,7 +221,7 @@ public float WheelSteer } /// - /// The state of the wheel steer trim control. A value between -1 and 1. + /// The state of the wheel steer trim control for rovers. A value between -1 and 1. /// [KRPCProperty] public float WheelSteerTrim @@ -241,7 +241,7 @@ public float WheelSteerTrim } /// - /// The state of the wheel throttle control. A value between -1 and 1. + /// The state of the wheel throttle control for rovers. A value between -1 and 1. /// [KRPCProperty] public float WheelThrottle @@ -261,7 +261,7 @@ public float WheelThrottle } /// - /// The state of the wheel throttle trim control. A value between -1 and 1. + /// The state of the wheel throttle trim control for rovers. A value between -1 and 1. /// [KRPCProperty] public float WheelThrottleTrim From 7fd1ecd8724257d5d6788ebda2c78968166059c5 Mon Sep 17 00:00:00 2001 From: dylan madisetti Date: Tue, 28 Mar 2023 08:07:53 -0400 Subject: [PATCH 3/3] Removed dead endpoints --- .gitignore | 1 + service/spacecenter/src/Control.cs | 179 ++++++----------------------- 2 files changed, 35 insertions(+), 145 deletions(-) diff --git a/.gitignore b/.gitignore index e93e6e4..c1b9487 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ bazel-* # KSP2 lib/ksp2 +*.log # Visual Studio .vs/ diff --git a/service/spacecenter/src/Control.cs b/service/spacecenter/src/Control.cs index 468319d..7c92249 100644 --- a/service/spacecenter/src/Control.cs +++ b/service/spacecenter/src/Control.cs @@ -81,8 +81,14 @@ public float Throttle } /// - /// The state of the roll control. A value between -1 and 1. - /// + /// Instantaneous change to the roll control, and current roll state. A value between -1 and 1. + /// + /// + /// Reading this value provides the instantaneous roll control value. + /// Setting this value is the same as tapping q/e in stock KSP2. + /// The roll experienced by the vehicle will be RollTrim + Roll. KSP + /// will internally clamp this value between -1 and 1. + /// [KRPCProperty] public float Roll { @@ -101,8 +107,14 @@ public float Roll } /// - /// The state of the yaw control. A value between -1 and 1. + /// Instantaneous change to the yaw control, and current yaw state. A value between -1 and 1. /// + /// + /// Reading this value provides the instantaneous yaw control value. + /// Setting this value is the same as tapping a/d in stock KSP2. + /// The yaw experienced by the vehicle will be YawTrim + Yaw. KSP + /// will internally clamp this value between -1 and 1. + /// [KRPCProperty] public float Yaw { @@ -121,8 +133,14 @@ public float Yaw } /// - /// The state of the pitch control. A value between -1 and 1. + /// Instantaneous change to the pitch control, and current pitch state. A value between -1 and 1. /// + /// + /// Reading this value provides the instantaneous pitch control value. + /// Setting this value is the same as tapping w/s in stock KSP2. + /// The pitch experienced by the vehicle will be PitchTrim + Pitch. KSP + /// will internally clamp this value between -1 and 1. + /// [KRPCProperty] public float Pitch { @@ -143,6 +161,10 @@ public float Pitch /// /// The state of the roll trim control. A value between -1 and 1. /// + /// + /// This will set the roll control to have this value as a baseline. + /// For an instantaneous roll change, see . + /// [KRPCProperty] public float RollTrim { @@ -163,6 +185,10 @@ public float RollTrim /// /// The state of the yaw trim control. A value between -1 and 1. /// + /// + /// This will set the yaw control to have this value as a baseline. + /// For an instantaneous yaw change, see . + /// [KRPCProperty] public float YawTrim { @@ -183,6 +209,10 @@ public float YawTrim /// /// The state of the pitch trim control. A value between -1 and 1. /// + /// + /// This will set the pitch control to have this value as a baseline. + /// For an instantaneous pitch change, see . + /// [KRPCProperty] public float PitchTrim { @@ -199,146 +229,5 @@ public float PitchTrim AtomicSet(incremental); } } - - /// - /// The state of the wheel steer control for rovers. A value between -1 and 1. - /// - [KRPCProperty] - public float WheelSteer - { - get - { - return InternalVessel.flightCtrlState.wheelSteer; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - wheelSteer = value - }; - AtomicSet(incremental); - } - } - - /// - /// The state of the wheel steer trim control for rovers. A value between -1 and 1. - /// - [KRPCProperty] - public float WheelSteerTrim - { - get - { - return InternalVessel.flightCtrlState.wheelSteerTrim; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - wheelSteerTrim = value - }; - AtomicSet(incremental); - } - } - - /// - /// The state of the wheel throttle control for rovers. A value between -1 and 1. - /// - [KRPCProperty] - public float WheelThrottle - { - get - { - return InternalVessel.flightCtrlState.wheelThrottle; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - wheelThrottle = value - }; - AtomicSet(incremental); - } - } - - /// - /// The state of the wheel throttle trim control for rovers. A value between -1 and 1. - /// - [KRPCProperty] - public float WheelThrottleTrim - { - get - { - return InternalVessel.flightCtrlState.wheelThrottleTrim; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - wheelThrottleTrim = value - }; - AtomicSet(incremental); - } - } - - /// - /// Whether the gear up control is active. - /// - [KRPCProperty] - public bool Gear - { - get - { - return InternalVessel.flightCtrlState.gearUp; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - gearUp = value, - gearDown = !value - }; - AtomicSet(incremental); - } - } - - /// - /// Whether the light control is active. - /// - [KRPCProperty] - public bool Lights - { - get - { - return InternalVessel.flightCtrlState.headlight; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - headlight = value - }; - AtomicSet(incremental); - } - } - - /// - /// Whether the brakes control is active. - /// - [KRPCProperty] - public bool Brakes - { - get - { - return InternalVessel.flightCtrlState.brakes; - } - set - { - var incremental = new FlightCtrlStateIncremental() - { - brakes = value - }; - AtomicSet(incremental); - } - } } }