protected void DefendNeutralPlanets(int maxTurnLookahead)
 {
     foreach (Planet neutralPlanet in OrderedListOfAttackedPlanets(0))
     {
         #region Try defend any owned planets.
         //if we have ships invested, protect investment,
         bool shouldDefend = neutralPlanet.IsUnderAttack &&
                             !neutralPlanet.WinningArmadaIsMine &&
                             !neutralPlanet.DoesNotChangeOwner;
         if (shouldDefend)
         {
             AttackPlan defensePlan = BuildDefensePlan(neutralPlanet, maxTurnLookahead);
             if (!neutralPlanet.IsLost)
             {
                 foreach (AttackPlanParticipant armada in defensePlan.Participants)
                 {
                     currentUniverse.MakeMove(armada.Source, defensePlan.Target, armada.Source.AttackForce);
                 }
                 neutralPlanet.AttackMovesAllowed = false;
             }
         }
         #endregion
     }
 }
        private bool MakeSweetestMoves(Universe uni, IEnumerable <AttackPlan> attackPlan)
        {
            bool   attackPerformed = false;
            Planet defender        = null;

            double bestSweetness = Double.MaxValue;

            foreach (var attack in attackPlan)
            {
                defender = attack.Target;
                if (bestSweetness == Double.MaxValue)
                {
                    bestSweetness = attack.Sweetness;
                }
                else
                {
                    double relativeSweetness = attack.Sweetness / bestSweetness;
                    if (relativeSweetness < 0.20)
                    {
                        // Compared to the moved that we like, this is really bad.
                        // So instead of making a very bad move, we will just end our turn.
                        break;
                    }
                }
                foreach (AttackPlanParticipant order in attack.Participants)
                {
                    Planet agressor = order.Source;
                    if (agressor.AttackMovesAllowed)
                    {
                        bool domination = currentUniverse.IsDominating || attack.DominationMove;
                        bool canConquer = agressor.DoesNotChangeOwner &&
                                          Math.Max(defender.ShipsRequiredToSurviveAttack, defender.AttackForce) < agressor.AttackForce;

                        if (domination || agressor.IsLost || canConquer)
                        {
                            if (defender.IsAttackable || domination)
                            {
                                uni.AddToWishList(defender);
                                //uni.MakeUnsafeMove(agressor, Defender, attack.ShipCount);
                                uni.MakeMove(agressor, defender, agressor.AttackForce);
                                attackPerformed = true;
                            }
                        }
                    }
                }
            }
            return(attackPerformed);
        }
        public void DoTurn(Universe uni)
        {
            currentUniverse = uni;
            ProcessAttackQueue();
            bool attackMade     = false;
            int  sumAttackForce = 0;


            Dictionary <Planet, PlanetTurn> possibleTargets = new Dictionary <Planet, PlanetTurn>();

            foreach (var planet in CanUseInAtttack())
            {
                sumAttackForce += planet.AttackForce;
                possibleTargets.Clear();
                PlanetTurn attackTurn = planet.TurnPrediction[0];
                //find planets that can be attacked with this planets force.
                foreach (var route in planet.Routes
                         .Where(target => IsNotEventuallyMine(target.Destination) &&
                                target.Destination.GrowthRate > 0))
                {
                    PlanetTurn state = route.Destination.TurnPrediction[route.DistanceInTurns + 1];
                    if (state.Owner != 1 && state.NumShips < planet.AttackForce)
                    {
                        possibleTargets.Add(route.Destination, state);
                    }
                }
                // from the list find the planets from which I have most to gain or which are nearest.

                if (Universe.TurnCount < 3)
                {
                    foreach (var item in possibleTargets.OrderBy(
                                 target => - target.Key.GrowthRate)
                             .ThenBy(target => target.Value.NumShips))
                    {
                        if (planet.IsNeutral)
                        {
                            if (planet.AttackForce > item.Value.NumShips)
                            {
                                AttackPlan plan = new AttackPlan();
                                plan.Enabled = true;
                                plan.AddParticipant(planet, attackTurn, item.Value);
                                plan.Target = item.Key;
                                attackMade  = true;
                                ExecutePlans(plan);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (var item in possibleTargets.OrderBy(
                                 target => - ((target.Key.IsNeutral ? 2 : 0) + target.Key.GrowthRate) / currentUniverse.Me.ShipOnPlanetFocus.Delta(target.Key)))
                    {
                        if (planet.AttackForce > item.Value.NumShips)
                        {
                            AttackPlan plan = new AttackPlan();
                            plan.Enabled = true;
                            plan.AddParticipant(planet, attackTurn, item.Value);
                            plan.Target = item.Key;
                            attackMade  = true;
                            ExecutePlans(plan);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            var centerPlanet = currentUniverse.Me.Planets
                               .OrderBy(item => Universe.Center.Delta(item)
                                        + currentUniverse.Enemy.ShipOnPlanetFocus.Delta(item)).FirstOrDefault();

            if (centerPlanet != null)
            {
                int supportCount = 0;
                foreach (var planet in CanUseInAtttack())
                {
                    if (centerPlanet != planet)
                    {
                        supportCount++;
                        currentUniverse.MakeMove(planet, centerPlanet, Math.Min(planet.AttackForce, 2));
                    }
                }
            }


            if (!attackMade)
            {
                int targetOwnerId = 0;
                if (!currentUniverse.IsDominating)
                {
                    if (rnd.NextDouble() > currentUniverse.Neutral.Planets.Count / (currentUniverse.Enemy.Planets.Count + 0.1))
                    {
                        targetOwnerId = 2;
                    }
                }
                else
                {
                    targetOwnerId = 2;
                }
                var target = currentUniverse.Planets.Values
                             .Where(planet => planet.LastAttackTurn.NumShips < sumAttackForce - planet.GrowthRate * 2 &&
                                    IsNotEventuallyMine(planet) &&
                                    planet.GrowthRate > 1 &&
                                    planet.Owner == targetOwnerId
                                    )
                             .OrderBy(planet => - ((planet.GrowthRate + Math.Sqrt(planet.NumShips)) / 3) * 3)
                             .ThenBy(planet => currentUniverse.Me.ShipOnPlanetFocus.Delta(planet))
                             .FirstOrDefault();

                if (target != null)
                {
                    AttackPlan plan = new AttackPlan();
                    plan.Enabled = true;
                    plan.Target  = target;
                    foreach (var planet in CanUseInAtttack())
                    {
                        plan.AddParticipant(planet, planet.TurnPrediction[0], target.TurnPrediction[0]);
                    }
                    attackMade = true;
                    ExecutePlans(plan);
                }
            }



            if (!attackMade && sumAttackForce > 100)
            {
                AttackPlan plan = new AttackPlan();
                plan.Enabled        = true;
                plan.DominationMove = true;
                plan.Target         = centerPlanet;
                foreach (var planet in CanUseInAtttack().Where(item => item != centerPlanet))
                {
                    plan.AddParticipant(planet, planet.TurnPrediction[0], centerPlanet.TurnPrediction[0]);
                }
                attackMade = true;
                ExecutePlans(plan);
            }
        }
        public void DoTurn(Universe uni)
        {
            currentUniverse = uni;
            ProcessAttackQueue();
            bool attackMade     = false;
            int  sumAttackForce = 0;


            List <AttackPlan> possibleTargets = new List <AttackPlan>();

            foreach (var planet in CanUseInAtttack())
            {
                sumAttackForce += planet.AttackForce;
                //possibleTargets.Clear();
                PlanetTurn attackTurn = planet.TurnPrediction[0];


                PlanetTurn highestStateTakeOver = new PlanetTurn(0);
                //find planets that can be attacked with this planets force, from which I have most to gain
                foreach (var route in planet.Routes
                         .Where(target => IsNotEventuallyMine(target.Destination) &&
                                target.Destination.GrowthRate > 0))
                {
                    PlanetTurn stateTakeOver    = null;
                    PlanetTurn destinationState = route.DestinationStateOnArrival;
                    PlanetTurn startAttackOnRun = attackTurn;
                    if (!destinationState.IsMine)
                    {
                        AttackPlan plan = new AttackPlan();

                        if (planet.AttackForce > destinationState.NumShips)
                        {
                            stateTakeOver = route.Destination.CalcMaxGainUsingThisManyShips(planet, route.DistanceInTurns, destinationState.NumShips + 5);
                            plan.Enabled  = true;
                        }
                        else
                        {
                            do
                            {
                                destinationState = destinationState.Next;
                                startAttackOnRun = startAttackOnRun.Next;
                            } while (destinationState != null &&
                                     startAttackOnRun.NumShips < destinationState.NumShips);
                            plan.Enabled = destinationState != null && startAttackOnRun != null;
                            if (plan.Enabled)
                            {
                                stateTakeOver = route.Destination.CalcMaxGainUsingThisManyShips(planet, destinationState.TurnsFromNow, startAttackOnRun.NumShips);
                            }
                        }

                        if (plan.Enabled)
                        {
                            plan.AddParticipant(planet, startAttackOnRun, destinationState);
                            plan.Target    = route.Destination;
                            plan.Enabled   = true;
                            plan.Sweetness = stateTakeOver.NumShips;
                            possibleTargets.Add(plan);
                            highestStateTakeOver = stateTakeOver;
                        }
                    }
                    //PlanetTurn fullAttackState = planet.TurnPrediction.CalcMaxGainUsingThisManyShips(destinationState, planet.AttackForce, TurnPrediction);
                    //planet.TurnPrediction.FindCheapestTakeOverPoint(destinationState.TurnsFromNow, TurnPrediction);
                }
            }
            foreach (AttackPlan plan in possibleTargets.OrderBy(item => - item.Sweetness).Take(10))
            {
                ExecutePlans(plan);
            }
            var centerPlanet = currentUniverse.Me.Planets
                               .OrderBy(item => Universe.Center.Delta(item)).FirstOrDefault();

            if (centerPlanet != null)
            {
                int supportCount = 0;
                foreach (var planet in CanUseInAtttack())
                {
                    if (centerPlanet != planet)
                    {
                        supportCount++;
                        currentUniverse.MakeMove(planet, centerPlanet, Math.Min(planet.AttackForce, 2));
                    }
                }
            }


            //if (!attackMade)
            //{
            //    int targetOwnerId = 0;
            //    if (!currentUniverse.IsDominating)
            //    {
            //        if (rnd.NextDouble() > currentUniverse.Neutral.Planets.Count / (currentUniverse.Enemy.Planets.Count + 0.1))
            //        {
            //            targetOwnerId = 2;
            //        }
            //    }
            //    else
            //    {
            //        targetOwnerId = 2;
            //    }
            //    var target = currentUniverse.Planets.Values
            //           .Where(planet => planet.LastAttackTurn.NumShips < sumAttackForce - planet.GrowthRate * 2
            //                            && IsNotEventuallyMine(planet)
            //                            && planet.GrowthRate > 1
            //                            && planet.Owner == targetOwnerId
            //                            )
            //        .OrderBy(planet => -((planet.GrowthRate + Math.Sqrt(planet.NumShips)) / 3) * 3)
            //        .ThenBy(planet => currentUniverse.Me.ShipOnPlanetFocus.Delta(planet))
            //        .FirstOrDefault();

            //    if (target != null)
            //    {
            //        AttackPlan plan = new AttackPlan();
            //        plan.Enabled = true;
            //        plan.Target = target;
            //        foreach (var planet in CanUseInAtttack())
            //        {
            //            plan.AddParticipant(planet, planet.TurnPrediction[0], target.TurnPrediction[0]);
            //        }
            //        attackMade = true;
            //        ExecutePlans(plan);
            //    }
            //}



            //if (!attackMade && sumAttackForce > 100)
            //{

            //    AttackPlan plan = new AttackPlan();
            //    plan.Enabled = true;
            //    plan.DominationMove = true;
            //    plan.Target = centerPlanet;
            //    foreach (var planet in CanUseInAtttack().Where(item => item != centerPlanet))
            //    {
            //        plan.AddParticipant(planet, planet.TurnPrediction[0], centerPlanet.TurnPrediction[0]);
            //    }
            //    attackMade = true;
            //    ExecutePlans(plan);
            //}
        }