Esempio n. 1
0
        public void TestNearestStation()
        {
            var algorithm   = new PastukhVitaliiAlgorithm();
            var map         = new Map();
            var stationPos  = new Position(3, 2);
            var stationPos2 = new Position(5, 2);
            var station1    = new EnergyStation()
            {
                Energy = 1234, Position = stationPos, RecoveryRate = 4
            };
            var station2 = new EnergyStation()
            {
                Energy = 1454, Position = stationPos2, RecoveryRate = 3
            };

            map.Stations.Add(station1);
            map.Stations.Add(station2);

            var robots = new List <Robot.Common.Robot>()
            {
                new Robot.Common.Robot()
                {
                    Energy = 200, Position = new Position(2, 2),
                }
            };

            Assert.AreEqual(algorithm.FindNearestFreeStation(robots[0], map, robots), station1.Position);
        }
Esempio n. 2
0
        public void TimeTest()
        {
            var algorithm  = new PastukhVitaliiAlgorithm();
            var map        = new Map();
            var stationPos = new Position(5, 5);
            var station1   = new EnergyStation()
            {
                Energy = 1234, Position = stationPos, RecoveryRate = 4
            };

            map.Stations.Add(station1);
            var robots = new List <Robot.Common.Robot>()
            {
                new Robot.Common.Robot()
                {
                    Energy = 500, Position = new Position(15, 5)
                }
            };
            var time = new Stopwatch();

            time.Start();
            var testmethod = algorithm.DoStep(robots, 0, map) as MoveCommand;

            time.Stop();
            Assert.IsTrue(time.Elapsed < new TimeSpan(0, 0, 1));
        }
Esempio n. 3
0
        public void TestDoStep()
        {
            var algorithm  = new PastukhVitaliiAlgorithm();
            var map        = new Map();
            var stationPos = new Position(3, 2);
            var station1   = new EnergyStation()
            {
                Energy = 1234, Position = stationPos, RecoveryRate = 4
            };
            var stationPos1 = new Position(5, 3);
            var station2    = new EnergyStation()
            {
                Energy = 1234, Position = stationPos, RecoveryRate = 4
            };

            map.Stations.Add(station1);

            var robots = new List <Robot.Common.Robot>()
            {
                new Robot.Common.Robot()
                {
                    Energy = 8000, Position = new Position(2, 2),
                }
            };
            var x = algorithm.DoStep(robots, 0, map);

            Assert.IsTrue(x is MoveCommand);
        }
Esempio n. 4
0
 public static bool CanCollect(EnergyStation station, Position position)
 {
     if ((Math.Abs(station.Position.X - position.X) <= distance) &&
         (Math.Abs(station.Position.Y - position.Y) <= distance))
     {
         return(true);
     }
     return(false);
 }
Esempio n. 5
0
    public override ResultType DoAction()
    {
        float value  = EnergyStation.GetInstance().Execute();
        bool  isDone = _baseSprite.ReplenishEnergy(value);

        ResultType resultType = isDone ? ResultType.Success : ResultType.Running;

        return(resultType);
    }
 public bool IsStationFree(EnergyStation station)
 {
     foreach (var robot in robots)
     {
         if (robot != movingRobot && Helper.CanCollect(station, robot.Position))
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 7
0
    public static EnergyStation GetInstance()
    {
        lock (obj)
        {
            if (null == Instance)
            {
                Instance = new EnergyStation();
            }
        }

        return(Instance);
    }
        /// <summary>
        /// First algorithm
        /// </summary>
        /// <param name="robots">All robots on the map</param>
        /// <param name="robotToMoveIndex">my Robot</param>
        /// <param name="map">Map</param>
        /// <returns>Command</returns>
        public RobotCommand DoStep(IList <Robot.Common.Robot> _robots, int _robotToMoveIndex, Map _map)
        {
            Init(_robots, _robotToMoveIndex, _map);

            if ((movingRobot.Energy > Helper.EnergyToBorn) && (myRobotsCount <= 100) &&
                (myRobotsCount <= map.Stations.Count) && (Round < Helper.RoundToStop))
            {
                return new CreateNewRobotCommand()
                       {
                           NewRobotEnergy = 200
                       }
            }
            ;


            if (IsCollecting(movingRobot.Position) && IsStationFree(sortedStations[0]))
            {
                return(new CollectEnergyCommand());
            }


            EnergyStation station = FindNearestFreeStation();

            do
            {
                if (station == null)
                {
                    station = FindNearestOccupiedStation();
                }
                Position position = FindNearestFreeCellAroundStation(station);
                position = GetNextPosition(position, out int steps);
                if (steps > 5 && IsStationFree(station))
                {
                    station = null;
                    continue;
                }
                if (position == movingRobot.Position)
                {
                    return(new CollectEnergyCommand());
                }
                return(new MoveCommand()
                {
                    NewPosition = GetNextPosition(position)
                });
            }while(true);
        }
Esempio n. 9
0
    public IMove GetIMove(TargetTypeEnum targetType)
    {
        IMove iMove = null;

        if (targetType == TargetTypeEnum.ENEMY)
        {
            iMove = MoveEnemy();
        }
        else if (targetType == TargetTypeEnum.ENERY_SUPPLY)
        {
            iMove = EnergyStation.GetInstance();
        }
        else if (targetType == TargetTypeEnum.PATROL)
        {
            iMove = PatrolMove();
        }

        return(iMove);
    }
Esempio n. 10
0
        public static Position FindNearestFreeStation(Robot.Common.Robot movingRobot, Map map, IList <Robot.Common.Robot> robots)
        {
            EnergyStation nearest     = null;
            int           minDistance = int.MaxValue;

            foreach (var station in map.Stations)
            {
                if (IsStationFree(station, movingRobot, robots))
                {
                    int d = DistanceHelper.FindDistance(station.Position, movingRobot.Position);
                    if (d < minDistance)
                    {
                        minDistance = d;
                        nearest     = station;
                    }
                }
            }
            return(nearest == null ? null : nearest.Position);
        }
Esempio n. 11
0
        public void ChangePositionNextStep()
        {
            var algorithm  = new PastukhVitaliiAlgorithm();
            var map        = new Map();
            var stationPos = new Position(5, 5);
            var station1   = new EnergyStation()
            {
                Energy = 1234, Position = stationPos, RecoveryRate = 4
            };

            map.Stations.Add(station1);
            var robots = new List <Robot.Common.Robot>()
            {
                new Robot.Common.Robot()
                {
                    Energy = 500, Position = new Position(15, 5)
                }
            };
            var testmethod = algorithm.DoStep(robots, 0, map) as MoveCommand;

            Assert.AreEqual(new Position(9, 5), testmethod.NewPosition);
        }
        //Find Cell
        public Position FindNearestFreeCellAroundStation(EnergyStation station)
        {
            Position[][] positions = new Position[Helper.distance * 2 + 1][];
            for (int i = 0; i < positions.Length; ++i)
            {
                positions[i] = new Position[positions.Length];
            }
            positions[0][0] = new Position(station.Position.X - Helper.distance, station.Position.Y - Helper.distance);

            int      minDistance = int.MaxValue;
            Position result      = null;

            for (int row = 0, distance; row < positions.Length; ++row)
            {
                if (positions[row][0] == null)
                {
                    positions[row][0] = new Position(positions[0][0].X, positions[0][0].Y + row);
                }
                for (int col = 0; col < positions.Length; ++col)
                {
                    if (positions[row][col] != null || !IsCellFree(positions[row][col]))
                    {
                        continue;
                    }
                    positions[row][col] = new Position(positions[0][0].X + col, positions[0][0].Y);
                    if (!IsCellValid(positions[row][col]))
                    {
                        continue;
                    }
                    distance = Helper.FindDistance(movingRobot.Position, positions[row][col]);
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        result      = positions[row][col];
                    }
                }
            }
            return(result);
        }
        private Position GetClosestStationPosition(Map map, IList <Machine> robots)
        {
            Position      closestPosition = null;
            int           closestDistance = -1;
            EnergyStation closestStation  = null;

            foreach (EnergyStation station in map.Stations)
            {
                if (closestPosition == null)
                {
                    closestPosition = station.Position;
                    closestDistance = utils.CalculateDistance(currentMachine.Position, station.Position);
                    closestStation  = station;
                    continue;
                }

                int distance = utils.CalculateDistance(currentMachine.Position, station.Position);

                if (distance < closestDistance)
                {
                    closestPosition = station.Position;
                    closestDistance = distance;
                    closestStation  = station;
                }
            }

            var closestFreePosition = new CellSearcher(closestStation, robots).Calculate(currentMachine.Position);

            if (closestFreePosition != null)
            {
                return(closestFreePosition);
            }
            else
            {
                return(map.FindFreeCell(closestPosition, robots));
            }
        }
 public FourthQuaterCellSearcher(EnergyStation station, IList <Robot.Common.Robot> robots) : base(station, robots)
 {
 }
        /// <summary>
        /// Checks how many robots are using station and reasonability to move to it
        /// </summary>
        /// <param name="station"></param>
        /// <param name="robotToAttack"></param>
        /// <param name="myRobotIndex"></param>
        /// <returns></returns>
        public StationUseVariant CheckStaionUsage(EnergyStation station, out Rb robotToAttack, int myRobotIndex = -1)
        {
            if (myRobotIndex == -1)
                myRobotIndex = Current.RobotIndex;

            robotToAttack = null;
            var usingRobots = GetRobotsUsingStation(station);

            if (!usingRobots.Any()) return StationUseVariant.JoinOrCollect;

            var otherUsingRobots = usingRobots.Where(robot => robot.Owner != Current.Robot.Owner).ToList();
            var myUsingRobots = usingRobots.Where(robot => robot.Owner == Current.Robot.Owner).ToList();
            var myCount = myUsingRobots.Count;
            if (!otherUsingRobots.Any() && myUsingRobots.Any()) // only my robots on station
            {
                var myMinIndex = myUsingRobots.Min(robot => Current.RobotIndexOf(robot));
                if (myMinIndex == myRobotIndex) // cur has the smallest index
                {
                    return myCount == 1 ? StationUseVariant.JoinOrCollect : StationUseVariant.SkipOrLeave;
                }
                //return myMinIndex < myRobotIndex ? StationUseVariant.JoinOrCollect : StationUseVariant.JoinReserve;
                return myMinIndex < myRobotIndex ? StationUseVariant.JoinReserve : StationUseVariant.JoinReserve;
            }
            else if (otherUsingRobots.Any() && !myUsingRobots.Any()) // only other robots on station
            {
                otherUsingRobots.Sort((r1, r2) => Current.RobotIndexOf(r1).CompareTo(Current.RobotIndexOf(r2)));
                var robotCount = otherUsingRobots.Count;
                if (Current.RobotIndexOf(otherUsingRobots[0]) > myRobotIndex) // first myRobotPos index > my
                    return StationUseVariant.JoinOrCollect;
                else if (robotCount >= 2 && Current.RobotIndexOf(otherUsingRobots[1]) > myRobotIndex || robotCount < 2) // other first < my && other second > my || no second
                {
                    if (PushesOutOfStation(station, otherUsingRobots[0], Current.RobotList[myRobotIndex]))
                    {
                        robotToAttack = otherUsingRobots[0];
                        return StationUseVariant.AttackOnly;
                    }
                    else return StationUseVariant.SkipOrLeave;
                }
                else // other first < my && other second < my
                {
                    return StationUseVariant.SkipOrLeave;
                }
            }
            else // other and my robots on station
            {
                myUsingRobots.Sort((r1, r2) => Current.RobotIndexOf(r1).CompareTo(Current.RobotIndexOf(r2)));
                otherUsingRobots.Sort((r1, r2) => Current.RobotIndexOf(r1).CompareTo(Current.RobotIndexOf(r2)));
                var otherCount = otherUsingRobots.Count;

                if (Current.RobotIndexOf(myUsingRobots[0]) > Current.RobotIndexOf(otherUsingRobots[0]))
                {
                    // my first > other first
                    if (otherCount >= 2 && Current.RobotIndexOf(myUsingRobots[0]) < Current.RobotIndexOf(otherUsingRobots[1]) || otherCount < 2)
                    {
                        // my first < other second || otherCount == 1
                        if (PushesOutOfStation(station, otherUsingRobots[0], Current.RobotList[myRobotIndex]))
                        {
                            robotToAttack = otherUsingRobots[0];
                            return StationUseVariant.AttackOnly;
                        }
                        else return StationUseVariant.SkipOrLeave;
                    }
                    else if (myRobotIndex < Current.RobotIndexOf(otherUsingRobots[0]))
                    {
                        // my first > other second && cur < other first
                        return StationUseVariant.JoinOrCollect;
                    }
                    else if (myRobotIndex < Current.RobotIndexOf(otherUsingRobots[1]))
                    {
                        // cur > other first && cur < other second
                        if (PushesOutOfStation(station, otherUsingRobots[0], Current.RobotList[myRobotIndex]))
                        {
                            robotToAttack = otherUsingRobots[0];
                            return StationUseVariant.AttackOnly;
                        }
                        else return StationUseVariant.SkipOrLeave;
                    }
                    else
                    {
                        return StationUseVariant.SkipOrLeave;
                    }
                }
                else // my first < other first
                {
                    if (myRobotIndex > Current.RobotIndexOf(myUsingRobots[0]))
                    {
                        // moving > my first
                        if (myRobotIndex < Current.RobotIndexOf(otherUsingRobots[0]))
                        {
                            // moving < other first
                            //return StationUseVariant.JoinOrCollect;
                            return StationUseVariant.JoinReserve;
                        }
                        else if (otherCount >= 2 && myRobotIndex < Current.RobotIndexOf(otherUsingRobots[1]) || otherCount < 2)
                        {
                            // moving < other second || otherCount == 1
                            if (PushesOutOfStation(station, otherUsingRobots[0], Current.RobotList[myRobotIndex]))
                            {
                                robotToAttack = otherUsingRobots[0];
                                //return StationUseVariant.AttackOnly;
                                return StationUseVariant.JoinReserve;
                            }
                            else return StationUseVariant.SkipOrLeave;
                        }
                        else
                        {
                            // moving > other second
                            return StationUseVariant.SkipOrLeave;
                        }
                    }
                    else
                    {
                        // moving < my first
                        return StationUseVariant.SkipOrLeave;
                    }
                }
            }
            //return StationUseVariant.JoinOrCollect;
        }
 public bool UsesStation(EnergyStation station, Rb robot = null)
 {
     if (robot == null)
         robot = Current.Robot;
     return DistanceHelper.FindDistance(robot.Position, station.Position) <= Constants.EnergyCollectingDistance;
 }
 /// <summary>
 /// Returns true if myRobotPos pushed attackedRobot out of the station influence radius
 /// </summary>
 /// <param name="station"></param>
 /// <param name="myRobot"></param>
 /// <param name="attackedRobot"></param>
 /// <returns></returns>
 public bool PushesOutOfStation(EnergyStation station, Rb attackedRobot, Rb myRobot = null)
 {
     if (myRobot == null)
         myRobot = Current.Robot;
     if (myRobot.Owner == attackedRobot.Owner)
         return false;
     var x = 2*attackedRobot.Position.X - myRobot.Position.X;
     var y = 2*attackedRobot.Position.Y - myRobot.Position.Y;
     var newPos = Current.Map.FindFreeCell(new Position(x, y), Current.RobotList);
     return Math.Abs(newPos.X - station.Position.X) > Constants.EnergyCollectingDistance &&
         Math.Abs(newPos.Y - station.Position.Y) > Constants.EnergyCollectingDistance;
 }
Esempio n. 18
0
 private bool HasEnaughEnergy(EnergyStation Station)
 {
     return(Station.Energy >= 50);
 }
        private List<Robot.Common.Robot> IsStationOnlyMy(EnergyStation station, Robot.Common.Robot movingRobot, IList<Robot.Common.Robot> robots, Map map)
        {
            List<EnergyStation> list = new List<EnergyStation>();
            List<Robot.Common.Robot> enemies = new List<Robot.Common.Robot>();

            try
            {
                foreach (var robot in robots)
                {
                    if (robot.Owner != movingRobot.Owner)
                    {
                        list = map.GetNearbyResources(robot.Position, 2);
                        foreach (var s in list)
                            if (s == station) enemies.Add(robot);
                    }
                }
                return enemies;
            }
            catch
            {
                return enemies;
            }
        }
        private int IsStationMoreFree(EnergyStation station, Robot.Common.Robot movingRobot, IList<Robot.Common.Robot> robots, Map map)
        {
            int robotsOnStation = 0;

            foreach (var robot in robots)
            {
                var list = map.GetNearbyResources(robot.Position, 2);
                foreach (var s in list)
                    if (s == station) robotsOnStation++;
            }
            return robotsOnStation - 1;
        }
 public bool IsStationFree(EnergyStation station, Robot.Common.Robot movingRobot, IList<Robot.Common.Robot> robots)
 {
     return IsCellFree(station.Position, movingRobot, robots);
 }
 public BaseCellSearcher(EnergyStation station, IList <Robot.Common.Robot> robots)
 {
     this.station = station;
     this.robots  = robots;
 }
 public List<Rb> GetRobotsUsingStation(EnergyStation station)
 {
     var stations = new List<EnergyStation>() { station };
     return GetRobotsUsingStations(stations, false).ToList();
 }
Esempio n. 24
0
 private void AddEntityFromFormat(int typeId, string entityData)
 {
     if (typeId == (int)EntityType.Player)
     {
         Character character = new Character(entityData);
         zoneManager.AddEntity(character);
         Logger.Output(this, "AddEntity() added new character ID {0}.", character.Id);
     }
     else if (typeId == (int)EntityType.Unit)
     {
         Unit entity = new Unit(entityData);
         SpriteUnit sprite = new SpriteUnit(entity, TextureManager.Singletone.Get("Windmill"));
         entity.Tag = sprite;
         zoneManager.AddEntity(entity);
         Logger.Output(this, "AddEntity() added new unit ID {0}.", entity.Id);
     }
     else if (typeId == (int)EntityType.EnergyStation)
     {
         EnergyStation station = new EnergyStation(entityData);
         SpriteEnergyStation sprite = new SpriteEnergyStation(station, TextureManager.Singletone.Get("EnergyStation"));
         station.Tag = sprite;
         zoneManager.AddEntity(station);
         Logger.Output(this, "AddEntity() added new energy station ID {0}.", station.Id);
     }
     else if (typeId == (int)EntityType.EnergyRelay)
     {
         EnergyRelay relay = new EnergyRelay(entityData);
         SpriteEnergyRelay sprite = new SpriteEnergyRelay(relay, TextureManager.Singletone.Get("EnergyRelay"));
         relay.Tag = sprite;
         zoneManager.AddEntity(relay);
         Logger.Output(this, "AddEntity() added new energy relay ID {0}.", relay.Id);
     }
     else if (typeId == (int)EntityType.MineralMiner)
     {
         MineralMiner miner = new MineralMiner(entityData);
         SpriteMineralMiner sprite = new SpriteMineralMiner(miner, TextureManager.Singletone.Get("MineralMiner"));
         miner.Tag = sprite;
         zoneManager.AddEntity(miner);
         Logger.Output(this, "AddEntity() added new mineral miner ID {0}.", miner.Id);
     }
     else if (typeId == (int)EntityType.BasicLaser)
     {
         BasicLaser laser = new BasicLaser(entityData);
         zoneManager.AddEntity(laser);
         Logger.Output(this, "AddEntity() added new basic laser ID {0}.", laser.Id);
     }
     else if (typeId == (int)EntityType.PulseLaser)
     {
         PulseLaser laser = new PulseLaser(entityData);
         zoneManager.AddEntity(laser);
         Logger.Output(this, "AddEntity() added new pulse laser ID {0}.", laser.Id);
     }
     else if (typeId == (int)EntityType.TacticalLaser)
     {
         TacticalLaser laser = new TacticalLaser(entityData);
         zoneManager.AddEntity(laser);
         Logger.Output(this, "AddEntity() added new tactical laser ID {0}.", laser.Id);
     }
     else if (typeId == (int)EntityType.Asteroid)
     {
         Asteroid asteroid = new Asteroid(entityData);
         SpriteAsteroid sprite = new SpriteAsteroid(asteroid, TextureManager.Singletone.Get("Asteroid1"));
         asteroid.Tag = sprite;
         zoneManager.AddEntity(asteroid);
         Logger.Output(this, "AddEntity() added new asteroid ID {0}.", asteroid.Id);
     }
     else
     {
         Entity entity = new Entity(entityData);
         zoneManager.AddEntity(entity);
         Logger.Output(this, "AddEntity() added unknown entity type ID {0}.", entity.Id);
     }
 }
Esempio n. 25
0
 public static bool IsStationFree(EnergyStation station, Robot.Common.Robot movingRobot,
                                  IList <Robot.Common.Robot> robots)
 {
     return(IsCellFree(station.Position, movingRobot, robots));
 }
 public static string Format(this EnergyStation station) => $"Pos. [{station.Position.X};{station.Position.Y}] - en. {station.Energy}";