Skip to content

Instantly share code, notes, and snippets.

@IanSSenne
Last active December 2, 2020 05:57
Show Gist options
  • Save IanSSenne/3f3752013fc3e1448ce82357cb9fcf9a to your computer and use it in GitHub Desktop.
Save IanSSenne/3f3752013fc3e1448ce82357cb9fcf9a to your computer and use it in GitHub Desktop.
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VRage;
using VRage.Collections;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ObjectBuilders.Definitions;
using VRageMath;
namespace IngameScript
{
partial class Program : MyGridProgram
{
public float size = 80f;
public int stage = 1;
public float z = 0f;
public float INCREMENT = 4f;
public float x = 0f;
public List<IMyPistonBase> up_pistons;
public List<IMyPistonBase> down_pistons;
public List<IMyPistonBase> x_pistons ;
public List<IMyPistonBase> z_pistons;
bool RunPistonChainAction(string action, List<IMyPistonBase> pistons, float target, bool all = false)
{
foreach (IMyPistonBase piston in pistons)
{
if (piston.CurrentPosition != target)
{
float time = 0f;
if (piston.Velocity < 0)
{
time = (piston.CurrentPosition - piston.MinLimit) / piston.Velocity;
}
else
{
time = (piston.CurrentPosition - piston.MaxLimit) / piston.Velocity;
}
Echo("=============\n" + piston.CustomName + "\n" + piston.CurrentPosition + " >< " + target + "\n" + Math.Abs(Math.Ceiling(time)) + "s");
piston.GetActionWithName(action).Apply(piston);
if (!all)
{
return false;
}
}
}
if (all)
{
foreach (IMyPistonBase piston in pistons)
{
if (piston.CurrentPosition != target)
{
return false;
}
}
}
return true;
}
bool GotoLength(List<IMyPistonBase> pistons, float target)
{
int i = 0;
bool res = true;
foreach (IMyPistonBase piston in pistons)
{
float thisLength = Math.Min(piston.HighestPosition, Math.Max(target - piston.HighestPosition * i, piston.LowestPosition));
i++;
float time = 0f;
if (piston.Velocity < 0)
{
time = (piston.CurrentPosition - piston.MinLimit) / piston.Velocity;
}
else
{
time = (piston.CurrentPosition - piston.MaxLimit) / piston.Velocity;
}
Echo("=============\n" + piston.CustomName + "\n" + piston.CurrentPosition + " >< " + target + "\n" + Math.Abs(Math.Ceiling(time)) + "s");
if (piston.CurrentPosition > thisLength)
{
piston.MinLimit = thisLength;
piston.GetActionWithName("Retract").Apply(piston);
res = false;
}
else if (piston.CurrentPosition < thisLength)
{
piston.MaxLimit = thisLength;
piston.GetActionWithName("Extend").Apply(piston);
res = false;
}
}
return res;
}
bool GotoPosition()
{
bool x_ok = GotoLength(x_pistons, x);
bool z_ok = GotoLength(z_pistons, z);
Echo("x=" + x_ok + ",z=" + z_ok);
return x_ok && z_ok;
}
void GetPistonsByName(string name, List<IMyPistonBase> output)
{
bool found = true;
int index = 0;
while (found)
{
IMyTerminalBlock result = GridTerminalSystem.GetBlockWithName(name + "-" + index.ToString());
if (result != null)
{
output.Add((IMyPistonBase)result);
}
else
{
found = false;
}
index++;
}
Echo("Found " + output.Capacity + "!");
}
public void reset()
{
x_pistons = new List<IMyPistonBase>();
up_pistons = new List<IMyPistonBase>();
z_pistons = new List<IMyPistonBase>();
down_pistons = new List<IMyPistonBase>();
GetPistonsByName("Piston-up", up_pistons);
GetPistonsByName("Piston-x", x_pistons);
GetPistonsByName("Piston-z", z_pistons);
GetPistonsByName("Piston-down", down_pistons);
}
public Program()
{
x -= INCREMENT;
Runtime.UpdateFrequency = UpdateFrequency.Once | UpdateFrequency.Update1;
reset();
}
public void Save()
{
// Called when the program needs to save its state. Use
// this method to save your state to the Storage field
// or some other means.
//
// This method is optional and can be removed if not
// needed.
}
public void Main(string argument, UpdateType updateSource)
{
if (updateSource == UpdateType.Terminal)
{
Echo("Starting!");
stage = 1;
reset();
}
else if (updateSource == UpdateType.Update1)
{
Echo("stage=" + stage + ", pos=(" + x + "," + z + ")");
if (stage == 0 && GotoExtendedState(false))
{
stage = 1;
}
else if (stage == 1 && GotoRetractedState(true))
{
stage = 2;
}
else if (stage == 2)
{
x += INCREMENT;
if (x > size)
{
x = 0f;
z += INCREMENT;
}
if (z > size)
{
stage = -1;
Echo("done!");
}
stage = 3;
}
else if (stage == 3)
{
Echo("x=" + x + "\nz=" + z);
if (GotoPosition())
{
stage = 0;
}
}
}
}
public bool GotoRetractedState(bool all = false)
{
return RunPistonChainAction("Extend", up_pistons, 10f, all) && RunPistonChainAction("Retract", down_pistons, 0f, all);
}
public bool GotoExtendedState(bool all = false)
{
if (RunPistonChainAction("Retract", up_pistons, 0f, all))
{
if (RunPistonChainAction("Extend", down_pistons, 10f, all))
{
return true;
}
}
return false;
}
}
}
float T=80f;int U=1;float V=0f;float W=4f;float X=0f;List<IMyPistonBase>Y;List<IMyPistonBase>Z;List<IMyPistonBase>a;List
<IMyPistonBase>b;bool c(string d,List<IMyPistonBase>e,float f,bool Q=false){foreach(IMyPistonBase R in e){if(R.
CurrentPosition!=f){float C=0f;if(R.Velocity<0){C=(R.CurrentPosition-R.MinLimit)/R.Velocity;}else{C=(R.CurrentPosition-R.MaxLimit)/R.
Velocity;}Echo("=============\n"+R.CustomName+"\n"+R.CurrentPosition+" >< "+f+"\n"+Math.Abs(Math.Ceiling(C))+"s");R.
GetActionWithName(d).Apply(R);if(!Q){return false;}}}if(Q){foreach(IMyPistonBase R in e){if(R.CurrentPosition!=f){return false;}}}return
true;}bool g(List<IMyPistonBase>e,float f){int h=0;bool S=true;foreach(IMyPistonBase R in e){float B=Math.Min(R.
HighestPosition,Math.Max(f-R.HighestPosition*h,R.LowestPosition));h++;float C=0f;if(R.Velocity<0){C=(R.CurrentPosition-R.MinLimit)/R.
Velocity;}else{C=(R.CurrentPosition-R.MaxLimit)/R.Velocity;}Echo("=============\n"+R.CustomName+"\n"+R.CurrentPosition+" >< "+f+
"\n"+Math.Abs(Math.Ceiling(C))+"s");if(R.CurrentPosition>B){R.MinLimit=B;R.GetActionWithName("Retract").Apply(R);S=false;}
else if(R.CurrentPosition<B){R.MaxLimit=B;R.GetActionWithName("Extend").Apply(R);S=false;}}return S;}bool D(){bool E=g(a,X);
bool F=g(b,V);Echo("x="+E+",z="+F);return E&&F;}void G(string H,List<IMyPistonBase>I){bool J=true;int K=0;while(J){
IMyTerminalBlock L=GridTerminalSystem.GetBlockWithName(H+"-"+K.ToString());if(L!=null){I.Add((IMyPistonBase)L);}else{J=false;}K++;}Echo(
"Found "+I.Capacity+"!");}void M(){a=new List<IMyPistonBase>();Y=new List<IMyPistonBase>();b=new List<IMyPistonBase>();Z=new
List<IMyPistonBase>();G("Piston-up",Y);G("Piston-x",a);G("Piston-z",b);G("Piston-down",Z);}Program(){X-=W;Runtime.
UpdateFrequency=UpdateFrequency.Once|UpdateFrequency.Update1;M();}void Save(){}void Main(string N,UpdateType O){if(O==UpdateType.
Terminal){Echo("Starting!");U=1;M();}else if(O==UpdateType.Update1){Echo("stage="+U+", pos=("+X+","+V+")");if(U==0&&A(false)){U=
1;}else if(U==1&&P(true)){U=2;}else if(U==2){X+=W;if(X>T){X=0f;V+=W;}if(V>T){U=-1;Echo("done!");}U=3;}else if(U==3){Echo(
"x="+X+"\nz="+V);if(D()){U=0;}}}}bool P(bool Q=false){return c("Extend",Y,10f,Q)&&c("Retract",Z,0f,Q);}bool A(bool Q=false){
if(c("Retract",Y,0f,Q)){if(c("Extend",Z,10f,Q)){return true;}}return false;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment