Phoenix84

qsek script

Feb 24th, 2016 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1.         public static readonly struct BlockMatchType
  2.         {
  3.             public static const int NoBlock = 0;
  4.             public static const int Exists = 1;
  5.             public static const int NotAccessible = 2;
  6.             public static const int SlimBlock = 3;
  7.             public static const int TerminalBlock = 4;
  8.             public static const int OriginalBlock = -1;
  9.         }
  10.  
  11.         public class BlockMatch
  12.         {
  13.             public int BlockMatchType;
  14.             public IMyTerminalBlock TermBlock;
  15.             public IMySlimBlock Slimblock;
  16.         }
  17.  
  18.         List<IMyCubeGrid> local_grids = new List<IMyCubeGrid>();
  19.         IMyTerminalBlock block;
  20.        
  21.         void Main()
  22.         {
  23.  
  24.             if (block == null) { block = GridTerminalSystem.GetBlockWithName("Original Block"); }
  25.  
  26.             string ret = GetNearBlock(block, 1, 0, 0);
  27.             Echo(ret);
  28.  
  29.         }
  30.  
  31.         bool IsAttachable(IMyTerminalBlock mrg)
  32.         {
  33.             List<BlockMatch> ret = GetNearBlockSub(mrg, 0, 0, 0);
  34.  
  35.             if (ret[0].BlockMatchType == BlockMatchType.Exists)
  36.             {
  37.                 return true;
  38.             }
  39.             else
  40.             {
  41.                 return false;
  42.             }
  43.         }
  44.  
  45.         string GetNearBlock(IMyTerminalBlock mrg, int PosX, int PosY, int PosZ)
  46.         {
  47.             List<BlockMatch> ret = GetNearBlockSub(mrg, PosX, PosY, PosZ);
  48.             var firstblock = ret[0];
  49.             if (firstblock.BlockMatchType != BlockMatchType.NoBlock)
  50.             {
  51.                 var blockname = "No Block found";
  52.                 switch (firstblock.BlockMatchType)
  53.                 {
  54.                     case BlockMatchType.TerminalBlock:
  55.                         blockname = "Terminal Block" + " (" + (firstblock.Slimblock.FatBlock as IMyTerminalBlock).CustomName + ")";
  56.                         break;
  57.                     case BlockMatchType.OriginalBlock:
  58.                         blockname = "Original Block";
  59.                         break;
  60.                     case BlockMatchType.NotAccessible:
  61.                         blockname = "Not Accessible";
  62.                         break;
  63.                     case BlockMatchType.SlimBlock:
  64.                         blockname = "Slim Block";
  65.                         break;
  66.                 }
  67.                 return blockname + " (" + (firstblock.Slimblock.FatBlock != null && firstblock.Slimblock.FatBlock is IMyTerminalBlock ? (firstblock.Slimblock.FatBlock as IMyTerminalBlock).CustomName : string.Empty) + ")";
  68.             }
  69.             else
  70.             {
  71.                 return "No Block found";
  72.             }
  73.         }
  74.  
  75.         // Only for blocks that are connected to PB's grid ( X = Red, Y = Green, Z = Blue)
  76.         List<BlockMatch> GetNearBlockSub(IMyTerminalBlock mrg1, int ShiftPosX, int ShiftPosY, int ShiftPosZ)
  77.         {
  78.  
  79.             List<BlockMatch> list = new List<BlockMatch>();
  80.             BlockMatch Isit = new BlockMatch();
  81.             Isit.BlockMatchType = BlockMatchType.NoBlock;
  82.             Isit.TermBlock = null;
  83.             Isit.Slimblock = null;
  84.  
  85.             if (local_grids.Count == 0)
  86.             {
  87.  
  88.                 List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  89.                 GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blocks);
  90.  
  91.                 for (int i = 0; i < blocks.Count; i++)
  92.                 {
  93.  
  94.                     IMyTerminalBlock block = blocks[i];
  95.  
  96.                     if (!local_grids.Contains(block.CubeGrid))
  97.                     {// Grid of Block not already in list
  98.  
  99.                         local_grids.Add(block.CubeGrid); // New Grid found. Add to list
  100.  
  101.                     }
  102.                     else
  103.                     {
  104.                         //Grid of Block already in List
  105.                     }
  106.                 }
  107.             }
  108.  
  109.  
  110.             bool sbex;
  111.  
  112.             Vector3I PosGrid = new VRageMath.Vector3I(0, 0, 0); // Empty Vector for storing WorldToGridInteger Position
  113.             Vector3I right1 = new Vector3I(ShiftPosX, ShiftPosY, ShiftPosZ); // Grid Vector applied to original Block
  114.             Vector3D PosWorld = mrg1.CubeGrid.GridIntegerToWorld(mrg1.Position + right1);
  115.  
  116.             for (int i = 0; i < local_grids.Count; i++)
  117.             {
  118.  
  119.                 PosGrid = local_grids[i].WorldToGridInteger(PosWorld);
  120.  
  121.                 sbex = local_grids[i].CubeExists(PosGrid);
  122.  
  123.                 if (sbex == true)
  124.                 { //Block Exists at requested Position in this Grid and is not OriginalBlock
  125.  
  126.                     Isit.BlockMatchType = BlockMatchType.Exists;
  127.                     Isit.TermBlock = null;
  128.                     Isit.Slimblock = null;
  129.  
  130.                     IMySlimBlock sb = local_grids[i].GetCubeBlock(PosGrid);
  131.  
  132.                     if (sb == null)
  133.                     { // Found non accessable Block/no SlimBlock
  134.                         Isit.BlockMatchType = BlockMatchType.NotAccessible;
  135.                     }
  136.                     else
  137.                     {
  138.                         IMyTerminalBlock tb = sb.FatBlock as IMyTerminalBlock;
  139.                         if (tb == null)
  140.                         { // Found SlimBlock
  141.                             Isit.BlockMatchType = BlockMatchType.SlimBlock;
  142.                             Isit.Slimblock = sb;
  143.                         }
  144.                         else
  145.                         {
  146.                             // Found Terminal Block
  147.                             if (tb == mrg1)
  148.                             { // its the original block
  149.                                 Isit.BlockMatchType = BlockMatchType.OriginalBlock;
  150.                                 Isit.Slimblock = sb;
  151.                             }
  152.                             else
  153.                             {
  154.                                 // Found Terminal Block on search position
  155.                                 Isit.BlockMatchType = BlockMatchType.TerminalBlock;
  156.                                 Isit.Slimblock = sb;
  157.                             }
  158.                         }
  159.  
  160.                     }
  161.  
  162.                     list.Add(Isit);
  163.                 }
  164.             }
  165.             return list;
  166.         }
  167.  
  168.     }
Advertisement
Add Comment
Please, Sign In to add comment