Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Programming Oxygen Gauge

Discussion in 'Programming (In-game)' started by Donald Seaton, Mar 29, 2020.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Donald Seaton Trainee Engineer

    Messages:
    5
    So I got this script, and it is not mine. However, it can be modified from a Hydrogen Gauge Script, to an Oxygen Gauge Script with some modification. I made some changes, and I was wondering where I am going wrong.



    // Space Engineers in-game scripting for Programmable block
    // public domain code by Sean L. Palmer

    // Relies on mod by Morphik with LCD images for fill percentages
    // [DX11] Hydrogen LCD Percentage Bars http://steamcommunity.com/sharedfiles/filedetails/?id=609123852
    // or can use this one instead:
    // [DX11] LCD Percentage Bars http://steamcommunity.com/sharedfiles/filedetails/?id=475922450

    // TROUBLESHOOTING:
    // If you get [OFFLINE] you probably don't have the mod installed, or it's using the wrong name for the
    // images, or you accidentally forgot the space at the end of the imagePrefix string, or perhaps
    // your Programmable Block doesn't have as much access as your LCD panel so can't modify the selection.
    // In that case try taking ownership of both blocks, or setting both owners to Nobody.
    // If it's stuck at 0% even though you know there's a Hydrogen gauge with gas, your Programmable Block
    // may not have enough access to check the Hydrogen Tank blocks fill levels, so taking ownership
    // would again be the proper solution.

    const string LCDName = "LCD Oxygen Gauge";

    const string imagePrefix = "Oxygen "; //"Percent_Invert "; // "Percent "; // NOTE included space character
    //const int nimages = 21; // percentage steps; 21 gives a step every 5%
    const int roundpercent = 1; //5; //(int)(100.0 / (nimages - 1)); //

    // TODO sound block alarm when fuel low
    //const string soundBlockName = "Sound Oxygen Low";
    //const int warnPercent = 30;

    float maxTanks; // in case tanks get destroyed
    int oldPercent = 0;

    IMyTextPanel lcd;
    //IMyTerminalBlock sound; //IMySoundBlock sound; // cast to IMySoundBlock breaks?!

    bool SameGrid(IMyTerminalBlock t) { return t.CubeGrid == Me.CubeGrid; }

    // TODO group!

    void Main(string argument)
    {
    string arglo = argument.ToLowerInvariant();
    if (arglo == "stop" || arglo == "reset") { // either Run with argument "reset"
    // or just recompile the script, same effect, but reset attempts to shut down gracefully
    Runtime.UpdateFrequency = UpdateFrequency.None;
    maxTanks = 0;
    oldPercent = 0;
    if (lcd != null) {
    lcd.WritePublicTitle("");
    lcd.ClearImagesFromSelection();
    lcd.ContentType = VRage.Game.GUI.TextPanel.ContentType.NONE; //
    }
    lcd = null;
    //if (sound != null)
    // sound.ApplyAction("Stop");
    //sound = null;
    return; // seems good idea
    }
    if (arglo == "go") {
    Runtime.UpdateFrequency = UpdateFrequency.Update100;
    }
    var blocks = new List<IMyTerminalBlock>();
    // ignoring tanks that aren't on the same grid! So attaching tanks via rotors
    // or pistons or connectors won't work, but manages to ignore any stations you dock with
    // and any tanks on ships that are docked to those stations. For stations it ignores
    // docked ships. If you think of a more elegant way to handle this, let me know.
    GridTerminalSystem.GetBlocksOfType<IMyGasTank>(blocks, SameGrid); //t => t.CubeGrid == Me.CubeGrid);
    double fuel = 0; double tanks = 0;
    for (int i = blocks.Count; --i >= 0; ) { //foreach (var b in blocks) // not sure, foreach may cause problems on Windows 7
    var b = blocks;
    // there is no distinct IMyOxygenTank
    var tank = b as IMyGasTank;
    //log = tank.BlockDefinition.SubtypeId;
    // it's either IMyOxygenTank but there may be mod-added tanks
    //if (tank.BlockDefinition.SubtypeId.Substring(5, 8) == "Oxygen") // only O2
    // To change this into an Oxygen gauge instead, just change this string to "Oxygen"!
    if (tank.BlockDefinition.SubtypeId.Contains("Oxygen")) { // only O2
    var cap = tank.Capacity;
    fuel += tank.FilledRatio * cap; // gas fill percentage
    tanks += cap;
    }
    }
    maxTanks = (float)Math.Max(maxTanks, tanks);
    var fill = (float)(fuel / maxTanks);
    if (tanks == 0) fill = 0;
    var roundfill = (int)Math.Round(fill * 100.0 / roundpercent) * roundpercent;
    string imagename = imagePrefix + roundfill.ToString("D3"); // zero pad field, 3 wide
    var log = "Oxygen " + (fill * 100).ToString("F0") + "%"; //fill.ToString("P1");
    Echo(log);
    if (lcd == null)
    lcd = GridTerminalSystem.GetBlockWithName(LCDName) as IMyTextPanel;
    if (lcd != null && !lcd.IsWorking) lcd = null;
    if (lcd != null) {
    lcd.WritePublicTitle(log);
    lcd.WriteText("");
    lcd.CustomData = log;
    lcd.ClearImagesFromSelection();
    lcd.ContentType = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE; //ShowTextureOnScreen();
    lcd.AddImageToSelection(imagename);
    }
    // var good = roundfill > warnPercent;
    // if ((oldPercent > warnPercent) != good) {
    // if (sound == null)
    // sound = GridTerminalSystem.GetBlockWithName(soundBlockName) as IMySoundBlock; // dynamic cast breaks?
    // if (sound != null)
    // sound.ApplyAction(good ? "Stop" : "Play");
    // }
    oldPercent = roundfill;
    }
     
Thread Status:
This last post in this thread was made more than 31 days old.