Skip to content

Instantly share code, notes, and snippets.

@TheMeinerLP
Created July 11, 2024 22:15
Show Gist options
  • Save TheMeinerLP/104e6d5fda7fa1d43609420d5cd563c4 to your computer and use it in GitHub Desktop.
Save TheMeinerLP/104e6d5fda7fa1d43609420d5cd563c4 to your computer and use it in GitHub Desktop.
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Update10;
}
public void Save()
{
// Save data when the script is terminated (if necessary).
}
// Configure the name of the hinge and the wheels
private string hingeName = "hinge1";
private string wheelGroupName = "wheel2";
public List<IMyMotorSuspension> getWheels()
{
// Reference all wheels
List<IMyMotorSuspension> wheels = new List<IMyMotorSuspension>();
GridTerminalSystem.GetBlocksOfType(wheels, wheel => wheel.CustomName.Contains(wheelGroupName));
if (wheels.Count == 0)
{
Echo("No wheels found!");
return null;
}
return wheels;
}
public void Main(string argument, UpdateType updateSource)
{
// Reference the control seat
IMyShipController controller = GridTerminalSystem.GetBlockWithName("maincontrol") as IMyShipController;
if (controller == null)
{
Echo("Control seat not found!");
return;
}
// Capture input from the control seat
bool isADown = controller.MoveIndicator.X < -0.5;
bool isDDown = controller.MoveIndicator.X > 0.5;
bool isWDown = controller.MoveIndicator.Z < -0.05;
bool isSDown = controller.MoveIndicator.Z > 0.05;
bool isPDown = controller.HandBrake;
if (!(isADown || isDDown || isWDown || isSDown || isPDown)) {
return;
}
List<IMyMotorSuspension> wheels = getWheels();
if (wheels == null) return;
// Reference the hinge block
IMyMotorStator hinge = GridTerminalSystem.GetBlockWithName(hingeName) as IMyMotorStator;
if (hinge == null)
{
Echo("Hinge not found!");
return;
}
// Control logic for the hinge
if (isADown)
{
hinge.TargetVelocityRPM = -5.0f; // Adjust speed as needed
}
else if (isDDown)
{
hinge.TargetVelocityRPM = 5.0f; // Adjust speed as needed
}
else
{
hinge.TargetVelocityRPM = 0.0f; // Stop the hinge if no key is pressed
}
// Control logic for the wheels
float wheelSpeed = 1.0f; // Speed of the wheels
foreach (var wheel in wheels)
{
if (isPDown)
{
wheel.Brake = true;
wheel.PropulsionOverride = 0.0f;
}
else
{
wheel.Brake = false;
if (isWDown)
{
wheel.PropulsionOverride = wheelSpeed;
}
else if (isSDown)
{
wheel.PropulsionOverride = -wheelSpeed;
}
else
{
wheel.PropulsionOverride = 0.0f; // Stop the wheel if no key is pressed
}
}
}
// Output for debugging
Echo("Hinge Velocity: " + hinge.TargetVelocityRPM);
Echo("Wheels Count: " + wheels.Count);
Echo("Move Forward: " + isWDown);
Echo("Move Backward: " + isSDown);
Echo("Handbrake: " + isPDown);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment