Esempio n. 1
0
        private static bool CanShout(IMyObjectToDetect detectedObject)
        {
            MyEntity detectedEntity = detectedObject as MyEntity;

            if (detectedEntity != null)
            {
                MyFactionRelationEnum factionRelation = MyFactions.GetFactionsRelation(MySession.PlayerShip, detectedEntity);
                if (detectedEntity is MySmallShipBot && factionRelation != MyFactionRelationEnum.Friend)
                {
                    MySmallShipBot bot = detectedEntity as MySmallShipBot;
                    return(!bot.IsParked() && !bot.IsPilotDead());
                }
                if (detectedEntity is MyPrefabLargeWeapon && factionRelation == MyFactionRelationEnum.Enemy)
                {
                    MyPrefabLargeWeapon largeWeapon = detectedEntity as MyPrefabLargeWeapon;
                    return(largeWeapon.IsWorking());
                }
            }
            return(false);
        }
        public void DoWork()
        {
            //  Search for target to attack
            ClosestEnemy  = null;
            ClosestVisual = null;

            float distanceSqr              = m_seeDistance * m_seeDistance;
            float closestEnemyDistanceSqr  = float.PositiveInfinity;
            float closestVisualDistanceSqr = float.PositiveInfinity;

            using (var rbFounded = PoolList <MyRBElement> .Get())
            {
                try
                {
                    MyEntities.EntityCloseLock.AcquireShared();

                    MyDynamicAABBTree prunningStructure = MyPhysics.physicsSystem.GetRigidBodyModule().GetPruningStructure();

                    BoundingBox rbInputElementGetWorldSpaceAABB = new BoundingBox(
                        m_botWorldMatrix.Translation - new Vector3(m_seeDistance),
                        m_botWorldMatrix.Translation + new Vector3(m_seeDistance));
                    prunningStructure.OverlapAllBoundingBox(ref rbInputElementGetWorldSpaceAABB, rbFounded, (uint)MyElementFlag.EF_RB_ELEMENT);

                    //now try find spot
                    foreach (MyRBElement rb in rbFounded)
                    {
                        if (m_bot == null)
                        {
                            return;
                        }

                        var rigidBody = rb.GetRigidBody();
                        if (rigidBody == null)
                        {
                            continue;
                        }

                        MyEntity entity = ((MyPhysicsBody)rigidBody.m_UserData).Entity;
                        if (entity == m_bot || entity == null || entity.AIPriority == -1)
                        {
                            continue;
                        }


                        entity = entity.GetBaseEntity();    // Large weapons

                        // Ignore spoiled holograms
                        if (m_bot.IsSpoiledHologram(entity))
                        {
                            continue;
                        }

                        // Don't attack disabled weapons
                        MyPrefabLargeWeapon largeWeapon = entity as MyPrefabLargeWeapon;
                        MySmallShip         smallShip   = entity as MySmallShip;
                        MyPrefabLargeShip   largeShip   = entity as MyPrefabLargeShip;

                        if (largeWeapon != null && !largeWeapon.IsWorking())
                        {
                            continue;
                        }

                        // Test smallships and largeweapons
                        if (smallShip != null || largeWeapon != null || largeShip != null)
                        {
                            // Is enemy?
                            if (MyFactions.GetFactionsRelation(m_bot, entity) == MyFactionRelationEnum.Enemy && CanSeeTarget(m_bot, entity))
                            {
                                var entityDistanceSqr = Vector3.DistanceSquared(entity.GetPosition(), m_position);

                                if (entityDistanceSqr < distanceSqr &&
                                    (ClosestEnemy == null || entity.AIPriority >= ClosestEnemy.AIPriority) &&
                                    (entityDistanceSqr < closestEnemyDistanceSqr || entity.AIPriority > ClosestEnemy.AIPriority))
                                {
                                    MyLine line   = new MyLine(m_position, entity.GetPosition(), true);
                                    var    result = MyEntities.GetIntersectionWithLine(ref line, m_bot, entity, true, ignoreChilds: true);
                                    if (!result.HasValue)
                                    {
                                        // Visual Detection - ignore visualy detected targets if they are further than any normaly detected target
                                        if (IsVisualyDetected(smallShip))
                                        {
                                            if (entityDistanceSqr < closestVisualDistanceSqr)
                                            {
                                                ClosestVisual            = entity;
                                                closestVisualDistanceSqr = entityDistanceSqr;
                                            }
                                        }
                                        else
                                        {
                                            closestEnemyDistanceSqr = entityDistanceSqr;
                                            ClosestEnemy            = entity;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    MyEntities.EntityCloseLock.ReleaseShared();
                }
            }
        }
        internal override void Update(MySmallShipBot bot)
        {
            base.Update(bot);

            if (m_target != null && !m_target.IsDead())
            {
                MySmallShip         smallShipTarget   = m_target as MySmallShip;
                MyPrefabLargeWeapon largeWeaponTarget = m_target as MyPrefabLargeWeapon;

                if (largeWeaponTarget != null && !largeWeaponTarget.IsWorking())
                {
                    m_isInvalid = true;
                    return;
                }

                if (m_timeToAlarmCheck >= 0)
                {
                    m_timeToAlarmCheck -= MyConstants.PHYSICS_STEP_SIZE_IN_SECONDS;
                    // When transition from curious behavior try raise alarm (if this doesn't work pass some flag curious behavior)
                    if (m_timeToAlarmCheck < 0 && smallShipTarget != null && smallShipTarget.HasRadarJammerActive())
                    {
                        bot.LaunchAlarm(smallShipTarget);
                    }
                }

                UpdateVisibility(bot);

                if (m_targetVisible)
                {
                    AttackTarget(bot, m_targetVisible);
                    findSmallship.Init(bot);

                    // Attack player to prevent his passivity when we have invicible wingmans
                    m_playerAttackDecisionTimer -= MyConstants.PHYSICS_STEP_SIZE_IN_SECONDS;
                    if (smallShipTarget != null && smallShipTarget.Leader == MySession.PlayerShip && m_playerAttackDecisionTimer <= 0)
                    {
                        m_playerAttackDecisionTimer = 2.5f;
                        if (MyMwcUtils.GetRandomFloat(0, 1.0f) < 0.7f)
                        {
                            m_target = smallShipTarget.Leader;
                        }
                    }
                }
                else
                {
                    m_state = StateEnum.CLOSING;

                    findSmallship.Update(bot, m_target);

                    if (findSmallship.PathNotFound)
                    {
                        bot.IsSleeping = true;
                        m_isInvalid    = true;
                    }
                    else if (!findSmallship.GotPosition())
                    {
                        AttackTarget(bot, false);
                    }
                }
            }
        }