From 30179a34a157101ae40bf869e2112e472317a5ce Mon Sep 17 00:00:00 2001 From: vkribo <39480910+vkribo@users.noreply.github.com> Date: Wed, 5 Jun 2019 15:10:07 +0200 Subject: [PATCH] Added Translatron window (#5) * Added Translatron module * Changes according to review * Update CHANGELOG.md --- CHANGELOG.md | 1 + ComputerModule.cs | 2 +- MechJeb.cs | 5 ++- README.md | 1 - Translatron.cs | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 Translatron.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c72673..7480d2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] ### Added - **SmartASS** window +- **Translatron** window ### Fixed - ValueError when setting enum properties ([#4](https://github.com/Genhis/KRPC.MechJeb/pull/4)) diff --git a/ComputerModule.cs b/ComputerModule.cs index 63dc98a..9eed382 100644 --- a/ComputerModule.cs +++ b/ComputerModule.cs @@ -11,7 +11,7 @@ public abstract class ComputerModule { private static MethodInfo usersAdd; private static MethodInfo usersRemove; - protected Type type; + protected internal Type type; protected internal readonly object instance; private readonly object users; diff --git a/MechJeb.cs b/MechJeb.cs index 97e7695..1381fd5 100644 --- a/MechJeb.cs +++ b/MechJeb.cs @@ -39,7 +39,7 @@ internal static bool InitInstance() { APIReady = false; modules = new object[5]; - windows = new object[3]; + windows = new object[4]; controllers = new object[7]; Instance = FlightGlobals.ActiveVessel.GetMasterMechJeb(); @@ -97,6 +97,9 @@ internal static object GetComputerModule(string moduleType) { [KRPCProperty] public static SmartRCS SmartRCS => GetComputerModule(windows, 1); + [KRPCProperty] + public static Translatron Translatron => GetComputerModule(windows, 3); + // CONTROLLERS [KRPCProperty] diff --git a/README.md b/README.md index 389958b..338db2b 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ so please create a new issue if something doesn't work for you. - Implement `AttitudeAdjustment` controller - Implement `RoverAutopilot` - Implement `SpaceplaneAutopilot` (known as Aircraft Approach & Autoland) -- Implement `Translation` module? ## Not implemented diff --git a/Translatron.cs b/Translatron.cs new file mode 100644 index 0000000..0d7b760 --- /dev/null +++ b/Translatron.cs @@ -0,0 +1,106 @@ +using System.Reflection; + +using KRPC.Service.Attributes; + +namespace KRPC.MechJeb { + /// + /// The Translatron module controls the vessel's throttle/velocity. + /// + [KRPCClass(Service = "MechJeb")] + public class Translatron : DisplayModule { + private readonly object thrustInstance; + private readonly FieldInfo transSpdAct; + private readonly FieldInfo transKillH; + private readonly FieldInfo tMode; + + private readonly FieldInfo transSpd; + + private readonly MethodInfo setMode; + private readonly MethodInfo panicSwitch; + + public Translatron() : base("Translatron") { + this.thrustInstance = MechJeb.ThrustController.instance; + var thrustType = MechJeb.ThrustController.type; + this.tMode = thrustType.GetField("tmode"); + + this.transSpdAct = thrustType.GetField("trans_spd_act"); + this.transKillH = thrustType.GetField("trans_kill_h"); + + this.transSpd = this.type.GetField("trans_spd"); + + this.setMode = this.type.GetMethod("SetMode"); + this.panicSwitch = this.type.GetMethod("PanicSwitch"); + } + + /// + /// Speed which trasnlatron will hold + /// + [KRPCProperty] + public double TranslationSpeed { + get => EditableVariables.GetDouble(this.transSpd, this.instance); + set { + EditableVariables.SetDouble(this.transSpd, this.instance, value); + this.transSpdAct.SetValue(this.thrustInstance, (float)value); + } + } + + /// + /// Kill horizontal speed + /// + [KRPCProperty] + public bool KillHorizontalSpeed { + get => (bool)this.transKillH.GetValue(this.thrustInstance); + set => this.transKillH.SetValue(this.thrustInstance, value); + } + + /// + /// Sets translatron mode + /// + [KRPCProperty] + public TranslatronMode Mode { + get => (TranslatronMode)this.tMode.GetValue(this.thrustInstance); + set => this.setMode.Invoke(this.instance, new object[] { (int)value }); + } + + /// + /// Abort mission by seperating all but the last stage and activating landing autopilot. + /// + [KRPCMethod] + public void PanicSwitch() { + this.panicSwitch.Invoke(this.instance, null); + } + + [KRPCEnum(Service = "MechJeb")] + public enum TranslatronMode { + /// + /// Switch off Translatron. + /// + Off, + + /// + /// Keep orbital velocity. + /// + KeepOrbital, + + /// + /// Keep surface velocity. + /// + KeepSurface, + + /// + /// Keep vertical velocity (climb/descent speed). + /// + KeepVertical, + + /// + /// Internal mode, do not set. + /// + KeepRelative, + + /// + /// Internal mode, do not set. + /// + Direct + } + } +}