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)); } }
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))); } }
private ACommand repairProcess(RepairCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield) { Battlefield battlefield = robotAndBattlefield.BATTLEFIELD; Repairman repairman = robotAndBattlefield.ROBOT as Repairman; if (repairman == null) { return(new ErrorCommand(robotAndBattlefield.ROBOT.ROBOT_TYPE + " cannot use repair command.")); } if (repairman.RepairToolUsed < repairman.RepairTool.MAX_USAGES && repairman.HitPoints > 0) { foreach (BattlefieldRobot robot in battlefield.robots) { if (robot.HitPoints > 0) { double distance = EuclideanSpaceUtils.Distance(robot.Position, repairman.Position); if (distance < command.MAX_DISTANCE) { Zone zone = Zone.GetZoneByDistance(repairman.RepairTool.ZONES, distance); robot.HitPoints += zone.EFFECT; robot.HitPoints = Math.Min(robot.HitPoints, robot.Armor.MAX_HP); if (repairman != robot) { repairman.Score += zone.EFFECT; } } battlefield.battlefieldTurn.AddRepair(new ViewerLibrary.Repair(robot.X, robot.Y)); } } repairman.RepairToolUsed++; return(new RepairAnswerCommand(true)); } else { return(new RepairAnswerCommand(false)); } }
public ACommand waitProcess(WaitCommand command, Battlefield.RobotAndBattlefield robotAndBattlefield) { return(null); }