Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Cats makes the best programmers!
- * A start on an Life Support management script
- *
- * SHIP_PREFIX sets the block name prefix (only blocks with the right prefix will be effected by the script or set null to disable filtering)
- * The following are also important to set up the way you want them and have descriptions beside them (they are located at the top of the script)
- * O2_SYSTEM_TAG, LIGHT_TAG, SOUND_TAG
- * TANK_O2_MIN, TANK_O2_MAX, TANK_O2_CRITICAL
- *
- * LCD_DISPLAY_TAG The keyword that identifies panels to be used for display (null uses all panels)
- *
- * LCD_DISPLAY_FONT_COL Sets the colour of the text on LCD panels
- * LCD_DISPLAY_COLOUR = Sets the colour of the Background on LCD panels
- * LCD_FONT_SIZE Sets the Size of the text on LCD panels
- *
- * 201903121103
- */
- bool GB_Debug = false;
- bool GB_Terminal = true;
- // false: controls only the grid that this PB is on
- // true: control grids adjasant to grid PB is on by rotors/pistols
- bool GB_ControlSubGrids = true;
- string GS_O2_GenTag = "[O2]"; // tag for controled O2 Generators
- string GS_O2_TankTag = "[O2]"; // tag for watched tanks
- string GS_O2_LCDTankTag = "[O2-T]"; // LCD Tag foor tanks
- string GS_O2_LCDVentTag = "[O2-V]"; // LCD Tag for vent
- string GS_O2_LCDTag = "[O2]"; // LCD Tag for both
- Color LCD_DISPLAY_FONT_COL = new Color( 0,255,0 ); // determins the colour of the text
- Color LCD_DISPLAY_COLOUR = new Color( 0,1,0 ); // determins the colour of the background.
- float LCD_FONT_SIZE = 0.8f; // determins the size of the text.
- string GS_O2_SountTag = "[O2]"; // sound block Tag
- string[] GA_SoundAlert = { "SoundblockAlert2", "Alert 2" }; // alert sound to use
- // INDICATOR LIGHT VARIABLES
- string GS_O2GenIndTag = "[O2Gen]"; // tag for Generator Light
- string GS_LightTag = "[O2]"; // tag for Tank level Lights
- const int TANK_O2_MAX = 25; // Tank pressure at which to turn off 02 generation (This needs to be equal to or greater than TANK_O2_MIN and below 100% if you want depressurisation to work)
- const int TANK_O2_MIN = 15; // Tank pressure level at which to turn on 02 generation (The minimum value you want to have in your tank at all times)
- const int TANK_O2_CRITICAL = 10; // Tank Pressure at which the alarms go off. (Be sure this is at least 5% below TANK_O2_MIN)
- // Variable Past Here are set by the script
- int GI_NumOfTanks = 0;
- double GD_Tank_Total = 0;
- double GD_Tank_Store = 0;
- int GI_NumOfStore = 0;
- double GD_Tank_Avail = 0;
- int GI_NumOfFree = 0;
- bool GB_O2Gen_Active = false;
- int GI_O2Gen_Reset = 0;
- bool GB_O2_Alert = false;
- double GD_Tank_History = -1;
- double GD_Tank_CoT = 0;
- // customising this will alter the display info
- public string buildFullDisplay() {
- var sb = new StringBuilder( );
- sb.Append( buildTankDisplay() );
- sb.Append( '\n' );
- return sb.ToString();
- }
- // customising this will alter the display info
- public string buildTankDisplay() {
- var sb = new StringBuilder( "___O2_Tank_Data___\n" );
- sb.Append( "Store: " ).Append( GD_Tank_Store.ToString("000.0") ).Append( "% In " ).Append( GI_NumOfStore ).Append( '\n' );
- sb.Append( "Avail: " ).Append( GD_Tank_Avail.ToString("000.0") ).Append( "% In " ).Append( GI_NumOfFree ).Append( '\n' );
- sb.Append( "Total: " ).Append( GD_Tank_Total.ToString("000.0") ).Append( "% In " ).Append( GI_NumOfTanks ).Append( '\n' );
- sb.Append( "\n___O2_Gen_Data___\n" );
- sb.Append( "O2 Gens Active: " ).Append( GB_O2Gen_Active ).Append( '\n' );
- sb.Append( "O2 Alert Active: " ).Append( GB_O2_Alert ).Append( '\n' );
- return sb.ToString();
- }
- // customising this will alter the display info
- public Program() {
- if( !GB_Debug ) {
- Echo = text => {}; // disable echo
- }
- Runtime.UpdateFrequency = UpdateFrequency.Update100;
- Setup();
- Terminal();
- }
- public void Save() {
- Terminal();
- }
- public void Main( string strIn, UpdateType trig ) {
- Echo( "RUN" );
- CheckTankStatus(
- out GD_Tank_Total, out GI_NumOfTanks,
- out GD_Tank_Avail, out GI_NumOfFree,
- out GD_Tank_Store, out GI_NumOfStore
- );
- GI_O2Gen_Reset -= 1;
- if( GD_Tank_Avail <= TANK_O2_CRITICAL ) {
- ProcAlert( GD_Tank_CoT, GD_Tank_History, GD_Tank_Avail );
- if( !GB_O2Gen_Active ) {
- GB_O2Gen_Active = true;
- SetOxygenProduction( GB_O2Gen_Active );
- SetLightColor( new Color( 255, 0, 0 ), GS_O2GenIndTag, 1f, 50f, 50f ); // set gen light
- GI_O2Gen_Reset = 100;
- } else if( GI_O2Gen_Reset <= 0 ) {
- SetOxygenProduction( GB_O2Gen_Active );
- SetLightColor( new Color( 255, 0, 0 ), GS_O2GenIndTag, 1f, 50f, 50f ); // set gen light
- GI_O2Gen_Reset = 100;
- }
- } else if( GD_Tank_Avail <= TANK_O2_MIN ) {
- if( GB_O2_Alert ) {
- ProcAlert( GD_Tank_CoT, GD_Tank_History, GD_Tank_Avail );
- }
- if( !GB_O2Gen_Active ) {
- GB_O2Gen_Active = true;
- SetOxygenProduction( GB_O2Gen_Active );
- SetLightColor( new Color( 255, 0, 0 ), GS_O2GenIndTag, 1f, 50f, 50f ); // set gen light
- GI_O2Gen_Reset = 100;
- } else if( GI_O2Gen_Reset <= 0 ) {
- SetOxygenProduction( GB_O2Gen_Active );
- SetLightColor( new Color( 255, 0, 0 ), GS_O2GenIndTag, 1f, 50f, 50f ); // set gen light
- GI_O2Gen_Reset = 100;
- }
- } else if( GD_Tank_Avail >= TANK_O2_MAX ){
- if( GB_O2_Alert ) {
- ProcAlert( GD_Tank_CoT, GD_Tank_History, GD_Tank_Avail );
- }
- if ( GB_O2Gen_Active ) {
- GB_O2Gen_Active = false;
- SetOxygenProduction( GB_O2Gen_Active );
- SetLightColor( new Color( 0, 255, 0 ), GS_O2GenIndTag, 3f, 33.333f, 33.333f );
- GI_O2Gen_Reset = 100;
- } else if( GI_O2Gen_Reset <= 0 ) {
- SetOxygenProduction( GB_O2Gen_Active );
- SetLightColor( new Color( 0, 255, 0 ), GS_O2GenIndTag, 3f, 33.333f, 33.333f );
- GI_O2Gen_Reset = 100;
- }
- }
- GD_Tank_CoT = (GD_Tank_CoT*0.75) + ((GD_Tank_Avail-GD_Tank_History)*0.25);
- GD_Tank_History = GD_Tank_Avail;
- Print();
- }
- /*
- public string RunLockCheck( string cmd ) {
- StringBuilder Output = new StringBuilder();
- IMyTerminalBlock Light = GridTerminalSystem.GetBlockWithName( LIGHT_LCK );
- IMyTerminalBlock Vent = GridTerminalSystem.GetBlockWithName( VENT_LCK );
- IMyTerminalBlock DoorI = GridTerminalSystem.GetBlockWithName( LOCK_DOOR_INT );
- IMyTerminalBlock DoorE = GridTerminalSystem.GetBlockWithName( LOCK_DOOR_EXT );
- if( DoorI == null || DoorE == null ) {
- Output.Append( "Airlock: Door(s) Not Found" );
- if( AIRLOCK_STATUS != -1 ) {
- (DoorI as IMyDoor).CloseDoor();
- (DoorE as IMyDoor).CloseDoor();
- AIRLOCK_STATUS = -1;
- }
- } else if( AIRLOCK_STATUS == -1 ) {
- AIRLOCK_STATUS = 2;
- (DoorI as IMyDoor).CloseDoor();
- (DoorE as IMyDoor).CloseDoor();
- Output.Append( "Airlock: Priming" );
- } else if( Light != null && Vent != null ) {
- double Pressure = (Vent as IMyAirVent).GetOxygenLevel() * 100;
- bool LightStatus = ((IMyFunctionalBlock)Light).Enabled;
- if( AIRLOCK_STATUS == 1 && LightStatus != true ) {
- Output.Append( "Airlock: Cycling (Out)" );
- AIRLOCK_STATUS = 2;
- (DoorI as IMyDoor).CloseDoor();
- (DoorE as IMyDoor).CloseDoor();
- } else if ( AIRLOCK_STATUS == 2 ) {
- Output.Append( "Airlock: Cycling (Out)" );
- if( !(Vent as IMyAirVent).Depressurize ) {
- (Vent as IMyAirVent).Depressurize = true;
- } else if( Pressure == 0 ) {
- (DoorE as IMyDoor).CloseDoor();
- //SealDoor( DoorE, false );
- AIRLOCK_STATUS = 3;
- }
- } else if( AIRLOCK_STATUS == 3 && LightStatus != false ) {
- Output.Append( "Airlock: Cycling (In) A" );
- AIRLOCK_STATUS = 4;
- (DoorI as IMyDoor).CloseDoor();
- (DoorE as IMyDoor).CloseDoor();
- } else if( AIRLOCK_STATUS == 4 ) {
- Output.Append( "Airlock: Cycling (In) B" );
- if( (Vent as IMyAirVent).Depressurize ) {
- Output.Append( "\nErr" );
- (Vent as IMyAirVent).Depressurize = false;
- } else if( Pressure == 100 ) {
- Output.Append( "\nGood" );
- (DoorI as IMyDoor).OpenDoor();
- AIRLOCK_STATUS = 1;
- }
- } else {
- if( AIRLOCK_STATUS == 1 ) {
- Output.Append( "Airlock: Pressurized" );
- } else if( AIRLOCK_STATUS == 3 ) {
- Output.Append( "Airlock: UnPressurized" );
- }
- }
- } else {
- if( Light == null ) {
- Output.Append( "\nAirlock: Light Not Found" );
- }
- if( Vent == null ) {
- Output.Append( "\nAirlock: Vent Not Found" );
- }
- }
- return Output.ToString();
- }
- */
- public void ProcAlert( double CoT, double his, double cur ) {
- Echo( "M: "+ CoT.ToString( "0.00" ) +" : "+ his.ToString( "0.00" ) +" : "+ cur.ToString( "0.00" ) );
- if( !GB_O2_Alert && ( CoT < 0 ) ) {
- GB_O2_Alert = true;
- SetSound( true, GS_O2_SountTag );
- SetLightColor( new Color( 255, 0, 0 ), GS_LightTag, 3, 1.5f, 0 );
- } else if ( GB_O2_Alert && ( CoT > 0 ) ){
- GB_O2_Alert = false;
- SetSound( false, GS_O2_SountTag );
- SetLightColor( new Color( 0, 0, 255 ), GS_LightTag );
- }
- }
- /*
- public void checkVent() {
- GL_VentData = new List<CAT_VentState>();
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyAirVent>( blocks, x=> IsSubjectGrid( x ) && x.CustomName.Contains( GS_O2_VentTag ) ); // IMyOxygenGenerator
- for( int i = 0; i < blocks.Count; ++i ) {
- var vent = blocks[i] as IMyAirVent;
- var data = new CAT_VentState( vent.CustomName, vent.GetOxygenLevel()*100, !vent.Depressurize, vent.CanPressurize );
- GL_VentData.Add( data );
- }
- }
- */
- int GI_LCD_Tick = 0;
- public void Print() {
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>( blocks,
- x=> IsSubjectGrid( x )
- && ( x.CustomName.Contains( GS_O2_LCDTag )
- || x.CustomName.Contains( GS_O2_LCDTankTag )
- || x.CustomName.Contains( GS_O2_LCDVentTag )
- ) );
- IMyTextSurface ref_a = null; // see blow
- IMyTextSurface ref_b = null; // see blow
- if( blocks.Count != 0 ) {
- for( int i=0; i<blocks.Count; ++i ) {
- var block = blocks[i];
- var t = block as IMyTextSurfaceProvider;
- if( t == null || t.SurfaceCount <= 0 ) {
- // SKIP IF NO SURFACE
- continue;
- }
- var lcd = t.GetSurface( 0 ); // non-first surfaces not considered yet
- // this may be an issue for displays in cockpits and the like
- if( block.CustomName.Contains( GS_O2_LCDTankTag ) ) {
- if( ref_b == null ) {
- ref_b = lcd;
- lcd.WriteText( buildTankDisplay() );
- } else {
- lcd.WriteText( ref_b.GetText() );
- }
- }
- else {
- if( ref_a == null ) {
- ref_a = lcd;
- lcd.WriteText( buildFullDisplay() );
- } else {
- lcd.WriteText( ref_a.GetText() );
- }
- }
- if( GI_LCD_Tick == 0 ) {
- lcd.ContentType = ContentType.TEXT_AND_IMAGE; // the one line I forgot. Oups.
- lcd.FontColor = LCD_DISPLAY_FONT_COL;
- lcd.BackgroundColor = LCD_DISPLAY_COLOUR;
- lcd.FontSize = LCD_FONT_SIZE;
- }
- }
- } else {
- Echo( "Err: Display(s) Not Found" );
- }
- GI_LCD_Tick -= 1;
- if( GI_LCD_Tick < 0 ) {
- GI_LCD_Tick = 30;
- }
- }
- // called on program load and game save
- // nothing but getting the PB to show what it is running
- public void Terminal() {
- if( !GB_Terminal ) {
- return;
- }
- try {
- var sb = new StringBuilder( "CAT Script\nO2 Management" );
- var img = "LCD_Economy_Trinity";
- var pro = Me as IMyTextSurfaceProvider;
- var lcd = pro.GetSurface( 0 );
- lcd.ClearImagesFromSelection();
- lcd.AddImageToSelection( img, true );
- //sb.Append( "\nA: "+ lcd.Alignment.ToString() );
- lcd.Alignment = TextAlignment.CENTER;
- //sb.Append( "\nB: "+ lcd.Font );
- lcd.Font = "Monospace";
- //sb.Append( "\nT: "+ lcd.ContentType );
- lcd.ContentType = ContentType.TEXT_AND_IMAGE;
- lcd.PreserveAspectRatio = false;
- lcd.TextPadding = 10.0f;
- lcd.FontColor = new Color( 0,255,0 );
- lcd.BackgroundColor = new Color( 0,0,0 );
- lcd.FontSize = 1.5f;
- lcd.WriteText( sb.ToString() );
- } catch( Exception e ) {
- Echo( "Caught: "+ e );
- }
- }
- /*
- * Run Once in a perfect world
- * Set Default States
- */
- public void Setup() {
- SetOxygenProduction( false );
- GB_O2_Alert = false;
- SetSound( false, GS_O2_SountTag );
- SetLightColor( new Color( 255, 0, 255 ), GS_LightTag );
- CheckTankStatus(
- out GD_Tank_Total, out GI_NumOfTanks,
- out GD_Tank_Avail, out GI_NumOfFree,
- out GD_Tank_Store, out GI_NumOfStore
- );
- GD_Tank_History = GD_Tank_Avail;
- //if( ENABLE_AIRLOCK_NAMAGER ) {
- //DoorTest();
- //}
- }
- // FIX01
- public int SetOxygenProduction( bool on ) {
- var block = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyGasGenerator>( block, x=> IsSubjectGrid( x ) && x.CustomName.Contains( GS_O2_GenTag ) );
- if( block.Count > 0 ) {
- string Act;
- if( on ) {
- Act = "OnOff_On";
- } else {
- Act = "OnOff_Off";
- }
- for( int e = 0; e < block.Count; e++ ) {
- //if( (block[e] as IMyFunctionalBlock).Enabled != on ) { // minimuse number of block updates?
- block[e].ApplyAction( Act );
- //}
- }
- }
- return block.Count;
- }
- public double CheckTankStatus( out double Total, out int NoT, out double Free, out int NoF, out double Stored, out int NoS ) {
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyGasTank>( blocks, x => IsSubjectGrid( x ) && x.CustomName.Contains( GS_O2_TankTag ) && x.BlockDefinition.TypeIdString.Contains( "OxygenTank" ) );
- double TP;
- Total = 0;
- Free = 0;
- Stored = 0;
- NoF = 0;
- NoS = 0;
- for( int e = 0; e < blocks.Count; e++ ) {
- var tank = blocks[e] as IMyGasTank;
- TP = tank.FilledRatio;
- if( tank.Stockpile ) {
- Stored += TP;
- NoS += 1;
- } else {
- Free += TP;
- NoF += 1;
- }
- }
- NoT = NoS+NoF;
- Total = 0;
- if( NoT != 0 ) {
- Total = ((Stored+Free)/NoT) * 100;
- }
- if( NoF != 0 ) {
- Free = (Free/NoF) * 100;
- }
- if( NoS != 0 ) {
- Stored = (Stored/NoS) * 100;
- }
- return Total;
- }
- void SetSound( bool on, string tag = null ) {
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMySoundBlock>( blocks, x => IsSubjectGrid( x ) && x.CustomName.Contains( tag ) );
- if( blocks.Count != 0 ) {
- string act = "PlaySound";
- if( !on ) {
- act = "StopSound";
- }
- for( int e = 0; e < blocks.Count; e++ ) {
- var sound = blocks[e] as IMySoundBlock;
- Echo( "Sound: "+ sound.SelectedSound );
- if( sound.SelectedSound != GA_SoundAlert[0] ) {
- sound.SelectedSound = GA_SoundAlert[1];
- }
- blocks[e].ApplyAction( act );
- }
- }
- }
- public void SetLightColor( Color col, string tag = null, float blink=0, float hold=0, float offset=0 ) {
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyLightingBlock>( blocks, x => IsSubjectGrid( x ) && x.CustomName.Contains( tag ) );
- for( int e = 0; e < blocks.Count; e++ ) {
- var light = blocks[e] as IMyLightingBlock;
- light.Color = col;
- light.BlinkIntervalSeconds = blink;
- light.BlinkLength = hold;
- light.BlinkOffset = offset;
- }
- }
- ///////////////////////
- //// FILTERS ////
- ///////////////////////
- public bool IsSubjectGrid( IMyTerminalBlock block ) {
- // massivly simplified thanks to more current avilable built in grid filtering methods
- if( GB_ControlSubGrids ) {
- return Me.CubeGrid.IsSameConstructAs( block.CubeGrid );
- }
- return IsLocalGrid( block );
- }
- /* FILTER
- * for grid refferane of blocks
- */
- public bool IsLocalGrid( IMyTerminalBlock block ) { // seperated out to make it clear what it is doing
- if( block.CubeGrid == Me.CubeGrid ) {
- return true;
- }
- return false;
- }
- /*
- * DEBUGGING TOOLS
- */
- private string DumpProps( IMyTerminalBlock block ) {
- List<ITerminalProperty> props = new List<ITerminalProperty>();
- block.GetProperties( props );
- StringBuilder sb = new StringBuilder( "__PROPS__" ).Append( Environment.NewLine );
- for( int i = 0; i < props.Count; i++ ) {
- sb.Append( props[i].Id +" : "+ props[i].TypeName ).Append( Environment.NewLine );
- }
- return sb.ToString();
- }
- private string DumpActs( IMyTerminalBlock block ) {
- List<ITerminalAction> acts = new List<ITerminalAction>();
- block.GetActions( acts );
- StringBuilder sb = new StringBuilder( "__ACTS__" ).Append( Environment.NewLine );
- for( int i = 0; i < acts.Count; i++ ) {
- sb.Append( acts[i].Id +" : "+ acts[i].Name.ToString() ).Append( Environment.NewLine );
- }
- return sb.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement