using Sandbox.ModAPI.Ingame; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Security.Cryptography; using VRage.Game.ModAPI.Ingame; namespace IngameScript { static class Utils { /// /// Get the first terminal block matching the given name or null if nothing is found. /// /// /// /// /// /// public static T GetBlock(this IMyGridTerminalSystem gridTerminal, string name = null, IMyCubeGrid grid = null) where T: class, IMyTerminalBlock { return Utils.GetBlocks(gridTerminal, name, grid).FirstOrDefault(); } /// /// Get all terminal blocks matching the given name. /// /// /// /// /// /// public static IEnumerable GetBlocks(this IMyGridTerminalSystem gridTerminal, string name = null, IMyCubeGrid grid = null, Func filter = null) where T : class, IMyTerminalBlock { var l = new List(); gridTerminal.GetBlocksOfType(l, (T block) => (name == null || block.CustomName == name) && (grid == null || block.CubeGrid == grid) && (filter == null || filter(block)) ); return l; } /// /// Get all groups matching the given name. /// /// /// /// /// public static IEnumerable GetBlockGroups(this IMyGridTerminalSystem gridTerminal, string groupName = null, IMyCubeGrid grid = null) { var groups = new List(); gridTerminal.GetBlockGroups( groups, group => { if (groupName != null && group.Name != groupName) return false; if (grid == null) return true; var l = new List(); group.GetBlocks(l, block => block.CubeGrid == grid); return l.Count > 0; } ); return groups; } /// /// Get all terminal blocks owned by the first given group name. /// /// /// /// /// /// public static IEnumerable GetBlocksFromGroup(this IMyGridTerminalSystem gridTerminal, string groupName = null, IMyCubeGrid grid = null) where T : class, IMyTerminalBlock { foreach (var group in gridTerminal.GetBlockGroups(groupName, grid)) { var l = new List(); group.GetBlocksOfType(l); if (l.Count > 0) return l; } return new List(); } public static IEnumerable GetAllInventories(this IMyGridTerminalSystem gridTerminal, IMyCubeGrid grid = null) { var inventories = new List(); // Containers. foreach (var container in gridTerminal.GetBlocks(grid: grid)) inventories.Add(container.GetInventory()); // Assemblers. foreach (var assembler in gridTerminal.GetBlocks(grid: grid)) { inventories.Add(assembler.InputInventory); inventories.Add(assembler.OutputInventory); } // Refineries foreach (var refinery in gridTerminal.GetBlocks(grid: grid)) { inventories.Add(refinery.InputInventory); inventories.Add(refinery.OutputInventory); } // Connectors. foreach (var connector in gridTerminal.GetBlocks(grid: grid)) inventories.Add(connector.GetInventory()); // Welders. foreach (var welder in gridTerminal.GetBlocks(grid: grid)) inventories.Add(welder.GetInventory()); // Turrets. foreach (var turret in gridTerminal.GetBlocks(grid: grid)) inventories.Add(turret.GetInventory()); // Artillery and missile launcher. foreach (var missileLauncher in gridTerminal.GetBlocks(grid: grid)) inventories.Add(missileLauncher.GetInventory()); return inventories; } } }