protected MyBehaviorTreeState IsHumanInArea([BTParam] int standingRadius, [BTParam] int crouchingRadius, [BTOut] ref MyBBMemoryTarget outTarget)
        {
            MyCharacter foundCharacter = null;

            if (TryFindValidHumanInArea(standingRadius, crouchingRadius, out foundCharacter))
            {
                MyBBMemoryTarget.SetTargetEntity(ref outTarget, MyAiTargetEnum.CHARACTER, foundCharacter.EntityId);
                return(MyBehaviorTreeState.SUCCESS);
            }
            else
            {
                return(MyBehaviorTreeState.FAILURE);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
 protected MyBehaviorTreeState SetTarget(bool aim, ref MyBBMemoryTarget inTarget)
 {
     if (inTarget == null)
     {
         return(MyBehaviorTreeState.FAILURE);
     }
     if (!this.AiTargetBase.SetTargetFromMemory(inTarget))
     {
         return(MyBehaviorTreeState.FAILURE);
     }
     if (aim)
     {
         this.AiTargetBase.AimAtTarget();
     }
     return(MyBehaviorTreeState.SUCCESS);
 }
Esempio n. 4
0
 protected MyBehaviorTreeState HasTargetArea([BTIn] ref MyBBMemoryTarget inTarget)
 {
     if (inTarget != null && inTarget.EntityId.HasValue)
     {
         MyEntity entity = null;
         if (MyEntities.TryGetEntityById(inTarget.EntityId.Value, out entity))
         {
             MyPlaceArea area = null;
             if (entity.Components.TryGet <MyPlaceArea>(out area))
             {
                 return(MyBehaviorTreeState.SUCCESS);
             }
         }
     }
     return(MyBehaviorTreeState.FAILURE);
 }
Esempio n. 5
0
        public MyBehaviorTreeState SetTarget(ref MyBBMemoryTarget inTarget)
        {
            if (inTarget != null)
            {
                if (AiTargetBase.SetTargetFromMemory(inTarget))
                {
                    return(MyBehaviorTreeState.SUCCESS);
                }
                else
                {
                    return(MyBehaviorTreeState.FAILURE);
                }
            }

            return(MyBehaviorTreeState.FAILURE);
        }
Esempio n. 6
0
 protected MyBehaviorTreeState HasTargetArea([BTIn] ref MyBBMemoryTarget inTarget)
 {
     if ((inTarget != null) && (inTarget.EntityId != null))
     {
         VRage.Game.Entity.MyEntity entity = null;
         if (Sandbox.Game.Entities.MyEntities.TryGetEntityById(inTarget.EntityId.Value, out entity, false))
         {
             MyPlaceArea component = null;
             if (entity.Components.TryGet <MyPlaceArea>(out component))
             {
                 return(MyBehaviorTreeState.SUCCESS);
             }
         }
     }
     return(MyBehaviorTreeState.FAILURE);
 }
Esempio n. 7
0
 protected MyBehaviorTreeState SetTarget([BTIn] ref MyBBMemoryTarget inTarget)
 {
     if (inTarget != null)
     {
         if (AiTargetBase.SetTargetFromMemory(inTarget))
         {
             return(MyBehaviorTreeState.SUCCESS);
         }
         else
         {
             return(MyBehaviorTreeState.FAILURE);
         }
     }
     else
     {
         AiTargetBase.UnsetTarget();
         return(MyBehaviorTreeState.SUCCESS);
     }
 }
Esempio n. 8
0
        protected virtual MyBehaviorTreeState IsTargetValid([BTIn] ref MyBBMemoryTarget inTarget)
        {
            if (inTarget != null && inTarget.EntityId.HasValue)
            {
                if (inTarget.EntityId.HasValue)
                {
                    MyEntity entity = null;
                    if (MyEntities.TryGetEntityById(inTarget.EntityId.Value, out entity))
                    {
                        return(MyBehaviorTreeState.SUCCESS);
                    }

                    return(MyBehaviorTreeState.FAILURE);
                }

                if (inTarget.TargetType != MyAiTargetEnum.NO_TARGET)
                {
                    return(MyBehaviorTreeState.SUCCESS);
                }
            }
            return(MyBehaviorTreeState.FAILURE);
        }
Esempio n. 9
0
        public virtual bool IsMemoryTargetValid(MyBBMemoryTarget targetMemory)
        {
            if (targetMemory != null)
            {
                switch (targetMemory.TargetType)
                {
                case MyAiTargetEnum.GRID:
                case MyAiTargetEnum.ENTITY:
                {
                    VRage.Game.Entity.MyEntity entity = null;
                    return(Sandbox.Game.Entities.MyEntities.TryGetEntityById(targetMemory.EntityId.Value, out entity, false) && this.IsEntityReachable(entity));
                }

                case MyAiTargetEnum.CUBE:
                case MyAiTargetEnum.COMPOUND_BLOCK:
                {
                    MyCubeGrid grid = null;
                    if (!Sandbox.Game.Entities.MyEntities.TryGetEntityById <MyCubeGrid>(targetMemory.EntityId.Value, out grid, false))
                    {
                        return(false);
                    }
                    MySlimBlock cubeBlock = grid.GetCubeBlock(targetMemory.BlockPosition);
                    return((cubeBlock != null) ? ((cubeBlock.FatBlock == null) ? this.IsEntityReachable(grid) : this.IsEntityReachable(cubeBlock.FatBlock)) : false);
                }

                case MyAiTargetEnum.CHARACTER:
                {
                    MyCharacter character = null;
                    return(Sandbox.Game.Entities.MyEntities.TryGetEntityById <MyCharacter>(targetMemory.EntityId.Value, out character, false) && this.IsEntityReachable(character));
                }

                case MyAiTargetEnum.ENVIRONMENT_ITEM:
                case MyAiTargetEnum.VOXEL:
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 10
0
        public Vector3D?GetMemoryTargetPosition(MyBBMemoryTarget targetMemory)
        {
            if (targetMemory != null)
            {
                switch (targetMemory.TargetType)
                {
                case MyAiTargetEnum.GRID:
                case MyAiTargetEnum.CHARACTER:
                case MyAiTargetEnum.ENTITY:
                {
                    MyCharacter entity = null;
                    if (Sandbox.Game.Entities.MyEntities.TryGetEntityById <MyCharacter>(targetMemory.EntityId.Value, out entity, false))
                    {
                        return(new Vector3D?(entity.PositionComp.GetPosition()));
                    }
                    return(null);
                }

                case MyAiTargetEnum.CUBE:
                case MyAiTargetEnum.COMPOUND_BLOCK:
                {
                    MyCubeGrid entity = null;
                    if (Sandbox.Game.Entities.MyEntities.TryGetEntityById <MyCubeGrid>(targetMemory.EntityId.Value, out entity, false) && entity.CubeExists(targetMemory.BlockPosition))
                    {
                        return(new Vector3D?(entity.GridIntegerToWorld(targetMemory.BlockPosition)));
                    }
                    return(null);
                }

                case MyAiTargetEnum.POSITION:
                case MyAiTargetEnum.ENVIRONMENT_ITEM:
                case MyAiTargetEnum.VOXEL:
                    return(new Vector3D?(this.m_targetPosition));
                }
            }
            return(null);
        }
Esempio n. 11
0
        protected MyBehaviorTreeState FindClosestBlock([BTOut] ref MyBBMemoryTarget outBlock)
        {
            if (!AiTargetBase.IsTargetGridOrBlock(AiTargetBase.TargetType))
            {
                outBlock = null;
                return(MyBehaviorTreeState.FAILURE);
            }

            var targetGrid = AiTargetBase.TargetGrid;

            Vector3 myPositionInGrid = Vector3D.Transform(Bot.BotEntity.PositionComp.GetPosition(), targetGrid.PositionComp.WorldMatrixNormalizedInv);

            float       closest      = float.MaxValue;
            MySlimBlock closestBlock = null;

            foreach (var block in targetGrid.GetBlocks())
            {
                Vector3 blockPos = block.Position;
                blockPos *= targetGrid.GridSize;

                float distSq = Vector3.DistanceSquared(blockPos, myPositionInGrid);
                if (distSq < closest)
                {
                    closestBlock = block;
                    closest      = distSq;
                }
            }

            if (closestBlock == null)
            {
                return(MyBehaviorTreeState.FAILURE);
            }

            MyBBMemoryTarget.SetTargetCube(ref outBlock, closestBlock.Position, closestBlock.CubeGrid.EntityId);
            return(MyBehaviorTreeState.SUCCESS);
        }
Esempio n. 12
0
 protected MyBehaviorTreeState SetAndAimTarget([BTIn] ref MyBBMemoryTarget inTarget)
 {
     return(SetTarget(true, ref inTarget));
 }
Esempio n. 13
0
 protected MyBehaviorTreeState FindClosestPlaceAreaInRadius([BTParam] float radius, [BTParam] string typeName, [BTOut] ref MyBBMemoryTarget outTarget)
 {
     if (MyItemsCollector.FindClosestPlaceAreaInSphere(new BoundingSphereD(Bot.AgentEntity.PositionComp.GetPosition(), radius), typeName, ref outTarget))
     {
         return(MyBehaviorTreeState.SUCCESS);
     }
     return(MyBehaviorTreeState.FAILURE);
 }
Esempio n. 14
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)
                        {
                            m_targetPosition = memoryTarget.Position.Value;
                        }
                        else
                        {
                            m_targetPosition = 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);
                    }
                    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);
            }
        }
 protected abstract MyBehaviorTreeState Condition_IsWoodInPlaceArea([BTIn] ref MyBBMemoryTarget inPlaceArea, [BTOut] ref MyBBMemoryTarget outTarget);
 protected abstract MyBehaviorTreeState Action_TargetForward([BTParam] float amount, [BTOut] ref MyBBMemoryTarget outFoundLocation);
Esempio n. 17
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 abstract MyBehaviorTreeState Condition_IsStoneInArea([BTParam] float radius, [BTOut] ref MyBBMemoryTarget outFoundLocation);
 protected MyBehaviorTreeState IsHumanNotInArea([BTParam] int standingRadius, [BTParam] int crouchingRadius, [BTOut] ref MyBBMemoryTarget outTarget)
 {
     return(InvertState(IsHumanInArea(standingRadius, crouchingRadius, ref outTarget)));
 }
 protected abstract MyBehaviorTreeState Condition_AreTreesInArea([BTParam] float radius, [BTOut] ref MyBBMemoryTarget outTarget);
 protected MyBehaviorTreeState FindWanderLocation([BTOut] ref MyBBMemoryTarget outTarget)
 {
     return(MyBehaviorTreeState.FAILURE);
 }
 protected abstract MyBehaviorTreeState Condition_IsStoneOreInRadius([BTParam] float radius, [BTOut] ref MyBBMemoryTarget outTarget);
Esempio n. 23
0
        public MyBehaviorTreeState TryReserveEntity(ref MyBBMemoryTarget inTarget, int timeMs)
        {
            MyBehaviorTreeState retStatus = MyBehaviorTreeState.FAILURE;

            if (Bot == null || Bot.Player == null)
            {
                return(MyBehaviorTreeState.FAILURE);
            }

            var logic = Bot.HumanoidLogic;

            if (inTarget != null && inTarget.EntityId.HasValue && inTarget.TargetType != MyAiTargetEnum.POSITION && inTarget.TargetType != MyAiTargetEnum.NO_TARGET)
            {
                switch (logic.EntityReservationStatus)
                {
                case Logic.MyEntityReservationStatus.NONE:
                    switch (inTarget.TargetType)
                    {
                    case MyAiTargetEnum.GRID:
                    case MyAiTargetEnum.CUBE:
                    case MyAiTargetEnum.CHARACTER:
                    case MyAiTargetEnum.ENTITY:
                        logic.EntityReservationStatus = Logic.MyEntityReservationStatus.WAITING;
                        logic.ReservationEntityData   = new MyAiTargetManager.ReservedEntityData()
                        {
                            Type       = MyReservedEntityType.ENTITY,
                            EntityId   = inTarget.EntityId.Value, ReservationTimer = timeMs,
                            ReserverId = new World.MyPlayer.PlayerId(Bot.Player.Id.SteamId, Bot.Player.Id.SerialId)
                        };
                        MyAiTargetManager.OnReservationResult += ReservationHandler;
                        MyAiTargetManager.Static.RequestEntityReservation(logic.ReservationEntityData.EntityId, logic.ReservationEntityData.ReservationTimer, Bot.Player.Id.SerialId);
                        break;

                    case MyAiTargetEnum.ENVIRONMENT_ITEM:
                        logic.EntityReservationStatus = Logic.MyEntityReservationStatus.WAITING;
                        logic.ReservationEntityData   = new MyAiTargetManager.ReservedEntityData()
                        {
                            Type             = MyReservedEntityType.ENVIRONMENT_ITEM,
                            EntityId         = inTarget.EntityId.Value,
                            LocalId          = inTarget.TreeId.Value,
                            ReservationTimer = timeMs,
                            ReserverId       = new World.MyPlayer.PlayerId(Bot.Player.Id.SteamId, Bot.Player.Id.SerialId)
                        };
                        MyAiTargetManager.OnReservationResult += ReservationHandler;
                        MyAiTargetManager.Static.RequestEnvironmentItemReservation(logic.ReservationEntityData.EntityId, logic.ReservationEntityData.LocalId,
                                                                                   logic.ReservationEntityData.ReservationTimer, Bot.Player.Id.SerialId);
                        break;

                    case MyAiTargetEnum.VOXEL:
                        logic.EntityReservationStatus = Logic.MyEntityReservationStatus.WAITING;
                        logic.ReservationEntityData   = new MyAiTargetManager.ReservedEntityData()
                        {
                            Type             = MyReservedEntityType.VOXEL,
                            EntityId         = inTarget.EntityId.Value,
                            GridPos          = inTarget.VoxelPosition,
                            ReservationTimer = timeMs,
                            ReserverId       = new World.MyPlayer.PlayerId(Bot.Player.Id.SteamId, Bot.Player.Id.SerialId)
                        };
                        MyAiTargetManager.OnReservationResult += ReservationHandler;
                        MyAiTargetManager.Static.RequestVoxelPositionReservation(logic.ReservationEntityData.EntityId, logic.ReservationEntityData.GridPos,
                                                                                 logic.ReservationEntityData.ReservationTimer, Bot.Player.Id.SerialId);
                        break;

                    default:
                        break;
                    }
                    m_reservationTimeOut          = Stopwatch.GetTimestamp() + Stopwatch.Frequency * RESERVATION_WAIT_TIMEOUT_SECONDS;
                    logic.EntityReservationStatus = Logic.MyEntityReservationStatus.WAITING;
                    retStatus = MyBehaviorTreeState.RUNNING;
                    break;

                case Logic.MyEntityReservationStatus.SUCCESS:
                    retStatus = MyBehaviorTreeState.SUCCESS;
                    break;

                case Logic.MyEntityReservationStatus.FAILURE:
                    retStatus = MyBehaviorTreeState.FAILURE;
                    break;

                case Logic.MyEntityReservationStatus.WAITING:
                    if (m_reservationTimeOut < Stopwatch.GetTimestamp())
                    {
                        retStatus = MyBehaviorTreeState.FAILURE;
                    }
                    else
                    {
                        retStatus = MyBehaviorTreeState.RUNNING;
                    }
                    break;
                }
            }
            return(retStatus);
        }
 protected abstract MyBehaviorTreeState Action_SetAndAimTarget([BTIn] ref MyBBMemoryTarget inTarget);
Esempio n. 25
0
        protected MyBehaviorTreeState GetTargetWithPriority([BTParam] float radius, [BTInOut] ref MyBBMemoryTarget outTarget, [BTInOut] ref MyBBMemoryInt priority)
        {
            if (CyberhoundLogic.SelfDestructionActivated) // self destruction activated, do not change target.
            {
                return(MyBehaviorTreeState.SUCCESS);
            }

            var             myPosition = Bot.Navigation.PositionAndOrientation.Translation;
            BoundingSphereD bb         = new BoundingSphereD(myPosition, radius);

            if (priority == null)
            {
                priority = new MyBBMemoryInt();
            }
            int bestPriority = priority.IntValue;

            if (bestPriority <= 0 || Bot.Navigation.Stuck)
            {
                bestPriority = int.MaxValue;
            }

            MyBehaviorTreeState retval = IsTargetValid(ref outTarget);

            if (retval == MyBehaviorTreeState.FAILURE)
            {
                bestPriority = 7;
                MyBBMemoryTarget.UnsetTarget(ref outTarget);
            }
            Vector3D?targetPosition = CyberhoundTarget.GetMemoryTargetPosition(outTarget);

            if (!targetPosition.HasValue ||
                Vector3D.DistanceSquared(targetPosition.Value, Bot.AgentEntity.PositionComp.GetPosition()) > 400.0f * 400.0f)
            {
                bestPriority = 7;
                MyBBMemoryTarget.UnsetTarget(ref outTarget);
            }
            if (targetPosition.HasValue)
            {
                Vector3D targetPositionValue = targetPosition.Value;
                var      planet = MyGravityProviderSystem.GetNearestPlanet(targetPositionValue);
                if (planet != null)
                {
                    Vector3D targetPositionProjected = planet.GetClosestSurfacePointGlobal(ref targetPositionValue);
                    if (Vector3D.DistanceSquared(targetPositionProjected, targetPositionValue) > 1.5f * 1.5f &&
                        Vector3D.DistanceSquared(targetPositionProjected, Bot.AgentEntity.PositionComp.GetPosition()) < 5.0f * 5.0f)
                    {
                        bestPriority = 7;
                        MyBBMemoryTarget.UnsetTarget(ref outTarget);
                    }
                }
            }

            var myFaction = MySession.Static.Factions.GetPlayerFaction(Bot.AgentEntity.ControllerInfo.ControllingIdentityId);


            // Priorities are as follows:
            // 1st characters, 3rd turrets, 4th weapons, 5th non-armor blocks, 6th armor blocks
            var entityList = MyEntities.GetTopMostEntitiesInSphere(ref bb);

            entityList.ShuffleList(); // Prevent all Cyberhounds going for the same player
            foreach (var entity in entityList)
            {
                if (entity == Bot.AgentEntity ||
                    entity is MyVoxelBase ||
                    !CyberhoundTarget.IsEntityReachable(entity))
                {
                    continue;
                }
                // exclude entities above ground
                Vector3D entityPos = entity.PositionComp.GetPosition();
                var      planet    = MyGravityProviderSystem.GetNearestPlanet(entityPos);
                if (planet != null)
                {
                    Vector3D entityPosProjected = planet.GetClosestSurfacePointGlobal(ref entityPos);
                    if (Vector3D.DistanceSquared(entityPosProjected, entityPos) > 1.0f)
                    {
                        continue;
                    }
                }

                int entityPriority = 6;
                var character      = entity as MyCharacter;
                var grid           = entity as MyCubeGrid;

                if (character != null)
                {
                    var faction = MySession.Static.Factions.GetPlayerFaction(character.ControllerInfo.ControllingIdentityId);
                    if (myFaction != null && faction == myFaction)
                    {
                        continue;
                    }
                    if (character.IsDead)
                    {
                        continue;
                    }

                    entityPriority = 1;

                    if (entityPriority < bestPriority)
                    {
                        retval       = MyBehaviorTreeState.SUCCESS;
                        bestPriority = entityPriority;
                        MyBBMemoryTarget.SetTargetEntity(ref outTarget, MyAiTargetEnum.CHARACTER, character.EntityId);
                        lastTargetedEntityPosition = character.PositionComp.GetPosition();
                        continue;
                    }
                }
                else if (grid != null && bestPriority > 3)
                {
                    Vector3D    CyberhoundPosInGrid = grid.WorldToGridScaledLocal(myPosition);
                    double      closestDist         = double.MaxValue;
                    MySlimBlock closestBlock        = null;
                    foreach (var block in grid.CubeBlocks)
                    {
                        Vector3D blockLocalPos = new Vector3D(block.Min + block.Max);
                        blockLocalPos = blockLocalPos * 0.5;

                        double dist = Vector3D.RectangularDistance(ref blockLocalPos, ref CyberhoundPosInGrid);
                        if (dist < closestDist)
                        {
                            closestBlock = block;
                            closestDist  = dist;
                        }
                    }

                    if (closestBlock != null)
                    {
                        retval       = MyBehaviorTreeState.SUCCESS;
                        bestPriority = 3;
                        MyBBMemoryTarget.SetTargetCube(ref outTarget, (closestBlock.Min + closestBlock.Max) / 2, grid.EntityId);
                        BoundingBoxD bbBlock;
                        closestBlock.GetWorldBoundingBox(out bbBlock);
                        lastTargetedEntityPosition = bbBlock.Center;
                    }
                }
            }
            entityList.Clear();
            priority.IntValue = bestPriority;

            // CH: TODO: This is temporary. Remove it!
            if (outTarget.TargetType == MyAiTargetEnum.CUBE)
            {
                MyEntity outGrid;
                MyEntities.TryGetEntityById(outTarget.EntityId.Value, out outGrid);
                Debug.Assert(outGrid != null);
                var         grid  = outGrid as MyCubeGrid;
                MySlimBlock block = grid.GetCubeBlock(outTarget.BlockPosition);
                Debug.Assert(block != null);

                //MyTrace.Send(TraceWindow.Ai, "TARGETTING CUBE: " + grid.ToString() + " " + block.ToString());
            }

            if (outTarget.TargetType == MyAiTargetEnum.NO_TARGET)
            {
                retval = MyBehaviorTreeState.FAILURE;
            }
            return(retval);
        }
        protected MyBehaviorTreeState FindRandomSafeLocation([BTIn] ref MyBBMemoryTarget inTargetEnemy, [BTOut] ref MyBBMemoryTarget outTargetLocation)
        {
            if (inTargetEnemy == null || !inTargetEnemy.EntityId.HasValue)
            {
                return(MyBehaviorTreeState.FAILURE);
            }
            MyEntity targetEntity = null;

            if (MyEntities.TryGetEntityById(inTargetEnemy.EntityId.Value, out targetEntity))
            {
                Vector3D botPosition    = m_bot.AgentEntity.PositionComp.GetPosition();
                Vector3D directionToBot = botPosition - targetEntity.PositionComp.GetPosition();
                directionToBot.Normalize();

                Vector3D safeLocation = default(Vector3D);
                if (!AiTargetBase.GetRandomDirectedPosition(botPosition, directionToBot, out safeLocation))
                {
                    safeLocation = botPosition + directionToBot * 30;
                }

                MyBBMemoryTarget.SetTargetPosition(ref outTargetLocation, safeLocation);
                return(MyBehaviorTreeState.SUCCESS);
            }
            else
            {
                return(MyBehaviorTreeState.FAILURE);
            }
        }
Esempio n. 27
0
        protected MyBehaviorTreeState TryReserveEntity([BTIn] ref MyBBMemoryTarget inTarget, [BTParam] int timeMs)
        {
            if ((this.Bot != null) && (this.Bot.Player != null))
            {
                MyHumanoidBotLogic humanoidLogic = this.Bot.HumanoidLogic;
                if (((inTarget != null) && ((inTarget.EntityId != null) && (inTarget.TargetType != MyAiTargetEnum.POSITION))) && (inTarget.TargetType != MyAiTargetEnum.NO_TARGET))
                {
                    switch (humanoidLogic.ReservationStatus)
                    {
                    case MyReservationStatus.NONE:
                        MyAiTargetManager.ReservedEntityData data;
                        switch (inTarget.TargetType)
                        {
                        case MyAiTargetEnum.GRID:
                        case MyAiTargetEnum.CUBE:
                        case MyAiTargetEnum.CHARACTER:
                        case MyAiTargetEnum.ENTITY:
                            humanoidLogic.ReservationStatus = MyReservationStatus.WAITING;
                            data = new MyAiTargetManager.ReservedEntityData {
                                Type             = MyReservedEntityType.ENTITY,
                                EntityId         = inTarget.EntityId.Value,
                                ReservationTimer = timeMs,
                                ReserverId       = new MyPlayer.PlayerId(this.Bot.Player.Id.SteamId, this.Bot.Player.Id.SerialId)
                            };
                            humanoidLogic.ReservationEntityData    = data;
                            MyAiTargetManager.OnReservationResult += new Sandbox.Game.AI.MyAiTargetManager.ReservationHandler(this.ReservationHandler);
                            MyAiTargetManager.Static.RequestEntityReservation(humanoidLogic.ReservationEntityData.EntityId, humanoidLogic.ReservationEntityData.ReservationTimer, this.Bot.Player.Id.SerialId);
                            break;

                        case MyAiTargetEnum.ENVIRONMENT_ITEM:
                            humanoidLogic.ReservationStatus = MyReservationStatus.WAITING;
                            data = new MyAiTargetManager.ReservedEntityData {
                                Type             = MyReservedEntityType.ENVIRONMENT_ITEM,
                                EntityId         = inTarget.EntityId.Value,
                                LocalId          = inTarget.TreeId.Value,
                                ReservationTimer = timeMs,
                                ReserverId       = new MyPlayer.PlayerId(this.Bot.Player.Id.SteamId, this.Bot.Player.Id.SerialId)
                            };
                            humanoidLogic.ReservationEntityData    = data;
                            MyAiTargetManager.OnReservationResult += new Sandbox.Game.AI.MyAiTargetManager.ReservationHandler(this.ReservationHandler);
                            MyAiTargetManager.Static.RequestEnvironmentItemReservation(humanoidLogic.ReservationEntityData.EntityId, humanoidLogic.ReservationEntityData.LocalId, humanoidLogic.ReservationEntityData.ReservationTimer, this.Bot.Player.Id.SerialId);
                            break;

                        case MyAiTargetEnum.VOXEL:
                            humanoidLogic.ReservationStatus = MyReservationStatus.WAITING;
                            data = new MyAiTargetManager.ReservedEntityData {
                                Type             = MyReservedEntityType.VOXEL,
                                EntityId         = inTarget.EntityId.Value,
                                GridPos          = inTarget.VoxelPosition,
                                ReservationTimer = timeMs,
                                ReserverId       = new MyPlayer.PlayerId(this.Bot.Player.Id.SteamId, this.Bot.Player.Id.SerialId)
                            };
                            humanoidLogic.ReservationEntityData    = data;
                            MyAiTargetManager.OnReservationResult += new Sandbox.Game.AI.MyAiTargetManager.ReservationHandler(this.ReservationHandler);
                            MyAiTargetManager.Static.RequestVoxelPositionReservation(humanoidLogic.ReservationEntityData.EntityId, humanoidLogic.ReservationEntityData.GridPos, humanoidLogic.ReservationEntityData.ReservationTimer, this.Bot.Player.Id.SerialId);
                            break;

                        default:
                            humanoidLogic.ReservationStatus = MyReservationStatus.FAILURE;
                            break;
                        }
                        this.m_reservationTimeOut = MySandboxGame.Static.TotalTime + MyTimeSpan.FromSeconds(3.0);
                        break;

                    case MyReservationStatus.WAITING:
                        if (this.m_reservationTimeOut < MySandboxGame.Static.TotalTime)
                        {
                            humanoidLogic.ReservationStatus = MyReservationStatus.FAILURE;
                        }
                        break;

                    default:
                        break;
                    }
                }
                switch (humanoidLogic.ReservationStatus)
                {
                case MyReservationStatus.WAITING:
                    return(MyBehaviorTreeState.RUNNING);

                case MyReservationStatus.SUCCESS:
                    return(MyBehaviorTreeState.SUCCESS);
                }
            }
            return(MyBehaviorTreeState.FAILURE);
        }
 protected abstract MyBehaviorTreeState Action_FindWanderLocation([BTOut] ref MyBBMemoryTarget outLocation);
Esempio n. 29
0
 public virtual bool SetTargetFromMemory(MyBBMemoryTarget memoryTarget)
 {
     if (memoryTarget.TargetType == MyAiTargetEnum.POSITION)
     {
         if (memoryTarget.Position == null)
         {
             return(false);
         }
         this.SetTargetPosition(memoryTarget.Position.Value);
         return(true);
     }
     if (memoryTarget.TargetType == MyAiTargetEnum.ENVIRONMENT_ITEM)
     {
         if (memoryTarget.TreeId == null)
         {
             return(false);
         }
         MyEnvironmentItems.ItemInfo targetTree = new MyEnvironmentItems.ItemInfo {
             LocalId   = memoryTarget.TreeId.Value,
             Transform = { Position = memoryTarget.Position.Value }
         };
         this.SetTargetTree(ref targetTree, memoryTarget.EntityId.Value);
         return(true);
     }
     if (memoryTarget.TargetType == MyAiTargetEnum.NO_TARGET)
     {
         if (memoryTarget.TargetType == MyAiTargetEnum.NO_TARGET)
         {
             this.UnsetTarget();
             return(true);
         }
         this.UnsetTarget();
         return(false);
     }
     if (memoryTarget.EntityId == null)
     {
         return(false);
     }
     VRage.Game.Entity.MyEntity entity = null;
     if (!Sandbox.Game.Entities.MyEntities.TryGetEntityById(memoryTarget.EntityId.Value, out entity, false))
     {
         this.UnsetTarget();
         return(false);
     }
     if ((memoryTarget.TargetType == MyAiTargetEnum.CUBE) || (memoryTarget.TargetType == MyAiTargetEnum.COMPOUND_BLOCK))
     {
         MySlimBlock cubeBlock = (entity as MyCubeGrid).GetCubeBlock(memoryTarget.BlockPosition);
         if (cubeBlock == null)
         {
             return(false);
         }
         if (memoryTarget.TargetType == MyAiTargetEnum.COMPOUND_BLOCK)
         {
             MySlimBlock block = (cubeBlock.FatBlock as MyCompoundCubeBlock).GetBlock(memoryTarget.CompoundId.Value);
             if (block == null)
             {
                 return(false);
             }
             cubeBlock         = block;
             this.m_compoundId = memoryTarget.CompoundId;
         }
         ushort?compoundId = null;
         this.SetTargetBlock(cubeBlock, compoundId);
     }
     else if (memoryTarget.TargetType == MyAiTargetEnum.ENTITY)
     {
         if ((memoryTarget.Position == null) || !(entity is MyFracturedPiece))
         {
             this.SetMTargetPosition(entity.PositionComp.GetPosition());
         }
         else
         {
             this.SetMTargetPosition(memoryTarget.Position.Value);
         }
         this.SetTargetEntity(entity);
         this.m_targetEntity = entity;
     }
     else if (memoryTarget.TargetType != MyAiTargetEnum.VOXEL)
     {
         this.SetTargetEntity(entity);
     }
     else
     {
         MyVoxelMap voxelMap = entity as MyVoxelMap;
         if (memoryTarget.Position == null)
         {
             return(false);
         }
         if (voxelMap == null)
         {
             return(false);
         }
         this.SetTargetVoxel(memoryTarget.Position.Value, voxelMap);
         this.m_targetEntity = voxelMap;
     }
     return(true);
 }
Esempio n. 30
0
 public abstract bool SetTargetFromMemory(MyBBMemoryTarget inTarget);