Exemplo n.º 1
0
        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]);
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
		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;
		}
Exemplo n.º 4
0
        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();*/
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
		public int GetEnemyAid(Planet planet, int numberOfTurns)
		{
			if (enemyAid == null)
			{
				int iSize = Planets().Count;
				int jSize = Router.MaxDistance + 1;
				enemyAid = new int[iSize, jSize];
				for (int i = 0; i < iSize; i++)
				{
					for (int j = 0; j < jSize; j++)
					{
						enemyAid[i, j] = -1;
					}
				}
			}

			if (numberOfTurns > Router.MaxDistance) 
				return GetEnemyAid(planet, Router.MaxDistance);
			if (enemyAid[planet.PlanetID(), numberOfTurns] != -1) 
				return enemyAid[planet.PlanetID(), numberOfTurns];

			enemyAid[planet.PlanetID(), numberOfTurns] = 0;

			/*Fleets enemyFleets = FleetsGoingToPlanet(EnemyFleets(), planet);
			foreach (Fleet enemyFleet in enemyFleets)
			{
				if (enemyFleet.TurnsRemaining() > numberOfTurns) continue;
				enemyAid[planet.PlanetID(), numberOfTurns] += enemyFleet.NumShips();
			}*/

			Planets enemyPlanets = Planets();
			foreach (Planet enemyPlanet in enemyPlanets)
			{
				if (enemyPlanet.PlanetID() == planet.PlanetID()) continue;

				int distance = Distance(enemyPlanet, planet);
				if (distance > numberOfTurns) continue;

				int sendTurn = numberOfTurns - distance;

				Planet futurePlanet = PlanetFutureStatus(enemyPlanet, sendTurn);
				if (futurePlanet.Owner() < 2) continue;

				int numShips = futurePlanet.NumShips();

				//enemyCanSend
				int turnsCount = GetPlanetHolder(enemyPlanet.PlanetID()).TurnsCount;
				for (int turn = sendTurn + 1; turn < turnsCount; turn++)
				{
					futurePlanet = PlanetFutureStatus(enemyPlanet, turn);
					if (futurePlanet.Owner() < 2)
					{
						numShips = 0;
						break;
					}
					if (futurePlanet.NumShips() < numShips) numShips = futurePlanet.NumShips();
				}

				enemyAid[planet.PlanetID(), numberOfTurns] += numShips;
			}
			return enemyAid[planet.PlanetID(), numberOfTurns];
		}
Exemplo n.º 7
0
		public Planets GetClosestPlanetsToTargetBySectors(Planet target, Planets planetList)
		{
			Planets closestPlanets = new Planets(Config.MaxPlanets);
			if (planetList.Count == 0) return closestPlanets;

			//Pair: Sector value, planetID
			List<Pair<Sectors, int>> pairs = new List<Pair<Sectors, int>>();
			foreach (Sectors value in Enum.GetValues(typeof(Sectors)))
			{
				if (value == Sectors.None) continue;

				pairs.Add(new Pair<Sectors, int>(value, -1));
			}

			foreach (Planet planet in planetList)
			{
				if (planet == target) continue;
				Sectors sector = GetSector(target, planet);

				foreach (Pair<Sectors, int> pair in pairs)
				{
					if (pair.First == sector)
					{
						if (pair.Second == -1) pair.Second = planet.PlanetID();
						else if (Distance(planet, target) < Distance(pair.Second, target.PlanetID()))
							pair.Second = planet.PlanetID();
						break;
					}
				}
			}

			foreach (Pair<Sectors, int> pair in pairs)
			{
				if (pair.Second != -1)
					closestPlanets.Add(GetPlanet(pair.Second));
			}
			return closestPlanets;
		}
Exemplo n.º 8
0
		public static int Distance(Planet planet1, Planet planet2)
		{
			return Distance(planet1.PlanetID(), planet2.PlanetID());
		}
Exemplo n.º 9
0
 public int CompareNumberOfShipsLT(Planet planet1, Planet planet2)
 {
     int result = (planet1.NumShips() - planet2.NumShips());
     if (result == 0) result = planet1.PlanetID() - planet2.PlanetID();
     return result;
 }
Exemplo n.º 10
0
		public static Sectors GetSector(Planet basePlanet, Planet objectPlanet)
		{
			return GetSector(basePlanet.PlanetID(), objectPlanet.PlanetID());
		}
Exemplo n.º 11
0
		public bool IsValid(Planet source, Planet dest, int numShips)
		{
			if (source.Owner() != 1)
			{
				//Logger.Log("InValid : not my planet: source = " + source + "    Move: dest = " + dest + " num = " + numShips);
				return false;
			}
			if (source.PlanetID() == dest.PlanetID())
			{
				//Logger.Log("InValid : source = dest: source = " + source + "    Move: dest = " + dest + " num = " + numShips);
				return false;
			}
			if (numShips > source.NumShips())
			{
				//Logger.Log("InValid : > numShips: source = " + source + "    Move: dest = " + dest + " num = " + numShips);
				return false;
			}
			if (numShips > CanSendByPlanets(source, dest))
			{
				//Logger.Log("InValid : > canSend: source = " + source + "    Move: dest = " + dest + " num = " + numShips + " canSend = "  +CanSend(source));
				return false;
			}
			return true;
		}
Exemplo n.º 12
0
 public int CompareGrowsRateGT(Planet planet1, Planet planet2)
 {
     int result = (planet2.GrowthRate() - planet1.GrowthRate());
     if (result == 0) result = planet1.PlanetID() - planet2.PlanetID();
     return result;
 }
Exemplo n.º 13
0
		public Fleets FleetsGoingToPlanet(Fleets fleetList, Planet planet)
		{
			Fleets attackingFleets = new Fleets();
			foreach (Fleet fleet in fleetList)
			{
				if (fleet.DestinationPlanet() == planet.PlanetID())
				{
					attackingFleets.Add(fleet);
				}
			}
			return attackingFleets;
		}
Exemplo n.º 14
0
		public Sectors GetSector(Planet basePlanet, Planet objectPlanet)
		{
			return Router.GetSector(basePlanet.PlanetID(), objectPlanet.PlanetID());
		}
Exemplo n.º 15
0
		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;
		}
Exemplo n.º 16
0
		public PlanetHolder GetPlanetHolder(Planet planet)
		{
			return planetHolders[planet.PlanetID()];
		}
Exemplo n.º 17
0
		/*public Planets MyInvasionNeutralPlanetsUnderAttack()
		{
			Planets neutralPlanetsUnderAttack = NeutralPlanetsUnderAttack();
			Planets myInvasionNeutralPlanetsUnderAttack = new Planets(Config.MaxPlanets);
			Fleets myFleets = MyFleets();
			foreach (Fleet fleet in myFleets)
			{
				Planet planet = GetPlanet(fleet.DestinationPlanet());
				if (neutralPlanetsUnderAttack.IndexOf(planet) != -1 &&
					myInvasionNeutralPlanetsUnderAttack.IndexOf(planet) == -1)
				{
					myInvasionNeutralPlanetsUnderAttack.Add(planet);
				}
			}
			return myInvasionNeutralPlanetsUnderAttack;
		}*/

		public Planets PlanetsWithinProximityToPlanet(Planets planetList, Planet thisPlanet, int proximityTreshold)
		{
			Planets nearbyPlanets = new Planets(Config.MaxPlanets);

			foreach (Planet planet in planetList)
			{
				if (planet.PlanetID() == thisPlanet.PlanetID())
				{
					continue;
				}
				int distance = Distance(planet, thisPlanet);
				if (distance <= proximityTreshold)
				{
					nearbyPlanets.Add(planet);
				}
			}
			return nearbyPlanets;
		}