private ACommand detonateMineProcess(DetonateMineCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield)
        {
            BattlefieldRobot robot       = robotAndBattlefield.ROBOT;
            Battlefield      battlefield = robotAndBattlefield.BATTLEFIELD;
            MineLayer        mineLayer   = robot as MineLayer;

            if (mineLayer == null)
            {
                return(new ErrorCommand(robot.ROBOT_TYPE + " cannot use detonate mine command."));
            }

            Mine mine;

            if (mineLayer.MINES_BY_ID.TryGetValue(command.MINE_ID, out mine) && mineLayer.HitPoints > 0)
            {
                mineLayer.MINES_BY_ID.Remove(command.MINE_ID);
                lock (battlefield.detonatedMines) {
                    battlefield.detonatedMines.Add(mine);
                }
                return(new DetonateMineAnswerCommand(true));
            }
            else
            {
                return(new DetonateMineAnswerCommand(false));
            }
        }
        private ACommand putMineProcess(PutMineCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield)
        {
            BattlefieldRobot robot = robotAndBattlefield.ROBOT;

            MineLayer mineLayer = robot as MineLayer;

            if (mineLayer == null)
            {
                return(new ErrorCommand(robot.ROBOT_TYPE + " cannot use put mine command."));
            }

            if (mineLayer.MinesNow < mineLayer.MineGun.MAX_MINES && mineLayer.HitPoints > 0)
            {
                int id = PutMineAnswerCommand.FALSE_MINE_ID + 1;
                while (mineLayer.MINES_BY_ID.ContainsKey(id))
                {
                    id++;
                }
                mineLayer.MINES_BY_ID.Add(id, new Mine(id, mineLayer.X, mineLayer.Y, mineLayer));
                return(new PutMineAnswerCommand(true, id));
            }
            else
            {
                return(new PutMineAnswerCommand(false, PutMineAnswerCommand.FALSE_MINE_ID));
            }
        }
        public ACommand scanProcess(ScanCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield)
        {
            double           minDistance = Battlefield.ARENA_MAX_SIZE * 10;
            BattlefieldRobot robot       = robotAndBattlefield.ROBOT;
            Battlefield      battlefield = robotAndBattlefield.BATTLEFIELD;

            BattlefieldRobot minTarget = robot;

            if (robot.HitPoints > 0)
            {
                foreach (BattlefieldRobot target in battlefield.robots)
                {
                    if (robot.ID != target.ID && target.HitPoints > 0 && battlefield.obstacleManager.CanScan(battlefield.turn, robot.X, robot.Y, target.X, target.Y))
                    {
                        double distance = EuclideanSpaceUtils.Distance(robot.X, robot.Y, target.X, target.Y);
                        if (distance < minDistance)
                        {
                            double degree = AngleUtils.NormalizeDegree(AngleUtils.AngleDegree(robot.X, robot.Y, target.X, target.Y));
                            if (Math.Abs(degree - command.ANGLE) <= command.PRECISION)
                            {
                                minDistance = distance;
                                minTarget   = target;
                            }
                        }
                    }
                }

                battlefield.battlefieldTurn.AddScan(new Scan(command.ANGLE, command.PRECISION, minDistance, robot.X, robot.Y));
            }
            return(new ScanAnswerCommand(minDistance, minTarget.ID));
        }
        private ACommand merchantProcess(MerchantCommand visitor, Battlefield.RobotAndBattlefield robotAndBattlefield)
        {
            BattlefieldRobot robot       = robotAndBattlefield.ROBOT;
            Battlefield      battlefield = robotAndBattlefield.BATTLEFIELD;

            if (battlefield._battlefieldState == Battlefield.BattlefieldState.MERCHANT)
            {
                return(battlefield.merchant.Buy(robot, visitor.MOTOR_ID, visitor.ARMOR_ID, visitor.CLASS_EQUIPMENT_ID, visitor.REPAIR_HP));
            }
            else
            {
                return(new ErrorCommand("Cannot use MerchantCommand in state " + battlefield._battlefieldState));
            }
        }
        private ACommand shootProcess(ShootCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield)
        {
            BattlefieldRobot robot       = robotAndBattlefield.ROBOT;
            Battlefield      battlefield = robotAndBattlefield.BATTLEFIELD;

            Tank tank = robot as Tank;

            if (tank == null)
            {
                return(new ErrorCommand(robot.ROBOT_TYPE + " cannot use shot command."));
            }

            if (tank.GunsToLoad < tank.Gun.BARREL_NUMBER && tank.HitPoints > 0)
            {
                double range = command.RANGE;
                if (range > tank.Gun.MAX_RANGE)
                {
                    range = tank.Gun.MAX_RANGE;
                }
                double toX = range * Math.Cos(AngleUtils.ToRads(command.ANGLE)) + tank.X;
                double toY = range * Math.Sin(AngleUtils.ToRads(command.ANGLE)) + tank.Y;
                battlefield.obstacleManager.ShotChange(battlefield.turn, tank.X, tank.Y, ref toX, ref toY);
                int toLap = (int)Math.Ceiling(range / tank.Gun.SHOT_SPEED) + battlefield.turn;
                lock (battlefield.heapBullet) {
                    if (!battlefield.heapBullet.TryGetValue(toLap, out List <Bullet> bulletList))
                    {
                        bulletList = new List <Bullet>();
                        battlefield.heapBullet.Add(toLap, bulletList);
                    }

                    bulletList.Add(new Bullet(battlefield.turn, toLap, toX, toY, tank));
                }
                int loadAtTurn = battlefield.turn + RELOAD_TIME;
                lock (battlefield.gunLoaded) {
                    if (!battlefield.gunLoaded.TryGetValue(loadAtTurn, out List <Tank> list))
                    {
                        list = new List <Tank>();
                        battlefield.gunLoaded.Add(loadAtTurn, list);
                    }
                    list.Add(tank);
                }

                tank.GunsToLoad++;
                return(new ShootAnswerCommand(true));
            }
            else
            {
                return(new ShootAnswerCommand(false));
            }
        }
        protected void initRobot(BattlefieldRobot robot)
        {
            robot.Motor     = Motors[0];
            robot.Armor     = Armors[0];
            robot.HitPoints = robot.Armor.MAX_HP;

            switch (robot.ROBOT_TYPE)
            {
            case RobotType.MINE_LAYER:
                MineLayer mineLayer = robot as MineLayer;
                if (mineLayer != null)
                {
                    mineLayer.MineGun  = MineGuns[0];
                    mineLayer.MinesNow = 0;
                    mineLayer.MINES_BY_ID.Clear();
                }
                break;

            case RobotType.TANK:
                Tank tank = robot as Tank;
                if (tank != null)
                {
                    tank.Gun        = Guns[0];
                    tank.GunsToLoad = 0;
                }
                break;

            case RobotType.REPAIRMAN:
                Repairman repairman = robot as Repairman;
                if (repairman != null)
                {
                    repairman.RepairTool     = RepairTools[0];
                    repairman.RepairToolUsed = 0;
                }
                break;

            default:
                throw new NotSupportedException("Unsupported type of robot.");
            }

            Point position = obstacleManager.StartRobotPosition(ARENA_MAX_SIZE, ARENA_MAX_SIZE);

            robot.X = position.X;
            robot.Y = position.Y;
        }
        private ACommand driveProcess(DriveCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield)
        {
            BattlefieldRobot robot = robotAndBattlefield.ROBOT;

            if (robot.HitPoints > 0)
            {
                if (robot.Power <= robot.Motor.ROTATE_IN)
                {
                    robot.AngleDrive = command.ANGLE;
                }
                robot.WantedPower = command.POWER > 100 ? 100 : (command.POWER < 0 ? 0 : command.POWER);
                return(new DriveAnswerCommand(robot.AngleDrive.DEquals(command.ANGLE)));
            }
            else
            {
                return(new DriveAnswerCommand(robot.AngleDrive.DEquals(command.ANGLE)));
            }
        }
Exemplo n.º 8
0
 public RobotAndBattlefield(BattlefieldRobot robot, Battlefield battlefield)
 {
     ROBOT       = robot;
     BATTLEFIELD = battlefield;
 }