Advertisement
nobodyinparticular

SE Block Profiler

Apr 7th, 2015
6,490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. //This code displays the data collected from existing grid(span or not) then displays it on a series of lcd
  2. //panels with the same name "Profile LCD Panel" or variations of that. Just some basic stats. Qon donated the
  3. //span code.
  4. //######################################################################
  5. //configuration variables:
  6. const string pbName = "Programmable block profiler"; //block that runs the script.
  7. const string LCDdisplays = "Profile LCD Panel"; //real names of panels: Profile LCD Panel 1, Profile LCD Panel 2,...
  8. //names are like that for sorting purposes, or else data is randomly displayed.
  9. const int displayChars = 800; //number of characters to put on the panel
  10. const int rowWidth = 30; //number of chars per row
  11. //the two const values there make a chunk of text 800 long and 30ish wide.
  12. //the number of lines in the text depends on the actual text length.
  13.  
  14.  
  15. void Main()
  16. {
  17.  
  18. bool spanConnected = true; //span grids, no span = no pistons or rotors.
  19. IMyTerminalBlock progBlock = GridTerminalSystem.GetBlockWithName(pbName);
  20. var gridSize = progBlock.CubeGrid; //only check same size blocks
  21. StringBuilder tester = new StringBuilder();
  22. Dictionary<String,int> collate = new Dictionary<String, int>();
  23. List<String> Names = new List<String>();
  24.  
  25. List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
  26. GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(allBlocks); //capture all blocks
  27.  
  28. for (int i = 0; i<allBlocks.Count; i++) { //count through the blocks
  29.     if (allBlocks[i].CubeGrid == progBlock.CubeGrid || spanConnected) { //look ma, one less instruction!
  30.     String BlockName = allBlocks[i].BlockDefinition.ToString(); //Gather Block info
  31.     if (allBlocks[i].CubeGrid.GridSizeEnum == gridSize.GridSizeEnum) {
  32.     int count = 1;
  33.     int Amount = 0;
  34.     if (collate.TryGetValue(BlockName, out Amount))
  35.         {
  36.             Amount += count;
  37.             collate[BlockName] = Amount;
  38.         } else {
  39.             collate.Add(BlockName, count);
  40.             Names.Add(BlockName); //use this list for key retrieval, no other way.
  41.         }
  42.     }//if Contains
  43.     }//if CubeGrid
  44. } //format data to text
  45. tester.Append(DateTime.Now.ToString() + "     Block Profile\n");
  46. for (int i = 0; i < Names.Count; i++) {
  47.     var truncIndex = Names[i].IndexOf('/') + 1;
  48.     var formatName = Names[i].Remove(0,truncIndex);
  49.     var newstring = formatName + ": " + collate[Names[i]].ToString().PadLeft(rowWidth - formatName.Length, '_') + "\n"; //good match or
  50.     if (formatName.Length < 5) {//not enough data ?
  51.         truncIndex = Names[i].IndexOf('_') + 1;
  52.         formatName = Names[i].Remove(0,truncIndex);
  53.         newstring =  ": " + collate[Names[i]].ToString().PadLeft(rowWidth - formatName.Length + 1, '_') + "\n";
  54.         truncIndex = formatName.IndexOf('/');
  55.         newstring = formatName.Remove(truncIndex) + newstring;
  56.     }//if
  57.     tester.Append(newstring);
  58. }//for
  59. //code segment below displays text in multiple lcds, no text it shows the graphic selected instead
  60. List<IMyTerminalBlock> lcd = new List<IMyTerminalBlock>();
  61. GridTerminalSystem.SearchBlocksOfName(LCDdisplays, lcd); //display(s)
  62. lcd = BlockSort(lcd);//sort by name.
  63. int startIndex = 0; //keep track of where we left off
  64. for (int i = 0; i < lcd.Count; i++) {  //break up the text, chunklet style
  65.     var Display = lcd[i] as IMyTextPanel;
  66.     if (startIndex < tester.Length-1) { // is the limit less than the data?
  67.         var textBreak = startIndex + displayChars; //start at the limit and go over :)
  68.         for (int j = textBreak; j < tester.Length; j++) { //possible to exceed the string length with the line above.
  69.             textBreak = j; //set it to current count
  70.             if (tester[j] == '\n') {break;}
  71.         }//for
  72.         if (textBreak > tester.Length) textBreak = tester.Length - 1; //this can happen with the loop entry.
  73.         Display.WritePublicText(tester.ToString(startIndex,textBreak-startIndex));
  74.         startIndex = textBreak+1; //advance
  75.         Display.ShowTextureOnScreen();  
  76.         Display.ShowPublicTextOnScreen();
  77.     } else {
  78.         Display.ShowTextureOnScreen();   //display selected texture instead of data
  79.     }// if startIndex
  80. }//for
  81. }//main
  82. List<IMyTerminalBlock> BlockSort (List<IMyTerminalBlock> blocks)
  83. {
  84. List<IMyTerminalBlock> ListSort = new List<IMyTerminalBlock>();
  85. List<string> blockNames = new List<string>();
  86. for (int i = 0; i < blocks.Count; i++)
  87. {
  88.     blockNames.Add(blocks[i].DisplayNameText.ToString());
  89. }
  90. blockNames.Sort();
  91. for (int i = 0; i < blockNames.Count; i++)  
  92. {
  93.     for (int j = 0; j < blocks.Count; j++)  
  94.     {
  95.         if (blockNames[i] == blocks[j].DisplayNameText.ToString() && !ListSort.Contains(blocks[j]))  
  96.         {
  97.             ListSort.Add(blocks[j]);
  98.         }
  99.     }
  100. }
  101. return ListSort;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement