예제 #1
0
 public static void SetTargetCompoundBlock(ref MyBBMemoryTarget target, Vector3I blockPosition, long entityId, ushort compoundId)
 {
     if (target == null) target = new MyBBMemoryTarget();
     target.TargetType = MyAiTargetEnum.COMPOUND_BLOCK;
     target.EntityId = entityId;
     target.CompoundId = compoundId;
     target.Position = blockPosition;
 }
예제 #2
0
 public static void SetTargetVoxel(ref MyBBMemoryTarget target, Vector3I voxelPosition, long entityId)
 {
     if (target == null) target = new MyBBMemoryTarget();
     target.TargetType = MyAiTargetEnum.VOXEL;
     target.EntityId = entityId;
     target.TreeId = null;
     target.Position = new Vector3D(voxelPosition);
 }
예제 #3
0
        public static void SetTargetTree(ref MyBBMemoryTarget target, Vector3D treePosition, long entityId, int treeId)
        {
            if (target == null) target = new MyBBMemoryTarget();
            target.TargetType = MyAiTargetEnum.ENVIRONMENT_ITEM;
            target.EntityId = entityId;
            target.TreeId = treeId;
            target.Position = treePosition;
		}
예제 #4
0
 public static void SetTargetPosition(ref MyBBMemoryTarget target, Vector3D position)
 {
     if (target == null) target = new MyBBMemoryTarget();
     target.TargetType = MyAiTargetEnum.POSITION;
     target.EntityId = null;
     target.TreeId = null;
     target.Position = position;
 }
예제 #5
0
 public static void SetTargetCube(ref MyBBMemoryTarget target, Vector3I blockPosition, long gridEntityId)
 {
     if (target == null) target = new MyBBMemoryTarget();
     target.TargetType = MyAiTargetEnum.CUBE;
     target.EntityId = gridEntityId;
     target.TreeId = null;
     target.Position = new Vector3D(blockPosition);
 }
예제 #6
0
 public static void SetTargetEntity(ref MyBBMemoryTarget target, MyAiTargetEnum targetType, long entityId, Vector3D? position = null)
 {
     if (target == null) target = new MyBBMemoryTarget();
     target.TargetType = targetType;
     target.EntityId = entityId;
     target.TreeId = null;
     target.Position = position;
 }
예제 #7
0
 public static void UnsetTarget(ref MyBBMemoryTarget target)
 {
     if (target == null) target = new MyBBMemoryTarget();
     target.TargetType = MyAiTargetEnum.NO_TARGET;
 }
예제 #8
0
        public virtual bool SetTargetFromMemory(MyBBMemoryTarget memoryTarget)
        {
            if (memoryTarget.TargetType == MyAiTargetEnum.POSITION)
            {
                Debug.Assert(memoryTarget.Position.HasValue, "Position was not set correctly in memory.");
                if (!memoryTarget.Position.HasValue) return false;

                SetTargetPosition(memoryTarget.Position.Value);
                return true;
            }
            else if (memoryTarget.TargetType == MyAiTargetEnum.ENVIRONMENT_ITEM)
            {
                Debug.Assert(memoryTarget.TreeId.HasValue, "Tree id was not set correctly in memory.");
                if (!memoryTarget.TreeId.HasValue) return false;

                var tree = new MyEnvironmentItems.ItemInfo();
                tree.LocalId = memoryTarget.TreeId.Value;
                tree.Transform.Position = memoryTarget.Position.Value;
                SetTargetTree(ref tree, memoryTarget.EntityId.Value);

                return true;
            }
            else if (memoryTarget.TargetType != MyAiTargetEnum.NO_TARGET)
            {
                Debug.Assert(memoryTarget.EntityId.HasValue, "Entity id was not set correctly in memory.");
                if (!memoryTarget.EntityId.HasValue) return false;

                MyEntity entity = null;
                if (MyEntities.TryGetEntityById(memoryTarget.EntityId.Value, out entity))
                {
                    if (memoryTarget.TargetType == MyAiTargetEnum.CUBE
                        || memoryTarget.TargetType == MyAiTargetEnum.COMPOUND_BLOCK)
                    {
                        var cubeGrid = entity as MyCubeGrid;
                        var cubeBlock = cubeGrid.GetCubeBlock(memoryTarget.BlockPosition);
                        Debug.Assert(cubeBlock != null, "Invalid position for a block");

                        if (cubeBlock != null)
                        {
                            if (memoryTarget.TargetType == MyAiTargetEnum.COMPOUND_BLOCK)
                            {
                                var realBlock = (cubeBlock.FatBlock as MyCompoundCubeBlock).GetBlock(memoryTarget.CompoundId.Value);
                                Debug.Assert(realBlock != null, "Block does not exist in the compound block");
                                if (realBlock == null)
                                    return false;
                                cubeBlock = realBlock;
                                m_compoundId = memoryTarget.CompoundId;
                            }
                            SetTargetBlock(cubeBlock);
                        }
                        else
                            return false;
                    }
                    else if (memoryTarget.TargetType == MyAiTargetEnum.ENTITY)
                    {
                        if (memoryTarget.Position.HasValue && entity is MyFracturedPiece)
                            SetMTargetPosition(memoryTarget.Position.Value);
                        else
                            SetMTargetPosition(entity.PositionComp.GetPosition());

                        SetTargetEntity(entity);
                        m_targetEntity = entity;
                    }
                    else if (memoryTarget.TargetType == MyAiTargetEnum.VOXEL)
                    {
                        var voxelMap = entity as MyVoxelMap;
                        Debug.Assert(memoryTarget.Position.HasValue, "Tree id was not set correctly in memory.");
                        if (!memoryTarget.Position.HasValue) return false;

                        Debug.Assert(voxelMap != null, "Voxel map hasn't been set.");
                        if (voxelMap == null) return false;

                        SetTargetVoxel(memoryTarget.Position.Value, voxelMap);
                        m_targetEntity = voxelMap;
                    }
                    else
                    {
                        SetTargetEntity(entity);
                        //SetMTargetPosition(Vector3D.Zero);
                    }
                    return true;
                }
                else
                {
                    UnsetTarget();
                    return false;
                }
            }
            else if (memoryTarget.TargetType == MyAiTargetEnum.NO_TARGET)
            {
                UnsetTarget();
                return true;
            }
            else
            {
                Debug.Assert(false, "Unrecognized type of target!");
                UnsetTarget();
                return false;
            }
        }
예제 #9
0
 public Vector3D? GetMemoryTargetPosition(MyBBMemoryTarget targetMemory)
 {
     if (targetMemory == null) return null;
     switch (targetMemory.TargetType)
     {
         case MyAiTargetEnum.CHARACTER:
         case MyAiTargetEnum.ENTITY:
         case MyAiTargetEnum.GRID:
             {
                 MyCharacter target = null;
                 if (MyEntities.TryGetEntityById(targetMemory.EntityId.Value, out target))
                     return target.PositionComp.GetPosition();
                 else
                     return null;
             }
         case MyAiTargetEnum.CUBE:
         case MyAiTargetEnum.COMPOUND_BLOCK:
             {
                 MyCubeGrid target = null;
                 if (MyEntities.TryGetEntityById(targetMemory.EntityId.Value, out target) &&
                     target.CubeExists(targetMemory.BlockPosition))
                 {
                     return target.GridIntegerToWorld(targetMemory.BlockPosition);
                 }
                 else
                     return null;
             }
         case MyAiTargetEnum.POSITION:
         case MyAiTargetEnum.VOXEL:
         case MyAiTargetEnum.ENVIRONMENT_ITEM:
             // CH: This seems wrong, but GetGotoPosition does the same weird thing.
             // I should look at this later and fix it if possible...
             return m_targetPosition;
         case MyAiTargetEnum.NO_TARGET:
         default:
             return null;
     }
 }
예제 #10
0
 public virtual bool IsMemoryTargetValid(MyBBMemoryTarget targetMemory)
 {
     if (targetMemory == null) return false;
     switch (targetMemory.TargetType)
     {
         case MyAiTargetEnum.CHARACTER:
             {
                 MyCharacter target = null;
                 if (MyEntities.TryGetEntityById(targetMemory.EntityId.Value, out target))
                     return IsEntityReachable(target);
                 else
                     return false;
             }
         case MyAiTargetEnum.CUBE:
         case MyAiTargetEnum.COMPOUND_BLOCK:
             {
                 MyCubeGrid target = null;
                 if (MyEntities.TryGetEntityById(targetMemory.EntityId.Value, out target))
                 {
                     MySlimBlock block = target.GetCubeBlock(targetMemory.BlockPosition);
                     if (block == null) return false;
                     if (block.FatBlock != null)
                         return IsEntityReachable(block.FatBlock);
                     else
                         return IsEntityReachable(target);
                 }
                 else
                     return false;
             }
         case MyAiTargetEnum.VOXEL:
         case MyAiTargetEnum.ENVIRONMENT_ITEM:
             return true;
         case MyAiTargetEnum.ENTITY:
         case MyAiTargetEnum.GRID:
             MyEntity entity = null;
             if (MyEntities.TryGetEntityById(targetMemory.EntityId.Value, out entity))
                 return IsEntityReachable(entity);
             else
                 return false;
         default:
             return false;
     }
 }
예제 #11
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;
        }
        protected MyBehaviorTreeState SetTarget(bool aim, ref MyBBMemoryTarget inTarget)
        {
            if (inTarget != null)
            {
                if (AiTargetBase.SetTargetFromMemory(inTarget))
                {
                    if (aim)
                    {
                        AiTargetBase.AimAtTarget();
                    }
                    return MyBehaviorTreeState.SUCCESS;
                }
                else
                    return MyBehaviorTreeState.FAILURE;

            }
            return MyBehaviorTreeState.FAILURE;
        }