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 new file mode 100644 index 0000000..7c92249 --- /dev/null +++ b/service/spacecenter/src/Control.cs @@ -0,0 +1,233 @@ +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 instantaneous (the same + /// as tapping a button) + /// + [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); + } + } + + /// + /// 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 + { + get + { + return InternalVessel.flightCtrlState.roll; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + roll = value + }; + AtomicSet(incremental); + } + } + + /// + /// 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 + { + get + { + return InternalVessel.flightCtrlState.yaw; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + yaw = value + }; + AtomicSet(incremental); + } + } + + /// + /// 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 + { + 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. + /// + /// + /// This will set the roll control to have this value as a baseline. + /// For an instantaneous roll change, see . + /// + [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. + /// + /// + /// This will set the yaw control to have this value as a baseline. + /// For an instantaneous yaw change, see . + /// + [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. + /// + /// + /// This will set the pitch control to have this value as a baseline. + /// For an instantaneous pitch change, see . + /// + [KRPCProperty] + public float PitchTrim + { + get + { + return InternalVessel.flightCtrlState.pitchTrim; + } + set + { + var incremental = new FlightCtrlStateIncremental() + { + pitchTrim = 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