Last active
August 29, 2015 14:25
-
-
Save TerrorBite/6b5fe7f683e81c37441d to your computer and use it in GitHub Desktop.
This file contains 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
/* Piston Controller script for Space Engineers - by TerrorBite */ | |
const float TOLERANCE = 0.08F; | |
string status = ""; | |
Dictionary<string, string> Properties; | |
void LoadProperties() { | |
Properties = new Dictionary<string, string>(); | |
if(string.IsNullOrEmpty(Storage)) return; | |
string[] records = Storage.Split('\x1E'); | |
for(int i=0; i<records.Length; i++) { | |
string[] fields = records[i].Split('\x1F'); | |
Properties.Add(fields[0], fields[1]); | |
} | |
} | |
void SaveProperties() { | |
StringBuilder sb = new StringBuilder(); | |
var i = Properties.GetEnumerator(); | |
if(i.MoveNext()) sb.Append(string.Format("{0}\x1F{1}", i.Current.Key, i.Current.Value)); | |
while(i.MoveNext()) { | |
sb.Append("\x1E"); | |
sb.Append(string.Format("{0}\x1F{1}", i.Current.Key, i.Current.Value)); | |
} | |
} | |
void ElevatorSendTo(IMyPistonBase piston, float targetHeight) { | |
if(piston.CurrentPosition + TOLERANCE > targetHeight && | |
piston.CurrentPosition - TOLERANCE < targetHeight) { | |
Echo("Piston already at destination"); | |
return; | |
} | |
Properties[string.Format("{0}.target", piston.CustomName)] = targetHeight.ToString(); | |
// Turn on the piston | |
piston.ApplyAction("OnOff_On"); | |
// Calculate whether we need to travel up or down, set limits accordingly | |
if(targetHeight > piston.CurrentPosition) { | |
// we need to move upwards (extend) | |
piston.SetValue("UpperLimit", targetHeight); | |
if(piston.Velocity < 0) piston.ApplyAction("Reverse"); | |
} else { | |
// we need to move downwards (retract) | |
piston.SetValue("LowerLimit", targetHeight); | |
if(piston.Velocity > 0) piston.ApplyAction("Reverse"); | |
} | |
status = string.Format("Piston {0} {2} at {1}m/s", piston.CustomName, piston.Velocity, piston.Status); | |
Echo(status); | |
} | |
void Main(string argument) | |
{ | |
if(Properties==null) LoadProperties(); | |
string[] args = argument.Split(','); | |
if(args.Length < 2) return; | |
// Get requested piston | |
IMyPistonBase piston = GridTerminalSystem.GetBlockWithName(args[0]) as IMyPistonBase; | |
if(piston == null) { | |
Echo(string.Format("Piston block \"{0}\" was not found.", args[0])); | |
return; | |
} | |
ElevatorSendTo(piston, float.Parse(args[1])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment