public static HashSet<GridGroup> GetGroups( HashSet<MyEntity> entities, GridLinkTypeEnum linkType = GridLinkTypeEnum.Logical )
{
HashSet<GridGroup> result = new HashSet<GridGroup>();
//Create a copy of the entities list;
//group processing can take so long that the internal list of entities can change
//which is Bad.
MyEntity[] entitiesCopy = new MyEntity[entities.Count];
entities.CopyTo( entitiesCopy );
//on large servers this can run into the tens of seconds, so parallelize it
Parallel.ForEach( entitiesCopy, ( entity ) =>
{
MyCubeGrid grid = entity as MyCubeGrid;
if (grid?.Physics == null || grid.Closed)
return;
lock (result)
foreach(var item in result)
if ( item._grids.Contains( grid ) )
return;
var newGroup = new GridGroup( grid, linkType );
lock (result)
result.Add( newGroup );
} );
return result;
}