Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Translatron module #5

Merged
merged 7 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
118 changes: 118 additions & 0 deletions Translatron.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using KRPC.Service.Attributes;
using System.Reflection;

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 trans_spd_act;
private readonly FieldInfo trans_kill_h;
Genhis marked this conversation as resolved.
Show resolved Hide resolved

private readonly FieldInfo trans_spd;

private readonly MethodInfo setMode;
private readonly MethodInfo panicSwitch;

public Translatron() : base("Translatron")
{
var core = this.type.GetField("core").GetValue(this.instance);
this.thrustInstance = core.GetType().GetField("thrust").GetValue(core);
Genhis marked this conversation as resolved.
Show resolved Hide resolved
var thrustType = this.thrustInstance.GetType();

this.trans_spd_act = thrustType.GetField("trans_spd_act");
this.trans_kill_h = thrustType.GetField("trans_kill_h");
Genhis marked this conversation as resolved.
Show resolved Hide resolved

this.trans_spd = 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.trans_spd, this.instance);
set => EditableVariables.SetDouble(this.trans_spd, this.instance, value);
}

/// <summary>
/// Kill horizontal speed
/// </summary>
[KRPCProperty]
public bool KillHs
Genhis marked this conversation as resolved.
Show resolved Hide resolved
{
get => (bool)this.trans_kill_h.GetValue(this.thrustInstance);
set => this.trans_kill_h.SetValue(this.thrustInstance, (bool)value);
}

/// <summary>
/// Sets translatron mode
/// </summary>
/// <param name="tmode">TranslatronMode you want to use <see cref="TranslatronMode"/></param>
[KRPCMethod]
public void SetMode(TranslatronMode tmode)
Genhis marked this conversation as resolved.
Show resolved Hide resolved
{
this.setMode.Invoke(this.instance, new object[] { (int)tmode });
}

/// <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);
}

/// <summary>
/// Execute the selected action.
/// </summary>
[KRPCMethod]
public void Execute()
{
this.trans_spd_act.SetValue(this.thrustInstance, (float)TranslationSpeed);
Genhis marked this conversation as resolved.
Show resolved Hide resolved
}

[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
}
}
}