Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static readonly struct BlockMatchType
- {
- public static const int NoBlock = 0;
- public static const int Exists = 1;
- public static const int NotAccessible = 2;
- public static const int SlimBlock = 3;
- public static const int TerminalBlock = 4;
- public static const int OriginalBlock = -1;
- }
- public class BlockMatch
- {
- public int BlockMatchType;
- public IMyTerminalBlock TermBlock;
- public IMySlimBlock Slimblock;
- }
- List<IMyCubeGrid> local_grids = new List<IMyCubeGrid>();
- IMyTerminalBlock block;
- void Main()
- {
- if (block == null) { block = GridTerminalSystem.GetBlockWithName("Original Block"); }
- string ret = GetNearBlock(block, 1, 0, 0);
- Echo(ret);
- }
- bool IsAttachable(IMyTerminalBlock mrg)
- {
- List<BlockMatch> ret = GetNearBlockSub(mrg, 0, 0, 0);
- if (ret[0].BlockMatchType == BlockMatchType.Exists)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- string GetNearBlock(IMyTerminalBlock mrg, int PosX, int PosY, int PosZ)
- {
- List<BlockMatch> ret = GetNearBlockSub(mrg, PosX, PosY, PosZ);
- var firstblock = ret[0];
- if (firstblock.BlockMatchType != BlockMatchType.NoBlock)
- {
- var blockname = "No Block found";
- switch (firstblock.BlockMatchType)
- {
- case BlockMatchType.TerminalBlock:
- blockname = "Terminal Block" + " (" + (firstblock.Slimblock.FatBlock as IMyTerminalBlock).CustomName + ")";
- break;
- case BlockMatchType.OriginalBlock:
- blockname = "Original Block";
- break;
- case BlockMatchType.NotAccessible:
- blockname = "Not Accessible";
- break;
- case BlockMatchType.SlimBlock:
- blockname = "Slim Block";
- break;
- }
- return blockname + " (" + (firstblock.Slimblock.FatBlock != null && firstblock.Slimblock.FatBlock is IMyTerminalBlock ? (firstblock.Slimblock.FatBlock as IMyTerminalBlock).CustomName : string.Empty) + ")";
- }
- else
- {
- return "No Block found";
- }
- }
- // Only for blocks that are connected to PB's grid ( X = Red, Y = Green, Z = Blue)
- List<BlockMatch> GetNearBlockSub(IMyTerminalBlock mrg1, int ShiftPosX, int ShiftPosY, int ShiftPosZ)
- {
- List<BlockMatch> list = new List<BlockMatch>();
- BlockMatch Isit = new BlockMatch();
- Isit.BlockMatchType = BlockMatchType.NoBlock;
- Isit.TermBlock = null;
- Isit.Slimblock = null;
- if (local_grids.Count == 0)
- {
- List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blocks);
- for (int i = 0; i < blocks.Count; i++)
- {
- IMyTerminalBlock block = blocks[i];
- if (!local_grids.Contains(block.CubeGrid))
- {// Grid of Block not already in list
- local_grids.Add(block.CubeGrid); // New Grid found. Add to list
- }
- else
- {
- //Grid of Block already in List
- }
- }
- }
- bool sbex;
- Vector3I PosGrid = new VRageMath.Vector3I(0, 0, 0); // Empty Vector for storing WorldToGridInteger Position
- Vector3I right1 = new Vector3I(ShiftPosX, ShiftPosY, ShiftPosZ); // Grid Vector applied to original Block
- Vector3D PosWorld = mrg1.CubeGrid.GridIntegerToWorld(mrg1.Position + right1);
- for (int i = 0; i < local_grids.Count; i++)
- {
- PosGrid = local_grids[i].WorldToGridInteger(PosWorld);
- sbex = local_grids[i].CubeExists(PosGrid);
- if (sbex == true)
- { //Block Exists at requested Position in this Grid and is not OriginalBlock
- Isit.BlockMatchType = BlockMatchType.Exists;
- Isit.TermBlock = null;
- Isit.Slimblock = null;
- IMySlimBlock sb = local_grids[i].GetCubeBlock(PosGrid);
- if (sb == null)
- { // Found non accessable Block/no SlimBlock
- Isit.BlockMatchType = BlockMatchType.NotAccessible;
- }
- else
- {
- IMyTerminalBlock tb = sb.FatBlock as IMyTerminalBlock;
- if (tb == null)
- { // Found SlimBlock
- Isit.BlockMatchType = BlockMatchType.SlimBlock;
- Isit.Slimblock = sb;
- }
- else
- {
- // Found Terminal Block
- if (tb == mrg1)
- { // its the original block
- Isit.BlockMatchType = BlockMatchType.OriginalBlock;
- Isit.Slimblock = sb;
- }
- else
- {
- // Found Terminal Block on search position
- Isit.BlockMatchType = BlockMatchType.TerminalBlock;
- Isit.Slimblock = sb;
- }
- }
- }
- list.Add(Isit);
- }
- }
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment