Exemplo n.º 1
0
        private static void AddItemToLootBag(MyEntity itemOwner, MyPhysicalInventoryItem item, ref MyEntity lootBagEntity)
        {
            MyLootBagDefinition lootBagDefinition = MyDefinitionManager.Static.GetLootBagDefinition();

            if (lootBagDefinition != null)
            {
                MyDefinitionBase itemDefinition = item.GetItemDefinition();
                if (itemDefinition != null)
                {
                    if ((lootBagEntity == null) && (lootBagDefinition.SearchRadius > 0f))
                    {
                        Vector3D        position         = itemOwner.PositionComp.GetPosition();
                        BoundingSphereD boundingSphere   = new BoundingSphereD(position, (double)lootBagDefinition.SearchRadius);
                        List <MyEntity> entitiesInSphere = MyEntities.GetEntitiesInSphere(ref boundingSphere);
                        double          maxValue         = double.MaxValue;
                        foreach (MyEntity entity in entitiesInSphere)
                        {
                            if (entity.MarkedForClose)
                            {
                                continue;
                            }
                            if ((entity.GetType() == typeof(MyEntity)) && ((entity.DefinitionId != null) && (entity.DefinitionId.Value == lootBagDefinition.ContainerDefinition)))
                            {
                                double num2 = (entity.PositionComp.GetPosition() - position).LengthSquared();
                                if (num2 < maxValue)
                                {
                                    lootBagEntity = entity;
                                    maxValue      = num2;
                                }
                            }
                        }
                        entitiesInSphere.Clear();
                    }
                    if ((lootBagEntity == null) || (lootBagEntity.Components.Has <MyInventoryBase>() && !(lootBagEntity.Components.Get <MyInventoryBase>() as MyInventory).CanItemsBeAdded(item.Amount, itemDefinition.Id)))
                    {
                        MyContainerDefinition definition2;
                        lootBagEntity = null;
                        if (MyComponentContainerExtension.TryGetContainerDefinition(lootBagDefinition.ContainerDefinition.TypeId, lootBagDefinition.ContainerDefinition.SubtypeId, out definition2))
                        {
                            lootBagEntity = SpawnBagAround(itemOwner, definition2, 3, 2, 5, 1f);
                        }
                    }
                    if (lootBagEntity != null)
                    {
                        MyInventory inventory = lootBagEntity.Components.Get <MyInventoryBase>() as MyInventory;
                        if (inventory != null)
                        {
                            if (itemDefinition is MyCubeBlockDefinition)
                            {
                                inventory.AddBlocks(itemDefinition as MyCubeBlockDefinition, item.Amount);
                            }
                            else
                            {
                                inventory.AddItems(item.Amount, item.Content);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private ClonesRequireMaterials Nearest(IMyPlayer player)
        {
            Vector3D position = player.GetPosition();

            BoundingSphereD sphere = new BoundingSphereD(position, 3.0);

            List <MyEntity> entities = MyEntities.GetEntitiesInSphere(ref sphere);

            double minDist = double.PositiveInfinity;
            ClonesRequireMaterials nearest = null;

            foreach (MyEntity entity in entities)
            {
                var medicalRoom = entity.Components.Get <ClonesRequireMaterials>();

                if (medicalRoom == null)
                {
                    continue;
                }

                var dist = Vector3D.DistanceSquared(medicalRoom.SpawnPos.Translation, position);

                if (dist < minDist)
                {
                    minDist = dist;
                    nearest = medicalRoom;
                }
            }
            return(nearest);
        }
Exemplo n.º 3
0
        private static void CheckTargetValid()
        {
            if (!m_currentTarget.HasValue)
            {
                return;
            }

            m_tmpEntityList.Clear();
            var bs = m_currentTarget.Value;

            m_tmpEntityList = MyEntities.GetEntitiesInSphere(ref bs);
            if (m_tmpEntityList.OfType <MyCubeGrid>().ToList().Count == 0)
            {
                m_waveCounter = -1;
            }
            if (m_waveCounter >= 0 && MyMusicController.Static != null)
            {
                foreach (var entity in m_tmpEntityList)
                {
                    if ((entity is MyCharacter) && MySession.Static != null && (entity as MyCharacter) == MySession.Static.LocalCharacter)
                    {
                        MyMusicController.Static.MeteorShowerIncoming();
                        break;
                    }
                }
            }
            m_tmpEntityList.Clear();
        }
Exemplo n.º 4
0
        private bool IsInGridAABB(Vector3D worldPosition)
        {
            var             sphere     = new BoundingSphereD(worldPosition, 0.1f);
            List <MyEntity> entityList = null;

            try
            {
                entityList = MyEntities.GetEntitiesInSphere(ref sphere);

                foreach (var entity in entityList)
                {
                    var grid = entity as MyCubeGrid;

                    if (grid == null)
                    {
                        continue;
                    }

                    return(true);
                }
            }
            finally
            {
                if (entityList != null)
                {
                    entityList.Clear();
                }
            }

            return(false);
        }
        private bool IsInGridAABB()
        {
            var             sphere     = new BoundingSphereD(MySector.MainCamera.Position, 0.1f);
            List <MyEntity> entityList = null;

            try
            {
                entityList = MyEntities.GetEntitiesInSphere(ref sphere);

                foreach (var entity in entityList)
                {
                    var grid = entity as MyCubeGrid;

                    if (grid == null || grid.GridSizeEnum == MyCubeSize.Small)
                    {
                        continue;
                    }

                    return(true);
                }
            }
            finally
            {
                if (entityList != null)
                {
                    entityList.Clear();
                }
            }

            return(false);
        }
Exemplo n.º 6
0
        private bool IsInGridAABB()
        {
            ProfilerShort.Begin("SpaceParticles.IsInGridAABB");
            var             isInGrid   = false;
            var             sphere     = new BoundingSphereD(MySector.MainCamera.Position, 0.1f);
            List <MyEntity> entityList = null;

            try
            {
                entityList = MyEntities.GetEntitiesInSphere(ref sphere);

                foreach (var entity in entityList)
                {
                    var grid = entity as MyCubeGrid;

                    if (grid == null || grid.GridSizeEnum == MyCubeSize.Small)
                    {
                        continue;
                    }

                    isInGrid = true;
                    break;
                }
            }
            finally
            {
                if (entityList != null)
                {
                    entityList.Clear();
                }
            }

            ProfilerShort.End();
            return(isInGrid);
        }
Exemplo n.º 7
0
        private static bool ResetVoxelInArea(Vector3D Center, float Radius)
        {
            try
            {
                BoundingSphereD    Sphere = new BoundingSphereD(Center, Radius);
                List <MyVoxelBase> Maps   = MyEntities.GetEntitiesInSphere(ref Sphere).OfType <MyVoxelBase>().ToList();
                if (Maps.Count == 0)
                {
                    return(true);
                }

                foreach (var voxelMap in Maps)
                {
                    using (voxelMap.Pin())
                    {
                        if (voxelMap.MarkedForClose)
                        {
                            continue;
                        }
                        MyShapeSphere shape = new MyShapeSphere();
                        shape.Center = Center;
                        shape.Radius = Radius;
                        Vector3I     minCorner;
                        Vector3I     maxCorner;
                        Vector3I     numCells;
                        BoundingBoxD shapeAabb   = shape.GetWorldBoundaries();
                        Vector3I     StorageSize = voxelMap.Storage.Size;
                        MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelMap.PositionLeftBottomCorner, ref shapeAabb.Min, out minCorner);
                        MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelMap.PositionLeftBottomCorner, ref shapeAabb.Max, out maxCorner);
                        minCorner   += voxelMap.StorageMin;
                        maxCorner   += voxelMap.StorageMin;
                        maxCorner   += 1;
                        StorageSize -= 1;
                        Vector3I.Clamp(ref minCorner, ref Vector3I.Zero, ref StorageSize, out minCorner);
                        Vector3I.Clamp(ref maxCorner, ref Vector3I.Zero, ref StorageSize, out maxCorner);
                        numCells = new Vector3I((maxCorner.X - minCorner.X) / 16, (maxCorner.Y - minCorner.Y) / 16, (maxCorner.Z - minCorner.Z) / 16);


                        minCorner = Vector3I.Max(Vector3I.One, minCorner);
                        maxCorner = Vector3I.Max(minCorner, maxCorner - Vector3I.One);
                        voxelMap.Storage.DeleteRange(MyStorageDataTypeFlags.ContentAndMaterial, minCorner, maxCorner, false);
                        BoundingBoxD cutOutBox = shape.GetWorldBoundaries();
                        MySandboxGame.Static.Invoke(delegate
                        {
                            if (voxelMap.Storage != null)
                            {
                                voxelMap.Storage.NotifyChanged(minCorner, maxCorner, MyStorageDataTypeFlags.ContentAndMaterial);
                                MyVoxelGenerator.NotifyVoxelChanged(MyVoxelBase.OperationType.Revert, voxelMap, ref cutOutBox);
                            }
                        }, "RevertShape notify");
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "Voxel reset failed!");
                return(false);
            }
        }
Exemplo n.º 8
0
        public static List <ComponentInfo> FindComponentsInRadius(Vector3D fromPosition, double radius)
        {
            Debug.Assert(m_retvalBlockInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            BoundingSphereD sphere = new BoundingSphereD(fromPosition, radius);

            var entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                if (entity is MyFloatingObject)
                {
                    var floatingObject = entity as MyFloatingObject;
                    if (floatingObject.Item.Content is MyObjectBuilder_Component)
                    {
                        ComponentInfo info = new ComponentInfo();
                        info.EntityId              = floatingObject.EntityId;
                        info.BlockPosition         = Vector3I.Zero;
                        info.ComponentDefinitionId = floatingObject.Item.Content.GetObjectId();
                        info.IsBlock        = false;
                        info.ComponentCount = (int)floatingObject.Item.Amount;
                        m_retvalBlockInfos.Add(info);
                    }
                }
                else
                {
                    MyCubeBlock block = null;
                    MyCubeGrid  grid  = TryGetAsComponent(entity, out block);
                    if (grid == null)
                    {
                        continue;
                    }

                    ComponentInfo info = new ComponentInfo();
                    info.IsBlock               = true;
                    info.EntityId              = grid.EntityId;
                    info.BlockPosition         = block.Position;
                    info.ComponentDefinitionId = GetComponentId(block.SlimBlock);

                    if (block.BlockDefinition.Components != null)
                    {
                        info.ComponentCount = block.BlockDefinition.Components[0].Count;
                    }
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.ComponentCount = 0;
                    }
                    m_retvalBlockInfos.Add(info);
                }
            }

            entities.Clear();

            return(m_retvalBlockInfos);
        }
Exemplo n.º 9
0
        List <IMyEntity> IMyEntities.GetEntitiesInSphere(ref VRageMath.BoundingSphereD boundingSphere)
        {
            var lst    = MyEntities.GetEntitiesInSphere(ref boundingSphere);
            var result = new List <IMyEntity>(lst.Count);

            foreach (var entity in lst)
            {
                result.Add(entity);
            }
            lst.Clear();
            return(result);
        }
Exemplo n.º 10
0
        private List <MyEntity> GetEntities()
        {
            var players = new List <IMyPlayer>();

            MyAPIGateway.Players.GetPlayers(players);
            var player         = players[0];
            var playerPosition = player.Character.PositionComp.GetPosition();
            var sphere         = new BoundingSphereD(playerPosition, radius: 100.0);
            var entities       = MyEntities.GetEntitiesInSphere(ref sphere);

            return(entities);
        }
Exemplo n.º 11
0
        private void GatherGradeComponents(Vector3D pos)
        {
            _gradeComponents.Clear();
            var sphere = new BoundingSphereD(pos, GradeScanDistance);
            var tmp    = MyEntities.GetEntitiesInSphere(ref sphere);

            using (tmp.GetClearToken())
            {
                foreach (var e in tmp)
                {
                    FillGradeComponents(pos, e);
                }
            }
        }
Exemplo n.º 12
0
        /*private static MyPlanet GetNearestPlanet(Vector3D position)
         * {
         *  MyPlanet planet = GetNearestPlanet(position);
         *  if (planet == null)
         *      planet = GetNearestFromCache(position);
         *  else
         *      knownPlanets.Add(planet);
         *
         *  return planet;
         * }*/

        /*private static MyPlanet GetNearestFromCache(Vector3D position)
         * {
         *  MyPlanet planet = null;
         *  double nearestDistanceSq = double.MaxValue;
         *  foreach (MyPlanet knownPlanet in knownPlanets)
         *  {
         *      double distanceSq = Vector3D.DistanceSquared(position, knownPlanet.WorldMatrix.Translation);
         *      if (distanceSq < nearestDistanceSq)
         *      {
         *          nearestDistanceSq = distanceSq;
         *          planet = knownPlanet;
         *      }
         *  }
         *  return planet;
         * }*/

        private static MyPlanet FindNearestPlanet(Vector3D position)
        {
            BoundingSphereD sphere   = new BoundingSphereD(position, 1);
            var             entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (MyEntity entity in entities)
            {
                if (entity is MyPlanet)
                {
                    return((MyPlanet)entity);
                }
            }

            return(null);
        }
Exemplo n.º 13
0
        private static void CheckTargetValid()
        {
            if (!m_currentTarget.HasValue)
            {
                return;
            }
            m_tmpEntityList.Clear();
            var bs = m_currentTarget.Value;

            m_tmpEntityList = MyEntities.GetEntitiesInSphere(ref bs);
            if (m_tmpEntityList.OfType <MyCubeGrid>().ToList().Count == 0)
            {
                m_waveCounter = -1;
            }
            m_tmpEntityList.Clear();
        }
Exemplo n.º 14
0
        public static Vector3D FindFreePos(BoundingSphereD gate, float sphereradius)
        {
            Random rand = new Random();

            MyEntity safezone = null;
            var      entities = MyEntities.GetEntitiesInSphere(ref gate);

            foreach (MyEntity myentity in entities)
            {
                if (myentity is MySafeZone)
                {
                    safezone = myentity;
                }
            }
            return((Vector3D)MyEntities.FindFreePlaceCustom(gate.RandomToUniformPointInSphere(rand.NextDouble(), rand.NextDouble(), rand.NextDouble()), sphereradius, 20, 5, 1, 0, safezone));
        }
Exemplo n.º 15
0
        private IEnumerable <MyEntity> EnumerateSurroundingEntities(BoundingSphereD sphere)
        {
            List <MyEntity> entities = MyEntities.GetEntitiesInSphere(ref sphere);

            try
            {
                foreach (MyEntity entity in entities)
                {
                    yield return(entity);
                }
            }
            finally
            {
                entities.Clear();
            }
        }
Exemplo n.º 16
0
        protected override void ReadEntitiesInRange()
        {
            m_entitiesInRange.Clear();

            BoundingSphereD bsphere = new BoundingSphereD(Center, m_radius);
            var             res     = MyEntities.GetEntitiesInSphere(ref bsphere);

            for (int i = 0; i < res.Count; ++i)
            {
                var rootEntity = res[i].GetTopMostParent();
                if (!IgnoredEntities.Contains(rootEntity))
                {
                    m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity, FrontPoint);
                }
            }
            res.Clear();
        }
Exemplo n.º 17
0
        private static bool positionHasNearbyLightSource(Vector3D position)
        {
            var sphere   = new BoundingSphereD(position, LIGHT_ENTITY_RADIUS);
            var entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                foreach (MyDefinitionId definitionId in lightEntityDefinitionIds)
                {
                    if (definitionId == entity.DefinitionId)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        private bool IsPointInAirtightSpace(Vector3D worldPosition)
        {
            if (!MySession.Static.Settings.EnableOxygen)
            {
                return(true);
            }

            bool            cameraInAirtightSpace = false;
            var             sphere     = new BoundingSphereD(worldPosition, 0.1);
            List <MyEntity> entityList = null;

            try
            {
                entityList = MyEntities.GetEntitiesInSphere(ref sphere);

                foreach (var entity in entityList)
                {
                    var grid = entity as MyCubeGrid;

                    if (grid == null || grid.GridSystems.GasSystem == null)
                    {
                        continue;
                    }

                    var safeOxygenBlock = grid.GridSystems.GasSystem.GetSafeOxygenBlock(worldPosition);

                    if (safeOxygenBlock.Room == null || !safeOxygenBlock.Room.IsPressurized)
                    {
                        continue;
                    }

                    cameraInAirtightSpace = true;
                    break;
                }
            }
            finally
            {
                if (entityList != null)
                {
                    entityList.Clear();
                }
            }

            return(cameraInAirtightSpace);
        }
        private void GetEntities()
        {
            var players = new List <IMyPlayer>();

            MyAPIGateway.Players.GetPlayers(players);
            var player         = players[0];
            var playerPosition = player.Character.PositionComp.GetPosition();
            var sphere         = new BoundingSphereD(playerPosition, radius: 50.0);

            var entities = MyEntities.GetEntitiesInSphere(ref sphere);

            var rotor1 = entities
                         .Where(x => x is MyMotorAdvancedStator)
                         .Cast <MyMotorAdvancedStator>()
                         .SingleOrDefault(x => x.CustomName.ToString() == "Rotor 1");

            entities.Clear();
        }
Exemplo n.º 21
0
        public static List <CollectibleInfo> FindCollectiblesInRadius(Vector3D fromPosition, double radius)
        {
            Debug.Assert(m_retvalCollectibleInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            BoundingSphereD sphere   = new BoundingSphereD(fromPosition, radius);
            var             entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                CollectibleInfo info  = new CollectibleInfo();
                MyCubeBlock     block = null;
                MyCubeGrid      grid  = TryGetAsComponent(entity, out block);
                if (grid != null)
                {
                    info.EntityId     = grid.EntityId;
                    info.DefinitionId = GetComponentId(block.SlimBlock);
                    if (block.BlockDefinition.Components != null)
                    {
                        info.Amount = block.BlockDefinition.Components[0].Count;
                    }
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.Amount = 0;
                    }
                    m_retvalCollectibleInfos.Add(info);
                }
                else if (entity is MyFloatingObject)
                {
                    var floatingObj = entity as MyFloatingObject;
                    var defId       = floatingObj.Item.Content.GetObjectId();
                    if (MyDefinitionManager.Static.GetPhysicalItemDefinition(defId).Public)
                    {
                        info.EntityId     = floatingObj.EntityId;
                        info.DefinitionId = defId;
                        info.Amount       = floatingObj.Item.Amount;
                        m_retvalCollectibleInfos.Add(info);
                    }
                }
            }
            entities.Clear();

            return(m_retvalCollectibleInfos);
        }
    private bool MustBeExecuted(IMyPlayer player)
    {
        Vector3D position = player.GetPosition();

        BoundingSphereD sphere = new BoundingSphereD(position, 3.0);

        List <MyEntity> entities = MyEntities.GetEntitiesInSphere(ref sphere);

        double         minDist = int.MaxValue;
        IMyMedicalRoom nearest = null;

        foreach (MyEntity entity in entities)
        {
            if (!(entity is IMyMedicalRoom))
            {
                continue;
            }

            IMyMedicalRoom medicalRoom = entity as IMyMedicalRoom;

            var dist = Vector3D.DistanceSquared(medicalRoom.GetPosition(), position);

            if (dist < minDist)
            {
                minDist = dist;
                nearest = medicalRoom;
            }
        }

        if (nearest == null)
        {
            return(false);
        }

        /* Only affect Medbays that have the following customData */
        if (nearest.CustomData.Contains("spawnPointMedbay"))
        {
            logger.WriteLine(player.DisplayName + " spawned at " + nearest.CustomName + " on grid " + nearest.CubeGrid.Name);
            return(true);
        }

        return(false);
    }
Exemplo n.º 23
0
        static MyEntityPhysicsStateGroup SphereBounds(MyCharacter character, float radius)
        {
            if (DEBUG_DRAW)
            {
                VRageRender.MyRenderProxy.DebugDrawSphere(character.PositionComp.GetPosition(), radius, Color.Green, 1, false, false);
            }

            // Sphere with radius 5m around character
            BoundingSphereD sphere = new BoundingSphereD(character.PositionComp.GetPosition(), radius);
            var             list   = MyEntities.GetEntitiesInSphere(ref sphere);

            try
            {
                MyEntityPhysicsStateGroup closest = null;
                float distanceSq = float.MaxValue;

                foreach (var entity in list)
                {
                    if (!IsValid(entity, false))
                    {
                        continue;
                    }

                    float distSq = (float)entity.PositionComp.WorldAABB.DistanceSquared(sphere.Center);
                    if (distSq < distanceSq && distSq < (radius * radius))
                    {
                        var phys = FindPhysics(entity);
                        if (phys != null)
                        {
                            closest    = phys;
                            distanceSq = distSq;
                        }
                    }
                }
                return(closest);
            }
            finally
            {
                list.Clear();
            }
        }
Exemplo n.º 24
0
        private static Vector3D?FindPastePosition(MyObjectBuilder_CubeGrid[] grids, BoundingSphereD position, double cutout)
        {
            BoundingSphere sphere = FindBoundingSphere(grids);

            /*
             * Now we know the radius that can house all grids which will now be
             * used to determine the perfect place to paste the grids to.
             */

            Random   rand     = new Random();
            MyEntity safezone = null;
            var      entities = MyEntities.GetEntitiesInSphere(ref position);

            foreach (MyEntity entity in entities)
            {
                if (entity is MySafeZone)
                {
                    safezone = entity;
                }
            }
            return(MyEntities.FindFreePlaceCustom(position.RandomToUniformPointInSphereWithInnerCutout(rand.NextDouble(), rand.NextDouble(), rand.NextDouble(), cutout).GetValueOrDefault(), sphere.Radius + 50, 20, 5, 1, 0, safezone));
        }
Exemplo n.º 25
0
        public static List <BlockInfo> FindComponentsInRadius(Vector3D fromPosition, double radius)
        {
            Debug.Assert(m_retvalBlockInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            BoundingSphereD sphere = new BoundingSphereD(fromPosition, radius);

            var entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                MyCubeBlock block = null;
                MyCubeGrid  grid  = TryGetAsComponent(entity, out block);
                if (grid == null)
                {
                    continue;
                }

                BlockInfo info = new BlockInfo();
                info.GridEntityId          = grid.EntityId;
                info.BlockPosition         = block.Position;
                info.ComponentDefinitionId = GetComponentId(block.SlimBlock);

                if (block.BlockDefinition.Components != null)
                {
                    info.ComponentCount = block.BlockDefinition.Components[0].Count;
                }
                else
                {
                    Debug.Assert(false, "Block definition does not have any components!");
                    info.ComponentCount = 0;
                }

                m_retvalBlockInfos.Add(info);
            }

            entities.Clear();

            return(m_retvalBlockInfos);
        }
Exemplo n.º 26
0
        private MyWelder.ProjectionRaycastData[] FindProjectedBlocks()
        {
            BoundingSphereD globalSphere = new BoundingSphereD(Vector3D.Transform(m_detectorSphere.Center, CubeGrid.WorldMatrix), m_detectorSphere.Radius);

            var m_raycastData = new List <MyWelder.ProjectionRaycastData>();

            var entitiesInSphere = MyEntities.GetEntitiesInSphere(ref globalSphere);

            foreach (var entity in entitiesInSphere)
            {
                var grid = entity as MyCubeGrid;
                if (grid != null)
                {
                    if (grid.Projector != null)
                    {
                        grid.GetBlocksInsideSphere(ref globalSphere, m_projectedBlock);
                        foreach (var block in m_projectedBlock)
                        {
                            var canBuild = grid.Projector.CanBuild(block, true);
                            if (canBuild == BuildCheckResult.OK)
                            {
                                var cubeBlock = grid.GetCubeBlock(block.Position);
                                if (cubeBlock != null)
                                {
                                    m_raycastData.Add(new MyWelder.ProjectionRaycastData(BuildCheckResult.OK, cubeBlock, grid.Projector));
                                }
                            }
                        }
                        m_projectedBlock.Clear();
                    }
                }
            }

            m_projectedBlock.Clear();
            entitiesInSphere.Clear();

            return(m_raycastData.ToArray());
        }
Exemplo n.º 27
0
        private bool IsPointInAirtightSpace(Vector3D worldPosition)
        {
            if (!MySession.Static.Settings.EnableOxygen)
            {
                return(true);
            }
            bool            flag           = false;
            BoundingSphereD boundingSphere = new BoundingSphereD(worldPosition, 0.1);
            List <MyEntity> list           = null;

            try
            {
                foreach (MyCubeGrid grid in MyEntities.GetEntitiesInSphere(ref boundingSphere))
                {
                    if (grid == null)
                    {
                        continue;
                    }
                    if (grid.GridSystems.GasSystem != null)
                    {
                        MyOxygenBlock safeOxygenBlock = grid.GridSystems.GasSystem.GetSafeOxygenBlock(worldPosition);
                        if ((safeOxygenBlock != null) && ((safeOxygenBlock.Room != null) && safeOxygenBlock.Room.IsAirtight))
                        {
                            flag = true;
                            break;
                        }
                    }
                }
            }
            finally
            {
                if (list != null)
                {
                    list.Clear();
                }
            }
            return(flag);
        }
Exemplo n.º 28
0
        public static bool FindClosestTreeInRadius(Vector3D fromPosition, float radius, out ItemInfo result)
        {
            result = default(ItemInfo);

            BoundingSphereD sphere   = new BoundingSphereD(fromPosition, (double)radius);
            var             entities = MyEntities.GetEntitiesInSphere(ref sphere);

            double closestDistanceSq = double.MaxValue;

            foreach (MyEntity entity in entities)
            {
                MyTrees trees = entity as MyTrees;
                if (trees == null)
                {
                    continue;
                }

                trees.GetPhysicalItemsInRadius(fromPosition, radius, m_tmpEnvItemList);

                foreach (var tree in m_tmpEnvItemList)
                {
                    double distanceSq = Vector3D.DistanceSquared(fromPosition, tree.Transform.Position);
                    if (distanceSq < closestDistanceSq)
                    {
                        result.ItemsEntityId = entity.EntityId;
                        result.ItemId        = tree.LocalId;
                        result.Target        = tree.Transform.Position;
                        closestDistanceSq    = distanceSq;
                    }
                }
            }

            entities.Clear();

            return(closestDistanceSq != double.MaxValue);
        }
Exemplo n.º 29
0
        public bool TryNamingAnBlockOrFloatingObject()
        {
            var worldMatrix = MyAPIGateway.Session.Camera.WorldMatrix; // most accurate for player view.
            var position    = worldMatrix.Translation + worldMatrix.Forward * 0.5f;
            var ray         = new RayD(position, worldMatrix.Forward * 1000);

            var boundingSphere = new BoundingSphereD(worldMatrix.Translation, 30);
            var entites        = MyEntities.GetEntitiesInSphere(ref boundingSphere);

            List <MyPhysics.HitInfo> hits = new List <MyPhysics.HitInfo>();

            MyPhysics.CastRay(worldMatrix.Translation, worldMatrix.Translation + worldMatrix.Forward * 5, hits, 15);

            foreach (var hitInfo in hits)
            {
                var body = (MyPhysicsBody)hitInfo.HkHitInfo.Body.UserObject;
                if (body.Entity is MyFloatingObject)
                {
                    var rayEntity = (MyEntity)body.Entity;
                    NameDialog(rayEntity);
                    return(true);
                }
            }

            foreach (var entity in entites)
            {
                var cubeGrid = entity as MyCubeGrid;

                if (cubeGrid != null && ray.Intersects(entity.PositionComp.WorldAABB).HasValue)
                {
                    var hit = cubeGrid.RayCastBlocks(worldMatrix.Translation, worldMatrix.Translation + worldMatrix.Forward * 100);
                    if (hit.HasValue)
                    {
                        var block = cubeGrid.GetCubeBlock(hit.Value);

                        if (block.FatBlock != null)
                        {
                            var dialog = new ValueGetScreenWithCaption("Name block dialog: " + block.FatBlock.DefinitionDisplayNameText, block.FatBlock.Name ?? block.FatBlock.DefinitionDisplayNameText + " has no name.", delegate(string text)
                            {
                                MyEntity foundEntity;
                                if (MyEntities.TryGetEntityByName(text, out foundEntity))
                                {
                                    MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
                                                               buttonType: MyMessageBoxButtonsType.OK,
                                                               messageText: new StringBuilder("Entity with same name already exits, please enter different name."),
                                                               messageCaption: new StringBuilder("Naming error")));
                                }
                                else
                                {
                                    block.FatBlock.Name = text;
                                    MyEntities.SetEntityName(block.FatBlock, true);

                                    return(true);
                                }

                                return(false);
                            });
                            MyGuiSandbox.AddScreen(dialog);
                            entites.Clear();
                            return(true);
                        }
                    }
                }
            }

            entites.Clear();
            return(false);
        }
Exemplo n.º 30
0
        public static List <CollectibleInfo> FindCollectiblesInRadius(Vector3D fromPosition, double radius, bool doRaycast = false)
        {
            Debug.Assert(m_retvalCollectibleInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            List <MyPhysics.HitInfo> hits = new List <MyPhysics.HitInfo>();

            BoundingSphereD sphere   = new BoundingSphereD(fromPosition, radius);
            var             entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                bool addCollectibleInfo = false;

                CollectibleInfo info  = new CollectibleInfo();
                MyCubeBlock     block = null;
                MyCubeGrid      grid  = TryGetAsComponent(entity, out block);
                if (grid != null)
                {
                    info.EntityId     = grid.EntityId;
                    info.DefinitionId = GetComponentId(block.SlimBlock);
                    if (block.BlockDefinition.Components != null)
                    {
                        info.Amount = block.BlockDefinition.Components[0].Count;
                    }
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.Amount = 0;
                    }
                    addCollectibleInfo = true;
                }
                else if (entity is MyFloatingObject)
                {
                    var floatingObj = entity as MyFloatingObject;
                    var defId       = floatingObj.Item.Content.GetObjectId();
                    if (MyDefinitionManager.Static.GetPhysicalItemDefinition(defId).Public)
                    {
                        info.EntityId      = floatingObj.EntityId;
                        info.DefinitionId  = defId;
                        info.Amount        = floatingObj.Item.Amount;
                        addCollectibleInfo = true;
                    }
                }

                if (addCollectibleInfo)
                {
                    bool hitSomething = false;
                    MyPhysics.CastRay(fromPosition, entity.WorldMatrix.Translation, hits, MyPhysics.CollisionLayers.DefaultCollisionLayer);
                    foreach (var hit in hits)
                    {
                        var hitEntity = hit.HkHitInfo.GetHitEntity();
                        if (hitEntity == entity)
                        {
                            continue;
                        }
                        if (hitEntity is MyCharacter)
                        {
                            continue;
                        }
                        if (hitEntity is MyFracturedPiece)
                        {
                            continue;
                        }
                        if (hitEntity is MyFloatingObject)
                        {
                            continue;
                        }
                        MyCubeBlock dummy = null;
                        if (TryGetAsComponent(hitEntity as MyEntity, out dummy) != null)
                        {
                            continue;
                        }
                        hitSomething = true;
                        break;
                    }

                    if (!hitSomething)
                    {
                        m_retvalCollectibleInfos.Add(info);
                    }
                }
            }
            entities.Clear();

            return(m_retvalCollectibleInfos);
        }