public static void ReduceFloatingObjects() { var count = m_floatingOres.Count + m_floatingItems.Count; int minFloatingOres = Math.Max(MySession.Static.MaxFloatingObjects / 5, 4); while (count > MySession.Static.MaxFloatingObjects) { SortedSet <MyFloatingObject> set; if (m_floatingOres.Count > minFloatingOres || m_floatingItems.Count == 0) { set = m_floatingOres; } else { set = m_floatingItems; } if (set.Count > 0) { var floatingObject = set.Last(); if (MyManipulationTool.IsEntityManipulated(floatingObject)) { break; } set.Remove(floatingObject); if (Sync.IsServer) { RemoveFloatingObject(floatingObject); } } --count; } }
public static bool FindCollectableItemInRadius(Vector3D position, float radius, HashSet <MyDefinitionId> itemDefs, Vector3D initialPosition, float ignoreRadius, out BlockInfo result) { BoundingSphereD sphere = new BoundingSphereD(position, radius); var entities = MyEntities.GetEntitiesInSphere(ref sphere); result = default(BlockInfo); double closestCubeDistanceSq = double.MaxValue; foreach (var entity in entities) { if (MyManipulationTool.IsEntityManipulated(entity)) { continue; } if (entity is MyCubeGrid) // TODO: Add logs and timbers { var cubeGrid = entity as MyCubeGrid; if (cubeGrid.BlocksCount == 1) { var first = cubeGrid.CubeBlocks.First(); if (itemDefs.Contains(first.BlockDefinition.Id)) { var worldPosition = cubeGrid.GridIntegerToWorld(first.Position); var cubeDistanceFromSpawnSq = Vector3D.DistanceSquared(worldPosition, initialPosition); if (cubeDistanceFromSpawnSq <= ignoreRadius * ignoreRadius) { continue; } var cubeDistanceFromCharacterSq = Vector3D.DistanceSquared(worldPosition, position); if (cubeDistanceFromCharacterSq < closestCubeDistanceSq) { closestCubeDistanceSq = cubeDistanceFromCharacterSq; result.GridEntityId = cubeGrid.EntityId; result.BlockPosition = first.Position; result.ComponentDefinitionId = GetComponentId(first); } } } } } entities.Clear(); return(closestCubeDistanceSq != double.MaxValue); }
protected internal virtual void ChoosePlacementObject() { MyCubeGrid grid; MyVoxelMap voxelMap; FindClosestPlacementObject(out grid, out voxelMap); // Check if not manipulated. (Currently only in ME but this check is generic. In SE manipulation list is always empty) if (!MyManipulationTool.IsEntityManipulated(grid)) { CurrentGrid = grid; } CurrentVoxelMap = voxelMap; Debug.Assert((CurrentGrid == null && CurrentVoxelMap == null) || (CurrentGrid != null && CurrentVoxelMap == null) || (CurrentGrid == null && CurrentVoxelMap != null)); m_invGridWorldMatrix = CurrentGrid != null?MatrixD.Invert(CurrentGrid.WorldMatrix) : MatrixD.Identity; }
public static bool FindRandomCollectableItemInPlaceArea(long entityId, HashSet <MyDefinitionId> itemDefinitions, out ComponentInfo result) { result = default(ComponentInfo); result.IsBlock = true; MyPlaceArea area = MyPlaceArea.FromEntity(entityId); if (area == null) { return(false); } var areaBoundingBox = area.WorldAABB; List <MyEntity> entities = null; try { entities = MyEntities.GetEntitiesInAABB(ref areaBoundingBox); for (int i = entities.Count - 1; i >= 0; i--) { var entity = entities[i]; if (MyManipulationTool.IsEntityManipulated(entity)) { entities.RemoveAtFast(i); continue; } var cubeGrid = entity as MyCubeGrid; var fo = entity as MyFloatingObject; if (fo == null && (cubeGrid == null || cubeGrid.BlocksCount != 1)) { entities.RemoveAtFast(i); continue; } if (cubeGrid != null) { MySlimBlock first = cubeGrid.CubeBlocks.First(); if (!itemDefinitions.Contains(first.BlockDefinition.Id)) { entities.RemoveAtFast(i); continue; } } else if (fo != null) { var id = fo.Item.Content.GetId(); if (!itemDefinitions.Contains(id)) { entities.RemoveAtFast(i); continue; } } } if (entities.Count() == 0) { return(false); } int randIdx = (int)Math.Round(MyRandom.Instance.NextFloat() * (entities.Count() - 1)); var selectedEntity = entities[randIdx]; result.EntityId = selectedEntity.EntityId; if (selectedEntity is MyCubeGrid) { var selectedCube = selectedEntity as MyCubeGrid; var first = selectedCube.GetBlocks().First(); result.EntityId = selectedCube.EntityId; result.BlockPosition = first.Position; result.ComponentDefinitionId = GetComponentId(first); result.IsBlock = true; } else { result.IsBlock = false; } return(true); } finally { entities.Clear(); } }
public static MyCubeGrid TryGetAsComponent(MyEntity entity, out MyCubeBlock block, bool blockManipulatedEntity = true) { block = null; if (MyManipulationTool.IsEntityManipulated(entity) && blockManipulatedEntity) { return(null); } if (entity.MarkedForClose) { return(null); } var grid = entity as MyCubeGrid; if (grid == null) { return(null); } if (grid.GridSizeEnum != MyCubeSize.Small) { return(null); } if (grid.CubeBlocks.Count != 1) { return(null); } if (grid.IsStatic) { return(null); } if (!MyCubeGrid.IsGridInCompleteState(grid)) { return(null); } var enumerator = grid.CubeBlocks.GetEnumerator(); enumerator.MoveNext(); block = enumerator.Current.FatBlock; enumerator.Dispose(); if (block == null) { return(null); } if (!MyDefinitionManager.Static.IsComponentBlock(block.BlockDefinition.Id)) { return(null); } if (block.IsSubBlock) { return(null); } if (block.GetSubBlocks().Count() > 0) { return(null); } if (MyCubeGridSmallToLargeConnection.Static.TestGridSmallToLargeConnection(grid)) { return(null); } return(grid); }
public static bool FindClosestCollectableItemInPlaceArea(Vector3D fromPosition, long entityId, HashSet <MyDefinitionId> itemDefinitions, out ComponentInfo result) { List <MyEntity> entities = null; result = default(ComponentInfo); try { MyEntity containingEntity = null; MyPlaceArea area = null; if (!MyEntities.TryGetEntityById(entityId, out containingEntity)) { return(false); } if (!containingEntity.Components.TryGet <MyPlaceArea>(out area)) { return(false); } var areaBoundingBox = area.WorldAABB; entities = MyEntities.GetEntitiesInAABB(ref areaBoundingBox); MyEntity closestObject = null; MySlimBlock first = null; double closestObjectDistanceSq = double.MaxValue; bool closestIsBlock = false; foreach (var entity in entities) { if (MyManipulationTool.IsEntityManipulated(entity)) { continue; } if (entity is MyCubeGrid) { var cubeGrid = entity as MyCubeGrid; if (cubeGrid.BlocksCount == 1) { first = cubeGrid.CubeBlocks.First(); if (itemDefinitions.Contains(first.BlockDefinition.Id)) { var worldPosition = cubeGrid.GridIntegerToWorld(first.Position); var cubeDistanceFromCharacterSq = Vector3D.DistanceSquared(worldPosition, fromPosition); if (cubeDistanceFromCharacterSq < closestObjectDistanceSq) { closestObjectDistanceSq = cubeDistanceFromCharacterSq; closestObject = cubeGrid; closestIsBlock = true; } } } } if (entity is MyFloatingObject) { var fo = entity as MyFloatingObject; var id = fo.Item.Content.GetId(); if (itemDefinitions.Contains(id)) { var foToPlayerDistSq = Vector3D.DistanceSquared(fo.PositionComp.WorldMatrix.Translation, fromPosition); if (foToPlayerDistSq < closestObjectDistanceSq) { closestObjectDistanceSq = foToPlayerDistSq; closestObject = fo; closestIsBlock = false; } } } } if (closestObject == null) { return(false); } result.IsBlock = closestIsBlock; result.EntityId = closestObject.EntityId; if (closestIsBlock) { result.BlockPosition = first.Position; result.ComponentDefinitionId = GetComponentId(first); } return(true); } finally { if (entities != null) { entities.Clear(); } } }
public static MyCubeGrid TryGetAsComponent(MyEntity entity, out MyCubeBlock block, bool blockManipulatedEntity = true, Vector3D?hitPosition = null) { block = null; if (MyManipulationTool.IsEntityManipulated(entity) && blockManipulatedEntity) { return(null); } if (entity.MarkedForClose) { return(null); } var grid = entity as MyCubeGrid; if (grid == null) { return(null); } if (grid.GridSizeEnum != MyCubeSize.Small) { return(null); } MyCubeGrid returnedGrid = null; if (MyFakes.ENABLE_GATHERING_SMALL_BLOCK_FROM_GRID && hitPosition != null) { var gridLocalPos = Vector3D.Transform(hitPosition.Value, grid.PositionComp.WorldMatrixNormalizedInv); Vector3I cubePosition; grid.FixTargetCube(out cubePosition, gridLocalPos / grid.GridSize); MySlimBlock slimBlock = grid.GetCubeBlock(cubePosition); if (slimBlock != null && slimBlock.IsFullIntegrity) { block = slimBlock.FatBlock; } } else { if (grid.CubeBlocks.Count != 1) { return(null); } if (grid.IsStatic) { return(null); } if (!MyCubeGrid.IsGridInCompleteState(grid)) { return(null); } if (MyCubeGridSmallToLargeConnection.Static.TestGridSmallToLargeConnection(grid)) { return(null); } var enumerator = grid.CubeBlocks.GetEnumerator(); enumerator.MoveNext(); block = enumerator.Current.FatBlock; enumerator.Dispose(); returnedGrid = grid; } if (block == null) { return(null); } if (!MyDefinitionManager.Static.IsComponentBlock(block.BlockDefinition.Id)) { return(null); } if (block.IsSubBlock) { return(null); } var subBlocks = block.GetSubBlocks(); if (subBlocks.HasValue && subBlocks.Count() > 0) { return(null); } return(returnedGrid); }