コード例 #1
0
        public static bool FindClosestPlaceAreaInSphere(BoundingSphereD sphere, string typeName, ref MyBBMemoryTarget foundTarget)
        {
            var foundAreas = new List <MyPlaceArea>();

            MyPlaceAreas.Static.GetAllAreasInSphere(sphere, foundAreas);

            var areaType = MyStringHash.GetOrCompute(typeName);

            double      closestDistanceSq = sphere.Radius * sphere.Radius;
            MyPlaceArea closestArea       = null;

            foreach (var area in foundAreas)
            {
                if (area.Container.Entity == null || area.AreaType != areaType)
                {
                    continue;
                }

                double distanceSq = area.DistanceSqToPoint(sphere.Center);
                if (distanceSq < closestDistanceSq)
                {
                    closestDistanceSq = distanceSq;
                    closestArea       = area;
                }
            }

            if (closestArea == null)
            {
                return(false);
            }
            MyBBMemoryTarget.SetTargetEntity(ref foundTarget, MyAiTargetEnum.ENTITY, closestArea.Container.Entity.EntityId);
            return(true);
        }
コード例 #2
0
ファイル: MyAreaMarker.cs プロジェクト: warex0/SpaceEngineers
        public override void Init(MyObjectBuilder_EntityBase objectBuilder)
        {
            base.Init(objectBuilder);

            MyDefinitionManager.Static.TryGetDefinition(objectBuilder.GetId(), out m_definition);
            Debug.Assert(m_definition != null, "Area marker definition cannot be null!");
            if (m_definition == null) return;

			m_tmpPlaceAreas.Clear();
			MyPlaceAreas.Static.GetAllAreas(m_tmpPlaceAreas);

			MyPlaceArea firstFound = null;
			int markerCount = 0;
			foreach (var area in m_tmpPlaceAreas)
			{
				if (area.AreaType == m_definition.Id.SubtypeId)
				{
					if (firstFound == null)
						firstFound = area;
					++markerCount;
				}
			}
			if (m_definition.MaxNumber >= 0 && markerCount >= m_definition.MaxNumber)
			{
				if (SyncFlag)
					firstFound.Entity.SyncObject.SendCloseRequest();
				else
					firstFound.Entity.Close();
			}

			m_tmpPlaceAreas.Clear();
			
            InitInternal();
        }
コード例 #3
0
        public static bool FindRandomFracturedTreeInPlaceArea(Vector3D fromPosition, long entityId, out EntityInfo result)
        {
            result = default(EntityInfo);

            MyPlaceArea area = MyPlaceArea.FromEntity(entityId);

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

            var             areaBB       = area.WorldAABB;
            BoundingSphereD searchSphere = new BoundingSphereD(areaBB.Center, (double)areaBB.HalfExtents.Length());

            m_tmpFracturePieceList.Clear();
            FindFracturedTreesInternal(fromPosition, area, searchSphere);

            if (m_tmpFracturePieceList.Count == 0)
            {
                m_tmpFracturePieceList.Clear();
                return(false);
            }

            int fractureIndex = (int)Math.Round(MyRandom.Instance.NextFloat() * (m_tmpFracturePieceList.Count - 1));
            MyFracturedPiece selectedTarget = m_tmpFracturePieceList[fractureIndex];

            m_tmpFracturePieceList.Clear();

            result.EntityId = selectedTarget.EntityId;
            result.Target   = selectedTarget.PositionComp.GetPosition();
            return(true);
        }
コード例 #4
0
 public override void Init(MyObjectBuilder_EntityBase objectBuilder)
 {
     base.Init(objectBuilder);
     MyDefinitionManager.Static.TryGetDefinition <MyAreaMarkerDefinition>(objectBuilder.GetId(), out this.m_definition);
     if (this.m_definition != null)
     {
         m_tmpPlaceAreas.Clear();
         MyPlaceAreas.Static.GetAllAreas(m_tmpPlaceAreas);
         MyPlaceArea area = null;
         int         num  = 0;
         foreach (MyPlaceArea area2 in m_tmpPlaceAreas)
         {
             if (area2.AreaType == this.m_definition.Id.SubtypeId)
             {
                 if (area == null)
                 {
                     area = area2;
                 }
                 num++;
             }
         }
         if ((this.m_definition.MaxNumber >= 0) && (num >= this.m_definition.MaxNumber))
         {
             MyEntities.SendCloseRequest(area.Entity);
         }
         m_tmpPlaceAreas.Clear();
         this.InitInternal();
     }
 }
コード例 #5
0
        public static bool FindRandomTreeInPlaceArea(long entityId, out ItemInfo result)
        {
            result = default(ItemInfo);

            MyPlaceArea area = MyPlaceArea.FromEntity(entityId);

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

            var             areaBoundingBox = area.WorldAABB;
            List <MyEntity> entities        = null;

            try
            {
                entities = MyEntities.GetEntitiesInAABB(ref areaBoundingBox);

                m_tmpItemInfoList.Clear();
                foreach (var entity in entities)
                {
                    MyTrees trees = entity as MyTrees;
                    if (trees == null)
                    {
                        continue;
                    }

                    m_tmpEnvItemList.Clear();
                    trees.GetPhysicalItemsInRadius(areaBoundingBox.Center, (float)areaBoundingBox.HalfExtents.Length(), m_tmpEnvItemList);
                    foreach (var tree in m_tmpEnvItemList)
                    {
                        if (area.TestPoint(tree.Transform.Position))
                        {
                            var itemInfo = new ItemInfo();
                            itemInfo.ItemsEntityId = trees.EntityId;
                            itemInfo.ItemId        = tree.LocalId;
                            itemInfo.Target        = tree.Transform.Position;
                            m_tmpItemInfoList.Add(itemInfo);
                        }
                    }
                    m_tmpEnvItemList.Clear();
                }

                if (m_tmpItemInfoList.Count == 0)
                {
                    m_tmpItemInfoList.Clear();
                    return(false);
                }

                int treeIndex = (int)Math.Round(MyRandom.Instance.NextFloat() * (m_tmpItemInfoList.Count - 1));
                result = m_tmpItemInfoList[treeIndex];
                m_tmpItemInfoList.Clear();

                return(true);
            }
            finally
            {
                entities.Clear();
            }
        }
コード例 #6
0
 public static void MovePlaceArea(MyPlaceArea area)
 {
     if (area.PlaceAreaProxyId != MyConstants.PRUNING_PROXY_ID_UNITIALIZED)
     {
         BoundingBoxD box = area.PositionComp.WorldAABB;
         m_aabbTree.MoveProxy(area.PlaceAreaProxyId, ref box, Vector3.Zero);
     }
 }
コード例 #7
0
 public static void AddPlaceArea(MyPlaceArea area)
 {
     if (area.PlaceAreaProxyId == MyConstants.PRUNING_PROXY_ID_UNITIALIZED)
     {
         BoundingBoxD box = area.PositionComp.WorldAABB;
         area.PlaceAreaProxyId = m_aabbTree.AddProxy(ref box, area, 0);
     }
 }
コード例 #8
0
 public static void RemovePlaceArea(MyPlaceArea area)
 {
     if (area.PlaceAreaProxyId != MyConstants.PRUNING_PROXY_ID_UNITIALIZED)
     {
         m_aabbTree.RemoveProxy(area.PlaceAreaProxyId);
         area.PlaceAreaProxyId = MyConstants.PRUNING_PROXY_ID_UNITIALIZED;
     }
 }
コード例 #9
0
 public static void MovePlaceArea(MyPlaceArea area)
 {
     if (area.PlaceAreaProxyId != MyConstants.PRUNING_PROXY_ID_UNITIALIZED)
     {
         BoundingBoxD box = area.PositionComp.WorldAABB;
         m_aabbTree.MoveProxy(area.PlaceAreaProxyId, ref box, Vector3.Zero);
     }
 }
コード例 #10
0
 public void AddPlaceArea(MyPlaceArea area)
 {
     if (area.PlaceAreaProxyId == MyVRageConstants.PRUNING_PROXY_ID_UNITIALIZED)
     {
         BoundingBoxD box = area.WorldAABB;
         area.PlaceAreaProxyId = m_aabbTree.AddProxy(ref box, area, 0);
     }
 }
コード例 #11
0
 public void RemovePlaceArea(MyPlaceArea area)
 {
     if (area.PlaceAreaProxyId != MyVRageConstants.PRUNING_PROXY_ID_UNITIALIZED)
     {
         m_aabbTree.RemoveProxy(area.PlaceAreaProxyId);
         area.PlaceAreaProxyId = MyVRageConstants.PRUNING_PROXY_ID_UNITIALIZED;
     }
 }
コード例 #12
0
        public static bool FindClosestTreeInPlaceArea(Vector3D fromPosition, long entityId, MyHumanoidBot bot, out ItemInfo result)
        {
            result = default(ItemInfo);
            MyPlaceArea area = MyPlaceArea.FromEntity(entityId);

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

            var areaBoundingBox = area.WorldAABB;
            var entities        = MyEntities.GetEntitiesInAABB(ref areaBoundingBox, true);

            double closestDistanceSq = double.MaxValue;

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

                m_tmpEnvItemList.Clear();
                trees.GetPhysicalItemsInRadius(areaBoundingBox.Center, (float)areaBoundingBox.HalfExtents.Length(), m_tmpEnvItemList);

                foreach (var tree in m_tmpEnvItemList)
                {
                    if (!area.TestPoint(tree.Transform.Position))
                    {
                        continue;
                    }

                    if (!bot.AgentLogic.AiTarget.IsTreeReachable(entity, tree.LocalId))
                    {
                        continue;
                    }

                    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;
                    }
                }
                m_tmpEnvItemList.Clear();
            }

            entities.Clear();

            return(closestDistanceSq != double.MaxValue);
        }
コード例 #13
0
        public static bool FindClosestFracturedTreeInArea(Vector3D fromPosition, long areaEntityId, out EntityInfo result)
        {
            result = default(EntityInfo);

            MyPlaceArea area = MyPlaceArea.FromEntity(areaEntityId);

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

            BoundingBoxD areaBB = area.WorldAABB;
            double       radius = (double)areaBB.HalfExtents.Length();


            return(FindClosestFracturedTreeInternal(fromPosition, areaBB.Center, radius, area, out result));;
        }
コード例 #14
0
        public static MyPlaceArea FromEntity(long entityId)
        {
            MyPlaceArea area   = null;
            MyEntity    entity = null;

            if (!MyEntities.TryGetEntityById(entityId, out entity))
            {
                return(area);
            }

            if (entity.Components.TryGet <MyPlaceArea>(out area))
            {
                return(area);
            }
            else
            {
                return(null);
            }
        }
コード例 #15
0
        private static void FindFracturedTreesInternal(Vector3D fromPosition, MyPlaceArea area, BoundingSphereD sphere)
        {
            Debug.Assert(m_tmpFracturePieceList.Count == 0, "m_tmpFracturePieceList was not cleared after last use!");

            MyFracturedPiecesManager.Static.GetFracturesInSphere(ref sphere, ref m_tmpFracturePieceList);

            for (int i = m_tmpFracturePieceList.Count - 1; i >= 0; i--)
            {
                MyFracturedPiece fracture = m_tmpFracturePieceList[i];

                if (!MyTrees.IsEntityFracturedTree(fracture))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }

                if (IsFracturedTreeStump(fracture))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }

                Vector3D positionInTrunkLocal = Vector3D.Transform(fromPosition, fracture.PositionComp.WorldMatrixNormalizedInv);
                Vector3D closestPointOnTrunk;

                if (!FindClosestPointOnFracturedTree(positionInTrunkLocal, fracture, out closestPointOnTrunk))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }

                if (!area.TestPoint(closestPointOnTrunk))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }
            }
        }
