Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Cats makes the best programmers!
- * Laser Relay Script
- * Use a ship with 2 or more laser antennae to keep in touch with far flung outposts without the need to actualy go there.
- * Transfer data from one location to another and allow scripts to act upon that data imediatly.
- * For example, set up your second base with a sensor. have the sensor status appended to the name of a laser antenna pointed at a relay. the relay can then forward that message on to your main base where the alarms go off! The distance between the two locations can be up to twice the range of the laser antenna
- * Please note: a value is null if value=null; _Not_ if value="null";
- *
- * 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)
- * LCD_DISPLAY_TAG The keyword that identifies panels to be used for display (null uses all panels)
- *
- * LASER_ANTENNA_TAG Identifies the Antennae to use for sending/receiving
- * SOUND_BLOCK the sound block to trigger when there is a new message (null disables)
- * OUTGOING_TAG The Tag identifying the LCD that's public title is the outgoing message (null disables)
- *
- * USER_SENSOR The Sensor that tells when a friend is near (null disables)
- * FOE_SENSOR The sensor that tells when an enemy is near (null disables)
- *
- * MAX_JUMPS the maximum number of jumps data can make before it expires
- *
- * 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
- *
- */
- const string SHIP_PREFIX = "[CAT]"; // The ship prefix (the bit of text that appears before all blocks. Set null to disable)
- const string LCD_DISPLAY_TAG = "[DTA]";// The tag that identifies panels to be used (null means all pannels!)
- const string LASER_ANTENNA_TAG = "[MIR]"; // Laser Antennae to use as recovers / transmitters
- const string SOUND_BLOCK = null; // sound block to trigger when new message received
- const string OUTGOING_TAG = "[OUT]"; // Read message from this LCD Display
- const string USER_SENSOR = null; // The tag that identifies the Friend sensor (or null for off).
- const string FOE_SENSOR = null; // The tag that identifies the Enemy sensor (or null for off).
- const int MAX_JUMPS = 6;
- /* PANEL VARIABLES */
- public Color LCD_DISPLAY_FONT_COL = new Color( 255, 0, 255 ); // Colour for LCD font
- public Color LCD_DISPLAY_COLOUR = new Color( 20, 0, 20 ); // Colour for LCD background
- public Single LCD_FONT_SIZE = (Single)1.0; // Font Size
- // HEY THERE: Variables beyond this point are set by the script itself and so don't need to be edited directly
- bool SET = false; // To help with Do Once actions. //Do Not Set True// The script needs this
- public CATPanel PANEL = null; // The script sets this
- Dictionary<string,int> DATA_LOG = new Dictionary<string,int> {};
- bool FRIEND;
- bool ENEMY;
- const int COOLDOWN_LENGTH = 6;
- int COOLDOWN;
- const int COUNTDOWN_LENGTH = 10;
- int COUNTDOWN;
- int ROT_POS;
- /*
- * vvv METHODS START HERE vvv
- */
- /* This is a mostly pointless wrapper, but what the heck */
- public class CATPanel {
- private List<IMyTextPanel> MyPanels = null;
- /* Constructor */
- public CATPanel( IMyTextPanel block ) {
- MyPanels = new List<IMyTextPanel>();
- AddPanel( block );
- }
- /* OVERLOAD */
- public CATPanel( IMyTerminalBlock block ) {
- MyPanels = new List<IMyTextPanel>();
- AddPanel( block );
- }
- /* OVERLOAD */
- public CATPanel( List<IMyTerminalBlock> blocks ) {
- MyPanels = new List<IMyTextPanel>();
- AddPanels( blocks );
- }
- /* Add Panel */
- public void AddPanel( IMyTextPanel block ) {
- if( block != null ) {
- MyPanels.Add( block );
- }
- }
- /* OVERLOAD */
- public void AddPanel( IMyTerminalBlock block ) {
- if( block != null ) {
- AddPanel( (IMyTextPanel)block );
- }
- }
- /* Add a range of Panels */
- public void AddPanels( List<IMyTerminalBlock> blocks ) {
- if( blocks != null ) {
- for( int e = 0; e < blocks.Count; e++ ) {
- if( !MyPanels.Contains( (IMyTextPanel)blocks[e] ) ) {
- AddPanel( (IMyTextPanel)blocks[e] );
- }
- }
- }
- }
- /* Remove Panel */
- public void RemovePanel( IMyTextPanel block ) {
- if( block != null ) {
- MyPanels.Remove( block );
- }
- }
- /* OVERLOAD */
- public void RemovePanel( IMyTerminalBlock block ) {
- RemovePanel( (IMyTextPanel)block );
- }
- /* Get Public Title */
- public string GetTitle() {
- if( MyPanels.Count >= 1 ) {
- return MyPanels[0].GetPublicTitle();
- } else {
- return null;
- }
- }
- /* Set Public Title */
- public int SetTitle( string txt, bool append ) {
- int count = 0;
- int error = 0;
- for( int e = 0; e < MyPanels.Count; e++ ) {
- if( MyPanels[e].WritePublicTitle( txt, append ) ) {
- count += 1;
- } else {
- error -= 1;
- }
- }
- if( error != 0 ) {
- return error;
- } else {
- return count;
- }
- }
- /* Get Public Title */
- public string GetText() {
- if( MyPanels.Count >= 1 ) {
- return MyPanels[0].GetPublicText();
- } else {
- return null;
- }
- }
- /* Set Public TextWriter */
- public int SetText( string txt, bool append ) {
- int count = 0;
- int error = 0;
- for( int e = 0; e < MyPanels.Count; e++ ) {
- if( MyPanels[e].WritePublicText( txt, append ) ) {
- TogglePanel( MyPanels[e] );
- count += 1;
- } else {
- error -= 1;
- }
- }
- if( error != 0 ) {
- return error;
- } else {
- return count;
- }
- }
- /* Set Font Size */
- public void SetFontSize( Single size ) {
- for( int e = 0; e < MyPanels.Count; e++ ) {
- MyPanels[e].SetValue( "FontSize", size );
- }
- }
- /* Set Font Colour */
- public void SetFontColor( Color col ) {
- for( int e = 0; e < MyPanels.Count; e++ ) {
- MyPanels[e].SetValue( "FontColor", col );
- }
- }
- /* Set Background Colour */
- public void SetBackColor( Color col ) {
- for( int e = 0; e < MyPanels.Count; e++ ) {
- MyPanels[e].SetValue( "BackgroundColor", col );
- }
- }
- /* Display Text */
- public void SetDispayText( bool disText ) {
- for( int e = 0; e < MyPanels.Count; e++ ) {
- if( disText ) {
- MyPanels[e].ShowPublicTextOnScreen();
- } else {
- MyPanels[e].ShowTextureOnScreen();
- }
- }
- }
- public void SetStatus( bool on ) {
- if( MyPanels.Count != 0 ) {
- string Act = "OnOff_On";
- if( !on ) {
- Act = "OnOff_Off";
- }
- var Action = ((IMyTerminalBlock)MyPanels[0]).GetActionWithName( Act );
- if( Action != null ) {
- for( int e = 0; e < MyPanels.Count; e++ ) {
- Action.Apply( MyPanels[e] );
- }
- }
- }
- }
- public void TogglePanel( IMyTextPanel Block ) {
- if( Block != null && Block.Enabled ) {
- var off = Block.GetActionWithName( "OnOff_Off" );
- var on = Block.GetActionWithName( "OnOff_On" );
- if( off != null && on != null ) {
- off.Apply( Block );
- on.Apply( Block );
- }
- }
- }
- }
- /* END OF PANNEL */
- /* Create the Pandel Handler if it doesn't exist */
- public CATPanel BuildMyPanel( ) {
- CATPanel output = null;
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>( blocks );
- if( blocks.Count > 0 ) {
- if( LCD_DISPLAY_TAG == null && SHIP_PREFIX == null) {
- output = new CATPanel( blocks );
- } else {
- var panels = new List<IMyTerminalBlock>();
- for( int e = 0; e < blocks.Count; e++ ) {
- if( ValidPrefix( blocks[e].CustomName ) && blocks[e].CustomName.Contains( LCD_DISPLAY_TAG ) ) {
- panels.Add( blocks[e] );
- }
- }
- if( panels.Count > 0 ) {
- output = new CATPanel( panels );
- }
- }
- }
- if( output != null ) {
- output.SetFontColor( LCD_DISPLAY_FONT_COL );
- output.SetBackColor( LCD_DISPLAY_COLOUR );
- output.SetFontSize( LCD_FONT_SIZE );
- output.SetText( "Cats makes the best programmers!", false );
- }
- return output;
- }
- /* DISPLAY STUFF */
- public bool Display( string txt ) {
- int end = 0;
- bool pan = true;
- if( PANEL != null ) {
- end = PANEL.SetText( txt, false );
- } else {
- PANEL = BuildMyPanel();
- if( PANEL != null ) {
- end = PANEL.SetText( txt, false );
- } else {
- pan = false;
- }
- }
- if( !pan ) {
- BackupDisplay( txt );
- } else {
- PANEL.SetDispayText( true );
- }
- if( end > 0 ) {
- return true;
- } else {
- return false;
- }
- }
- /* OVERLOAD */
- public bool Display( string[] txt ) {
- string txtOut = "";
- for( int e = 0; e < txt.Length; e++ ) {
- txtOut += ">> "+ txt[e] +'\n';
- }
- return Display( txtOut );
- }
- /* Mostly used for debugging or if call else fails */
- public void BackupDisplay( string txt ) {
- var blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyBeacon>( blocks );
- if( blocks.Count >= 1 ) {
- for( int e = 0; e < blocks.Count; e++ ) {
- if( SHIP_PREFIX != null ) {
- if( blocks[e].CustomName.StartsWith( SHIP_PREFIX ) ) {
- blocks[e].SetCustomName( SHIP_PREFIX +'\n'+ txt );
- }
- } else {
- blocks[e].SetCustomName( ">> "+ txt );
- }
- }
- }
- }
- /* VERIFY A BLOCK HAS THE CORECT PREFIX */
- public bool ValidPrefix( string BlockName, string Prefix = SHIP_PREFIX ) {
- if( SHIP_PREFIX == null || BlockName.StartsWith( Prefix ) ) {
- return true;
- } else {
- return false;
- }
- }
- /*
- * The Core function
- */
- public void Update() {
- Status();
- if( COOLDOWN >= 1 ) {
- COOLDOWN -= 1;
- } else {
- List<IMyTerminalBlock> Blocks = GetTargetBlocks<IMyLaserAntenna>( LASER_ANTENNA_TAG, SHIP_PREFIX );
- DATA_LOG = BuildMessageLog( Blocks );
- string End = "Laser Comm Status";
- string Outgoing = GetOutgoingMessage( );
- string Broadcast = "/";
- if( Outgoing != null ) {
- End += '\n' +"Outgoing Msg: "+ Outgoing;
- if( DATA_LOG.ContainsKey( Outgoing ) ) {
- DATA_LOG[ Outgoing ] = 0;
- } else {
- DATA_LOG.Add( Outgoing, 0 );
- }
- } else {
- End += '\n' +"Outgoing Msg: None";
- }
- int val;
- string[] Keys = new string[DATA_LOG.Count];
- DATA_LOG.Keys.CopyTo( Keys, 0 );
- for( int e = 0; e < Keys.Length; e++ ) {
- if( DATA_LOG.TryGetValue( Keys[e], out val ) ) {
- if( val < MAX_JUMPS ) {
- val++;
- Broadcast += "/"+ val +"#"+ Keys[e];
- }
- }
- }
- UpdateMsgBoard( Outgoing );
- string[] ParseData;
- for( int e = 0; e < Blocks.Count; e++ ) {
- if( Blocks[e] != null ) {
- ParseData = ParseName( Blocks[e].CustomName );
- if( !Broadcast.Equals( "/" ) ) {
- if( !Broadcast.Equals( ParseData[1] ) ) {
- Blocks[e].SetCustomName( ParseData[0] + Broadcast );
- ToggleBlock( Blocks[e] );
- End += '\n'+ ParseData[0] +" - Update";
- COOLDOWN = COOLDOWN_LENGTH;
- COUNTDOWN = COUNTDOWN_LENGTH;
- } else {
- End += '\n'+ ParseData[0] +" - Persist";
- }
- } else if( ParseData[1] != null ) {
- Blocks[e].SetCustomName( ParseData[0] );
- ToggleBlock( Blocks[e] );
- End += '\n'+ ParseData[0] +" - Clear";
- COOLDOWN = COOLDOWN_LENGTH;
- COUNTDOWN = COUNTDOWN_LENGTH;
- } else {
- End += '\n'+ ParseData[0] +" - Good";
- }
- }
- }
- if( COUNTDOWN <= 0 && Blocks.Count > 0 ) {
- if( ROT_POS >= Blocks.Count ) {
- ROT_POS = 0;
- }
- ResetConnection( Blocks[ROT_POS] );
- ROT_POS++;
- } else {
- COUNTDOWN -= 1;
- }
- Display( Report( " Comm Report", Keys, End ) );
- }
- }
- /*
- * Builds a Dictionary of inbound messages with their associated jump count
- */
- public Dictionary<string,int> BuildMessageLog( List<IMyTerminalBlock> Blocks ) {
- Dictionary<string,int> Output = new Dictionary<string,int> {};
- int errors = 0;
- int Index;
- string Data;
- string[] SplitData;
- bool New = false;
- for( int e = 0; e < Blocks.Count; e++ ) {
- Data = Blocks[e].DetailedInfo;
- Index = Data.IndexOf( "//" );
- if( Index != -1 && Index < (Data.Length-2) ) {
- Data = Data.Substring( Index+2 );
- SplitData = Data.Split( '/' );
- for( int j = 0; j < SplitData.Length; j++ ) {
- var msg = SplitData[j].Split( '#' );
- if( msg.Length == 2 ) {
- int val;
- if( Int32.TryParse( msg[0], out val ) && val > 0 ) {
- if( New != true && !msg[1].Equals( "RESET" ) && !DATA_LOG.ContainsKey( msg[1] ) ) {
- New = true;
- }
- int test;
- if( Output.TryGetValue( msg[1], out test ) ) {
- if( test > val ) {
- Output[msg[1]] = val;
- }
- } else {
- Output.Add( msg[1], val );
- }
- } else {
- errors++;
- }
- } else {
- errors++;
- }
- }
- }
- }
- if( New ) {
- Sound();
- }
- return Output;
- }
- /*
- * Produce a string with a start middle and end
- */
- public string Report( string TopTag, string[] Msgs, string End ) {
- string Output = "";
- if( TopTag != null ) {
- Output += TopTag +'\n';
- }
- if( Msgs != null && Msgs.Length > 0 ) {
- for( int e = 0; e < Msgs.Length; e++ ) {
- if( !Msgs[e].Equals( "RESET" ) ) {
- Output += Msgs[e] +'\n';
- }
- }
- }
- if( End != null ) {
- Output += '\n'+ End;
- }
- return Output;
- }
- /*
- * Because the system isnt perfect
- */
- public void ResetConnection( IMyTerminalBlock Block ) {
- string name = Block.CustomName;
- Block.SetCustomName( name +"//"+ (MAX_JUMPS-1) +"#RESET" );
- ToggleBlock( Block );
- COUNTDOWN = COUNTDOWN_LENGTH;
- }
- /*
- * Find all the The target Type with matches Keyword and Prefix
- */
- public List<IMyTerminalBlock> GetTargetBlocks<T>( string Keyword, string Prefix = null ) {
- List<IMyTerminalBlock> Output = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> Blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<T>( Blocks );
- for( int e = 0; e < Blocks.Count; e++ ) {
- if( ValidPrefix( Blocks[e].CustomName ) && Keyword != null && Blocks[e].CustomName.Contains( Keyword ) ) {
- Output.Add( Blocks[e] );
- }
- }
- return Output;
- }
- /*
- * Parse the name to get both the base name and the message
- */
- public string[] ParseName( string Name ) {
- string[] Output = new string[2]{ null, null };
- if( Name != null ) {
- int Index = Name.IndexOf( "//" );
- if( Index == -1 ) {
- Output[0]= Name;
- } else if( Index < (Name.Length) ) {
- Output[0] = Name.Substring( 0, Index );
- Output[1] = Name.Substring( Index );
- } else {
- Output[0] = Name +"//ERR";
- Output[1] = null;
- }
- }
- return Output;
- }
- /*
- * Get the outgoing message from the Msg LCD and Sensors
- */
- public string GetOutgoingMessage() {
- string Output = null;
- string id = "";
- if( SHIP_PREFIX != null ) {
- id = SHIP_PREFIX +"&";
- }
- if( OUTGOING_TAG != null ) {
- List<IMyTerminalBlock>Blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>( Blocks );
- for( int e = 0; e < Blocks.Count; e++ ) {
- if( Blocks[e].CustomName.Contains( OUTGOING_TAG ) && ValidPrefix( Blocks[e].CustomName ) ) {
- Output = ((IMyTextPanel)Blocks[e]).GetPublicTitle();
- if( Output.Length == 0 ) {
- Output = null;
- } else {
- Output = id + Output;
- }
- break;
- }
- }
- }
- if( FRIEND ) {
- if( Output != null ) {
- Output += "&Active";
- } else {
- Output = id +"Active";
- }
- }
- if( ENEMY ) {
- if( Output != null ) {
- Output += "&Hostile";
- } else {
- Output = id +"Hostile";
- }
- }
- if( Output != null ) {
- Output = Output;
- }
- return Output;
- }
- /* Because it never gets old
- * Play a sound. Called when a new message comes in
- */
- public void Sound( ) {
- if( SOUND_BLOCK != null ) {
- List<IMyTerminalBlock> Blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMySoundBlock>( Blocks );
- for( int e = 0; e < Blocks.Count; e++ ) {
- if( ValidPrefix( Blocks[e].CustomName ) && Blocks[e].CustomName.Contains( SOUND_BLOCK ) ) {
- var action = Blocks[e].GetActionWithName( "PlaySound" );
- if( action != null ) {
- action.Apply( Blocks[e] );
- }
- }
- }
- }
- }
- /*
- * Have you tried turning it off and on again?
- * no really. That is all this does.
- * _Do_Not_ change this to toggle or you might be turning it on then off again!
- */
- public void ToggleBlock( IMyTerminalBlock Block ) {
- if( Block != null ) {
- var off = Block.GetActionWithName( "OnOff_Off" );
- var on = Block.GetActionWithName( "OnOff_On" );
- if( off != null && on != null ) {
- off.Apply( Block );
- on.Apply( Block );
- }
- }
- }
- /*
- * Check status of target sensor
- */
- public bool CheckSensor( string SensorTag ) {
- bool Output = false;
- if( SensorTag != null ) {
- List<IMyTerminalBlock> Blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMySensorBlock>( Blocks );
- for( int e = 0; e < Blocks.Count; e++ ) {
- if( ValidPrefix( Blocks[e].CustomName ) && Blocks[e].CustomName.Contains( SensorTag ) ) {
- if( ((IMySensorBlock)Blocks[e]).IsActive ) {
- Output = true;
- break;
- }
- }
- }
- }
- return Output;
- }
- /*
- * Check for Sensor Status Change
- */
- public void Status() {
- if( USER_SENSOR != null ) {
- bool Frnd = CheckSensor( USER_SENSOR );
- if( Frnd != FRIEND ) {
- FRIEND = Frnd;
- PANEL.SetStatus( FRIEND );
- }
- }
- if( FOE_SENSOR != null ) {
- bool Foe = CheckSensor( FOE_SENSOR );
- if( Foe != ENEMY ) {
- ENEMY = Foe;
- }
- }
- }
- const string LCD_DISPLAY_MSG_TAG = "[MSG]";
- public void UpdateMsgBoard( string Out ) {
- if( LCD_DISPLAY_MSG_TAG != null ) {
- string[] Keys = new string[DATA_LOG.Count];
- DATA_LOG.Keys.CopyTo( Keys, 0 );
- StringBuilder Msgs = new StringBuilder();
- string nl = "";
- for( int e = 0; e < Keys.Length; e++ ) {
- if( !Keys[e].Equals( Out ) ) {
- Msgs.Append( nl + Keys[e] );
- nl = "\n";
- }
- }
- string Txt = Msgs.ToString();
- var Blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>( Blocks );
- for( int e = 0; e < Blocks.Count; e++ ) {
- if( Blocks[e].CustomName.Contains( LCD_DISPLAY_MSG_TAG ) ) {
- ((IMyTextPanel)Blocks[e]).WritePublicText( Txt, false );
- }
- }
- }
- }
- /*
- * MAIN
- */
- void Main() {
- if( !SET ) {
- SET = true;
- Display( "_CAT_SYS_ONLINE_" );
- }
- Update();
- }
Advertisement
Add Comment
Please, Sign In to add comment