public Planet FindNextPlanetInPath(Planet source) { Dijkstra dijkstra = new Dijkstra(Graph, source.PlanetID()); int min = int.MaxValue; int minID = -1; Planets frontPlanets = Context.GetFrontPlanets(); foreach (Planet frontPlanet in frontPlanets) { if (min <= dijkstra.Dist[frontPlanet.PlanetID()] || dijkstra.Dist[frontPlanet.PlanetID()] <= 0) continue; min = dijkstra.Dist[frontPlanet.PlanetID()]; minID = frontPlanet.PlanetID(); } if (minID == -1) return null; string s = ""; List<int> path = new List<int>(); for (int v = minID; v != source.PlanetID(); v = dijkstra.Path[v]) { path.Add(v); s = v + "," +s; } path.Reverse(); return Context.GetPlanet(path[0]); }
public override Moves Run(Planet supplyPlanet) { Moves moves = new Moves(); if (supplyPlanet == null) return moves; Planets frontPlanets = Context.GetFrontPlanets(); if (frontPlanets == null) return moves; if (frontPlanets.Count == 0) return moves; if (frontPlanets.IndexOf(supplyPlanet) != -1) return moves; IPathFinder pathFinder = new ClosestPathFinder(Context); //new DirectPathFinder(Context); //new DijkstraPathFinder(Context); Planet dest = pathFinder.FindNextPlanetInPath(supplyPlanet); if (dest != null) { int canSend = Context.CanSend(supplyPlanet); if (canSend == 0) return moves; Move move = new Move(supplyPlanet, dest, canSend); moves.Add(move); } return moves; }
public PlanetHolder(Planet planet, Fleets fleetList) { thisPlanet = planet; thisPlanetFleets = fleetList; TurnsCount = PlanetWars.GetFarestFleetDistance(thisPlanetFleets); ownerSwitches = new List<PlanetOwnerSwitch>(); }
public int CompareDistanceToTargetPlanetLT(Planet planet1, Planet planet2) { if (TargetPlanet == null) throw new ArgumentNullException("planet1", "Target planet is not defined!"); if (planet1.PlanetID() == planet2.PlanetID()) return planet1.PlanetID() - planet2.PlanetID(); int result = (Context.Distance(planet1, TargetPlanet) - Context.Distance(planet2, TargetPlanet)); if (result == 0) result = planet2.GrowthRate() - planet1.GrowthRate(); if (result == 0) result = planet1.PlanetID() - planet2.PlanetID(); return result; }
private static int CalcDistance(Planet source, Planet destination) { double dx = source.X() - destination.X(); double dy = source.Y() - destination.Y(); double squared = dx * dx + dy * dy; double rooted = Math.Sqrt(squared); int result = (int) Math.Ceiling(rooted); distances[source.PlanetID(), destination.PlanetID()] = result; distances[destination.PlanetID(), source.PlanetID()] = result; return result; }
public Planet FindNextPlanetInPath(Planet source) { Planets enemyPlanets = Context.GetPlanetsByLastOwner(Context.PlanetHolders(), 2); int supplyPlanetFrontLevel = Context.GetClosestPlanetDistance(source, enemyPlanets); //Context.GetPlanetSummaryDistance(Context.EnemyPlanets(), supplyPlanet); //Planets nearPlanets = Context.GetFrontPlanets(); Planets nearPlanets = Context.Planets(); if (nearPlanets.Count == 0) return null; //if (nearPlanets.Contains(source)) return null; Comparer comparer = new Comparer(Context) {TargetPlanet = source}; nearPlanets.Sort(comparer.CompareDistanceToTargetPlanetLT); //return nearPlanets[0]; Planet result = null; int resultFrontLevel = supplyPlanetFrontLevel; int resultDistance = 0; foreach (Planet nearPlanet in nearPlanets) { if (nearPlanet.PlanetID() == source.PlanetID()) continue; int distance = Context.Distance(nearPlanet, source); Planet futurePlanet = Context.PlanetFutureStatus(nearPlanet, distance); if (futurePlanet.Owner() != 1) continue; int nearPlanetFrontLevel = Context.GetClosestPlanetDistance(nearPlanet, enemyPlanets); if (nearPlanetFrontLevel < resultFrontLevel) { if ((result == null) || (distance < resultDistance * 1.2)) { result = nearPlanet; resultFrontLevel = nearPlanetFrontLevel; resultDistance = distance; } } } return result; /*return (from nearPlanet in nearPlanets where nearPlanet.PlanetID() != source.PlanetID() let distance = Context.Distance(nearPlanet, source) let futurePlanet = Context.PlanetFutureStatus(nearPlanet, distance) where futurePlanet.Owner() == 1 let nearPlanetFrontLevel = Context.GetClosestPlanetDistance(nearPlanet, enemyPlanets) where nearPlanetFrontLevel < supplyPlanetFrontLevel select nearPlanet).FirstOrDefault();*/ }
public Planet GetFutureState(int numberOfTurns) { FillFutureStatesIfNeeded(); if (numberOfTurns > TurnsCount) { Planet futureState = new Planet(futureStates[TurnsCount]); if (futureState.Owner() <= 0) return futureState; futureState.NumShips(futureState.NumShips() + futureState.GrowthRate() * (numberOfTurns - TurnsCount)); return futureState; } return futureStates[numberOfTurns]; }
public override Moves Run(Planet planet) { Moves moves = new Moves(); loseTurn = 0; //Planet planet = SelectPlanetForAdvise(); if (planet == null) return moves; List<Step> saveSteps = Context.GetMyPlanetSaveSteps(planet); if (saveSteps.Count == 0) return moves; foreach (Step t in saveSteps) { Planets planetsCanHelp = Context.MyPlanetsWithinProximityToPlanet(planet, t.ToTurn); Comparer comparer = new Comparer(Context) {TargetPlanet = planet}; planetsCanHelp.Sort(comparer.CompareDistanceToTargetPlanetLT); int sendedShipsNum = 0; foreach (Planet nearPlanet in planetsCanHelp) { int canSend = Math.Min(t.NumShips - sendedShipsNum, Context.CanSendByPlanets(nearPlanet, planet)); if (canSend <= 0) continue; int distance = Context.Distance(planet, nearPlanet); Move move = new Move(nearPlanet.PlanetID(), planet.PlanetID(), canSend); if (distance < t.ToTurn) { //delay move move.TurnsBefore = t.ToTurn - distance; //move = new Move(nearPlanet.PlanetID(), planet.PlanetID(), Context.CanSend(nearPlanet, move.TurnsBefore)); } moves.Add(move); sendedShipsNum += canSend; } if (sendedShipsNum < t.NumShips) { loseTurn = t.NumShips; } } return moves; }
public override Moves Run(Planet stealPlanet) { Moves moves = new Moves(); PlanetHolder planetHolder = Context.GetPlanetHolder(stealPlanet); List<PlanetOwnerSwitch> switches = planetHolder.GetOwnerSwitchesFromNeutralToEnemy(); if (switches.Count == 0) return moves; Planet futurePlanet = null; int turn = 0; for (int i = 0; i < switches.Count; i++) { turn = switches[i].TurnsBefore + 1; futurePlanet = Context.PlanetFutureStatus(stealPlanet, turn); if (futurePlanet.Owner() != 1) break; futurePlanet = null; } if (futurePlanet == null) return moves; Planets myPlanets = Context.MyPlanetsWithinProximityToPlanet(stealPlanet, turn); if (myPlanets.Count == 0) return moves; int needToSend = futurePlanet.NumShips() + 1; //needToSend += Context.GetEnemyAid(stealPlanet, turn); foreach (Planet myPlanet in myPlanets) { int distance = Context.Distance(myPlanet, stealPlanet); int canSend = Context.CanSendByPlanets(myPlanet, stealPlanet); if (canSend == 0) continue; int send = Math.Min(canSend, needToSend); needToSend -= send; Move move = new Move(myPlanet, stealPlanet, send) {TurnsBefore = turn - distance}; moves.Add(move); if (needToSend <= 0) return moves; } moves.Clear(); return moves; }
public override Moves Run(Planet targetPlanet) { Moves moves = new Moves(); if (targetPlanet == null) return moves; Planets myPlanets = Context.MyPlanets(); if (myPlanets.Count == 0) return moves; Comparer comparer = new Comparer(Context) {TargetPlanet = targetPlanet}; myPlanets.Sort(comparer.CompareDistanceToTargetPlanetLT); PlanetHolder holder = Context.GetPlanetHolder(targetPlanet); foreach (Planet myPlanet in myPlanets) { int targetDistance = Context.Distance(myPlanet, targetPlanet); int myCanSend = Context.CanSendByPlanets(myPlanet, targetPlanet); if (myCanSend == 0) continue; Planet futurePlanet = Context.PlanetFutureStatus(targetPlanet, targetDistance); if (futurePlanet.Owner() != 2) continue; if (holder.IsNeutralToEnemySwith(targetDistance)) continue; int needToSend = 1 + futurePlanet.NumShips(); if (Config.AttackSendMoreThanEnemyCanDefend) needToSend += Context.GetEnemyAid(targetPlanet, targetDistance); needToSend = moves.Aggregate(needToSend, (current, eachMove) => current - Context.CanSend(Context.GetPlanet(eachMove.SourceID))); if (needToSend <= 0) return moves; int canSend = Math.Min(needToSend, myCanSend); needToSend -= canSend; Move move = new Move(myPlanet, targetPlanet, canSend); moves.Add(move); if (needToSend <= 0) return moves; } return new Moves(); }
public static Sectors GetSector(Planet basePlanet, Planet objectPlanet) { return GetSector(basePlanet.PlanetID(), objectPlanet.PlanetID()); }
public int CompareNumberOfShipsLT(Planet planet1, Planet planet2) { int result = (planet1.NumShips() - planet2.NumShips()); if (result == 0) result = planet1.PlanetID() - planet2.PlanetID(); return result; }
public int CompareGrowsRateLT(Planet planet1, Planet planet2) { return -CompareGrowsRateGT(planet1, planet2); }
public int CompareGrowsRateGT(Planet planet1, Planet planet2) { int result = (planet2.GrowthRate() - planet1.GrowthRate()); if (result == 0) result = planet1.PlanetID() - planet2.PlanetID(); return result; }
private static void BattleForPlanet(Planet planetInFuture, List<Pair<int, int>> ships) { // Were there any fleets other than the one on the planet? if (ships.Count <= 1) return; // Sorts the fleets in descending order by the number of ships in the fleet ships.Sort(Pair<int, int>.CompareSecondOfPair); Pair<int, int> winner = ships[0]; Pair<int, int> secondToWinner = ships[1]; if (winner.Second == secondToWinner.Second) { //old owner stays planetInFuture.NumShips(0); } else { planetInFuture.Owner(winner.First); planetInFuture.NumShips(winner.Second - secondToWinner.Second); } }
public Planet PlanetFutureStatus(Planet planet, int numberOfTurns) { return GetPlanetHolder(planet).GetFutureState(numberOfTurns); }
public Planets EnemyPlanetsWithinProximityToPlanet(Planet thisPlanet, int proximityTreshold) { return PlanetsWithinProximityToPlanet(EnemyPlanets(), thisPlanet, proximityTreshold); }
public int CanSend(Planet planet, int turn) { return GetPlanetHolder(planet).CanSend(turn); }
public override List<MovesSet> RunAll() { List<MovesSet> setList = new List<MovesSet>(); myPlanet = Context.MyPlanets()[0]; enemyPlanet = Context.EnemyPlanets()[0]; enemyDistance = Context.Distance(myPlanet, enemyPlanet); int canSend = Math.Min(myPlanet.NumShips(), myPlanet.GrowthRate() * Context.Distance(myPlanet, enemyPlanet)); Planets neutralPlanets = Context.NeutralPlanets(); Planets planets = new Planets(Config.MaxPlanets); planets.AddRange(neutralPlanets.Where(neutralPlanet => (Context.Distance(myPlanet, neutralPlanet) < Context.Distance(enemyPlanet, neutralPlanet)) && neutralPlanet.GrowthRate() > 0)); setList.Add(BruteForce(planets, canSend)); return setList; }
private void CalcFutureState() { futureStates.Clear(); futureStates.Add(thisPlanet); canSend = thisPlanet.NumShips(); for (int turn = 1; turn <= TurnsCount; turn++) { Planet planetInFuture = new Planet(futureStates[turn - 1]); PlanetGrowth(planetInFuture); Fleets thisTurnFleets = GetThisTurnFleets(turn); int oldPlanetOwner = planetInFuture.Owner(); CalcFleetsOnPlanet(planetInFuture, thisTurnFleets); if (planetInFuture.Owner() != oldPlanetOwner) { PlanetOwnerSwitch pos = new PlanetOwnerSwitch( oldPlanetOwner, planetInFuture.Owner(), turn); ownerSwitches.Add(pos); } futureStates.Add(planetInFuture); if (planetInFuture.Owner() != 1) canSend = 0; if (planetInFuture.NumShips() >= canSend) continue; canSend = planetInFuture.NumShips(); } }
private static void PlanetGrowth(Planet planetInFuture) { if (planetInFuture.Owner() != 0) { planetInFuture.NumShips(planetInFuture.NumShips() + planetInFuture.GrowthRate()); } }
private static void CalcFleetsOnPlanet(Planet planetInFuture, Fleets thisTurnFleets) { // First is ownerID, second is number of ships List<Pair<int, int>> ships = new List<Pair<int, int>>(); if (thisTurnFleets.Count <= 0) return; const int owners = 2; for (int id = 1; id <= owners; ++id) { Fleets ownerFleets = PlanetWars.FleetsWithGivenOwner(thisTurnFleets, id); Pair<int, int> ownerShips = new Pair<int, int>(id, 0); // Add up fleets with the same owner foreach (Fleet ownerFleet in ownerFleets) { ownerShips.Second += ownerFleet.NumShips(); } // Add the ships from the planet to the corresponding fleet if (planetInFuture.Owner() == id) { ownerShips.Second += planetInFuture.NumShips(); } ships.Add(ownerShips); } // If the planet was neutral, it has it's own fleet if (planetInFuture.Owner() == 0) { ships.Add(new Pair<int, int>(0, planetInFuture.NumShips())); } BattleForPlanet(planetInFuture, ships); }
public int GetPlanetSummaryDistance(Planets planetList, Planet thisPlanet) { int distance = 0; foreach (Planet planet in planetList) { if (planet.PlanetID() == thisPlanet.PlanetID()) continue; distance += Distance(planet, thisPlanet); } return distance; }
public int CanSend(Planet planet) { if (planet.Owner() != 1) return 0; return GetPlanetHolder(planet).CanSend(); }
public static int Distance(Planet planet1, Planet planet2) { return Distance(planet1.PlanetID(), planet2.PlanetID()); }
public List<Step> GetMyPlanetSaveSteps(Planet planet) { List<Step> saveSteps = new List<Step>(); PlanetHolder planetHolder = GetPlanetHolder(planet); List<PlanetOwnerSwitch> switches = planetHolder.GetOwnerSwitchesFromMyToEnemy(); if (switches.Count == 0) return saveSteps; for (int i = 0; i < switches.Count; i++) { Step step = null; int turn = switches[i].TurnsBefore; Planet planetInFuture = planetHolder.GetFutureState(turn); if (planetInFuture.Owner() != 1) { step = new Step(0, turn, planetInFuture.NumShips() + Config.MinShipsOnPlanetsAfterDefend); } else if (planetInFuture.NumShips() < Config.MinShipsOnPlanetsAfterDefend) { step = new Step(0, turn, Config.MinShipsOnPlanetsAfterDefend - planetInFuture.NumShips()); } if (step != null) { saveSteps.Add(step); } } return saveSteps; }
public override Moves Run(Planet planet) { throw new NotImplementedException(); }
public int CanSendSafe(Planet planet) { Planet closestEnemyPlanet = GetClosestPlanet(planet, EnemyPlanets()); if (closestEnemyPlanet == null) { return planet.NumShips(); } int distance = Distance(planet, closestEnemyPlanet); Planets closePlanets = EnemyPlanetsWithinProximityToPlanet(planet, distance); int ships = 0; foreach (Planet closePlanet in closePlanets) { ships += closePlanet.NumShips(); } int safeCanSend = Math.Max(0, (planet.NumShips() - (ships - planet.GrowthRate() * distance))); //Logger.Log("Safe: " + safeCanSend + " notSafe:" + CanSend(planet)); //if (MyPlanets().Count == 1) return safeCanSend; //if (distance > 6) return CanSend(planet); //GetEnemyAid(planet, safeTurns);)); return Math.Min(safeCanSend, CanSend(planet)); }
public override Moves Run(Planet targetPlanet) { Moves moves = new Moves(); if (targetPlanet == null) return moves; Planets nearestPlanets = Context.GetClosestPlanetsToTargetBySectors(targetPlanet, Context.MyPlanets()); //Context.MyPlanets(); //MyPlanetsWithinProximityToPlanet(planet, Config.InvokeDistanceForInvade););); if (nearestPlanets.Count == 0) return moves; if (nearestPlanets.Count > 1) { Comparer comparer = new Comparer(Context) { TargetPlanet = targetPlanet }; nearestPlanets.Sort(comparer.CompareDistanceToTargetPlanetLT); } foreach (Planet nearestPlanet in nearestPlanets) { int canSend = Context.CanSendByPlanets(nearestPlanet, targetPlanet); if (canSend == 0) continue; int distance = Context.Distance(targetPlanet, nearestPlanet); Planet futurePlanet = Context.PlanetFutureStatus(targetPlanet, distance); if (futurePlanet.Owner() == 2)//Error? { #if LOG Logger.Log("InvadeAdvizer: Error?"); #endif moves.Clear(); return moves; } int needToSend = futurePlanet.NumShips() + 1; if (Config.InvadeSendMoreThanEnemyCanDefend) { int extraTurns = (int)Math.Ceiling(targetPlanet.NumShips() / (double)targetPlanet.GrowthRate()); if (Context.MyFutureProduction < Context.EnemyFutureProduction) extraTurns = 0; if ((Context.MyFutureProduction == Context.EnemyFutureProduction) && (Context.MyTotalShipCount <= Context.EnemyTotalShipCount)) extraTurns = 0; needToSend += Context.GetEnemyAid(targetPlanet, distance + extraTurns); } needToSend = moves.Aggregate(needToSend, (current, eachMove) => current - Context.CanSendByPlanets(Context.GetPlanet(eachMove.SourceID), Context.GetPlanet(eachMove.DestinationID))); //delay closer moves /*foreach (Move eachMove in moves) { int moveDistance = Context.Distance(eachMove.DestinationID, eachMove.SourceID); int turns = distance - moveDistance; eachMove.TurnsBefore = turns; needToSend -= Context.CanSend(Context.GetPlanet(eachMove.SourceID), turns); }*/ if (needToSend <= 0) return moves; canSend = Math.Min(needToSend, canSend); needToSend -= canSend; Move move = new Move(nearestPlanet, targetPlanet, canSend); moves.Add(move); if (needToSend <= 0) return moves; } moves.Clear(); return moves; }
public Planets NeutralPlanetsWithinProximityToPlanet(Planet thisPlanet, int proximityTreshold) { return PlanetsWithinProximityToPlanet(NeutralPlanets(), thisPlanet, proximityTreshold); }