Skip to content

Commit

Permalink
Added Translatron window (#5)
Browse files Browse the repository at this point in the history
* Added Translatron module
* Changes according to review
* Update CHANGELOG.md
  • Loading branch information
vkribo authored and Genhis committed Jun 5, 2019
1 parent 53e740b commit 30179a3
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion ComputerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion MechJeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -97,6 +97,9 @@ internal static object GetComputerModule(string moduleType) {
[KRPCProperty]
public static SmartRCS SmartRCS => GetComputerModule<SmartRCS>(windows, 1);

[KRPCProperty]
public static Translatron Translatron => GetComputerModule<Translatron>(windows, 3);

// CONTROLLERS

[KRPCProperty]
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
106 changes: 106 additions & 0 deletions Translatron.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Reflection;

using KRPC.Service.Attributes;

namespace KRPC.MechJeb {
/// <summary>
/// The Translatron module controls the vessel's throttle/velocity.
/// </summary>
[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");
}

/// <summary>
/// Speed which trasnlatron will hold
/// </summary>
[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);
}
}

/// <summary>
/// Kill horizontal speed
/// </summary>
[KRPCProperty]
public bool KillHorizontalSpeed {
get => (bool)this.transKillH.GetValue(this.thrustInstance);
set => this.transKillH.SetValue(this.thrustInstance, value);
}

/// <summary>
/// Sets translatron mode
/// </summary>
[KRPCProperty]
public TranslatronMode Mode {
get => (TranslatronMode)this.tMode.GetValue(this.thrustInstance);
set => this.setMode.Invoke(this.instance, new object[] { (int)value });
}

/// <summary>
/// Abort mission by seperating all but the last stage and activating landing autopilot.
/// </summary>
[KRPCMethod]
public void PanicSwitch() {
this.panicSwitch.Invoke(this.instance, null);
}

[KRPCEnum(Service = "MechJeb")]
public enum TranslatronMode {
/// <summary>
/// Switch off Translatron.
/// </summary>
Off,

/// <summary>
/// Keep orbital velocity.
/// </summary>
KeepOrbital,

/// <summary>
/// Keep surface velocity.
/// </summary>
KeepSurface,

/// <summary>
/// Keep vertical velocity (climb/descent speed).
/// </summary>
KeepVertical,

/// <summary>
/// Internal mode, do not set.
/// </summary>
KeepRelative,

/// <summary>
/// Internal mode, do not set.
/// </summary>
Direct
}
}
}

0 comments on commit 30179a3

Please sign in to comment.