public static IEnumerator TrailLeader(AIBlackboard blackboard, ComponentBlackboard components, float speed, int bufferIndex)
        {
            blackboard.Waiting = false;
            components.StopShootingPlayer();
            TrailingFollower  target      = TrailingFollower.SpawnTrailer(blackboard.targetBuffer[bufferIndex]);
            NavMeshAgent      navigation  = components.navigation;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            navigation.SetDestination(target.Waypoint.position);

            locomotion.speed           = 8;
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            yield return(null);

            while (Vector3.Distance(target.Waypoint.position, aiTransform.position) > 5)
            {
                navigation.SetDestination(target.Waypoint.position);
                components.Navigate();
                target.PositionWaypoint();
                yield return(new WaitForSeconds(0.1f));
            }

            locomotion.speed           = 3;
            locomotion.lookWhereMoving = false;
            locomotion.turn            = true;
            locomotion.move            = true;

            yield return(null);

            while (true)
            {
                if (Vector3.Distance(target.Waypoint.position, aiTransform.position) > 1)
                {
                    locomotion.lookDirection = -locomotion.moveDirection;
                    navigation.SetDestination(target.Waypoint.position);
                    target.PositionWaypoint();
                    components.Navigate();
                }
                else
                {
                    components.StopNavigating();
                    locomotion.turn = false;
                    locomotion.move = false;
                }
                yield return(null);
            }
        }
        public static IEnumerator GotoPerson(AIBlackboard blackboard, int bufferIndex, float speed, ComponentBlackboard components, float radius = 2, bool waiting = false)
        {
            blackboard.Waiting = waiting;
            components.StopShootingPlayer();
            Transform         target      = blackboard.targetBuffer[bufferIndex];
            NavMeshAgent      navigation  = components.navigation;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            navigation.SetDestination(target.position);

            locomotion.speed           = speed;
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            yield return(null);

            while (Vector3.Distance(target.position, aiTransform.position) > radius)
            {
                navigation.SetDestination(target.position);
                components.Navigate();
                yield return(new WaitForSeconds(0.1f));
            }

            locomotion.move = false;
            components.AdvanceState();
        }
        public static IEnumerator GotoPlayer(AIBlackboard blackboard, float speed, ComponentBlackboard components, bool waiting = false)
        {
            components.stateMachineManager.state = "GOTO";
            blackboard.Waiting = waiting;
            components.StopShootingPlayer();
            NavMeshAgent      navigation  = components.navigation;
            Transform         target      = blackboard.player.transform;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            navigation.SetDestination(target.position);


            yield return(null);

            while (true)
            {
                if (blackboard.CanSeePlayer)
                {
                    locomotion.speed = 4;
                    components.ShootPlayer();
                    components.AimAtPlayer(target);
                }
                else
                {
                    locomotion.speed = speed;
                    components.StopShootingPlayer();
                }

                locomotion.lookWhereMoving = true;
                locomotion.turn            = false;
                locomotion.move            = true;

                /*if (AIBlackboard.PlayerSpotted)
                 *  navigation.SetDestination(target.position);
                 * else
                 *  navigation.SetDestination(AIBlackboard.playerSuspectedPosition);*/

                navigation.SetDestination(target.position);
                components.Navigate();

                yield return(new WaitForSeconds(0.1f));
            }
        }
        public static IEnumerator GotoPosition(AIBlackboard blackboard, int bufferIndex, float speed, ComponentBlackboard components, float radius = 2, float waitTime = 0, bool waiting = false)
        {
            components.stateMachineManager.state = "Going to a place";
            blackboard.Waiting = waiting;
            components.StopShootingPlayer();
            Vector3           pos         = blackboard.positionBuffer[bufferIndex];
            NavMeshAgent      navigation  = components.navigation;
            MecanimLocomotion locomotion  = components.locomotion;
            Transform         aiTransform = components.aiObject.transform;

            navigation.SetDestination(pos);

            locomotion.speed           = speed;
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            bool legitLocation = pos.magnitude > 0.001f;

            while (Vector3.Distance(pos, aiTransform.position) > radius && legitLocation)
            {
                navigation.SetDestination(pos);
                components.Navigate();
                yield return(null);
            }

            locomotion.move = false;
            components.StopNavigating();

            if (waitTime > 0)
            {
                yield return(new WaitForSeconds(waitTime));
            }

            components.AdvanceState();
        }
        public static IEnumerator ApproachAttack(AIBlackboard blackboard, ComponentBlackboard components, float speed)
        {
            blackboard.Waiting = true;
            Debug.Log("Attack");
            Transform         aiTransform     = components.aiObject.transform;
            Transform         playerTransform = blackboard.player.transform;
            MecanimLocomotion locomotion      = components.locomotion;
            NavMeshAgent      navigation      = components.navigation;

            locomotion.speed = speed;
            locomotion.turn  = false;
            locomotion.move  = true;
            components.StopShootingPlayer();
            while (true)
            {
                while (Vector3.Distance(playerTransform.position, aiTransform.position) > blackboard.AttackRange || !blackboard.CanSeePlayer)
                {
                    if (blackboard.CanSeePlayer)
                    {
                        locomotion.lookWhereMoving = false;
                        locomotion.turn            = true;
                        locomotion.lookDirection   = playerTransform.position - aiTransform.position;
                        components.ShootPlayer();
                        components.AimAtPlayer(playerTransform);
                    }
                    else
                    {
                        locomotion.lookWhereMoving = true;
                        locomotion.turn            = false;
                        components.StopShootingPlayer();
                    }

                    navigation.SetDestination(playerTransform.position);
                    components.Navigate();
                    yield return(null);
                }

                float r         = blackboard.AttackRange + 7;
                float breakTime = 0;
                bool  canSee    = blackboard.CanSeePlayer;

                while (Vector3.Distance(playerTransform.position, aiTransform.position) < r)
                {
                    if (blackboard.CanSeePlayer)
                    {
                        locomotion.turn          = true;
                        locomotion.lookDirection = playerTransform.position - aiTransform.position;
                        components.ShootPlayer();
                        components.AimAtPlayer(playerTransform);
                    }
                    else
                    {
                        if (canSee)
                        {
                            breakTime = Time.time + 2.5f;
                        }
                        else if (Time.time > breakTime)
                        {
                            break;
                        }
                        locomotion.turn = false;
                    }

                    canSee = blackboard.CanSeePlayer;

                    yield return(null);
                }
            }
        }
        public static IEnumerator StandardAttack(AIBlackboard blackboard, ComponentBlackboard components)
        {
            blackboard.Waiting = true;
            Transform         aiTransform     = components.aiObject.transform;
            Transform         playerTransform = blackboard.player.transform;
            MecanimLocomotion locomotion      = components.locomotion;
            NavMeshAgent      navigation      = components.navigation;

            locomotion.move  = false;
            locomotion.speed = 3;
            components.StopNavigating();

            while (true)
            {
                float moveTime = Time.time + Mathf.Max(2f, Random.Range(1, 8));
                locomotion.move = false;

                while (moveTime > Time.time)//stand time
                {
                    if (blackboard.distanceToPlayer > 6 || !blackboard.CanSeePlayer)
                    {
                        if (blackboard.CanSeePlayer)
                        {
                            locomotion.move          = false;
                            locomotion.moveDirection = Vector3.zero;
                            locomotion.turn          = true;
                            locomotion.lookDirection = playerTransform.position - aiTransform.position;
                            components.ShootPlayer();
                            components.AimAtPlayer(playerTransform);
                        }
                        else
                        {
                            locomotion.turn = false;
                            components.StopShootingPlayer();
                        }
                        components.StopNavigating();
                    }
                    else
                    {
                        Vector3 backUp = locomotion.transform.position - playerTransform.position * 5;
                        backUp.y = locomotion.transform.position.y;

                        locomotion.move            = true;
                        locomotion.turn            = true;
                        locomotion.lookWhereMoving = false;

                        locomotion.lookDirection = playerTransform.position - aiTransform.position;
                        components.ShootPlayer();
                        components.AimAtPlayer(playerTransform);
                        components.Navigate();

                        navigation.SetDestination(backUp);
                    }


                    yield return(null);
                }

                CoverPoint originalCover = (Random.Range(0, 10) > 3)? blackboard.BestAntiCover : blackboard.BestCover;

                if (originalCover != null)
                {
                    locomotion.move = true;
                    navigation.SetDestination(originalCover.transform.position);

                    moveTime = Time.time + Mathf.Max(1, Random.Range(2, 6));


                    bool coverValid = (originalCover.GetInstanceID() == blackboard.BestAntiCover.GetInstanceID() || originalCover.GetInstanceID() == blackboard.BestCover.GetInstanceID() || Time.time < moveTime);

                    locomotion.turn            = true;
                    locomotion.lookWhereMoving = false;

                    while (coverValid && Vector3.Distance(originalCover.transform.position, aiTransform.position) > 2)
                    {
                        components.Navigate();
                        if (blackboard.LineOfSight)
                        {
                            locomotion.lookDirection = playerTransform.position - aiTransform.position;
                            components.ShootPlayer();
                            components.AimAtPlayer(playerTransform);
                        }
                        else
                        {
                            components.StopShootingPlayer();
                        }
                        yield return(null);
                    }
                }
                else
                {
                    locomotion.turn            = false;
                    locomotion.lookWhereMoving = true;
                    locomotion.move            = true;
                    components.StopShootingPlayer();
                    while (Vector3.Distance(playerTransform.position, aiTransform.position) > blackboard.AttackRange || !blackboard.CanSeePlayer)
                    {
                        if (blackboard.CanSeePlayer)
                        {
                            components.AimAtPlayer(playerTransform);
                            components.ShootPlayer();
                        }
                        else
                        {
                            components.StopShootingPlayer();
                        }

                        navigation.SetDestination(playerTransform.position);
                        components.Navigate();
                        yield return(null);
                    }
                }
            }
        }
        public static IEnumerator GotoCover(AIBlackboard blackboard, ComponentBlackboard components, float speed, bool strafeShoot, float waitTime, bool antiCover = false)
        {
            components.stateMachineManager.state = "COVER";
            //don't want a new instruction
            blackboard.Waiting = false;
            Debug.Log("Cover");
            components.StopShootingPlayer();

            NavMeshAgent      navigation      = components.navigation;
            MecanimLocomotion locomotion      = components.locomotion;
            Transform         aiTransform     = components.aiObject.transform;
            Transform         playerTransform = blackboard.player.transform;

            Vector3    pos   = aiTransform.position;
            CoverPoint cover = antiCover ? blackboard.BestAntiCover : blackboard.BestCover;

            if (cover != null)
            {
                pos = cover.transform.position;
            }
            else
            {
                Debug.Log("NO COVER!!");
            }


            bool validCover = true;

            //don't turn back on player
            float angle = Vector3.Angle(new Vector3(pos.x, 0, pos.z), new Vector3(aiTransform.position.x, aiTransform.position.z));

            if (angle > 100)
            {
                speed = 3;
            }

            navigation.SetDestination(pos);
            locomotion.lookWhereMoving = true;
            locomotion.turn            = false;
            locomotion.move            = true;

            while (Vector3.Distance(pos, aiTransform.position) > 3.5f && validCover)
            {
                /*if(antiCover)
                 * {
                 *  validCover = blackboard.BestAntiCover.GetInstanceID() == cover.GetInstanceID();
                 * }
                 * else
                 * {
                 *  validCover = blackboard.BestCover.GetInstanceID() == cover.GetInstanceID();
                 * }*/


                if (strafeShoot)
                {
                    if (!blackboard.LostPlayer && speed < 6)
                    {
                        locomotion.speed = speed;
                        locomotion.turn  = true;

                        if (playerTransform != null)
                        {
                            locomotion.lookDirection = playerTransform.position - aiTransform.position;
                        }

                        locomotion.lookWhereMoving = false;

                        if (blackboard.LineOfSight)
                        {
                            components.ShootPlayer();
                        }
                        else
                        {
                            components.StopShootingPlayer();
                        }

                        components.AimAtPlayer(playerTransform);
                    }
                    else
                    {
                        locomotion.speed = speed;
                        //locomotion.turn = true;
                        //locomotion.lookWhereMoving = true;
                        components.StopShootingPlayer();
                    }
                }
                navigation.SetDestination(pos);
                components.Navigate();

                yield return(null);

                cover = antiCover ? blackboard.BestAntiCover : blackboard.BestCover;
                if (cover != null)
                {
                    pos = cover.transform.position;
                }
            }

            float leaveTime = Time.time + waitTime;

            locomotion.move = false;
            components.StopNavigating();

            components.stateMachineManager.state = "AT COVER";

            while (leaveTime > Time.time)
            {
                if (blackboard.LineOfSight)
                {
                    locomotion.speed = 0;
                    locomotion.turn  = true;

                    if (playerTransform != null)
                    {
                        locomotion.lookDirection = playerTransform.position - aiTransform.position;
                    }

                    components.ShootPlayer();
                    components.AimAtPlayer(playerTransform);
                }
                else
                {
                    locomotion.lookDirection = aiTransform.forward;
                    locomotion.turn          = false;
                    components.StopShootingPlayer();
                }

                yield return(null);
            }

            components.AdvanceState();
        }