public Fleet(bool temp, List<Ship> shiplist)
 {
     Fleet.Squad squad = new Fleet.Squad();
     squad.Fleet = this;
     for (int index = 0; index < Enumerable.Count<Ship>((IEnumerable<Ship>)shiplist); ++index)
     {
         if (squad.Ships.Count < 4)
             squad.Ships.Add(Enumerable.ElementAt<Ship>((IEnumerable<Ship>)shiplist, index));
         if (squad.Ships.Count == 4 || index == Enumerable.Count<Ship>((IEnumerable<Ship>)shiplist) - 1)
         {
             this.CenterFlank.Add(squad);
             squad = new Fleet.Squad();
             squad.Fleet = this;
         }
         this.Ships.Add(shiplist[index]);
     }
     this.AllFlanks.Add(this.CenterFlank);
     int num1 = 0;
     int num2 = 0;
     for (int index = 0; index < this.CenterFlank.Count; ++index)
     {
         if (index == 0)
             this.CenterFlank[index].Offset = new Vector2(0.0f, 0.0f);
         else if (index % 2 == 1)
         {
             ++num1;
             this.CenterFlank[index].Offset = new Vector2((float)(num1 * -1400), 0.0f);
         }
         else
         {
             ++num2;
             this.CenterFlank[index].Offset = new Vector2((float)(num2 * 1400), 0.0f);
         }
     }
 }
 public override void HandleInput(InputState input)
 {
     if (this.close.HandleInput(input))
     {
         this.ExitScreen();
         return;
     }
     if (input.CurrentKeyboardState.IsKeyDown(Keys.J) && !input.LastKeyboardState.IsKeyDown(Keys.J) && !GlobalStats.TakingInput)
     {
         AudioManager.PlayCue("echo_affirm");
         this.ExitScreen();
         return;
     }
     this.current = Mouse.GetState();
     Vector2 MousePos = new Vector2((float)input.CurrentMouseState.X, (float)input.CurrentMouseState.Y);
     if (this.SelectedNodeList.Count != 1 && this.FleetToEdit != -1)
     {
         if (!HelperFunctions.CheckIntersection(this.FleetNameEntry.ClickableArea, MousePos))
         {
             this.FleetNameEntry.Hover = false;
         }
         else
         {
             this.FleetNameEntry.Hover = true;
             if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Released)
             {
                 this.FleetNameEntry.HandlingInput = true;
                 return;
             }
         }
     }
     if (!this.FleetNameEntry.HandlingInput)
     {
         GlobalStats.TakingInput = false;
     }
     else
     {
         GlobalStats.TakingInput = true;
         this.FleetNameEntry.HandleTextInput(ref EmpireManager.GetEmpireByName(this.EmpireUI.screen.PlayerLoyalty).GetFleetsDict()[this.FleetToEdit].Name);
     }
     if (input.CurrentKeyboardState.IsKeyDown(Keys.D1) && input.LastKeyboardState.IsKeyUp(Keys.D1))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(1);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D2) && input.LastKeyboardState.IsKeyUp(Keys.D2))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(2);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D3) && input.LastKeyboardState.IsKeyUp(Keys.D3))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(3);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D4) && input.LastKeyboardState.IsKeyUp(Keys.D4))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(4);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D5) && input.LastKeyboardState.IsKeyUp(Keys.D5))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(5);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D6) && input.LastKeyboardState.IsKeyUp(Keys.D6))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(6);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D7) && input.LastKeyboardState.IsKeyUp(Keys.D7))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(7);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D8) && input.LastKeyboardState.IsKeyUp(Keys.D8))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(8);
     }
     else if (input.CurrentKeyboardState.IsKeyDown(Keys.D9) && input.LastKeyboardState.IsKeyUp(Keys.D9))
     {
         AudioManager.PlayCue("echo_affirm");
         this.ChangeFleet(9);
     }
     foreach (KeyValuePair<int, Rectangle> rect in this.FleetsRects)
     {
         if (!HelperFunctions.CheckIntersection(rect.Value, MousePos) || input.CurrentMouseState.LeftButton != ButtonState.Pressed || input.LastMouseState.LeftButton != ButtonState.Released)
         {
             continue;
         }
         AudioManager.PlayCue("echo_affirm");
         this.FleetToEdit = rect.Key;
         this.ChangeFleet(this.FleetToEdit);
     }
     if (this.FleetToEdit != -1)
     {
         this.sub_ships.HandleInput(this);
         if (this.ShipSL.HandleInput(input))
         {
             return;
         }
     }
     if (this.SelectedNodeList.Count == 1)
     {
         this.SelectedNodeList[0].AttackShieldedWeight = this.Slider_Shield.HandleInput(input);
         this.SelectedNodeList[0].DPSWeight = this.Slider_DPS.HandleInput(input);
         this.SelectedNodeList[0].VultureWeight = this.Slider_Vulture.HandleInput(input);
         this.SelectedNodeList[0].ArmoredWeight = this.Slider_Armor.HandleInput(input);
         this.SelectedNodeList[0].DefenderWeight = this.Slider_Defend.HandleInput(input);
         this.SelectedNodeList[0].AssistWeight = this.Slider_Assist.HandleInput(input);
         this.SelectedNodeList[0].SizeWeight = this.Slider_Size.HandleInput(input);
         if (HelperFunctions.CheckIntersection(this.OperationsRect, MousePos))
         {
             this.dragTimer = 0f;
             return;
         }
         if (HelperFunctions.CheckIntersection(this.PrioritiesRect, MousePos))
         {
             this.dragTimer = 0f;
             this.SelectedNodeList[0].OrdersRadius = this.OperationalRadius.HandleInput(input);
             return;
         }
         if (HelperFunctions.CheckIntersection(this.SelectedStuffRect, MousePos))
         {
             foreach (ToggleButton button in this.OrdersButtons)
             {
                 if (!HelperFunctions.CheckIntersection(button.r, MousePos))
                 {
                     button.Hover = false;
                 }
                 else
                 {
                     button.Hover = true;
                     if (input.CurrentMouseState.LeftButton != ButtonState.Pressed || input.LastMouseState.LeftButton != ButtonState.Released)
                     {
                         continue;
                     }
                     foreach (ToggleButton b in this.OrdersButtons)
                     {
                         b.Active = false;
                     }
                     string action = button.Action;
                     string str = action;
                     if (action != null)
                     {
                         if (str == "attack")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.AttackRuns;
                         }
                         else if (str == "arty")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.Artillery;
                         }
                         else if (str == "hold")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.HoldPosition;
                         }
                         else if (str == "orbit_left")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.OrbitLeft;
                         }
                         else if (str == "broadside_left")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.BroadsideLeft;
                         }
                         else if (str == "orbit_right")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.OrbitRight;
                         }
                         else if (str == "broadside_right")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.BroadsideRight;
                         }
                         else if (str == "evade")
                         {
                             this.SelectedNodeList[0].CombatState = CombatState.Evade;
                         }
                     }
                     if (this.SelectedNodeList[0].GetShip() == null)
                     {
                         continue;
                     }
                     this.SelectedNodeList[0].GetShip().GetAI().CombatState = this.SelectedNodeList[0].CombatState;
                     button.Active = true;
                     AudioManager.PlayCue("echo_affirm");
                     break;
                 }
             }
             return;
         }
     }
     else if (this.SelectedNodeList.Count > 1)
     {
         FleetDataNode fleetDataNode = new FleetDataNode();
         this.Slider_DPS.HandleInput(input);
         this.Slider_Vulture.HandleInput(input);
         this.Slider_Armor.HandleInput(input);
         this.Slider_Defend.HandleInput(input);
         this.Slider_Assist.HandleInput(input);
         this.Slider_Size.HandleInput(input);
         foreach (FleetDataNode node in this.SelectedNodeList)
         {
             node.DPSWeight = this.Slider_DPS.amount;
             node.VultureWeight = this.Slider_Vulture.amount;
             node.ArmoredWeight = this.Slider_Armor.amount;
             node.DefenderWeight = this.Slider_Defend.amount;
             node.AssistWeight = this.Slider_Assist.amount;
             node.SizeWeight = this.Slider_Size.amount;
         }
         if (HelperFunctions.CheckIntersection(this.OperationsRect, MousePos))
         {
             this.dragTimer = 0f;
             return;
         }
         if (HelperFunctions.CheckIntersection(this.PrioritiesRect, MousePos))
         {
             this.dragTimer = 0f;
             this.SelectedNodeList[0].OrdersRadius = this.OperationalRadius.HandleInput(input);
             return;
         }
         if (HelperFunctions.CheckIntersection(this.SelectedStuffRect, MousePos))
         {
             foreach (ToggleButton button in this.OrdersButtons)
             {
                 if (!HelperFunctions.CheckIntersection(button.r, MousePos))
                 {
                     button.Hover = false;
                 }
                 else
                 {
                     button.Hover = true;
                     if (input.CurrentMouseState.LeftButton != ButtonState.Pressed || input.LastMouseState.LeftButton != ButtonState.Released)
                     {
                         continue;
                     }
                     foreach (ToggleButton b in this.OrdersButtons)
                     {
                         b.Active = false;
                     }
                     AudioManager.PlayCue("echo_affirm");
                     button.Active = true;
                     foreach (FleetDataNode node in this.SelectedNodeList)
                     {
                         string action1 = button.Action;
                         string str1 = action1;
                         if (action1 != null)
                         {
                             if (str1 == "attack")
                             {
                                 node.CombatState = CombatState.AttackRuns;
                             }
                             else if (str1 == "arty")
                             {
                                 node.CombatState = CombatState.Artillery;
                             }
                             else if (str1 == "hold")
                             {
                                 node.CombatState = CombatState.HoldPosition;
                             }
                             else if (str1 == "orbit_left")
                             {
                                 node.CombatState = CombatState.OrbitLeft;
                             }
                             else if (str1 == "broadside_left")
                             {
                                 node.CombatState = CombatState.BroadsideLeft;
                             }
                             else if (str1 == "orbit_right")
                             {
                                 node.CombatState = CombatState.OrbitRight;
                             }
                             else if (str1 == "broadside_right")
                             {
                                 node.CombatState = CombatState.BroadsideRight;
                             }
                             else if (str1 == "evade")
                             {
                                 node.CombatState = CombatState.Evade;
                             }
                         }
                         if (node.GetShip() == null)
                         {
                             continue;
                         }
                         node.GetShip().GetAI().CombatState = node.CombatState;
                     }
                 }
             }
             return;
         }
     }
     else if (this.FleetToEdit != -1 && this.SelectedNodeList.Count == 0 && HelperFunctions.CheckIntersection(this.SelectedStuffRect, MousePos))
     {
         if (this.RequisitionForces.HandleInput(input))
         {
             base.ScreenManager.AddScreen(new RequisitionScreen(this));
         }
         if (this.SaveDesign.HandleInput(input))
         {
             base.ScreenManager.AddScreen(new SaveFleetDesignScreen(this.fleet));
         }
         if (this.LoadDesign.HandleInput(input))
         {
             base.ScreenManager.AddScreen(new LoadSavedFleetDesignScreen(this));
         }
     }
     if (this.ActiveShipDesign != null)
     {
         if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Released)
         {
             Viewport viewport = base.ScreenManager.GraphicsDevice.Viewport;
             Vector3 nearPoint = viewport.Unproject(new Vector3(MousePos.X, MousePos.Y, 0f), this.projection, this.view, Matrix.Identity);
             Viewport viewport1 = base.ScreenManager.GraphicsDevice.Viewport;
             Vector3 farPoint = viewport1.Unproject(new Vector3(MousePos.X, MousePos.Y, 1f), this.projection, this.view, Matrix.Identity);
             Vector3 direction = farPoint - nearPoint;
             direction.Normalize();
             Ray pickRay = new Ray(nearPoint, direction);
             float k = -pickRay.Position.Z / pickRay.Direction.Z;
             Vector3 pickedPosition = new Vector3(pickRay.Position.X + k * pickRay.Direction.X, pickRay.Position.Y + k * pickRay.Direction.Y, 0f);
             FleetDataNode node = new FleetDataNode()
             {
                 FleetOffset = new Vector2(pickedPosition.X, pickedPosition.Y),
                 ShipName = this.ActiveShipDesign.Name
             };
             this.fleet.DataNodes.Add(node);
             if (this.AvailableShips.Contains(this.ActiveShipDesign))
             {
                 if (this.fleet.Ships.Count == 0)
                 {
                     this.fleet.Position = this.ActiveShipDesign.Position;
                 }
                 node.SetShip(this.ActiveShipDesign);
                 node.GetShip().GetSO().World = Matrix.CreateTranslation(new Vector3(node.FleetOffset, 0f));
                 node.GetShip().RelativeFleetOffset = node.FleetOffset;
                 this.AvailableShips.Remove(this.ActiveShipDesign);
                 node.GetShip().fleet = this.fleet;
                 this.fleet.AddShip(node.GetShip());
                 if (this.sub_ships.Tabs[1].Selected)
                 {
                     ScrollList.Entry toremove = null;
                     foreach (ScrollList.Entry e in this.ShipSL.Copied)
                     {
                         if (!(e.item is Ship) || e.item as Ship != this.ActiveShipDesign)
                         {
                             continue;
                         }
                         toremove = e;
                         break;
                     }
                     if (toremove != null)
                     {
                         foreach (ScrollList.Entry e in this.ShipSL.Copied)
                         {
                             e.SubEntries.Remove(toremove);
                         }
                         this.ShipSL.Entries.Remove(toremove);
                         this.ShipSL.Copied.Remove(toremove);
                         this.ShipSL.Update();
                     }
                 }
                 this.ActiveShipDesign = null;
             }
             if (!input.CurrentKeyboardState.IsKeyDown(Keys.LeftShift))
             {
                 this.ActiveShipDesign = null;
             }
         }
         if (input.CurrentMouseState.RightButton == ButtonState.Pressed && input.LastMouseState.RightButton == ButtonState.Released)
         {
             this.ActiveShipDesign = null;
         }
     }
     if (this.FleetToEdit != -1)
     {
         foreach (ScrollList.Entry e in this.ShipSL.Copied)
         {
             if (!(e.item is ModuleHeader))
             {
                 if (!HelperFunctions.CheckIntersection(e.clickRect, MousePos) || input.CurrentMouseState.LeftButton != ButtonState.Pressed || input.LastMouseState.LeftButton != ButtonState.Released)
                 {
                     continue;
                 }
                 this.ActiveShipDesign = e.item as Ship;
                 this.SelectedNodeList.Clear();
                 this.SelectedSquad = null;
             }
             else
             {
                 (e.item as ModuleHeader).HandleInput(input, e);
             }
         }
     }
     this.HandleEdgeDetection(input);
     this.HandleSelectionBox(input);
     if (input.ScrollIn)
     {
         FleetDesignScreen desiredCamHeight = this;
         desiredCamHeight.DesiredCamHeight = desiredCamHeight.DesiredCamHeight - 1500f;
     }
     if (input.ScrollOut)
     {
         FleetDesignScreen fleetDesignScreen = this;
         fleetDesignScreen.DesiredCamHeight = fleetDesignScreen.DesiredCamHeight + 1500f;
     }
     if (this.DesiredCamHeight < 3000f)
     {
         this.DesiredCamHeight = 3000f;
     }
     else if (this.DesiredCamHeight > 100000f)
     {
         this.DesiredCamHeight = 100000f;
     }
     bool dragging = false;
     if (input.CurrentMouseState.RightButton == ButtonState.Pressed && input.LastMouseState.RightButton == ButtonState.Released)
     {
         dragging = true;
         this.startDrag = MousePos;
     }
     if (input.CurrentMouseState.RightButton == ButtonState.Pressed && input.LastMouseState.RightButton == ButtonState.Pressed)
     {
         dragging = true;
         this.endDrag = MousePos;
         if (Vector2.Distance(this.startDrag, this.endDrag) > 10f)
         {
             this.CamVelocity = HelperFunctions.FindVectorToTarget(this.endDrag, this.startDrag);
             this.CamVelocity = Vector2.Normalize(this.CamVelocity) * Vector2.Distance(this.startDrag, this.endDrag);
         }
     }
     if (!dragging)
     {
         this.CamVelocity = Vector2.Zero;
     }
     if (this.CamVelocity.Length() > 150f)
     {
         this.CamVelocity = Vector2.Normalize(this.CamVelocity) * 150f;
     }
     if (float.IsNaN(this.CamVelocity.X) || float.IsNaN(this.CamVelocity.Y))
     {
         this.CamVelocity = Vector2.Zero;
     }
     if (input.CurrentKeyboardState.IsKeyDown(Keys.Back) || input.CurrentKeyboardState.IsKeyDown(Keys.Delete))
     {
         if (this.SelectedSquad != null)
         {
             this.fleet.CenterFlank.Remove(this.SelectedSquad);
             this.fleet.LeftFlank.Remove(this.SelectedSquad);
             this.fleet.RearFlank.Remove(this.SelectedSquad);
             this.fleet.RightFlank.Remove(this.SelectedSquad);
             this.fleet.ScreenFlank.Remove(this.SelectedSquad);
             this.SelectedSquad = null;
             this.SelectedNodeList.Clear();
         }
         if (this.SelectedNodeList.Count > 0)
         {
             foreach (List<Fleet.Squad> flanks in this.fleet.AllFlanks)
             {
                 foreach (Fleet.Squad squad in flanks)
                 {
                     foreach (FleetDataNode node in this.SelectedNodeList)
                     {
                         if (!squad.DataNodes.Contains(node))
                         {
                             continue;
                         }
                         squad.DataNodes.QueuePendingRemoval(node);
                         if (node.GetShip() == null)
                         {
                             continue;
                         }
                         squad.Ships.QueuePendingRemoval(node.GetShip());
                     }
                     squad.DataNodes.ApplyPendingRemovals();
                     squad.Ships.ApplyPendingRemovals();
                 }
             }
             foreach (FleetDataNode node in this.SelectedNodeList)
             {
                 this.fleet.DataNodes.Remove(node);
                 if (node.GetShip() == null)
                 {
                     continue;
                 }
                 node.GetShip().GetSO().World = Matrix.CreateTranslation(new Vector3(node.GetShip().RelativeFleetOffset, -500000f));
                 this.fleet.Ships.Remove(node.GetShip());
                 node.GetShip().fleet = null;
             }
             this.SelectedNodeList.Clear();
             this.PopulateShipSL();
         }
     }
     if (input.Escaped)
     {
         FleetDesignScreen.Open = false;
         this.ExitScreen();
     }
     this.previous = this.current;
 }
        public void AutoArrange()
        {
            this.CenterShips.Clear();
            this.LeftShips.Clear();
            this.RightShips.Clear();
            this.ScreenShips.Clear();
            this.RearShips.Clear();
            this.CenterFlank.Clear();
            this.LeftFlank.Clear();
            this.RightFlank.Clear();
            this.ScreenFlank.Clear();
            this.RearFlank.Clear();
            this.AllFlanks.Add(this.CenterFlank);
            this.AllFlanks.Add(this.LeftFlank);
            this.AllFlanks.Add(this.RightFlank);
            this.AllFlanks.Add(this.ScreenFlank);
            this.AllFlanks.Add(this.RearFlank);
            BatchRemovalCollection<Ship> removalCollection = new BatchRemovalCollection<Ship>();
            foreach (Ship ship in (List<Ship>)this.Ships)
                removalCollection.Add(ship);
            foreach (Ship ship in (List<Ship>)removalCollection)
            {
                if (ship.Role == "scout")
                {
                    this.ScreenShips.Add(ship);
                    removalCollection.QueuePendingRemoval(ship);
                }
                if (ship.Role == "freighter")
                {
                    this.RearShips.Add(ship);
                    removalCollection.QueuePendingRemoval(ship);
                }
                if (ship.Role == "capital" || ship.Role == "carrier" || ship.Role == "cruiser")
                {
                    this.CenterShips.Add(ship);
                    removalCollection.QueuePendingRemoval(ship);
                }
            }
            removalCollection.ApplyPendingRemovals();
            IOrderedEnumerable<Ship> orderedEnumerable1 = Enumerable.OrderBy<Ship, float>((IEnumerable<Ship>)this.Ships, (Func<Ship, float>)(ship => ship.speed));
            this.speed = Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable1) > 0 ? Enumerable.ElementAt<Ship>((IEnumerable<Ship>)orderedEnumerable1, 0).speed : 200f;
            IOrderedEnumerable<Ship> orderedEnumerable2 = Enumerable.OrderByDescending<Ship, float>((IEnumerable<Ship>)removalCollection, (Func<Ship, float>)(ship => ship.GetStrength() + (float)ship.Size));
            int num1 = 0;
            foreach (Ship ship in (IEnumerable<Ship>)orderedEnumerable2)
            {
                if (num1 < 4)
                {
                    this.CenterShips.Add(ship);
                    ++num1;
                }
                else if (num1 < 7)
                {
                    this.LeftShips.Add(ship);
                    ++num1;
                }
                else if (num1 < 11)
                {
                    this.RightShips.Add(ship);
                    ++num1;
                }
                else if (num1 < 15)
                {
                    this.ScreenShips.Add(ship);
                    ship.FleetCombatStatus = FleetCombatStatus.Maintain;

                    ++num1;
                }
                if (num1 == 15)
                    num1 = 0;
            }
            IOrderedEnumerable<Ship> orderedEnumerable3 = Enumerable.OrderByDescending<Ship, int>((IEnumerable<Ship>)this.CenterShips, (Func<Ship, int>)(ship => ship.Size));
            Fleet.Squad squad1 = new Fleet.Squad();
            squad1.Fleet = this;
            for (int index = 0; index < Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable3); ++index)
            {
                if (squad1.Ships.Count < 4)
                    squad1.Ships.Add(Enumerable.ElementAt<Ship>((IEnumerable<Ship>)orderedEnumerable3, index));
                if (squad1.Ships.Count == 4 || index == Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable3) - 1)
                {
                    this.CenterFlank.Add(squad1);
                    squad1 = new Fleet.Squad();
                    squad1.Fleet = this;
                }
            }
            IOrderedEnumerable<Ship> orderedEnumerable4 = Enumerable.OrderByDescending<Ship, float>((IEnumerable<Ship>)this.LeftShips, (Func<Ship, float>)(ship => ship.speed));
            Fleet.Squad squad2 = new Fleet.Squad();
            squad2.Fleet = this;
            for (int index = 0; index < Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable4); ++index)
            {
                if (squad2.Ships.Count < 4)
                    squad2.Ships.Add(Enumerable.ElementAt<Ship>((IEnumerable<Ship>)orderedEnumerable4, index));
                if (squad2.Ships.Count == 4 || index == Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable4) - 1)
                {
                    this.LeftFlank.Add(squad2);
                    squad2 = new Fleet.Squad();
                    squad2.Fleet = this;
                }
            }
            IOrderedEnumerable<Ship> orderedEnumerable5 = Enumerable.OrderByDescending<Ship, float>((IEnumerable<Ship>)this.RightShips, (Func<Ship, float>)(ship => ship.speed));
            Fleet.Squad squad3 = new Fleet.Squad();
            squad3.Fleet = this;
            for (int index = 0; index < Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable5); ++index)
            {
                if (squad3.Ships.Count < 4)
                    squad3.Ships.Add(Enumerable.ElementAt<Ship>((IEnumerable<Ship>)orderedEnumerable5, index));
                if (squad3.Ships.Count == 4 || index == Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable5) - 1)
                {
                    this.RightFlank.Add(squad3);
                    squad3 = new Fleet.Squad();
                    squad3.Fleet = this;
                }
            }
            IOrderedEnumerable<Ship> orderedEnumerable6 = Enumerable.OrderByDescending<Ship, float>((IEnumerable<Ship>)this.ScreenShips, (Func<Ship, float>)(ship => ship.speed));
            Fleet.Squad squad4 = new Fleet.Squad();
            squad4.Fleet = this;
            for (int index = 0; index < Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable6); ++index)
            {
                if (squad4.Ships.Count < 4)
                    squad4.Ships.Add(Enumerable.ElementAt<Ship>((IEnumerable<Ship>)orderedEnumerable6, index));
                if (squad4.Ships.Count == 4 || index == Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable6) - 1)
                {
                    this.ScreenFlank.Add(squad4);
                    squad4 = new Fleet.Squad();
                    squad4.Fleet = this;
                }
            }
            IOrderedEnumerable<Ship> orderedEnumerable7 = Enumerable.OrderByDescending<Ship, float>((IEnumerable<Ship>)this.RearShips, (Func<Ship, float>)(ship => ship.speed));
            Fleet.Squad squad5 = new Fleet.Squad();
            squad5.Fleet = this;
            for (int index = 0; index < Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable7); ++index)
            {
                if (squad5.Ships.Count < 4)
                    squad5.Ships.Add(Enumerable.ElementAt<Ship>((IEnumerable<Ship>)orderedEnumerable7, index));
                if (squad5.Ships.Count == 4 || index == Enumerable.Count<Ship>((IEnumerable<Ship>)orderedEnumerable7) - 1)
                {
                    this.RearFlank.Add(squad5);
                    squad5 = new Fleet.Squad();
                    squad5.Fleet = this;
                }
            }
            this.Position = this.findAveragePosition();
            int num2 = 0;
            int num3 = 0;
            for (int index = 0; index < this.CenterFlank.Count; ++index)
            {
                if (index == 0)
                    this.CenterFlank[index].Offset = new Vector2(0.0f, 0.0f);
                else if (index % 2 == 1)
                {
                    ++num2;
                    this.CenterFlank[index].Offset = new Vector2((float)(num2 * -1400), 0.0f);
                }
                else
                {
                    ++num3;
                    this.CenterFlank[index].Offset = new Vector2((float)(num3 * 1400), 0.0f);
                }
            }
            int num4 = 0;
            int num5 = 0;
            for (int index = 0; index < this.ScreenFlank.Count; ++index)
            {
                if (index == 0)
                    this.ScreenFlank[index].Offset = new Vector2(0.0f, -2500f);
                else if (index % 2 == 1)
                {
                    ++num4;
                    this.ScreenFlank[index].Offset = new Vector2((float)(num4 * -1400), -2500f);
                }
                else
                {
                    ++num5;
                    this.ScreenFlank[index].Offset = new Vector2((float)(num5 * 1400), -2500f);
                }
            }
            int num6 = 0;
            int num7 = 0;
            for (int index = 0; index < this.RearFlank.Count; ++index)
            {
                if (index == 0)
                    this.RearFlank[index].Offset = new Vector2(0.0f, 2500f);
                else if (index % 2 == 1)
                {
                    ++num6;
                    this.RearFlank[index].Offset = new Vector2((float)(num6 * -1400), 2500f);
                }
                else
                {
                    ++num7;
                    this.RearFlank[index].Offset = new Vector2((float)(num7 * 1400), 2500f);
                }
            }
            for (int index = 0; index < this.LeftFlank.Count; ++index)
                this.LeftFlank[index].Offset = new Vector2((float)(-this.CenterFlank.Count * 1400 - (this.LeftFlank.Count == 1 ? 1400 : index * 1400)), 0.0f);
            for (int index = 0; index < this.RightFlank.Count; ++index)
                this.RightFlank[index].Offset = new Vector2((float)(this.CenterFlank.Count * 1400 + (this.RightFlank.Count == 1 ? 1400 : index * 1400)), 0.0f);
            this.AutoAssembleFleet(0.0f, new Vector2(0.0f, -1f));
            foreach (Ship s in (List<Ship>)this.Ships)
            {
                if (!s.InCombat)
                {
                    lock (s.GetAI().wayPointLocker)
                        s.GetAI().OrderThrustTowardsPosition(this.Position + s.FleetOffset, this.facing, new Vector2(0.0f, -1f), true);
                }
                FleetDataNode fleetDataNode = new FleetDataNode();
                fleetDataNode.SetShip(s);
                fleetDataNode.ShipName = s.Name;
                fleetDataNode.FleetOffset = s.RelativeFleetOffset;
                fleetDataNode.OrdersOffset = s.RelativeFleetOffset;
                this.DataNodes.Add(fleetDataNode);

            }

            //foreach (List<Fleet.Squad> list in this.AllFlanks)
            ////Parallel.ForEach(this.AllFlanks, list =>
            //{
            //    foreach (Fleet.Squad squad6 in list)
            //    {

            //        foreach (Ship ship in (List<Ship>)squad6.Ships)
            //        {
            //            foreach (FleetDataNode fleetDataNode in (List<FleetDataNode>)this.DataNodes)
            //            {
            //                if (ship == fleetDataNode.GetShip())
            //                    squad6.DataNodes.Add(fleetDataNode);
            //            }
            //        }
            //    }
            //}//);
        }
 protected void HandleSelectionBox(InputState input)
 {
     if (HelperFunctions.CheckIntersection(this.LeftMenu.Menu, input.CursorPosition) || HelperFunctions.CheckIntersection(this.RightMenu.Menu, input.CursorPosition))
     {
         this.SelectionBox = new Rectangle(0, 0, -1, -1);
         this.StartSelectionBox = false;
         return;
     }
     Vector2 MousePosition = new Vector2((float)input.CurrentMouseState.X, (float)input.CurrentMouseState.Y);
     this.HoveredNodeList.Clear();
     bool hovering = false;
     foreach (FleetDesignScreen.ClickableSquad squad in this.ClickableSquads)
     {
         if (Vector2.Distance(input.CursorPosition, squad.screenPos) > 8f)
         {
             continue;
         }
         this.HoveredSquad = squad.squad;
         hovering = true;
         List<FleetDataNode>.Enumerator enumerator = this.HoveredSquad.DataNodes.GetEnumerator();
         try
         {
             while (enumerator.MoveNext())
             {
                 FleetDataNode node = enumerator.Current;
                 this.HoveredNodeList.Add(node);
             }
             break;
         }
         finally
         {
             ((IDisposable)enumerator).Dispose();
         }
     }
     if (!hovering)
     {
         foreach (FleetDesignScreen.ClickableNode node in this.ClickableNodes)
         {
             if (Vector2.Distance(input.CursorPosition, node.ScreenPos) > node.Radius)
             {
                 continue;
             }
             this.HoveredNodeList.Add(node.nodeToClick);
             hovering = true;
         }
     }
     if (!hovering)
     {
         this.HoveredNodeList.Clear();
     }
     bool hitsomething = false;
     if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Released)
     {
         this.SelectedSquad = null;
         foreach (FleetDesignScreen.ClickableNode node in this.ClickableNodes)
         {
             if (Vector2.Distance(input.CursorPosition, node.ScreenPos) > node.Radius)
             {
                 continue;
             }
             if (this.SelectedNodeList.Count > 0 && !input.CurrentKeyboardState.IsKeyDown(Keys.LeftShift))
             {
                 this.SelectedNodeList.Clear();
             }
             AudioManager.GetCue("techy_affirm1").Play();
             hitsomething = true;
             if (!this.SelectedNodeList.Contains(node.nodeToClick))
             {
                 this.SelectedNodeList.Add(node.nodeToClick);
             }
             foreach (ToggleButton button in this.OrdersButtons)
             {
                 button.Active = false;
                 CombatState toset = CombatState.Artillery;
                 string action = button.Action;
                 string str = action;
                 if (action != null)
                 {
                     if (str == "attack")
                     {
                         toset = CombatState.AttackRuns;
                     }
                     else if (str == "arty")
                     {
                         toset = CombatState.Artillery;
                     }
                     else if (str == "hold")
                     {
                         toset = CombatState.HoldPosition;
                     }
                     else if (str == "orbit_left")
                     {
                         toset = CombatState.OrbitLeft;
                     }
                     else if (str == "broadside_left")
                     {
                         toset = CombatState.BroadsideLeft;
                     }
                     else if (str == "orbit_right")
                     {
                         toset = CombatState.OrbitRight;
                     }
                     else if (str == "broadside_right")
                     {
                         toset = CombatState.BroadsideRight;
                     }
                     else if (str == "evade")
                     {
                         toset = CombatState.Evade;
                     }
                 }
                 if (node.nodeToClick.CombatState != toset)
                 {
                     continue;
                 }
                 button.Active = true;
             }
             this.Slider_Armor.SetAmount(node.nodeToClick.ArmoredWeight);
             this.Slider_Assist.SetAmount(node.nodeToClick.AssistWeight);
             this.Slider_Defend.SetAmount(node.nodeToClick.DefenderWeight);
             this.Slider_DPS.SetAmount(node.nodeToClick.DPSWeight);
             this.Slider_Shield.SetAmount(node.nodeToClick.AttackShieldedWeight);
             this.Slider_Vulture.SetAmount(node.nodeToClick.VultureWeight);
             this.OperationalRadius.SetAmount(node.nodeToClick.OrdersRadius);
             this.Slider_Size.SetAmount(node.nodeToClick.SizeWeight);
             break;
         }
         foreach (FleetDesignScreen.ClickableSquad squad in this.ClickableSquads)
         {
             if (Vector2.Distance(input.CursorPosition, squad.screenPos) > 4f)
             {
                 continue;
             }
             this.SelectedSquad = squad.squad;
             if (this.SelectedNodeList.Count > 0 && !input.CurrentKeyboardState.IsKeyDown(Keys.LeftShift))
             {
                 this.SelectedNodeList.Clear();
             }
             hitsomething = true;
             AudioManager.GetCue("techy_affirm1").Play();
             this.SelectedNodeList.Clear();
             foreach (FleetDataNode node in this.SelectedSquad.DataNodes)
             {
                 this.SelectedNodeList.Add(node);
             }
             this.Slider_Armor.SetAmount(this.SelectedSquad.MasterDataNode.ArmoredWeight);
             this.Slider_Assist.SetAmount(this.SelectedSquad.MasterDataNode.AssistWeight);
             this.Slider_Defend.SetAmount(this.SelectedSquad.MasterDataNode.DefenderWeight);
             this.Slider_DPS.SetAmount(this.SelectedSquad.MasterDataNode.DPSWeight);
             this.Slider_Shield.SetAmount(this.SelectedSquad.MasterDataNode.AttackShieldedWeight);
             this.Slider_Vulture.SetAmount(this.SelectedSquad.MasterDataNode.VultureWeight);
             this.OperationalRadius.SetAmount(this.SelectedSquad.MasterDataNode.OrdersRadius);
             this.Slider_Size.SetAmount(this.SelectedSquad.MasterDataNode.SizeWeight);
             break;
         }
         if (!hitsomething)
         {
             this.SelectedSquad = null;
             this.SelectedNodeList.Clear();
         }
     }
     if (this.SelectedSquad != null)
     {
         if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Pressed && this.dragTimer > 0.1f)
         {
             Viewport viewport = base.ScreenManager.GraphicsDevice.Viewport;
             Vector3 nearPoint = viewport.Unproject(new Vector3(MousePosition.X, MousePosition.Y, 0f), this.projection, this.view, Matrix.Identity);
             Viewport viewport1 = base.ScreenManager.GraphicsDevice.Viewport;
             Vector3 farPoint = viewport1.Unproject(new Vector3(MousePosition.X, MousePosition.Y, 1f), this.projection, this.view, Matrix.Identity);
             Vector3 direction = farPoint - nearPoint;
             direction.Normalize();
             Ray pickRay = new Ray(nearPoint, direction);
             float k = -pickRay.Position.Z / pickRay.Direction.Z;
             Vector3 pickedPosition = new Vector3(pickRay.Position.X + k * pickRay.Direction.X, pickRay.Position.Y + k * pickRay.Direction.Y, 0f);
             Vector2 newspot = new Vector2((float)((int)pickedPosition.X), (float)((int)pickedPosition.Y));
             Vector2 difference = newspot - this.SelectedSquad.Offset;
             if (difference.Length() > 30f)
             {
                 Fleet.Squad selectedSquad = this.SelectedSquad;
                 selectedSquad.Offset = selectedSquad.Offset + difference;
                 foreach (FleetDataNode node in this.SelectedSquad.DataNodes)
                 {
                     FleetDataNode fleetOffset = node;
                     fleetOffset.FleetOffset = fleetOffset.FleetOffset + difference;
                     if (node.GetShip() == null)
                     {
                         continue;
                     }
                     Ship ship = node.GetShip();
                     ship.RelativeFleetOffset = ship.RelativeFleetOffset + difference;
                 }
             }
         }
     }
     else if (this.SelectedNodeList.Count != 1)
     {
         if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Released)
         {
             this.SelectionBox = new Rectangle(input.CurrentMouseState.X, input.CurrentMouseState.Y, 0, 0);
             this.StartSelectionBox = true;
         }
         if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && this.StartSelectionBox)
         {
             this.SelectionBox = new Rectangle(this.SelectionBox.X, this.SelectionBox.Y, input.CurrentMouseState.X - this.SelectionBox.X, input.CurrentMouseState.Y - this.SelectionBox.Y);
             return;
         }
         if (input.CurrentKeyboardState.IsKeyDown(Keys.LeftShift) && input.CurrentMouseState.LeftButton == ButtonState.Released && input.LastMouseState.LeftButton == ButtonState.Pressed && this.StartSelectionBox)
         {
             if (input.CurrentMouseState.X < this.SelectionBox.X)
             {
                 this.SelectionBox.X = input.CurrentMouseState.X;
             }
             if (input.CurrentMouseState.Y < this.SelectionBox.Y)
             {
                 this.SelectionBox.Y = input.CurrentMouseState.Y;
             }
             this.SelectionBox.Width = Math.Abs(this.SelectionBox.Width);
             this.SelectionBox.Height = Math.Abs(this.SelectionBox.Height);
             foreach (FleetDesignScreen.ClickableNode node in this.ClickableNodes)
             {
                 if (!this.SelectionBox.Contains(new Point((int)node.ScreenPos.X, (int)node.ScreenPos.Y)))
                 {
                     continue;
                 }
                 this.SelectedNodeList.Add(node.nodeToClick);
             }
             this.SelectionBox = new Rectangle(0, 0, -1, -1);
             return;
         }
         if (input.CurrentMouseState.LeftButton == ButtonState.Released && input.LastMouseState.LeftButton == ButtonState.Pressed)
         {
             if (input.CurrentMouseState.X < this.SelectionBox.X)
             {
                 this.SelectionBox.X = input.CurrentMouseState.X;
             }
             if (input.CurrentMouseState.Y < this.SelectionBox.Y)
             {
                 this.SelectionBox.Y = input.CurrentMouseState.Y;
             }
             this.SelectionBox.Width = Math.Abs(this.SelectionBox.Width);
             this.SelectionBox.Height = Math.Abs(this.SelectionBox.Height);
             foreach (FleetDesignScreen.ClickableNode node in this.ClickableNodes)
             {
                 if (!this.SelectionBox.Contains(new Point((int)node.ScreenPos.X, (int)node.ScreenPos.Y)))
                 {
                     continue;
                 }
                 this.SelectedNodeList.Add(node.nodeToClick);
             }
             this.SelectionBox = new Rectangle(0, 0, -1, -1);
         }
     }
     else if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Pressed && this.dragTimer > 0.1f)
     {
         Viewport viewport2 = base.ScreenManager.GraphicsDevice.Viewport;
         Vector3 nearPoint = viewport2.Unproject(new Vector3(MousePosition.X, MousePosition.Y, 0f), this.projection, this.view, Matrix.Identity);
         Viewport viewport3 = base.ScreenManager.GraphicsDevice.Viewport;
         Vector3 farPoint = viewport3.Unproject(new Vector3(MousePosition.X, MousePosition.Y, 1f), this.projection, this.view, Matrix.Identity);
         Vector3 direction = farPoint - nearPoint;
         direction.Normalize();
         Ray pickRay = new Ray(nearPoint, direction);
         float k = -pickRay.Position.Z / pickRay.Direction.Z;
         Vector3 pickedPosition = new Vector3(pickRay.Position.X + k * pickRay.Direction.X, pickRay.Position.Y + k * pickRay.Direction.Y, 0f);
         Vector2 newspot = new Vector2((float)((int)pickedPosition.X), (float)((int)pickedPosition.Y));
         if (Vector2.Distance(newspot, this.SelectedNodeList[0].FleetOffset) > 1000f)
         {
             return;
         }
         Vector2 difference = newspot - this.SelectedNodeList[0].FleetOffset;
         if (difference.Length() > 30f)
         {
             FleetDataNode item = this.SelectedNodeList[0];
             item.FleetOffset = item.FleetOffset + difference;
             if (this.SelectedNodeList[0].GetShip() != null)
             {
                 this.SelectedNodeList[0].GetShip().RelativeFleetOffset = this.SelectedNodeList[0].FleetOffset;
             }
         }
         foreach (FleetDesignScreen.ClickableSquad cs in this.ClickableSquads)
         {
             if (Vector2.Distance(cs.screenPos, MousePosition) >= 5f || cs.squad.DataNodes.Contains(this.SelectedNodeList[0]))
             {
                 continue;
             }
             foreach (List<Fleet.Squad> flank in this.fleet.AllFlanks)
             {
                 foreach (Fleet.Squad squad in flank)
                 {
                     squad.DataNodes.Remove(this.SelectedNodeList[0]);
                     if (this.SelectedNodeList[0].GetShip() == null)
                     {
                         continue;
                     }
                     squad.Ships.Remove(this.SelectedNodeList[0].GetShip());
                 }
             }
             cs.squad.DataNodes.Add(this.SelectedNodeList[0]);
             if (this.SelectedNodeList[0].GetShip() == null)
             {
                 continue;
             }
             cs.squad.Ships.Add(this.SelectedNodeList[0].GetShip());
         }
     }
 }