public static IEnumerator StandAndShootPlayer(AIBlackboard blackboard, ComponentBlackboard components)
        {
            components.stateMachineManager.state = "STAND AND SHOOT";
            blackboard.Waiting = true;
            components.StopNavigating();
            Transform         aiTransform     = components.aiObject.transform;
            Transform         playerTransform = blackboard.player.transform;
            MecanimLocomotion locomotion      = components.locomotion;

            locomotion.ResetWalkAngle();

            locomotion.move  = false;
            locomotion.speed = 0;

            while (true)
            {
                //if (blackboard.distanceToPlayer > 6 || !blackboard.LineOfSight || !blackboard.canBackUp)
                //{
                if (blackboard.LineOfSight)
                {
                    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 if(blackboard.canBackUp)

                /*{
                 *  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();
                 *
                 *  components.navigation.SetDestination(backUp);
                 * }*/
                yield return(null);
            }
        }
        public static IEnumerator CalculateFlank(AIBlackboard blackboard, ComponentBlackboard components)
        {
            Debug.Log("CalculatingFlank");
            components.stateMachineManager.state = "Calculating Flank";
            components.StopNavigating();
            components.locomotion.move = false;
            blackboard.Waiting         = false;


            yield return(null);

            yield return(new WaitForEndOfFrame());

            blackboard.positionBuffer[0] = Vector3.zero;

            FlankCalculator flanker = new FlankCalculator(components.controller.currentPosition, blackboard.player.transform.position, 200, 50, 80, components.stateMachineManager.flankMask, components.stateMachineManager);

            flanker.OnCalculateFlank += components.SetWaypoints;

            while (flanker.Working)
            {
                yield return(null);
            }

            flanker.OnCalculateFlank -= components.SetWaypoints;

            components.AdvanceState();
        }
        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 LookAround(AIBlackboard blackboard, ComponentBlackboard components)
        {
            blackboard.Waiting = true;
            components.StopNavigating();
            Transform         aiTransform     = components.aiObject.transform;
            Transform         playerTransform = blackboard.player.transform;
            MecanimLocomotion locomotion      = components.locomotion;

            locomotion.move = false;

            while (true)
            {
                Vector3 direction = new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));

                locomotion.lookDirection = direction;
                locomotion.turn          = true;

                int wait = Random.Range(2, 4);
                yield return(new WaitForSeconds(wait));
            }
        }
        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 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();
        }