Skip to content

Instantly share code, notes, and snippets.

@Larkenx
Created March 24, 2019 15:22
Show Gist options
  • Save Larkenx/2f88a139cb99e56d88a3f96514233da4 to your computer and use it in GitHub Desktop.
Save Larkenx/2f88a139cb99e56d88a3f96514233da4 to your computer and use it in GitHub Desktop.
Space Engineers to script to calculate ratio between maximum effective thrust and the gravitational force applied to your ship
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System;
using VRage.Collections;
using VRage.Game.Components;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ObjectBuilders.Definitions;
using VRage.Game;
using VRageMath;
namespace IngameScript
{
partial class Program : MyGridProgram
{
List<IMyShipController> shipControllers = new List<IMyShipController>();
List<IMyThrust> thrusters = new List<IMyThrust>();
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Update100;
}
public void Main()
{
// Grab all of the current ship controllers (only need to have one)
GridTerminalSystem.GetBlocksOfType<IMyShipController>(shipControllers);
if (shipControllers.Count >= 1)
{
IMyShipController shipController = shipControllers.First();
MyShipMass shipMass = shipController.CalculateShipMass();
float emptyShipMass = shipMass.BaseMass; // mass not including inventory / cargo
float currentTotalMass = shipMass.TotalMass; // mass including inventory cargo
// Grab all of the thrusters on the ship
GridTerminalSystem.GetBlocksOfType<IMyThrust>(thrusters);
// Calculate total max thrust in Newtons
float maxEffectiveThrust = 0;
float currentEffectiveThrust = 0;
foreach (IMyThrust thruster in thrusters)
{
if (thruster.Orientation.Forward == Base6Directions.Direction.Up) // only add the thruster if its grid thrust direction is "up"
{
// Grab max effective thrust for the current atmosphere, assuming the thruster is 100% perpendicular to the natural gravity well...
maxEffectiveThrust += thruster.MaxEffectiveThrust;
currentEffectiveThrust += thruster.CurrentThrust;
}
}
// TODO: Calculate maximum total mass by grabbing all blocks that have a Storage Capacity (this includes both input & output inventories!)
String lcdText = "";
int percentageBlocks = Convert.ToInt32(Math.Floor((maxEffectiveThrust / currentTotalMass) * 10));
int blocksToWrite = percentageBlocks >= 10 ? 20 : percentageBlocks <= 0 ? 0 : percentageBlocks * 2;
int blanksToWrite = 20 - blocksToWrite;
lcdText += $"Ship Current Total Gravitational Force:\n";
lcdText += $"{currentTotalMass.ToString("#,##0.00")}N\n";
lcdText += $"Ship Max Effective Thrust (Up):\n";
lcdText += $"{maxEffectiveThrust.ToString("#,##0.00")}N\n";
lcdText += $"Maximum Thrust Efficiency Ratio:\n";
lcdText += $"{(maxEffectiveThrust / currentTotalMass) * 100}% [{new String('|', blocksToWrite)}{new String('.', blanksToWrite)}]\n";
Echo(lcdText);
var thrustEfficiencyLCD = GridTerminalSystem.GetBlockWithName("ThrustLCD") as IMyTextPanel;
thrustEfficiencyLCD?.WritePublicText(lcdText);
}
else
{
Echo("No ship control stations or cockpits exist, cannot calculate thrust:gravitational force ratio!");
}
}
}
}
@AceKing13
Copy link

I tried using your code in game, kept getting issues when I attempted to compile it. Are you able to support?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment