/// <summary> /// Iterates over every IMyCubeBlock with a definition matching any string from BlockNamesContain. /// </summary> /// <param name="cache">Where to get blocks from.</param> /// <param name="index">The index corresponding to the position in BlockNamesContain of the current block.</param> /// <param name="action">Invoked on each block</param> public void ForEach(CubeGridCache cache, ref int index, Action<IMyCubeBlock> action) { if (m_sourceCount != CubeGridCache.DefinitionType .Count) UpdateFromSource(); using (m_lock.AcquireSharedUsing()) for (index = 0; index < m_blocks.Count; index++) foreach (MyObjectBuilderType blockType in m_blocks[index]) { var blockList = cache.GetBlocksOfType(blockType); if (blockList == null) continue; foreach (IMyCubeBlock block in blockList) action(block); } }
/// <summary> /// Count the number of blocks the cache contains that match BlockNamesContain. /// </summary> /// <param name="cache">Where to search for blocks.</param> /// <returns>An array of integers indicating how many blocks match each string in BlockNamesContain.</returns> public int[] Count(CubeGridCache cache) { if (m_sourceCount != CubeGridCache.DefinitionType.Count) UpdateFromSource(); int[] results = new int[m_blocks.Count]; using (m_lock.AcquireSharedUsing()) for (int index = 0; index < m_blocks.Count; index++) foreach (MyObjectBuilderType blockType in m_blocks[index]) { int count = cache.CountByType(blockType); results[index] += count; } return results; }
/// <summary> /// Count the number of blocks the cache contains that match BlockNamesContain. /// </summary> /// <param name="cache">Where to search for blocks.</param> /// <returns>An array of integers indicating how many blocks match each string in BlockNamesContain.</returns> public int[] Count(CubeGridCache cache) { if (m_sourceCount != CubeGridCache.DefinitionType.Count) { UpdateFromSource(); } int[] results = new int[m_blocks.Length]; using (m_lock.AcquireSharedUsing()) for (int order = 0; order < m_blocks.Length; order++) { MyDefinitionId[] array = m_blocks[order]; for (int index = 0; index < array.Length; index++) { results[order] += cache.CountByType(array[index]); } } return(results); }
/// <summary> /// Gets the closest occupied cell. /// </summary> public Vector3I GetClosestOccupiedCell(Vector3I startCell, Vector3I previousCell) { Vector3I closestCell = previousCell; int closestDistance = CubeGrid.GetCubeBlock(closestCell) != null?previousCell.DistanceSquared(startCell) - 2 : int.MaxValue; CubeGridCache cache = CubeGridCache.GetFor(CubeGrid); if (cache == null) { return(closestCell); } foreach (Vector3I cell in cache.OccupiedCells()) { int dist = cell.DistanceSquared(startCell); if (dist < closestDistance) { closestCell = cell; closestDistance = dist; } } return(closestCell); }
/// <summary> /// Checks for the cache containing any blocks that match BlockNamesContain. /// </summary> /// <param name="cache">Where to search for blocks.</param> /// <returns>True iff the cache contains any blocks matching any string from BlockNamesContain</returns> public bool HasAny(CubeGridCache cache, Func<IMyCubeBlock, bool> condition) { if (m_sourceCount != CubeGridCache.DefinitionType.Count) UpdateFromSource(); using (m_lock.AcquireSharedUsing()) foreach (List<MyObjectBuilderType> typesWithString in m_blocks) foreach (MyObjectBuilderType blockType in typesWithString) if (cache.CountByType(blockType, condition, 1) != 0) return true; return false; }