Skip to content

Instantly share code, notes, and snippets.

@YarekTyshchenko
Last active January 16, 2023 23:45
Show Gist options
  • Select an option

  • Save YarekTyshchenko/8d5760966fc1b5d56c20 to your computer and use it in GitHub Desktop.

Select an option

Save YarekTyshchenko/8d5760966fc1b5d56c20 to your computer and use it in GitHub Desktop.
Collection of Space Engineers C# Scripts
class Dispatch {
Action<string> log = delegate(string s) {};
Dictionary<string, Action> methods = new Dictionary<string, Action>();
public void addHandler(string method, Action action) {
methods.Add(method, action);
}
public void dispatch(string argument) {
if (! methods.ContainsKey(argument)) {
log("Argument '"+argument+"' doesn't exist in collection");
return;
}
Action action = methods[argument];
action();
}
public void setLogger(Action<string> logger) {
this.log = logger;
}
}
void Main(string arg) {
Dispatch d = new Dispatch();
d.setLogger(delegate(string s) {
Echo("Log: "+s);
});
d.addHandler("foo", delegate() {
Echo("Custom Foo action");
});
d.addHandler("", delegate() {
Echo("Default action");
});
d.dispatch(arg);
}
public class LCD {
IMyTextPanel lcd;
// Grid terminal system is needed here so the search happens in the right grid
public LCD(IMyGridTerminalSystem gts, string panelName) {
this.lcd = gts.GetBlockWithName(panelName) as IMyTextPanel;
this.lcd.ShowPublicTextOnScreen();
}
public LCD writeText(string text) {
this.lcd.WritePublicText(text, true);
return this;
}
public LCD writeLine(string line) {
this.writeText(line+"\n");
return this;
}
public LCD clear() {
this.lcd.WritePublicText("", false);
return this;
}
}
void Main(string arg) {
// Second parameter is the name of the panel to search for
LCD lcd = new LCD(GridTerminalSystem, "LCD Panel").clear();
lcd.writeLine("Speed: "+10);
lcd.writeLine("Seconds Delta: "+2);
}
public class SpeedCalculator {
public struct Store {
// Previous Time
public DateTime time;
// Previous position
public Vector3D position;
public static Store fromString(string storage) {
Store store = new Store();
// Get the fragments (or get one fragment)
string[] Fragments = storage.Split('|');
if(Fragments.Length == 2) {
// Extract time
store.time = System.DateTime.FromBinary(Convert.ToInt64(Fragments[0]));
// Vomit a bit here because this is how we have to store variables at the moment ...
string[] Coords = Fragments[1].Split(',');
double X = Math.Round(Convert.ToDouble(Coords[0]), 4);
double Y = Math.Round(Convert.ToDouble(Coords[1]), 4);
double Z = Math.Round(Convert.ToDouble(Coords[2]), 4);
store.position = new Vector3D(X, Y, Z);
} else {
store.time = System.DateTime.Now;
store.position = new Vector3D(0);
}
return store;
}
public string ToString() {
// Get coordinates (VRageMath.Vector3D, so pull it in the ugly way)
double x = Math.Round(this.position.GetDim(0), 4);
double y = Math.Round(this.position.GetDim(1), 4);
double z = Math.Round(this.position.GetDim(2), 4);
// Time|X,Y,Z
string newStorage = this.time.ToBinary().ToString() + "|" +
x.ToString() + "," + y.ToString() + "," + z.ToString();
return newStorage;
}
}
IMyTerminalBlock origin;
double delta = 0.0;
double speed = 0.0;
public SpeedCalculator(IMyTerminalBlock origin) {
this.origin = origin;
}
public double getDeltaSeconds() {
return this.delta;
}
public double getSpeed() {
return this.speed;
}
public void calculate(ref Store store) {
// Get times
DateTime TimeNow = System.DateTime.Now;
DateTime OldTime = store.time;
// We have 's' for m/s.
this.delta = (TimeNow - OldTime).TotalSeconds;
// Caluculate distance
Vector3D currentVector = this.origin.GetPosition();
double Distance = Vector3D.Distance(currentVector, store.position);
// If the base coordinates
if (Distance != 0 && this.delta != 0)
{
// We have our distance
double Speed = Distance / this.delta;
this.speed = Speed;
} else {
this.speed = 0.0;
}
// Update the store with current coordinates
store.time = TimeNow;
store.position = currentVector;
}
}
void Main(string arg) {
// Instantiate a speed calculator using the programming block for measurments
// You can use any block, something near the center of the ship so the rotation
// doesn't contribute to the overall speed.
SpeedCalculator sc = new SpeedCalculator((IMyTerminalBlock)Me);
// Storage malarky: Required because we need to store previous 3D Vector and time
// to calculate distance traveled and speed
SpeedCalculator.Store store = SpeedCalculator.Store.fromString(Storage);
sc.calculate(ref store);
Storage = store.ToString();
// Speed in Meters Per Second
double speed = sc.getSpeed();
// Seconds passed since last calculation
double delta = sc.getDeltaSeconds();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment