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, true); 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, 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); }
public static bool FindCollectableItemInRadius(Vector3D position, float radius, HashSet <MyDefinitionId> itemDefs, Vector3D initialPosition, float ignoreRadius, out ComponentInfo result) { BoundingSphereD sphere = new BoundingSphereD(position, radius); var entities = MyEntities.GetEntitiesInSphere(ref sphere); result = default(ComponentInfo); 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.EntityId = cubeGrid.EntityId; result.BlockPosition = first.Position; result.ComponentDefinitionId = GetComponentId(first); result.IsBlock = true; } } } } if (entity is MyFloatingObject) { var fo = entity as MyFloatingObject; var id = fo.Item.Content.GetId(); if (itemDefs.Contains(id)) { var foToPlayerDistSq = Vector3D.DistanceSquared(fo.PositionComp.WorldMatrix.Translation, position); if (foToPlayerDistSq < closestCubeDistanceSq) { closestCubeDistanceSq = foToPlayerDistSq; result.EntityId = fo.EntityId; result.IsBlock = false; } } } } entities.Clear(); return(closestCubeDistanceSq != double.MaxValue); }
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, true); 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) { 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); } 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); }