Example #1
0
        private static void CalculateMoves(List <Planet> ownedPlanets, List <Planet> unOwnedPlanets, List <Move> moveList, GameMap map)
        {
            if (unOwnedPlanets.All(p => p.IsOwned()))
            {
                attackPhase = true;
            }

            // Fill up already owned planets first.
            var unfilledPlanet = ownedPlanets.FirstOrDefault(p => attackPhase && !p.IsFull() /* || !attackPhase && p.GetDockedShips().Count < 1/*Math.Ceiling(p.GetDockingSpots() * percentToConquer)*/);

            if (unfilledPlanet != null)
            {
                DebugLog.AddLog($"Unfilled: {unfilledPlanet.GetId()}");
                NavigateToDock(unfilledPlanet, ownedPlanets, unOwnedPlanets, moveList, map);
                return;
            }

            // Try to capture unowned planets next.
            var emptyPlanet = unOwnedPlanets.FirstOrDefault(p => attackPhase && !p.IsOwned() && !p.IsFull() /* || !attackPhase && !p.IsOwned() && p.GetDockedShips().Count < 1/*Math.Ceiling(p.GetDockingSpots() * percentToConquer)*/);

            if (emptyPlanet != null)
            {
                DebugLog.AddLog($"Empty: {emptyPlanet.GetId()}");
                NavigateToDock(emptyPlanet, ownedPlanets, unOwnedPlanets, moveList, map);
                return;
            }

            var planet = unOwnedPlanets.FirstOrDefault();

            if (planet != null)
            {
                DebugLog.AddLog($"Attack: {planet.GetId()}");
                NavigateToAttack(planet, ownedPlanets, unOwnedPlanets, moveList, map);
            }
        }
Example #2
0
 void StartDraw()
 {
     if (currentTrail)
     {
         return;
     }
     DebugLog.AddLog("StartDraw");
     currentTrail = Instantiate(trail, transform.position, transform.rotation, transform) as GameObject;
     currentTrail.GetComponent <TrailRenderer>().material.color = ColorPicker.GetCurrentColor();
 }
Example #3
0
    void EndDraw()
    {
        if (!currentTrail)
        {
            return;
        }

        savedTrails.Add(currentTrail.GetComponent <TrailRenderer>());
        currentTrail.transform.SetParent(savedTrailsObj);
        currentTrail = null;
        DebugLog.AddLog("EndDraw; " + savedTrails.Count + " trails saved");
    }
Example #4
0
        public static void Main(string[] args)
        {
            string name = args.Length > 0 ? args[0] : "T-800";

            _gameMap = Networking.Initialize(name);

            for (;;)
            {
                _moveList.Clear();
                _gameMap.UpdateMap(Networking.ReadLineIntoMetadata());
                _allPlanetsAreOwned = _gameMap.Planets.Values.All(p => p.IsOwned());
                _player             = _gameMap.GetMyPlayer();

                var ships            = _player.Ships.Values;
                var currentMissions  = ships.Where(s => _missions.ContainsKey(s.Id)).ToList();
                var obsoleteMissions = _missions.Keys.Where(k => !_player.Ships.Keys.Contains(k)).ToList();
                foreach (var k in obsoleteMissions)
                {
                    _missions.Remove(k);
                }

                foreach (var ship in currentMissions)
                {
                    try
                    {
                        UpdateMission(ship);
                    }
                    catch (Exception e)
                    {
                        DebugLog.AddLog($"UpdateMission: {e.Message}");
                    }
                }

                foreach (var ship in ships.Except(currentMissions))
                {
                    try
                    {
                        AssignMission(ship);
                    }
                    catch (Exception e)
                    {
                        DebugLog.AddLog($"AssignMission: {e.Message}");
                    }
                }

                Networking.SendMoves(_moveList);
            }
        }
Example #5
0
        private static void NavigateToDock(Planet planetToDock, List <Planet> ownedPlanets, List <Planet> unOwnedPlanets, List <Move> moveList, GameMap map, bool makeNextMove = true, Ship ship = null)
        {
            DebugLog.AddLog($"Preparing to Dock: {planetToDock.GetId()}, Open Spots: {planetToDock.GetDockingSpots() - planetToDock.GetDockedShips().Count}");
            if (ship == null)
            {
                ship = planetToDock.ClosestUnclaimedShip;
            }

            if (ship == null)
            {
                return;
            }

            if (ship.CanDock(planetToDock))
            {
                DebugLog.AddLog("Docking with planet");
                moveList.Add(new DockMove(ship, planetToDock));
            }
            else
            {
                DebugLog.AddLog("Navigating to dock");
                var move = Navigation.NavigateShipToDock(map, ship, planetToDock, Constants.MAX_SPEED);
                if (move != null)
                {
                    moveList.Add(move);
                }
            }

            planetToDock.ClaimDockingSpot(ship.GetId());

            if (makeNextMove)
            {
                ship.Claim(planetToDock);
                MakeNextMove(ownedPlanets, unOwnedPlanets, moveList, map);
            }
        }
Example #6
0
        public static void Main(string[] args)
        {
            try {
                string name = args.Length > 0 ? args[0] : "Ze-rone";

                Networking networking = new Networking();
                GameMap    gameMap    = networking.Initialize(name);

                List <Move> moveList = new List <Move>();
                int         step     = 1;
                for (;;)
                {
                    DebugLog.AddLog($"New Move: {step++}----------------------------------------------------------------------");
                    moveList.Clear();
                    gameMap.UpdateMap(Networking.ReadLineIntoMetadata());

                    List <Planet> ownedPlanets   = new List <Planet>();
                    List <Planet> unOwnedPlanets = new List <Planet>();

                    foreach (Planet planet in gameMap.GetAllPlanets().Select(kvp => kvp.Value))
                    {
                        if (planet.IsOwnedBy(gameMap.GetMyPlayerId()))
                        {
                            ownedPlanets.Add(planet);
                        }
                        else
                        {
                            unOwnedPlanets.Add(planet);
                        }
                        planet.ShipsByDistance = gameMap.NearbyEntitiesByDistance(planet).Where(e => e.Value.GetType() == typeof(Ship) && e.Value.GetOwner() == gameMap.GetMyPlayerId()).OrderBy(kvp => kvp.Key).ToList();
                    }

                    // To prevent wasted movements from ships, if a ship starts toward a planet it will continue to that planet to complete it's mission.
                    // If the planet is taken over then the bots are no longer "claimed" by it and they can redirect, this happens when we're attacking and in the
                    // constructor for the planet.
                    foreach (var kvp in Planet.ShipsClaimed)
                    {
                        var toRemove = new List <Ship>();
                        foreach (var ship in kvp.Value)
                        {
                            // Make sure the ship is still around.
                            var realShip = gameMap.GetAllShips().FirstOrDefault(s => s.GetId() == ship.GetId());
                            if (realShip == null)
                            {
                                toRemove.Add(ship);
                            }
                            else
                            {
                                // Don't try to redock docking ships.
                                if (realShip.GetDockingStatus() != Ship.DockingStatus.Undocked)
                                {
                                    continue;
                                }
                                var planet = gameMap.GetPlanet(kvp.Key);
                                // We own this planet, or it is not owned, so we must have been flying to it to dock, continue doing so.
                                if (!planet.IsOwned() || planet.IsOwnedBy(gameMap.GetMyPlayerId()))
                                {
                                    NavigateToDock(planet, null, null, moveList, gameMap, false, realShip);
                                }
                                else
                                {
                                    NavigateToAttack(planet, null, null, moveList, gameMap, false, realShip);
                                }
                                realShip.ClaimStateless();
                            }
                        }
                        foreach (var ship in toRemove)
                        {
                            kvp.Value.Remove(ship);
                        }
                    }
                    ownedPlanets.Sort(PlanetComparer);
                    unOwnedPlanets.Sort(PlanetComparer);

                    CalculateMoves(ownedPlanets, unOwnedPlanets, moveList, gameMap);

                    Networking.SendMoves(moveList);
                }
            }
            catch (Exception ex) {
                DebugLog.AddLog($"{ex.Message}: {ex.StackTrace}");
                Networking.SendMoves(new List <Move>());
            }
        }
Example #7
0
 public void OnPointerUp(PointerEventData eventData)
 {
     pressed = false;
     DebugLog.AddLog("OnPointerUp");
 }
Example #8
0
 public void OnPointerDown(PointerEventData eventData)
 {
     pressed = true;
     DebugLog.AddLog("OnPointerDown");
 }
Example #9
0
        private static void InitTurn(GameMap gameMap, List <Move> moveList, ICollection <Ship> ships)
        {
            int current      = 0;
            int totalCurrent = 0;
            var shipCount    = gameMap.GetMyPlayer().GetShips().Values.Count;
            var useableShips = ships.Where(s => s.GetDockingStatus() == Ship.DockingStatus.Undocked).ToList();
            var useableCount = useableShips.Count;
            var enemyPlanets = gameMap.GetAllPlanets().Values
                               .Where(p => p.IsOwned() && p.GetOwner() != gameMap.GetMyPlayerId()).ToList();
            var myPlanets = gameMap.GetAllPlanets().Values
                            .Where(p => p.IsOwned() && p.GetOwner() == gameMap.GetMyPlayerId()).ToList();


            if (enemyPlanets.Count <= 0 && myPlanets.Count >= 5)
            {
                DebugLog.AddLog("No enemy planets - Now Attack");
                Parallel.ForEach(useableShips, ship =>
                {
                    bool moved = false;
                    var sh     = gameMap.GetAllShips().FindNearestEnemyShip(ship);
                    ThrustMove newThrustMove = Navigation.NavigateShipToDock(gameMap, ship, sh, Constants.MAX_SPEED);
                    if (newThrustMove != null)
                    {
                        moved = true;
                        moveList.Add(newThrustMove);
                    }

                    if (!moved)
                    {
                        var planets = gameMap.GetAllPlanets().Values.OrderBy(ship.GetDistanceTo).ToList();
                        foreach (var planet in planets)
                        {
                            if (ship.CanDock(planet))
                            {
                                moved = true;
                                moveList.Add(new DockMove(ship, planet));
                                break;
                            }
                        }
                    }
                });
                return;
            }

            Parallel.ForEach(useableShips, ship =>
            {
                totalCurrent++;
                bool moved = false;
                if (ship.GetDockingStatus() != Ship.DockingStatus.Undocked)
                {
                    return;
                }
                current++;

                List <Planet> planets;

                try
                {
                    planets = gameMap.GetAllPlanets().Values.OrderBy(ship.GetDistanceTo).ThenByDescending(p => p.GetRadius()).ToList();
                }
                catch (Exception e)
                {
                    planets = gameMap.GetAllPlanets().Values.ToList();
                }

                if (current == 1 || (useableCount > 20 && current > useableCount - 4))
                {
                    if (gameMap.GetAllPlayers().Count == 2 && shipCount > 1)
                    {
                        goto ATTACK_ENEMY;
                    }
                    var nPs = gameMap.GetAllPlanets().Values.Where(p => p.GetDistanceTo(ship) <= planets[4].GetDistanceTo(ship)).OrderByDescending(p => p.GetRadius()).ToList();
                    if (nPs.Count > 0)
                    {
                        planets = nPs;
                    }
                }

                foreach (Planet planet in planets)
                {
                    if (planet.IsOwned())
                    {
                        if ( //current > 15 && (current > useableCount / 4 * 5)||
                            useableCount >= 4 ||
                            planet.GetRadius() >= 8)

                        {
                            if (planet.GetOwner() == gameMap.GetMyPlayerId() && !planet.DeservesMine())
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (ship.CanDock(planet))
                    {
                        if (!planet.IsOwned() || planet.GetOwner() == gameMap.GetMyPlayerId() || planet.GetDockedShips().Count <= 0)
                        {
                            if (!planet.IsFull())
                            {
                                moved = true;
                                moveList.Add(new DockMove(ship, planet));
                                break;
                            }
                        }

                        //var eShip = gameMap.GetShip(planet.GetOwner(),
                        //    planet.GetDockedShips()[planet.GetDockedShips().RandomPick()]);
                        var eShip = gameMap.GetAllShips().FindNearestEnemyShip(ship);
                        if (eShip != null)
                        {
                            ThrustMove attackMove = Navigation.NavigateShipToDock(gameMap, ship, eShip, Constants.MAX_SPEED);
                            if (attackMove != null)
                            {
                                moved = true;
                                moveList.Add(attackMove);
                                break;
                            }
                        }
                    }

                    var speed = myPlanets.Count > 0 ? Constants.MAX_SPEED : Helper.Random.Next(5, 8);
                    ThrustMove newThrustMove = Navigation.NavigateShipToDock(gameMap, ship, planet, speed);
                    if (newThrustMove != null)
                    {
                        moved = true;
                        moveList.Add(newThrustMove);
                    }
                    break;
                }
                ATTACK_ENEMY:
                if (!moved)
                {
                    DebugLog.AddLog("Not moved - Now Attacking Enemy");
                    //foreach (var sh in gameMap.GetAllShips().Where(s => s.GetOwner() != gameMap.GetMyPlayerId()).Take(20).OrderBy(s => s.GetDistanceTo(ship)))
                    {
                        var sh = gameMap.GetAllShips().FindNearestEnemyShip(ship);
                        ThrustMove newThrustMove = Navigation.NavigateShipToDock(gameMap, ship, sh, Constants.MAX_SPEED);
                        if (newThrustMove != null)
                        {
                            moved = true;
                            moveList.Add(newThrustMove);
                            //break;
                        }
                    }
                }

                if (!moved)
                {
                    DebugLog.AddLog("Not moved - Now Dock Self");
                    foreach (Planet planet in planets)
                    {
                        if (ship.CanDock(planet))
                        {
                            moved = true;
                            moveList.Add(new DockMove(ship, planet));
                            break;
                        }

                        ThrustMove newThrustMove = Navigation.NavigateShipToDock(gameMap, ship, planet, Constants.MAX_SPEED);
                        if (newThrustMove != null)
                        {
                            moved = true;
                            moveList.Add(newThrustMove);
                        }
                        break;
                    }
                }

                if (!moved)
                {
                    DebugLog.AddLog("Not moved - Now Random");
                    foreach (Planet planet in planets)
                    {
                        if (planet.IsOwned())
                        {
                            continue;
                        }

                        moved = true;
                        moveList.Add(new DockMove(ship, planet));
                        break;
                    }
                }
            });
        }