コード例 #16
0
        private static bool FindClosestFracturedTreeInternal(Vector3D fromPosition, Vector3D searchCenter, double searchRadius, MyPlaceArea area, out EntityInfo result)
        {
            result = default(EntityInfo);

            double closestDistanceSq = double.MaxValue;
            MyFracturedPiece closestTarget = null;
            Vector3D closestPoint = default(Vector3D);

            BoundingSphereD searchSphere = new BoundingSphereD(searchCenter, searchRadius);
            
            m_tmpFracturePieceList.Clear();
            MyFracturedPiecesManager.Static.GetFracturesInSphere(ref searchSphere, ref m_tmpFracturePieceList);
            for (int i = 0; i < m_tmpFracturePieceList.Count; ++i)
            {
                var fracture = m_tmpFracturePieceList[i];

                // Skip non-tree fractures
                if (!MyTrees.IsEntityFracturedTree(fracture))
                {
                    continue;
                }

                if (IsFracturedTreeStump(fracture))
                    continue;

                Vector3D positionInTrunkLocal = Vector3D.Transform(fromPosition, fracture.PositionComp.WorldMatrixNormalizedInv);
                Vector3D closestPointOnTrunk;

                if (!FindClosestPointOnFracturedTree(positionInTrunkLocal, fracture, out closestPointOnTrunk))
                    continue;

                if (area == null || area.TestPoint(closestPointOnTrunk))
                {
                    double distanceSq = Vector3D.DistanceSquared(closestPointOnTrunk, fromPosition);

                    if (distanceSq < closestDistanceSq)
                    {
                        closestDistanceSq = distanceSq;
                        closestTarget = fracture;
                        closestPoint = closestPointOnTrunk;
                    }
                }
            }
            m_tmpFracturePieceList.Clear();

            if (closestTarget == null)
                return false;

            result.EntityId = closestTarget.EntityId;
            result.Target = closestPoint;
            return true;
        }
コード例 #17
0
        private static bool FindClosestFracturedTreeInternal(Vector3D fromPosition, Vector3D searchCenter, double searchRadius, MyPlaceArea area, out EntityInfo result)
        {
            result = default(EntityInfo);

            double           closestDistanceSq = double.MaxValue;
            MyFracturedPiece closestTarget     = null;
            Vector3D         closestPoint      = default(Vector3D);

            BoundingSphereD searchSphere = new BoundingSphereD(searchCenter, searchRadius);

            m_tmpFracturePieceList.Clear();
            MyFracturedPiecesManager.Static.GetFracturesInSphere(ref searchSphere, ref m_tmpFracturePieceList);
            for (int i = 0; i < m_tmpFracturePieceList.Count; ++i)
            {
                var fracture = m_tmpFracturePieceList[i];

                // Skip non-tree fractures
                if (!MyTrees.IsEntityFracturedTree(fracture))
                {
                    continue;
                }

                if (IsFracturedTreeStump(fracture))
                {
                    continue;
                }

                Vector3D positionInTrunkLocal = Vector3D.Transform(fromPosition, fracture.PositionComp.WorldMatrixNormalizedInv);
                Vector3D closestPointOnTrunk;

                if (!FindClosestPointOnFracturedTree(positionInTrunkLocal, fracture, out closestPointOnTrunk))
                {
                    continue;
                }

                if (area == null || area.TestPoint(closestPointOnTrunk))
                {
                    double distanceSq = Vector3D.DistanceSquared(closestPointOnTrunk, fromPosition);

                    if (distanceSq < closestDistanceSq)
                    {
                        closestDistanceSq = distanceSq;
                        closestTarget     = fracture;
                        closestPoint      = closestPointOnTrunk;
                    }
                }
            }
            m_tmpFracturePieceList.Clear();

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

            result.EntityId = closestTarget.EntityId;
            result.Target   = closestPoint;
            return(true);
        }
コード例 #18
0
        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();
                }
            }
        }
コード例 #19
0
        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();
            }
        }
コード例 #20
0
        private static void FindFracturedTreesInternal(Vector3D fromPosition, MyPlaceArea area, BoundingSphereD sphere)
        {
            Debug.Assert(m_tmpFracturePieceList.Count == 0, "m_tmpFracturePieceList was not cleared after last use!");

            MyFracturedPiecesManager.Static.GetFracturesInSphere(ref sphere, ref m_tmpFracturePieceList);

            for (int i = m_tmpFracturePieceList.Count - 1; i >= 0; i--)
            {
                MyFracturedPiece fracture = m_tmpFracturePieceList[i];

                if (!MyTrees.IsEntityFracturedTree(fracture))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }

                if (IsFracturedTreeStump(fracture))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }

                Vector3D positionInTrunkLocal = Vector3D.Transform(fromPosition, fracture.PositionComp.WorldMatrixNormalizedInv);
                Vector3D closestPointOnTrunk;

                if (!FindClosestPointOnFracturedTree(positionInTrunkLocal, fracture, out closestPointOnTrunk))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }

                if (!area.TestPoint(closestPointOnTrunk))
                {
                    m_tmpFracturePieceList.RemoveAtFast(i);
                    continue;
                }
            }
        }