Skip to content

Instantly share code, notes, and snippets.

@MatteoJoliveau
Last active August 26, 2018 21:26
Show Gist options
  • Save MatteoJoliveau/f08876067c2682bea4dda0d0997e7005 to your computer and use it in GitHub Desktop.
Save MatteoJoliveau/f08876067c2682bea4dda0d0997e7005 to your computer and use it in GitHub Desktop.
static string version = "v1.0.0";
IMyTextPanel screen_1;
IMyTextPanel screen_2;
List<IMyGasTank> oxygen_tanks = new List<IMyGasTank>();
List<IMyAirVent> air_vents = new List<IMyAirVent>();
static string screen_prefix = "Engineering Screen";
public Program()
{
Runtime.UpdateFrequency = UpdateFrequency.Update10;
//This makes the program automatically run every 10 ticks.
screen_1 = GridTerminalSystem.GetBlockWithName($"{screen_prefix} 1") as IMyTextPanel;
screen_2 = GridTerminalSystem.GetBlockWithName($"{screen_prefix} 2") as IMyTextPanel;
GridTerminalSystem.GetBlocksOfType<IMyGasTank>(oxygen_tanks, (tank => tank.CubeGrid == Me.CubeGrid && TankType(tank) == "oxygen"));
GridTerminalSystem.GetBlocksOfType<IMyAirVent>(air_vents, (vent => vent.CubeGrid == Me.CubeGrid));
}
public void Main(string argument, UpdateType updateSource)
{
Echo($"Engineering Room Software {version}");
var oxygen_ratio = FilledRatioMedium(oxygen_tanks);
var air_vents_status = AirVentsStatus(air_vents);
var failed_air_vents = FailingAirVents(air_vents);
string failed_vents_names = "";
string failed_vents_count = "";
if (failed_air_vents.Any())
{
failed_vents_names = failed_air_vents
.Select(vent => vent.CustomName)
.Select(name => $"- {name}")
.Aggregate((acc, name) => $"{acc}\n\t{name}");
failed_vents_count = $"{failed_air_vents.Count} Failed Vents";
}
var text = string.Format(@"AIR STATUS
Oxygen Reserves: {0}%
Air Vents: {1}
{2}
{3}
", oxygen_ratio * 100, air_vents_status, failed_vents_count, failed_vents_names);
UpdateText(screen_1, text);
}
private float FilledRatioMedium(List<IMyGasTank> tanks)
{
return tanks.Select(tank => (float) tank.FilledRatio).Sum() / tanks.Count;
}
private string AirVentsStatus(List<IMyAirVent> air_vents)
{
var operational = air_vents.Select(vent => vent.CanPressurize).Aggregate((acc, next) => acc && next);
return operational ? "ALL OPERATIONAL" : "ERROR";
}
private List<IMyAirVent> FailingAirVents(List<IMyAirVent> air_vents)
{
return air_vents.Where(vent => !vent.CanPressurize).ToList();
}
private void UpdateText(IMyTextPanel screen, string text)
{
screen.WritePublicText(text, false);
}
// returns the 'type' of the tank
string TankType(IMyTerminalBlock theBlock)
{
if (theBlock is IMyGasTank)
{
return theBlock.BlockDefinition.SubtypeId.Contains("Hydro") ? "hydrogen" : "oxygen";
}
return "NaT";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment