Skip to content

Instantly share code, notes, and snippets.

@jpcsupplies
Created March 27, 2016 13:06
Show Gist options
  • Save jpcsupplies/d99da5ca22e21b4f22ed to your computer and use it in GitHub Desktop.
Save jpcsupplies/d99da5ca22e21b4f22ed to your computer and use it in GitHub Desktop.
pb code for lcd readout
const string SHIPNAME = "My Ship";
const string VER = "SHIPDOS 0.33";
const bool EMERGENCY_DAMPENERS = true;
void Main(string argument) {
IMyTextPanel screen = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("LCD");
if (screen == null) { throw new Exception("No screen present"); }
IMyTextPanel oxyscreen = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("LCD-Oxy");
ScreenHelper scr = new ScreenHelper(screen, 36, 17);
MShip myShip = new MShip(GridTerminalSystem);
scr.setContent(myShip);
scr.Draw();
if (oxyscreen != null) {
ScreenHelper oxyscr = new ScreenHelper(oxyscreen, 36, 17);
oxyscr.setOxygenTankContent(myShip);
oxyscr.Draw(false);
}
}
public class MShip {
public string shipName;
public float batteryLevel;
public float cargoLevel;
public IMyGridTerminalSystem gts;
public string control;
public bool dampeners;
public bool docked;
public IMyShipController theHelm;
public float oxygenLevel;
public float[] oxygenTanks;
public MShip(IMyGridTerminalSystem gts) {
this.gts = gts;
this.shipName = SHIPNAME;
this.batteryLevel = getBatteryLevel();
this.cargoLevel = getCargoLevel();
this.oxygenLevel = getOxygenLevel();
getCockpitDetails();
this.docked = checkDockingStatus();
}
public float getOxygenLevel() {
List<IMyTerminalBlock> tanks = new List<IMyTerminalBlock>();
gts.GetBlocksOfType<IMyOxygenTank>(tanks);
IMyOxygenTank tank;
oxygenTanks = new float[tanks.Count];
float level = 0;
for (int i=0; i<tanks.Count; i++) {
tank = tanks[i] as IMyOxygenTank;
level += tank.GetOxygenLevel();
oxygenTanks[i] = tank.GetOxygenLevel();
}
if (tanks.Count > 0) { return level / tanks.Count; }
return 0;
}
public float getBatteryLevel() {
List<IMyTerminalBlock> batteries = new List<IMyTerminalBlock>();
gts.GetBlocksOfType<IMyBatteryBlock>(batteries);
IMyBatteryBlock battery;
float currentBatteryPower = 0;
float maxBatteryPower = 0;
for (int i=0; i<batteries.Count; i++) {
battery = batteries[i] as IMyBatteryBlock;
currentBatteryPower += battery.CurrentStoredPower;
maxBatteryPower += battery.MaxStoredPower;
}
if (maxBatteryPower != 0) { return currentBatteryPower/maxBatteryPower; }
return 0;
}
public float getCargoLevel() {
float usedStorage = 0, totalStorage = 0;
List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();
gts.GetBlocksOfType<IMyInventoryOwner>(containers);
for (int i=0; i < containers.Count; i++) {
if (containers[i] is IMyCargoContainer) {
usedStorage += (float)containers[i].GetInventory(0).CurrentVolume ;
totalStorage += (float)containers[i].GetInventory(0).MaxVolume;
}
}
if (totalStorage != 0.0) { return usedStorage/totalStorage; }
return 0;
}
void getCockpitDetails() {
this.control = "VACANT";
this.dampeners = false;
List<IMyTerminalBlock> controllers = new List<IMyTerminalBlock>();
gts.GetBlocksOfType<IMyShipController>(controllers);
IMyShipController controller;
for (int i=0; i<controllers.Count; i++) {
controller = controllers[i] as IMyShipController;
if (controller.IsUnderControl) { this.control = "CONTROLLED"; }
if (controller.DampenersOverride) { this.dampeners = true; }
}
if ((EMERGENCY_DAMPENERS) && (this.dampeners == false) && ( this.control != "CONTROLLED")) {
List<IMyTerminalBlock> econtrollers = new List<IMyTerminalBlock>();
gts.GetBlocksOfType<IMyShipController>(econtrollers);
IMyShipController econtrol;
for (int i=0; i<econtrollers.Count;i++) {
econtrol = econtrollers[i] as IMyShipController;
if (!econtrol.DampenersOverride) {
econtrol.GetActionWithName("DampenersOverride").Apply(econtrol);
}
}
}
}
public bool checkDockingStatus() {
List<IMyTerminalBlock> connectors = new List<IMyTerminalBlock>();
gts.GetBlocksOfType<IMyShipConnector>(connectors);
IMyShipConnector connector;
for (int i=0; i<connectors.Count; i++) {
connector = connectors[i] as IMyShipConnector;
if (connector.IsLocked) { return true; }
}
return false;
}
}
public class ScreenHelper {
private IMyTextPanel panel;
private int COLS;
private int ROWS;
const int HEADERS = 1;
const int FOOTERS = 1;
private string contents;
private string header;
private string footer;
public ScreenHelper(IMyTextPanel panel, int cols, int rows) {
this.panel = panel;
this.COLS = cols;
this.ROWS = rows;
this.contents = "";
this.header = VER;
}
public void setContent(MShip ship) {
const string ON = "ON"; const string OFF = "OFF";
const string YES = "YES"; const string NO = "NO";
string dampeners, emergencyDampeners;
string dockedText;
if (ship.dampeners) { dampeners = ON; } else { dampeners = OFF; }
if (ship.docked) { dockedText = YES; } else { dockedText = NO; }
if (Program.EMERGENCY_DAMPENERS) { emergencyDampeners = ON; } else { emergencyDampeners = OFF; }
contents = drawBar(ship.batteryLevel, COLS) + " BATT\n";
contents += drawBar(ship.cargoLevel, COLS) + " CARG\n";
contents += drawBar(ship.oxygenLevel, COLS) + "OXYG\n";
contents += "Emergency Dampeners: " + emergencyDampeners + "\n";
contents += "Dampeners: " + dampeners + "\n";
contents += "Control: " + ship.control + "\n";
contents += "Docked: " + dockedText + "\n";
contents += new String('\n', ROWS-12);
}
public void setOxygenTankContent(MShip ship) {
this.header = "OXYGEN TANK STATUS";
contents = "";
for (int i=0; i < ship.oxygenTanks.Length; i++) {
contents += drawBar(ship.oxygenTanks[i], COLS) + "\n";
}
}
public void Draw(bool showFooter = true) {
panel.WritePublicText("", false);
string line = "";
string dashline = new String('-', 60) + "\n";
panel.WritePublicText(header+ "\n", true);
panel.WritePublicText(dashline, true);
panel.WritePublicText(contents + "\n", true);
if (showFooter) {
panel.WritePublicText(dashline, true);
panel.WritePublicText(footer, true);
}
panel.ShowPublicTextOnScreen();
}
public void DrawOxygenTanks() {
panel.WritePublicText("", false);
string line = "";
string dashline = new String('-', 60) + "\n";
panel.WritePublicText("OXYGEN TANKS\n", true);
panel.ShowPublicTextOnScreen();
}
string drawBar(double percent, int width) {
string bar = "<";
for (int i=0; i<(width-2); i++) {
double barPos = (double)i/width;
if (percent >= barPos) { bar += "|"; } else { if (i % 2 == 0) { bar += "''";} }
}
if ((bar.Length + 1) < width) { bar += "'"; }
bar += ">[" + percent.ToString("P0").PadLeft(5) + "]";
return bar;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment