Skip to content

Instantly share code, notes, and snippets.

@Keridos
Created March 8, 2017 00:17
Show Gist options
  • Save Keridos/c57329e4587c125e93be07fef5de4131 to your computer and use it in GitHub Desktop.
Save Keridos/c57329e4587c125e93be07fef5de4131 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using VRage.Collections;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ObjectBuilders.Definitions;
using VRageMath;
namespace Script {
public class Program {
IMyGridTerminalSystem GridTerminalSystem;
IMyProgrammableBlock Me;
private void Echo(String echo) {
}
//Actual Program
//Prefix and block definitions.
private String prefix;
private IMyDoor doorIn;
private IMyDoor doorOut;
private IMyAirVent ventIn;
private IMyAirVent ventOut;
private IMyTimerBlock timerIn;
private IMyTimerBlock timerOut;
private IMyInteriorLight light;
private IMySoundBlock sound;
//Colors
private static Color red = new Color(255, 0, 0);
private static Color green = new Color(0, 255, 0);
private bool soundPlaying = false; // fixing a small bug in the sound block
private bool shouldRefreshComponents = false;
//Utility functions.
private static void openDoor(IMyDoor door) {
door.GetActionWithName("Open").Apply(door);
}
private static void closeDoor(IMyDoor door) {
door.GetActionWithName("Open_Off").Apply(door);
}
private static void setPressurization(bool state, IMyAirVent ventIn, IMyAirVent ventOut) {
if (state) {
ventOut.Enabled = false;
ventIn.Enabled = true;
} else {
ventIn.Enabled = false;
ventOut.Enabled = true;
}
}
private static void setLightColor(Color color, bool blinking, IMyInteriorLight light) {
if (light != null) {
if (!blinking) {
light.BlinkLength = 100F;
light.SetValue("Color", color);
} else {
light.SetValue("Color", color);
light.BlinkLength = 50F;
}
}
}
private void setSoundPlaying(bool state, IMySoundBlock sound) {
if (sound != null) {
if (state && soundPlaying) {
sound.Play();
soundPlaying = true;
} else if (!state) {
sound.Stop();
soundPlaying = false;
}
}
}
private static int checkPressureStatus(IMyAirVent vent) {
string pressureInfo = vent.DetailedInfo;
float level = 0;
if (pressureInfo.IndexOf("Not pressurized") == -1) {
level = float.Parse(System.Text.RegularExpressions.Regex.Match(pressureInfo, @"Room pressure: (?<level>[0-9\.]+)%").Groups["level"].Value);
}
if (level == 0) {
return 0;
} else if (level >= 99) {
return 1;
}
return -1;
}
private static void startTimer(IMyTimerBlock timer) {
timer.GetActionWithName("Start").Apply(timer);
}
private static void setTimerDuration(float value, IMyTimerBlock timer) {
timer.TriggerDelay = value;
}
private String getPrefixFromCustomData() {
return Me.CustomData;
}
private bool checkForRequiredComponents() {
return !(doorIn == null || doorOut == null || ventIn == null || ventOut == null || timerIn == null || timerOut == null);
}
private void fixVentOutBug(IMyAirVent vent) {
if (vent != null) {
vent.Enabled = true;
vent.Enabled = false;
}
}
//init function
private void init() {
prefix = getPrefixFromCustomData();
doorIn = (IMyDoor) GridTerminalSystem.GetBlockWithName("Door " + prefix + "In");
doorOut = (IMyDoor) GridTerminalSystem.GetBlockWithName("Door " + prefix + "Out");
ventIn = (IMyAirVent) GridTerminalSystem.GetBlockWithName("Vent " + prefix + "In");
ventOut = (IMyAirVent) GridTerminalSystem.GetBlockWithName("Vent " + prefix + "Out");
timerIn = (IMyTimerBlock) GridTerminalSystem.GetBlockWithName("Timer " + prefix + "In");
timerOut = (IMyTimerBlock) GridTerminalSystem.GetBlockWithName("Timer " + prefix + "Out");
light = (IMyInteriorLight) GridTerminalSystem.GetBlockWithName("Light " + prefix);
sound = (IMySoundBlock) GridTerminalSystem.GetBlockWithName("Sound " + prefix);
}
//Constructor
public Program() {
init();
fixVentOutBug(ventOut);
}
//main function.
public void Main(string argument) {
if (argument == "refreshComponents") {
shouldRefreshComponents = true;
}
if (shouldRefreshComponents) {
init();
shouldRefreshComponents = false;
}
if (!checkForRequiredComponents()) {
Echo("Error, missing required Components");
shouldRefreshComponents = true;
return;
}
if (argument == "openInside" && timerOut.IsCountingDown) {
return;
} else if (argument == "openInside" && doorOut.Open) {
closeDoor(doorOut);
setLightColor(red, true, light);
setSoundPlaying(true, sound);
setTimerDuration(2.2f, timerIn);
startTimer(timerIn);
} else if (argument == "openInside" && checkPressureStatus(ventIn) == 1) {
doorIn.Enabled = true;
doorOut.Enabled = false;
setLightColor(green, false, light);
setSoundPlaying(false, sound);
ventIn.Enabled = false;
} else if (argument == "openInside" && checkPressureStatus(ventIn) <= 0) {
setLightColor(red, true, light);
setSoundPlaying(true, sound);
setPressurization(true, ventIn, ventOut);
doorIn.Enabled = false;
doorOut.Enabled = false;
setTimerDuration(1f, timerIn);
startTimer(timerIn);
} else if (argument == "openInside" && ventIn.Enabled && checkPressureStatus(ventIn) <= 0) {
setTimerDuration(1f, timerIn);
startTimer(timerIn);
} else if (argument == "openOutside" && timerIn.IsCountingDown) {
return;
} else if (argument == "openOutside" && doorIn.Open) {
closeDoor(doorIn);
setLightColor(red, true, light);
setSoundPlaying(true, sound);
setTimerDuration(2.2f, timerOut);
startTimer(timerOut);
} else if (argument == "openOutside" && checkPressureStatus(ventIn) == 0) {
doorOut.Enabled = true;
doorIn.Enabled = false;
setLightColor(green, false, light);
setSoundPlaying(false, sound);
ventOut.Enabled = false;
} else if (argument == "openOutside" && (checkPressureStatus(ventIn) == 1 || checkPressureStatus(ventIn) == -1) && !ventOut.Enabled) {
setLightColor(red, true, light);
setSoundPlaying(true, sound);
setPressurization(false, ventIn, ventOut);
doorIn.Enabled = false;
doorOut.Enabled = false;
setTimerDuration(1f, timerOut);
startTimer(timerOut);
} else if (argument == "openOutside" && ventOut.Enabled && checkPressureStatus(ventOut) != 0) {
setTimerDuration(1f, timerOut);
startTimer(timerOut);
} else if (argument == "closeDoors") {
closeDoor(doorIn);
closeDoor(doorOut);
}
}
//Actual Program End
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment