Beispiel #1
0
        public RobotCommand DoStep(IList <Robot.Common.Robot> robots, int robotToMoveIndex, Map map)
        {
            Robot.Common.Robot movingRobot = robots[robotToMoveIndex];

            //CountRobots(robots); //comment when testing

            Position pos = FindNearestFreeStation(movingRobot, map, robots);

            if (DistanceHelper.FindDistance(movingRobot.Position, pos) < 400)
            {
                if (NeedToCreateRobot(movingRobot))
                {
                    return(new CreateNewRobotCommand());
                }
            }

            stationPosition = GetMovePoint(movingRobot, map, robots);

            if (stationPosition == null)
            {
                return(null);
            }

            if (NeedToCollectEnergy(movingRobot, map))
            {
                return(new CollectEnergyCommand());
            }
            else
            {
                return(new MoveCommand()
                {
                    NewPosition = stationPosition
                });
            }
        }
        public RobotCommand DoStep(IList <Robot.Common.Robot> robots, int robotToMoveIndex, Map map)
        {
            Robot.Common.Robot movingRobot = robots[robotToMoveIndex];
            if ((movingRobot.Energy > 200) && (robots.Count < map.Stations.Count))
            {
                return(new CreateNewRobotCommand());
            }

            Position stationPosition = FindNearestFreeStation(robots[robotToMoveIndex], map, robots);

            if (stationPosition == null)
            {
                return(null);
            }

            if (stationPosition == movingRobot.Position)
            {
                return(new CollectEnergyCommand());
            }
            else
            {
                Position newPosition = stationPosition;

                int dx = Math.Sign(stationPosition.X - movingRobot.Position.X);
                int dy = Math.Sign(stationPosition.Y - movingRobot.Position.Y);
                newPosition = new Position(movingRobot.Position.X + dx, movingRobot.Position.Y + dy);

                return(new MoveCommand()
                {
                    NewPosition = newPosition
                });
            }
        }
Beispiel #3
0
        public RobotCommand DoStep(IList <Robot.Common.Robot> robots, int robotToMoveIndex, Map map)
        {
            Robot.Common.Robot movingRobot = robots[robotToMoveIndex];
            if ((movingRobot.Energy > 500) && (robots.Count < map.Stations.Count))
            {
                return(new CreateNewRobotCommand());
            }
            Position stationPosition = DistanceHelper.FindNearestFreeStation(robots[robotToMoveIndex], map, robots);

            if (stationPosition == null)
            {
                return(null);
            }
            if (stationPosition == movingRobot.Position)
            {
                return(new CollectEnergyCommand());
            }
            else
            {
                return(new MoveCommand()
                {
                    NewPosition = stationPosition
                });
            }
        }
 //Init
 public void Init(IList <Robot.Common.Robot> _robots, int _robotToMoveIndex, Map _map)
 {
     robots      = _robots;
     map         = _map;
     movingRobot = robots[_robotToMoveIndex];
     //map.Stations.CopyTo(sortedStations, 0);
     sortedStations = map.Stations.ToArray();
     sortedStations = sortedStations.OrderBy(x => Helper.FindDistance(x.Position, movingRobot.Position)).ToArray();
     myRobotsCount  = robots.Count(x => x.Owner == movingRobot.Owner);
 }
 private bool isFarmer(Robot.Common.Robot robot, Map map)
 {
     foreach (var station in map.GetNearbyResources(robot.Position, 2))
     {
         if (station.Energy >= station.RecoveryRate)
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #6
0
        public bool NeedToCollectEnergy(Robot.Common.Robot movingRobot, Map map)
        {
            foreach (EnergyStation station in map.Stations)
            {
                if (station.Position == movingRobot.Position)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #7
0
        //comment when testing
        public bool IsCellFree(Position cell, Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
        {
            //foreach (Robot.Common.Robot robot in robots) {
            //    if (robot.Owner.Name.Equals(robotOwnerName)) {
            //        if (robot.Position == cell) {
            //            return false;
            //        }
            //    }
            //}

            return(true);
        }
 public static bool IsCellFree(Position cell, Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
 {
     foreach (var robot in robots)
     {
         if (robot != movingRobot)
         {
             if (robot.Position == cell)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
 public bool IsCellFreeFromMyRobot(Position cell, Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
 {
     foreach (var robot in robots)
     {
         if (robot != movingRobot)
         {
             if (robot.Position == cell && (String.Compare(robot.Owner.Name, movingRobot.Owner.Name) == 0))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
        public List <Position> FindAllMyStation(Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
        {
            List <Position> temp = new List <Position>();

            for (int i = 0; i < listStationPosition.Count; i++)
            {
                for (int j = 0; j < robots.Count; j++)
                {
                    if (listStationPosition[i].Pos == robots[j].Position && robots[j].Owner.Name == movingRobot.Owner.Name)
                    {
                        temp.Add(listStationPosition[i].Pos);
                    }
                }
            }

            return(temp);
        }
        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);
        }
        //If it is no energy to perform the movement returns -1
        //If there is no profit from atack returns 0
        //Else returns energy profit
        private int CalculateAtackProfit(Machine robot)
        {
            int lostEnergy = utils.CalculateEnergyToMove(currentMachine.Position, robot.Position) + Constants.FIGHTING_ENERGY_LOOSING;

            if (currentMachine.Energy < lostEnergy)
            {
                return(-1);
            }

            int gotEnergy = (int)(robot.Energy * 0.3);

            if (lostEnergy >= gotEnergy)
            {
                return(0);
            }
            else
            {
                return(gotEnergy - lostEnergy);
            }
        }
Beispiel #13
0
        public Position GetMovePoint(Robot.Common.Robot movingRobot,
                                     Map map, IList <Robot.Common.Robot> robots)
        {
            movePoint = FindNearestFreeStation(movingRobot, map, robots);

            for (int i = 0; i < 10; i++)
            {
                if (DistanceHelper.FindDistance(movePoint, movingRobot.Position) <= movingRobot.Energy + minRobotEnergy)
                {
                    return(movePoint);
                }
                else
                {
                    movePoint.X = (movePoint.X + movingRobot.Position.X) / 2;
                    movePoint.Y = (movePoint.Y + movingRobot.Position.Y) / 2;
                }
            }

            return(null);
        }
Beispiel #14
0
        public RobotCommand DoStep(IList <Robot.Common.Robot> robots, int robotToMoveIndex, Map map)
        {
            /*
             * if ((movingRobot.Energy > CreateNewRobotCommand.MinEnergyToCreateNewRobot) && (robots.Count < map.Stations.Count))
             * {
             *  return new CreateNewRobotCommand();
             * }
             */


            Robot.Common.Robot movingRobot     = robots[robotToMoveIndex];
            Position           stationPosition = FindNearestFreeStation(robots[robotToMoveIndex], map, robots);

            if (stationPosition == null)
            {
                return(null);
            }

            if (stationPosition == movingRobot.Position)
            {
                return(new CollectEnergyCommand());
            }
            else
            {
                Position newPosition = stationPosition;
                int      distance    = DistanceHelper.FindDistance(stationPosition, movingRobot.Position);
                if (distance > 20)
                {
                    int dx = Math.Sign(stationPosition.X - movingRobot.Position.X) * Math.Min(Math.Abs(stationPosition.X - movingRobot.Position.X), 5);
                    int dy = Math.Sign(stationPosition.Y - movingRobot.Position.Y) * Math.Min(Math.Abs(stationPosition.Y - movingRobot.Position.Y), 5);
                    newPosition = new Position(movingRobot.Position.X + dx, movingRobot.Position.Y + dy);
                }
                return(new MoveCommand()
                {
                    NewPosition = newPosition
                });
            }
        }
        public Position FindNearEnemyStation(Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
        {
            Position stationPosition = null;

            for (int i = 0; i < listStationPosition.Count; i++)
            {
                if (IsCellFreeFromMyRobot(listStationPosition[i].Pos, movingRobot, robots))
                {
                    if (stationPosition != null)
                    {
                        if (DistanceHelper.FindDistance(movingRobot.Position, listStationPosition[i].Pos) < DistanceHelper.FindDistance(movingRobot.Position, stationPosition))
                        {
                            stationPosition = listStationPosition[i].Pos;
                        }
                    }
                    else
                    {
                        stationPosition = listStationPosition[i].Pos;
                    }
                }
            }
            return(stationPosition);
        }
        public Position FindNearAvailableStation(Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
        {
            Position stationPosition = null;

            for (int i = 0; i < li.listAim.Count; i++)
            {
                if (IsCellFreeFromMyRobot(li.listAim[i].station, movingRobot, robots) && li.listAim[i].inProgress == false)
                {
                    if (stationPosition != null)
                    {
                        if (DistanceHelper.FindDistance(movingRobot.Position, li.listAim[i].station) < DistanceHelper.FindDistance(movingRobot.Position, stationPosition))
                        {
                            stationPosition = li.listAim[i].station;
                        }
                    }
                    else
                    {
                        stationPosition = li.listAim[i].station;
                    }
                }
            }
            return(stationPosition);
        }
        public int GetAttackProfit(Rb attackedRobot, Rb myRobot = null)
        {
            if (attackedRobot == null)
                return 0;
            if (myRobot == null)
                myRobot = Current.Robot;

            var profit = -DistanceHelper.FindCost(attackedRobot.Position, myRobot.Position)*2 - Constants.EnergyUsedToAttack;
            profit += (int) (attackedRobot.Energy*Constants.EnergyStolenPercent);
            return profit;
        }
 public static bool IsStationFree(EnergyStation station, Robot.Common.Robot movingRobot,
                                  IList <Robot.Common.Robot> robots)
 {
     return(IsCellFree(station.Position, movingRobot, robots));
 }
        public RobotCommand DoStep(IList <Robot.Common.Robot> robots, int robotToMoveIndex, Map map)
        {
            if (counter3)
            {
                FindAllStation(map);
                li       = new ListAim(listStationPosition);
                counter3 = false;
            }
            if (counter % 2 == 0)
            {
                li.GetFreeAim();
            }


            Robot.Common.Robot movingRobot = robots[robotToMoveIndex];
            li.DeleteErrorAim(FindAllMyStation(movingRobot, robots));
            Position nearestFreeStation      = FindNearestFreeStation(movingRobot, map, robots);
            Position movingRobotPosition     = movingRobot.Position;
            Position nearestEnemyStation     = FindNearEnemyStation(movingRobot, robots);
            Position nearestAvailableStation = FindNearAvailableStation(movingRobot, robots);


            int x = 8;

            if (DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) <= 200 && nearestAvailableStation != null && robotCount < 200)
            {
                if (nearestFreeStation != null)
                {
                    if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) <= 7 && (movingRobot.Energy >= (counter <= x ? 300 : 450)))
                    {
                        robotCount++;
                        li.SetAim(new Aim(nearestAvailableStation, true));
                        return(new CreateNewRobotCommand()
                        {
                            NewRobotEnergy = (counter <= x ? 100 : 250)
                        });
                    }

                    else if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) < 12 && movingRobot.Energy > (counter <= x ? 350 : 500) && counter < 40)
                    {
                        robotCount++;
                        return(new CreateNewRobotCommand()
                        {
                            NewRobotEnergy = (counter <= x ? 150 : 300)
                        });
                    }
                }

                if (Math.Sqrt(DistanceHelper.FindDistance(nearestEnemyStation, movingRobotPosition)) <= 7 && movingRobot.Energy >= (counter <= x ? 400 : 500))
                {
                    robotCount++;
                    li.SetAim(new Aim(nearestAvailableStation, true));
                    return(new CreateNewRobotCommand()
                    {
                        NewRobotEnergy = (counter <= x ? 200 : 300)
                    });
                }
                else if (Math.Sqrt(DistanceHelper.FindDistance(nearestEnemyStation, movingRobotPosition)) < 12 && movingRobot.Energy > (counter <= x ? 450 : 600) && counter < 40)
                {
                    robotCount++;
                    return(new CreateNewRobotCommand()
                    {
                        NewRobotEnergy = (counter <= x ? 250 : 400)
                    });
                }
            }
            else if (DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) <= 625 && nearestAvailableStation != null && robotCount < 200 && counter < 40)
            {
                if (nearestFreeStation != null)
                {
                    if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) < 25 && movingRobot.Energy > 700 && counter <= 35)
                    {
                        robotCount++;
                        return(new CreateNewRobotCommand()
                        {
                            NewRobotEnergy = 400
                        });
                    }
                }
                else if (Math.Sqrt(DistanceHelper.FindDistance(nearestEnemyStation, movingRobotPosition)) < 20 && movingRobot.Energy > 700 && counter <= 35)
                {
                    robotCount++;
                    return(new CreateNewRobotCommand()
                    {
                        NewRobotEnergy = 450
                    });
                }
            }


            for (int i = 0; i < listStationPosition.Count; i++)
            {
                if (movingRobotPosition == listStationPosition[i].Pos)
                {
                    if (!li.IsMyStation(listStationPosition[i].Pos))
                    {
                        li.listMyStation.Add(new Aim(listStationPosition[i].Pos, true));
                        return(new CollectEnergyCommand());
                    }
                    else
                    {
                        return(new CollectEnergyCommand());
                    }
                }
            }
            if (nearestFreeStation != null)
            {
                if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) <= 12 && movingRobot.Energy - (DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) > 0)
                {
                    li.SetAim(new Aim(nearestFreeStation, true));
                    return(new MoveCommand()
                    {
                        NewPosition = nearestFreeStation
                    });
                }
            }

            if (Math.Sqrt(DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) <= 12 && movingRobot.Energy - (DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) + 50) > 0)
            {
                li.SetAim(new Aim(nearestAvailableStation, true));
                return(new MoveCommand()
                {
                    NewPosition = nearestAvailableStation
                });
            }
            if (Math.Sqrt(DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) < 20 && movingRobot.Energy >= 300)
            {
                return(new MoveCommand()
                {
                    NewPosition = GetNextPosition(movingRobotPosition, nearestAvailableStation, (movingRobot.Energy / DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) > 1 ? movingRobot.Energy / (int)DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) + 4 : 1)
                });
            }
            if (Math.Sqrt(DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) < 30 && movingRobot.Energy >= 200)
            {
                return(new MoveCommand()
                {
                    NewPosition = GetNextPosition(movingRobotPosition, nearestAvailableStation, (movingRobot.Energy / DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) > 1 ? movingRobot.Energy / (int)DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) + 1 : 1)
                });
            }
            else
            {
                return new MoveCommand()
                       {
                           NewPosition = GetNextPosition(movingRobotPosition, nearestAvailableStation, 1)
                       }
            };
        }
 public static int RobotIndexOf(Rb robot)
 {
     return RobotList.IndexOf(robot);
 }
        /// <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 UsingAnyStation(Rb robot = null)
 {
     if (robot == null)
         robot = Current.Robot;
     return
         Current.Map.Stations.Any(
             s => DistanceHelper.FindDistance(s.Position, robot.Position) <= Constants.EnergyCollectingDistance);
 }
 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;
 }
        /// <summary>
        /// Returns energy required for new myRobotPos to get to his best station
        /// </summary>
        /// <returns></returns>
        public int GetNewRobotEnergy(Position newPosition)
        {
            if (MyRobotsCount >= MaxRobotCount || RoundNumber > 40) return 0;
            var maxEnergy = Current.Robot.Energy - Constants.EnergyUsedToCreateRobot - 50;
            if (maxEnergy <= 0) return 0;

            var newRobot = new Rb {Position = newPosition, Owner = Current.Robot.Owner};

            int moveProfit, attackProfit;
            var movePosition = GetBestMovePosition(maxEnergy, out moveProfit, newRobot);
            var attackPosition = GetBestAttackPosition(maxEnergy, out attackProfit);
            var finalPosition = moveProfit > attackProfit ? movePosition : attackPosition;
            var minEnergy = int.MinValue;
            if (finalPosition != null)
                minEnergy = DistanceHelper.FindCost(newPosition, finalPosition);
            if (minEnergy == int.MinValue)
                return 0;
            return maxEnergy/2 > minEnergy + 100 ? maxEnergy : Math.Min(minEnergy + 100, maxEnergy);
        }
        /// <summary>
        /// Returns best position to move and profit
        /// </summary>
        /// <param name="maxEnergy"></param>
        /// <param name="profit"></param>
        /// <param name="robot"></param>
        /// <returns></returns>
        public Position GetBestMovePosition(int maxEnergy, out int profit, Rb robot = null)
        {
            if (robot == null)
                robot = Current.Robot;
            Position bestPosition = null;
            var bestProfit = int.MinValue;
            var posVariants = new Dictionary<Position, PositionInfo>();
            var stationsInTouch = DistanceHelper.GetNearbyStations(robot.Position, maxEnergy);

            foreach (var station in stationsInTouch)
            {
                for (var i = -Constants.EnergyCollectingDistance; i <= Constants.EnergyCollectingDistance; ++i)
                {
                    for (var j = -Constants.EnergyCollectingDistance; j <= Constants.EnergyCollectingDistance; ++j)
                    {
                        var pos = new Position(station.Position.X + i, station.Position.Y + j);
                        if(DistanceHelper.FindCost(pos, robot.Position) > maxEnergy) continue;
                        Rb attRob;
                        var stInfo = new StationInfo()
                        {
                            UsageVariant = CheckStaionUsage(station, out attRob),
                            AttackRobot = attRob
                        };
                        if(stInfo.UsageVariant == StationUseVariant.SkipOrLeave) continue;

                        if (posVariants.ContainsKey(pos))
                        {
                            posVariants[pos].NearestStations.Add(station, stInfo);
                        }
                        else
                        {
                            var posInfo = new PositionInfo
                            {
                                NearestStations = new Dictionary<EnergyStation, StationInfo> {{station, stInfo}}
                            };
                            posVariants[pos] = posInfo;
                        }
                    }
                }
            }

            foreach (var posKV in posVariants)
            {
                var pos = posKV.Key;
                var posInfo = posVariants[posKV.Key];
                var joinableStations = 0;
                var reserveStations = 0;
                posInfo.Profit = 0;
                foreach (var stationKV in posInfo.NearestStations)
                {
                    var station = stationKV.Key;
                    var stInfo = stationKV.Value;
                    posInfo.Profit += station.Energy;
                    switch (stInfo.UsageVariant)
                    {
                        case StationUseVariant.JoinOrCollect:
                            joinableStations++;
                            break;
                        case StationUseVariant.AttackOnly:
                            if (pos == stInfo.AttackRobot.Position)
                                posInfo.Profit += -50 + stInfo.AttackRobot.Energy/20;
                            else
                                posInfo.Profit -= station.Energy;
                            break;
                        case StationUseVariant.JoinReserve:
                            posInfo.Profit -= station.Energy;
                            reserveStations++;
                            break;
                    }
                }
                posInfo.Profit += (int) (posInfo.Profit*((double) joinableStations/2));
                posInfo.Profit += (int) (posInfo.Profit*((double) reserveStations/8));
                posInfo.Profit -= DistanceHelper.FindCost(robot.Position, pos);
                if (posInfo.Profit > bestProfit)
                {
                    bestProfit = posInfo.Profit;
                    bestPosition = pos;
                }
            }

            profit = bestProfit;
            return bestPosition;
        }
        /// <summary>
        /// Returns best position to attack and profit
        /// </summary>
        /// <param name="maxEnergy"></param>
        /// <param name="profit"></param>
        /// <param name="myRobot"></param>
        /// <returns></returns>
        public Position GetBestAttackPosition(int maxEnergy, out int profit, Rb myRobot = null)
        {
            if (myRobot == null)
                myRobot = Current.Robot;

            var robotsToAttack = DistanceHelper.GetNeabyRobots(myRobot.Position, maxEnergy).Where(robot => robot.Owner != myRobot.Owner);
            var bestProfit = int.MinValue;
            Position bestPosition = null;
            foreach (var robot in robotsToAttack)
            {
                var curProfit = GetAttackProfit(robot);
                if (curProfit > bestProfit)
                {
                    bestProfit = curProfit;
                    bestPosition = robot.Position;
                }
            }

            profit = bestProfit;
            return bestPosition;
        }
 private bool IsMyRobot(Machine robot) => robot.OwnerName == currentMachine.OwnerName;
Beispiel #29
0
 public bool NeedToCreateRobot(Robot.Common.Robot movingRobot)
 {
     return(movingRobot.Energy >= minEnergyToSpawn && myRobotsCount < maxRobotsCount);
 }
 public BestAlgorithmGenerator(Machine currentMachine)
 {
     this.currentMachine = currentMachine;
 }