Last active
June 17, 2021 01:09
-
-
Save nailgun/2f2893a6ac64fe9915b1d5981a6f8911 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IMyCockpit Cockpit; | |
IMyShipController ShipController; | |
IMyTextSurface Screen1; | |
List<IMyThrust> Thrusters = new List<IMyThrust>(); | |
Vector3D LastV = new Vector3D(); | |
void debug(String str) { | |
Screen1.WriteText(str, true); | |
} | |
public Program() | |
{ | |
var cockpits = new List<IMyCockpit>(); | |
GridTerminalSystem.GetBlocksOfType<IMyCockpit>(cockpits); | |
Cockpit = cockpits[0]; | |
ShipController = Cockpit; | |
Screen1 = Cockpit.GetSurface(0); | |
GridTerminalSystem.GetBlocksOfType<IMyThrust>(Thrusters); | |
Runtime.UpdateFrequency = UpdateFrequency.Update1; | |
Screen1.ContentType = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE; | |
Screen1.FontSize = 1.5f; | |
} | |
public void Main(string arg, UpdateType updateSource) | |
{ | |
if (arg == "Start") { | |
ShipController.DampenersOverride = false; | |
Runtime.UpdateFrequency = UpdateFrequency.Update1; | |
} else if (arg == "Stop") { | |
foreach (var t in Thrusters) { | |
t.ThrustOverridePercentage = 0f; | |
} | |
ShipController.DampenersOverride = true; | |
Runtime.UpdateFrequency = UpdateFrequency.None; | |
return; | |
} | |
Run1(); | |
} | |
void Run1() | |
{ | |
Screen1.WriteText(""); | |
var thrustMap = new Dictionary<Vector3I,double>(); | |
var maxThrustMap = new Dictionary<Vector3I,double>(); | |
Vector3D g = ShipController.GetTotalGravity(); | |
float m = ShipController.CalculateShipMass().PhysicalMass; | |
Vector3D gf = g * m; | |
thrustMap[Vector3I.Forward] = gf.Dot(ShipController.WorldMatrix.Forward); | |
thrustMap[Vector3I.Left] = gf.Dot(ShipController.WorldMatrix.Left); | |
thrustMap[Vector3I.Up] = gf.Dot(ShipController.WorldMatrix.Up); | |
thrustMap[Vector3I.Backward] = -thrustMap[Vector3I.Forward]; | |
thrustMap[Vector3I.Right] = -thrustMap[Vector3I.Left]; | |
thrustMap[Vector3I.Down] = -thrustMap[Vector3I.Up]; | |
foreach (var tDir in thrustMap.Keys) { | |
maxThrustMap[tDir] = 0; | |
} | |
foreach (IMyThrust t in Thrusters) { | |
var tDir = t.GridThrustDirection; | |
maxThrustMap[tDir] += t.MaxEffectiveThrust; | |
} | |
foreach (IMyThrust t in Thrusters) { | |
var tDir = t.GridThrustDirection; | |
t.ThrustOverridePercentage = (float)(thrustMap[tDir] / maxThrustMap[tDir]); | |
} | |
} | |
void Run2() | |
{ | |
Screen1.WriteText(""); | |
Vector3D g = ShipController.GetTotalGravity(); | |
float m = ShipController.CalculateShipMass().PhysicalMass; | |
Vector3D gf = g * m; | |
var invMat = MatrixD.Invert(ShipController.WorldMatrix); | |
var gfLocal = Vector3D.Rotate(gf, invMat); | |
var maxThrustA = new Vector3(); | |
var maxThrustB = new Vector3(); | |
foreach (IMyThrust t in Thrusters) { | |
var tDir = t.GridThrustDirection; | |
if (tDir.X + tDir.Y + tDir.Z > 0) { | |
maxThrustA += tDir * t.MaxEffectiveThrust; | |
} else { | |
maxThrustB += tDir * -t.MaxEffectiveThrust; | |
} | |
} | |
var thrustRatioA = (Vector3)gfLocal / maxThrustA; | |
var thrustRatioB = (Vector3)gfLocal / maxThrustB; | |
foreach (IMyThrust t in Thrusters) { | |
var tDir = t.GridThrustDirection; | |
Vector3 thrustRatio; | |
if (tDir.X + tDir.Y + tDir.Z > 0) { | |
thrustRatio = thrustRatioA; | |
} else { | |
thrustRatio = thrustRatioB; | |
} | |
t.ThrustOverridePercentage = thrustRatio.Dot(tDir); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment