Skip to content

Instantly share code, notes, and snippets.

@se5a
Created September 25, 2021 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save se5a/e56dbe2aa78336b9715d86b5f9517d48 to your computer and use it in GitHub Desktop.
Save se5a/e56dbe2aa78336b9715d86b5f9517d48 to your computer and use it in GitHub Desktop.
landercalc
using System;
using System.Collections.Generic;
using UnityEngine;
namespace LanderCalc
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class LanderCalc: MonoBehaviour
{
private string _targetName = "";
private string _timeToBurn = "";
private DialogGUIBase[] _engines = new DialogGUIBase[0];
/// <summary>
/// Called once and is where any setup or “on-spawn” code should go.
/// This method runs when your class spawns into the correct scene and runs before all other methods (except Awake()).
/// Note that all Start() methods that need to run in a scene run sequentially,
/// if you are trying to reference another object it may not exist yet and could require using a Coroutine to delay running some of your code.
/// </summary>
public void Start()
{
}
/// <summary>
/// Called every update frame and is in sync with the actual frame that gets drawn on the monitor.
/// If KSP is running at 60fps, this method will get called 60 times a second,
/// once for each frame on the monitor.
/// Anything code relating to the UI or the player interacts with should go here.
/// </summary>
public void Update()
{
if (HighLogic.LoadedScene == GameScenes.FLIGHT)
{
var r1 = new Rect(0.5f, 0.5f, 150f, 60f);
List<DialogGUIBase> widgets = new List<DialogGUIBase>();
var dg1 = new DialogGUILabel(
"Target: " + _targetName, false, false);
var dg2 = new DialogGUILabel(
"Time To DeorbitBurn: " + _timeToBurn, false, false);
widgets.Add(dg1);
widgets.Add(dg2);
foreach (var engine in _engines)
{
widgets.Add(engine);
}
var mod = new MultiOptionDialog(
"name",
"msg",
"title",
HighLogic.UISkin,
r1,
widgets.ToArray()
);
PopupDialog.SpawnPopupDialog(
new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f),
mod,
false,
HighLogic.UISkin);
}
}
/// <summary>
/// Called every physics frame and anything physics or game related should happen here.
/// </summary>
public void FixedUpdate()
{
var av = FlightGlobals.ActiveVessel;
var tv = FlightGlobals.activeTarget;
_targetName = tv.name;
var avSurfSpd = av.srf_velocity.magnitude;
var avAlt = av.altitude;
var tvAlt = tv.vessel.altitude;
var h = avAlt - tvAlt;
var avOrbSpd = av.obt_speed;
var avmass = av.totalMass;
float burnRate = 0;
var engines = av.FindPartModulesImplementing<ModuleEngines>();
if (_engines.Length != engines.Count * 3)
{
_engines = new DialogGUIBase[engines.Count * 3];
}
float avev = 0;
int i = 0;
foreach (var aveng in engines)
{
var dg1 = new DialogGUILabel(
"Engine: " + i, false, false);
var dg2 = new DialogGUILabel(
"Staged: " + aveng.staged, false, false);
var dg3 = new DialogGUILabel(
"Shutdown: " + aveng.engineShutdown, false, false);
_engines[i] = dg1;
_engines[i + 1] = dg2;
_engines[i + 2] = dg3;
if (aveng.staged && !aveng.engineShutdown)
{
avev = aveng.realIsp;
var props = aveng.propellants;
foreach (Propellant propellant in props)
{
burnRate += propellant.ratio;
}
}
i++;
}
double dryMass = avmass / Math.Exp(avOrbSpd / avev);
double fuelUse = avmass - dryMass;
double tburn = fuelUse / burnRate;
string br = "BurnRate: " + burnRate + "\n";
string tb = "Ttt: " + tburn;
_timeToBurn = tburn.ToString();
//Debug.Log(br + tb);
}
/// <summary>
/// Runs when the class is destroyed. Important for unsubscribing from GameEvents and destroying Toolbar buttons.
/// </summary>
public void OnDisable()
{
}
public void OnSave(ConfigNode node)
{
}
public void OnLoad(ConfigNode node)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment