protected void DefendOnwedPlanets() { foreach (Planet onwedPlanet in OrderedListOfAttackedPlanets(1)) { #region Try defend any owned planets. bool shouldDefend = onwedPlanet.IsUnderAttack && !onwedPlanet.WinningArmadaIsMine; if (shouldDefend) { AttackPlan defensePlan = BuildDefensePlan(onwedPlanet, 1000); if (onwedPlanet.IsLost) { foreach (var route in onwedPlanet.Routes) { if (route.DistanceInTurns <= onwedPlanet.LastAttackTurn.TurnsFromNow) { Planet target = route.Destination; if (target.IsNeutral && target.MaxDesertersAllowed < onwedPlanet.NumShips) { currentUniverse.MakeMove(onwedPlanet, target, onwedPlanet.NumShips); break; } } else { break; } } //Recapture it the planet provides good resource. if (onwedPlanet.GrowthRate > 2) { currentUniverse.AddToWishList(onwedPlanet); } } else { foreach (AttackPlanParticipant armada in defensePlan.Participants) { currentUniverse.MakeMove(armada.Source, defensePlan.Target, armada.Source.AttackForce); armada.Source.AttackMovesAllowed = false; armada.Source.CreateTurnPredictions(); } onwedPlanet.AttackMovesAllowed = false; onwedPlanet.CreateTurnPredictions(); } } else { } #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); }