private void CleanupTimer_Elapsed(object sender, ElapsedEventArgs e) { var toRemove = new MyConcurrentHashSet <IMyEntity>(); var entities = new HashSet <IMyEntity>(); var players = new List <IMyPlayer>(); MyAPIGateway.Entities.GetEntities(entities); MyAPIGateway.Players.GetPlayers(players); MyAPIGateway.Parallel.ForEach(entities, entity => { var grid = entity as IMyCubeGrid; if (grid == null) { return; } if (grid.BigOwners.Count == 0) { toRemove.Add(entity); return; } if (!grid.BigOwners.Any(o => players.Any(p => p.IdentityId == o))) { toRemove.Add(entity); } }); if (toRemove.Any()) { MyAPIGateway.Utilities.InvokeOnGameThread(() => { foreach (IMyEntity ent in toRemove) { ent.Close(); } Communication.SendServerChat(0, $"Removed {toRemove.Count} grids without online owners"); }); } }
public override void Handle() { Logging.Instance.WriteDebug("ProcessLocalYards Start"); var removeYards = new HashSet <ShipyardItem>(); foreach (ShipyardItem item in LocalYards) { //see if the shipyard has been deleted if (item.YardEntity.Closed || item.YardEntity.Physics == null || item.YardType == ShipyardType.Invalid || (item.StaticYard && !item.YardEntity.Physics.IsStatic)) { //the client shouldn't tell the server the yard is invalid //item.Disable(); removeYards.Add(item); continue; } if (item.StaticYard) { UpdateBoxLines(item); } //don't draw boxes inside active yards, it's distracting if (item.YardType != ShipyardType.Disabled) { continue; } var corners = new Vector3D[8]; item.ShipyardBox.GetCorners(corners, 0); double dist = Vector3D.DistanceSquared(corners[0], item.ShipyardBox.Center); var sphere = new BoundingSphereD(item.ShipyardBox.Center, dist); //Utilities.InvokeBlocking(()=> entities = MyAPIGateway.Entities.GetTopMostEntitiesInSphere(ref sphere)); List <IMyEntity> entities = MyAPIGateway.Entities.GetTopMostEntitiesInSphere(ref sphere); if (entities.Count == 0) { Logging.Instance.WriteDebug("Couldn't get entities in ProcessLocalYards"); continue; } var removeGrids = new HashSet <IMyCubeGrid>(); foreach (IMyEntity entity in entities) { var grid = entity as IMyCubeGrid; if (grid == null) { continue; } if (grid.EntityId == item.EntityId) { continue; } //workaround to not blind people with digi's helmet mod if (grid.Physics == null && grid.Projector() == null) { continue; } if (grid.Closed || grid.MarkedForClose) { removeGrids.Add(grid); continue; } if (LocalYards.Any(x => x.EntityId == grid.EntityId)) { continue; } //create a bounding box around the ship MyOrientedBoundingBoxD gridBox = MathUtility.CreateOrientedBoundingBox(grid); //check if the ship bounding box is completely inside the yard box ContainmentType result = item.ShipyardBox.Contains(ref gridBox); if (result == ContainmentType.Contains) { item.ContainsGrids.Add(grid); item.IntersectsGrids.Remove(grid); } else if (result == ContainmentType.Intersects) { item.IntersectsGrids.Add(grid); item.ContainsGrids.Remove(grid); } else { removeGrids.Add(grid); } } foreach (IMyCubeGrid containGrid in item.ContainsGrids) { if (!entities.Contains(containGrid)) { removeGrids.Add(containGrid); } } foreach (IMyCubeGrid intersectGrid in item.IntersectsGrids) { if (!entities.Contains(intersectGrid)) { removeGrids.Add(intersectGrid); } } foreach (IMyCubeGrid removeGrid in removeGrids) { ShipyardCore.BoxDict.Remove(removeGrid.EntityId); item.ContainsGrids.Remove(removeGrid); item.IntersectsGrids.Remove(removeGrid); } } foreach (ShipyardItem removeItem in removeYards) { foreach (IMyCubeGrid grid in removeItem.ContainsGrids) { ShipyardCore.BoxDict.Remove(grid.EntityId); } foreach (IMyCubeGrid grid in removeItem.IntersectsGrids) { ShipyardCore.BoxDict.Remove(grid.EntityId); } LocalYards.Remove(removeItem); } }