Example #1
0
 protected MyBehaviorTreeState IsIntLargerThan([BTIn] ref MyBBMemoryInt variable, [BTParam] int value)
 {
     if (variable == null)
     {
         variable = new MyBBMemoryInt();
     }
     return((variable.IntValue > value) ? MyBehaviorTreeState.SUCCESS : MyBehaviorTreeState.FAILURE);
 }
Example #2
0
 protected MyBehaviorTreeState SetInt([BTOut] ref MyBBMemoryInt variable, [BTParam] int value)
 {
     if (variable == null)
     {
         variable = new MyBBMemoryInt();
     }
     variable.IntValue = value;
     return(MyBehaviorTreeState.SUCCESS);
 }
Example #3
0
 protected MyBehaviorTreeState Increment([BTInOut] ref MyBBMemoryInt variable)
 {
     if (variable == null)
     {
         variable = new MyBBMemoryInt();
     }
     variable.IntValue++;
     return(MyBehaviorTreeState.SUCCESS);
 }
Example #4
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);
        }
Example #5
0
        protected MyBehaviorTreeState GetTargetWithPriority([BTParam] float radius, [BTInOut] ref MyBBMemoryTarget outTarget, [BTInOut] ref MyBBMemoryInt priority)
        {
            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)
            {
                bestPriority = int.MaxValue;
            }

            MyBehaviorTreeState retval = IsTargetValid(ref outTarget);

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

            if (!targetPosition.HasValue || Vector3D.Distance(targetPosition.Value, Bot.AgentEntity.PositionComp.GetPosition()) > 400.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 spiders going for the same player
            foreach (var entity in entityList)
            {
                if (entity == Bot.AgentEntity)
                {
                    continue;
                }
                if (!SpiderTarget.IsEntityReachable(entity))
                {
                    continue;
                }

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

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

                    //if character fly up exclude him from targets
                    var result = Sandbox.Engine.Physics.MyPhysics.CastRay(character.WorldMatrix.Translation - 3 * character.WorldMatrix.Up, character.WorldMatrix.Translation + 3 * character.WorldMatrix.Up, Sandbox.Engine.Physics.MyPhysics.CollisionLayers.DefaultCollisionLayer);
                    if (result == null || (result as VRage.Game.ModAPI.IHitInfo).HitEntity == character)
                    {
                        continue;
                    }

                    entityPriority = 1;

                    if (entityPriority < bestPriority)
                    {
                        retval       = MyBehaviorTreeState.SUCCESS;
                        bestPriority = entityPriority;
                        MyBBMemoryTarget.SetTargetEntity(ref outTarget, MyAiTargetEnum.CHARACTER, character.EntityId);
                        continue;
                    }
                }
                else if (grid != null && bestPriority > 3)
                {
                    Vector3D    spiderPosInGrid = 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 spiderPosInGrid);
                        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);
                    }
                }
            }
            entityList.Clear();

            /*var players = Sync.Players.GetOnlinePlayers();
             * MyCharacter closestCharacter = null;
             * double closestDistanceSq = float.MaxValue;
             * foreach (var player in players)
             * {
             *  if (player.Id.SerialId != 0)
             *  {
             *      var bot = MyAIComponent.Static.Bots.TryGetBot<MyHumanoidBot>(player.Id.SerialId);
             *      if (bot == null || bot.BotDefinition.BehaviorType == "Barbarian")
             *          continue;
             *  }
             *
             *  if (!(player.Character is MyCharacter) || !AiTargetBase.IsEntityReachable(player.Character))
             *  {
             *      continue;
             *  }
             *
             *  if (player.Character.IsDead)
             *      continue;
             *
             *  var distanceSq = Vector3D.DistanceSquared(player.Character.PositionComp.GetPosition(), myPosition);
             *  if (distanceSq < radius * radius && distanceSq < closestDistanceSq)
             *  {
             *      closestCharacter = player.Character;
             *      closestDistanceSq = distanceSq;
             *  }
             * }*/

            //return closestCharacter;

            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());
            //}

            return(retval);
        }