public BehaviorProtectFlank(Formation formation)
     : base(formation)
 {
     this._protectFlankState = BehaviorProtectFlank.BehaviourState.HoldingFlank;
     this.behaviorSide       = formation.AI.Side;
     this._mainFormation     = formation.Team.Formations.FirstOrDefault <Formation>((Func <Formation, bool>)(f => f.AI.IsMainFormation));
     this.CalculateCurrentOrder();
     this.CurrentOrder = this._movementOrder;
 }
        private void CheckAndChangeState()
        {
            WorldPosition position = this._movementOrder.GetPosition(this.formation);

            switch (this._protectFlankState)
            {
            case BehaviorProtectFlank.BehaviourState.HoldingFlank:
                if (this.formation.QuerySystem.ClosestEnemyFormation == null)
                {
                    break;
                }
                float num1 = (float)(50.0 + ((double)this.formation.Depth + (double)this.formation.QuerySystem.ClosestEnemyFormation.Formation.Depth) / 2.0);
                if ((double)this.formation.QuerySystem.ClosestEnemyFormation.MedianPosition.AsVec2.DistanceSquared(position.AsVec2) >= (double)num1 * (double)num1)
                {
                    break;
                }
                this._chargeToTargetOrder = MovementOrder.MovementOrderChargeToTarget(this.formation.QuerySystem.ClosestEnemyFormation.Formation);
                this.CurrentOrder         = this._chargeToTargetOrder;
                this._protectFlankState   = BehaviorProtectFlank.BehaviourState.Charging;
                break;

            case BehaviorProtectFlank.BehaviourState.Charging:
                if (this.formation.QuerySystem.ClosestEnemyFormation == null)
                {
                    this.CurrentOrder       = this._movementOrder;
                    this._protectFlankState = BehaviorProtectFlank.BehaviourState.Returning;
                    break;
                }
                float num2 = (float)(60.0 + ((double)this.formation.Depth + (double)this.formation.QuerySystem.ClosestEnemyFormation.Formation.Depth) / 2.0);
                if ((double)this.formation.QuerySystem.AveragePosition.DistanceSquared(position.AsVec2) <= (double)num2 * (double)num2)
                {
                    break;
                }
                this.CurrentOrder       = this._movementOrder;
                this._protectFlankState = BehaviorProtectFlank.BehaviourState.Returning;
                break;

            case BehaviorProtectFlank.BehaviourState.Returning:
                if ((double)this.formation.QuerySystem.AveragePosition.DistanceSquared(position.AsVec2) >= 400.0)
                {
                    break;
                }
                this._protectFlankState = BehaviorProtectFlank.BehaviourState.HoldingFlank;
                break;
            }
        }