static void runAttacker(RobotController rc, int squad) throws GameActionException { Team team = rc.getTeam(); Team enemy = team.opponent(); int squadInfo = rc.readBroadcast(squad); MapLocation target = Conversion.intToMapLocation(squadInfo); MapLocation curr = rc.getLocation(); Robot[] allies = rc.senseNearbyGameObjects(Robot.class, rc.getType().attackRadiusMaxSquared * 2, team); // First steps away from home HQ if (curr.distanceSquaredTo(rc.senseHQLocation()) < 25) Move.tryToMove(rc); // attack! Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, rc.getType().sensorRadiusSquared * 2, enemy); MapLocation eloc = Attack.nearestEnemyLoc(rc, enemyRobots, rc.getLocation()); if (eloc != null) { if (rc.isActive()) Move.moveToward(rc, eloc); if (rc.isActive() && rc.canAttackSquare(eloc)) rc.attackSquare(eloc); } // Go to right place if (curr.distanceSquaredTo(target) > 7) { // System.out.println(target + " target " + allies.length + "ally length"); Move.moveTo(rc, target); } }
public static void run(RobotController theRC) { rc = theRC; // These don't change so get them once and use the local variable (performance) myType = rc.getType(); myTeam = rc.getTeam(); enemyTeam = myTeam.opponent(); myHQ = rc.senseHQLocation(); myLoc = rc.getLocation(); senseRange = myType.sensorRadiusSquared; attackRange = myType.attackRadiusSquared; maxRounds = rc.getRoundLimit(); if (myType == RobotType.MISSILE) runMissile(); if (myType.canMove()) { bfs = new Bfs(rc); // We need to check the breadth first search results to move optimally } threats = new Threats(rc); if (myType == RobotType.HQ) runHQ(); else if (myType == RobotType.TOWER) runTower(); else if (myType.isBuilding) runBuilding(); else if (myType.canBuild()) runBeaver(); else if (myType.canMine()) runMiner(); // Includes Beavers else if (myType == RobotType.DRONE) runDrone(); else if (myType.canAttack() || myType == RobotType.LAUNCHER) runCombat(); else runOther(); }
static void init(RobotController rc) { int roundLimit = rc.getRoundLimit(); Common.rc = rc; rand = new Random(rc.getID()); id = rc.getID(); myTeam = rc.getTeam(); enemyTeam = myTeam.opponent(); history = new MapLocation[roundLimit]; robotType = rc.getType(); enrollment = rc.getRoundNum(); if (robotType != RobotType.ARCHON) birthday = enrollment - robotType.buildTurns - BUILD_LAG; hometown = rc.getLocation(); sightRadius = robotType.sensorRadiusSquared; straightSight = (int) Math.sqrt(sightRadius); canMessageSignal = robotType.canMessageSignal(); Signals.buildTarget = new MapLocation[roundLimit]; Signals.buildStrategy = new SignalStrategy[roundLimit]; try { addInfo(rc.senseRobot(id)); myArchonHometowns = rc.getInitialArchonLocations(myTeam); enemyArchonHometowns = rc.getInitialArchonLocations(enemyTeam); int coordinates[] = new int[MAP_MAX]; int x = 0; int y = 0; for (int i = enemyArchonHometowns.length - 1; i >= 0; --i) { MapLocation loc = enemyArchonHometowns[i]; twiceCenterX += loc.x; twiceCenterY += loc.y; coordinates[loc.y] *= MAP_MAX; coordinates[loc.y] += loc.x + 1; } for (int i = 0; i < myArchonHometowns.length; ++i) { MapLocation loc = myArchonHometowns[i]; twiceCenterX += loc.x; twiceCenterY += loc.y; x += loc.x; y += loc.y; } twiceCenterX /= myArchonHometowns.length; twiceCenterY /= myArchonHometowns.length; x /= myArchonHometowns.length; y /= myArchonHometowns.length; for (int i = 0; i < myArchonHometowns.length; ++i) { MapLocation loc = myArchonHometowns[i]; int xCoord = coordinates[loc.y] - 1; coordinates[loc.y] /= MAP_MAX; if (loc.x != twiceCenterX - xCoord) rotation = true; } Archon.center = new MapLocation(x, y); myBase = new MapLocation(twiceCenterX / 2, twiceCenterY / 2).directionTo(Archon.center); enemyBase = myBase.opposite(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } }
public Robot(RobotController rc) { this.rc = rc; rand = new Random(rc.getID()); team = rc.getTeam(); enemy = team.opponent(); currentLocation = rc.getLocation(); RobotType type = rc.getType(); senseRadius = type.sensorRadiusSquared; attackRadius = type.attackRadiusSquared; }
public RobotRunner(RobotController rcin) { this.rc = rcin; randall = new Random(rc.getID()); myTeam = rc.getTeam(); enemyTeam = myTeam.opponent(); eden = rc.getInitialArchonLocations(myTeam)[0]; memory = new Information(); // Everybody has a brain robotSenseRadius = (int) Math.sqrt(rc.getType().sensorRadiusSquared); marco = new Move(); enigma = new MessageHash(); }
private static void moveAwayFromEnemies(RobotController rc) throws GameActionException { // TODO might want a different value for how far you sense enemies and // do a "center of mass" of enemies weighted by their attack values then // move away from that? RobotInfo[] enemies = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, myTeam.opponent()); for (int i = 0; i < enemies.length; i++) { if (rc.isCoreReady()) { Direction awayFrom = rc.getLocation().directionTo(enemies[0].location).opposite(); moveTowards(rc, awayFrom); } } }
static void runDefender(RobotController rc, int squad) throws GameActionException { Team team = rc.getTeam(); Team enemy = team.opponent(); int squadInfo = rc.readBroadcast(squad); MapLocation target = Conversion.intToMapLocation(squadInfo); MapLocation curr = rc.getLocation(); int status = rc.readBroadcast(squad + 1); int PASTRstatus = Channels.NTPASTRDecoding(status)[1]; int NTstatus = Channels.NTPASTRDecoding(status)[0]; Robot[] allies = rc.senseNearbyGameObjects(Robot.class, rc.getType().attackRadiusMaxSquared * 2, team); // Create a PASTR/NT if not already there // System.out.println(allies.length + " " + rc.readBroadcast(Channels.numAlliesNeededChannel)); if (allies.length >= rc.readBroadcast(Channels.numAlliesNeededChannel) && curr.distanceSquaredTo(target) < distanceThreshold && rc.isActive()) { if (PASTRstatus == 0) { rc.construct(RobotType.PASTR); rc.broadcast(squad + 1, Channels.NTPASTREncoding(NTstatus, 1)); System.out.println("Constructing a PASTR..."); } else if (NTstatus == 0) { rc.construct(RobotType.NOISETOWER); rc.broadcast(squad + 1, Channels.NTPASTREncoding(1, PASTRstatus)); System.out.println("Constructing a NT..."); } } // Then go to right place if (curr.distanceSquaredTo(target) > 8) Move.moveTo(rc, target); // Then attack! Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, rc.getType().sensorRadiusSquared * 2, enemy); MapLocation eloc = Attack.nearestEnemyLoc(rc, enemyRobots, rc.getLocation()); if (eloc != null) { if (rc.isActive()) Move.moveToward(rc, eloc); if (rc.isActive() && rc.canAttackSquare(eloc)) rc.attackSquare(eloc); } // If there is a pastr and noisetower, don't block them! if (PASTRstatus == 1 && NTstatus == 1) { if (enemyRobots.length == 0 && rc.senseCowsAtLocation(rc.getLocation()) > 30) { Move.tryToSneak(rc); } } }
public static void init(RobotController the_rc) { rc = the_rc; friendly = rc.getTeam(); enemy = friendly.opponent(); neutral = Team.NEUTRAL; zombie = Team.ZOMBIE; current_location = rc.getLocation(); my_type = rc.getType(); byte_code_limiter = my_type.bytecodeLimit; }
protected static void init(RobotController theRC) throws GameActionException { rc = theRC; us = rc.getTeam(); them = us.opponent(); ourHQ = rc.senseHQLocation(); theirHQ = rc.senseEnemyHQLocation(); mapWidth = rc.getMapWidth(); mapHeight = rc.getMapHeight(); FastRandom.init(); MessageBoard.init(theRC); Bfs.init(theRC); }
private double[] getEnemies2Or3StepsAwaySquare(MapLocation square, Team squareTeam) throws GameActionException { double count1 = 0; double count2 = 0; double count3 = 0; Robot[] enemiesInVision = rc.senseNearbyGameObjects(Robot.class, square, 18, squareTeam.opponent()); for (int i = enemiesInVision.length; --i >= 0; ) { Robot enemy = enemiesInVision[i]; RobotInfo rinfo = rc.senseRobotInfo(enemy); int dist = rinfo.location.distanceSquaredTo(square); if (rinfo.type == RobotType.SOLDIER && rinfo.roundsUntilMovementIdle < 3) { if (dist <= 2) { count1++; } else if (dist <= 8) { count2++; } else if (dist <= 14 || dist == 18) { count3++; } } else { if (dist <= 2) { count1 += 0.2; } else if (dist <= 8) { count2 += 0.2; } else if (dist <= 14 || dist == 18) { count3 += 0.2; } } } int selfDist = square.distanceSquaredTo(rc.getLocation()); if (selfDist <= 2) { count1++; } else if (selfDist <= 8) { count2++; } else if (selfDist <= 14 || selfDist == 18) { count3++; } double[] output = {count1, count2, count3}; return output; }
public static void initializeGameVars(RobotController rc) throws GameActionException { hq = rc; team = hq.getTeam(); enemy = team.opponent(); cowDensMap = hq.senseCowGrowth(); mapY = cowDensMap.length; mapX = cowDensMap[0].length; idealNumPastures = computeNumPastures(); enemyHQ = rc.senseEnemyHQLocation(); teamHQ = rc.senseHQLocation(); createTerrainMap(); desiredPASTRs = findPastureLocs(); // System.out.println("Desired pastures : " + Arrays.deepToString(desiredPASTRs)); initializerRun = true; rush = startRush(rc); rand = new Random(17); }
private double[][] enemyActivites(MapLocation square, Team squareTeam) throws GameActionException { double acount1 = 0; double acount2 = 0; double acount3 = 0; double icount1 = 0; double icount2 = 0; double icount3 = 0; Robot[] enemiesInVision = rc.senseNearbyGameObjects(Robot.class, 18, squareTeam.opponent()); for (int i = enemiesInVision.length; --i >= 0; ) { Robot enemy = enemiesInVision[i]; if (rc.senseRobotInfo(enemy).roundsUntilMovementIdle == 0) { RobotInfo rinfo = rc.senseRobotInfo(enemy); int dist = rinfo.location.distanceSquaredTo(currentLocation); if (dist <= 2) { acount1++; } else if (dist <= 8) { acount2++; } else if (dist > 8 && (dist <= 14 || dist == 18)) { acount3++; } } else { RobotInfo rinfo = rc.senseRobotInfo(enemy); int dist = rinfo.location.distanceSquaredTo(currentLocation); if (dist <= 2) { icount1++; } else if (dist <= 8) { icount2++; } else if (dist > 8 && (dist <= 14 || dist == 18)) { icount3++; } } } double[][] output = {{acount1, icount1}, {acount2, icount2}, {acount3, icount3}}; return output; }
public static void run(RobotController rc) { Random rand = new Random(rc.getID()); Team myTeam = rc.getTeam(); Team enemyTeam = myTeam.opponent(); int numFriendly = 0; RobotInfo[] adjNeutralRobots = rc.senseNearbyRobots(2, Team.NEUTRAL); // if useTurrets = true, then use turret strategy boolean useTurrets = false; try { // Any code here gets executed exactly once at the beginning of the game. RobotInfo[] nearbyArchons = rc.senseNearbyRobots(RobotType.ARCHON.sensorRadiusSquared, rc.getTeam()); if (nearbyArchons.length >= 2) { useTurrets = true; } // rc.setIndicatorString(1, "newString"); } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { if (useTurrets) { boolean escape = false; // if (rc.isCoreReady()) { // int numAdjTurrets = 0; // for (RobotInfo f : friendlyAdjacent) { // if (f.type == RobotType.TURRET) { // numAdjTurrets++; // } // } // if (numAdjTurrets < 3) { // escape = Movement.moveAwayFromEnemy(rc); // } // } // if (rc.isCoreReady()) { // escape = Movement.moveAwayFromEnemy(rc); // } if (!escape) { if (adjNeutralRobots.length > 0) { // if there is a neutral robot adjacent, activate it or wait until there's no core // delay if (rc.isCoreReady()) { rc.activate(adjNeutralRobots[0].location); } } // careful- moving to parts might get into enemy turret range if (Movement.getToAdjParts(rc)) { } else { boolean toheal = false; // repair a nearby friendly robot if (rc.isWeaponReady()) { RobotInfo[] friendlyWithinRange = rc.senseNearbyRobots(24, myTeam); numFriendly = friendlyWithinRange.length; if (friendlyWithinRange.length > 0) { RobotInfo toRepair = friendlyWithinRange[0]; for (RobotInfo r : friendlyWithinRange) { if ((r.health < toRepair.health) && (r.type != RobotType.ARCHON) && (r.maxHealth - r.health > 1)) { toRepair = r; } } if ((toRepair.maxHealth - toRepair.health > 1) && (toRepair.type != RobotType.ARCHON)) { toheal = true; rc.repair(toRepair.location); } } } if (toheal == false && rc.isCoreReady()) { // did not heal any robots // sense all the hostile robots within the archon's radius MapLocation myLoc = rc.getLocation(); RobotInfo[] hostileWithinRange = rc.senseHostileRobots(myLoc, RobotType.ARCHON.sensorRadiusSquared); RobotInfo closestRobot = null; int closestDistance = 0; // get the furthest robot from the scout for (RobotInfo r : hostileWithinRange) { if (r.location.distanceSquaredTo(myLoc) > closestDistance) { closestRobot = r; closestDistance = r.location.distanceSquaredTo(myLoc); } } // if there is such an enemy, signal it to range 8 if (closestRobot != null) { try { // this signaling is only effective against non turret enemies rc.broadcastMessageSignal(closestRobot.location.x, closestRobot.location.y, 8); } catch (GameActionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } int turnNum = rc.getRoundNum(); int numVeryCloseScouts = 0; int numNearbyTurrets = 0; RobotInfo[] friendlyNearby = rc.senseNearbyRobots(RobotType.ARCHON.sensorRadiusSquared, myTeam); for (RobotInfo f : friendlyNearby) { if (f.type == RobotType.TURRET) { numNearbyTurrets++; } } // for sensing if there are guards within range 24 RobotInfo[] friendlyClose = rc.senseNearbyRobots(24, myTeam); int numNearbyGuards = 0; for (RobotInfo f : friendlyClose) { if (f.type == RobotType.GUARD) { numNearbyGuards++; } } // check for scouts; how close should they be???? RobotInfo[] friendlyVeryClose = rc.senseNearbyRobots(15, myTeam); for (RobotInfo f : friendlyClose) { if (f.type == RobotType.SCOUT) { numVeryCloseScouts++; } } if (rc.hasBuildRequirements(RobotType.GUARD) && rc.isCoreReady() && numNearbyGuards < 1) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.GUARD)) { rc.build(dirToBuild, RobotType.GUARD); break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } // if there are <1 turrets next to archon, build asap if (rc.hasBuildRequirements(RobotType.TURRET) && rc.isCoreReady() && numNearbyTurrets < 1) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(4) * 2]; for (int i = 0; i < 4; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.TURRET)) { rc.build(dirToBuild, RobotType.TURRET); turretCount++; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); dirToBuild = dirToBuild.rotateLeft(); } } } // if there are <1 scout next to archon and 1 turret, build scout asap if (rc.hasBuildRequirements(RobotType.SCOUT) && rc.isCoreReady() && numNearbyTurrets > 0 && numVeryCloseScouts == 0 && turnNum < 400) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.SCOUT)) { rc.build(dirToBuild, RobotType.SCOUT); scoutCount++; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } // build turret every 100 turns until turn 400 // if (turnNum > 1 && turnNum < 400) { // if (turnNum % 100 == 85 && rc.isCoreReady()) { // Direction dirToBuild = RobotPlayer.directions[rand.nextInt(4)*2]; // for (int i = 0; i < 4; i++) { // // If possible, build in this direction // if (rc.canBuild(dirToBuild, RobotType.TURRET)) { // rc.build(dirToBuild, RobotType.TURRET); // turretCount++; // break; // } else { // // Rotate the direction to try // dirToBuild = dirToBuild.rotateLeft(); // dirToBuild = dirToBuild.rotateLeft(); // } // } // } // } else { // Check if this ARCHON's core is ready if (rc.isCoreReady()) { boolean built = false; RobotType typeToBuild = RobotType.TURRET; if (scoutCount < turretCount / 5) { typeToBuild = RobotType.SCOUT; } // never build scouts after a certain turn if (turnNum < 1500) { typeToBuild = RobotType.TURRET; } // Check for sufficient parts if (rc.hasBuildRequirements(typeToBuild)) { // Choose a random direction to try to build in; NESW for turrets; all 8 for // scouts if (typeToBuild.equals(RobotType.TURRET)) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(4) * 2]; for (int i = 0; i < 4; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.TURRET)) { rc.build(dirToBuild, RobotType.TURRET); turretCount++; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); dirToBuild = dirToBuild.rotateLeft(); } } } else { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.SCOUT)) { rc.build(dirToBuild, RobotType.SCOUT); scoutCount++; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } } // only move around if there are resources if ((!built) && rc.hasBuildRequirements(RobotType.TURRET) && (rc.isCoreReady())) { // don't move into enemy turret range if scout sends signal about it Set<Direction> dangerousDirs = new HashSet<>(); Signal currentSignal = rc.readSignal(); while (currentSignal != null) { int messageX = currentSignal.getMessage()[0]; int messageY = currentSignal.getMessage()[1]; // if signal message > 80000, then the message is signaling a turret // location if (messageX > 80000) { messageX = messageX - 100000; messageY = messageY - 100000; MapLocation enemyTurretLoc = new MapLocation(messageX, messageY); Direction dirToEnemyTurret = myLoc.directionTo(enemyTurretLoc); Direction dirToEnemyTurretL = myLoc.directionTo(enemyTurretLoc).rotateLeft(); Direction dirToEnemyTurretR = myLoc.directionTo(enemyTurretLoc).rotateRight(); if (myLoc.add(dirToEnemyTurret).distanceSquaredTo(enemyTurretLoc) <= 48) { dangerousDirs.add(dirToEnemyTurret); } if (myLoc.add(dirToEnemyTurretL).distanceSquaredTo(enemyTurretLoc) <= 48) { dangerousDirs.add(dirToEnemyTurretL); } if (myLoc.add(dirToEnemyTurretR).distanceSquaredTo(enemyTurretLoc) <= 48) { dangerousDirs.add(dirToEnemyTurretR); } } currentSignal = rc.readSignal(); } Direction dirToMove = RobotPlayer.directions[(rand.nextInt(4) * 2) + 1]; for (int i = 0; i < 4; i++) { if (rc.canMove(dirToMove) && !dangerousDirs.contains(dirToMove)) { rc.move(dirToMove); break; } else { dirToMove = dirToMove.rotateLeft(); dirToMove = dirToMove.rotateLeft(); } } } } } } } } } else { // use soldiers boolean escape = false; if (rc.isCoreReady()) { escape = Movement.moveAwayFromEnemy(rc); } if (!escape) { if (adjNeutralRobots.length > 0) { // if there is a neutral robot adjacent, activate it or wait until there's no core // delay if (rc.isCoreReady()) { rc.activate(adjNeutralRobots[0].location); } } if (Movement.getToAdjParts(rc)) { } else { boolean toheal = false; // repair a nearby friendly robot if (rc.isWeaponReady()) { RobotInfo[] friendlyWithinRange = rc.senseNearbyRobots(24, myTeam); numFriendly = friendlyWithinRange.length; if (friendlyWithinRange.length > 0) { RobotInfo toRepair = friendlyWithinRange[0]; for (RobotInfo r : friendlyWithinRange) { if ((r.health < toRepair.health) && (r.type != RobotType.ARCHON) && (r.maxHealth - r.health > 1)) { toRepair = r; } } if ((toRepair.maxHealth - toRepair.health > 1) && (toRepair.type != RobotType.ARCHON)) { toheal = true; rc.repair(toRepair.location); } } } if (toheal == false) { // for sensing if there are guards within range 24 RobotInfo[] friendlyClose = rc.senseNearbyRobots(24, myTeam); int numNearbyGuards = 0; for (RobotInfo f : friendlyClose) { if (f.type == RobotType.GUARD) { numNearbyGuards++; } } boolean built = false; int turnNum = rc.getRoundNum(); if (rc.hasBuildRequirements(RobotType.SCOUT) && rc.isCoreReady() && turnNum > 1 && turnNum % 150 >= 0 && turnNum % 150 <= 19 && turnNum < 900) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.SCOUT)) { rc.build(dirToBuild, RobotType.SCOUT); built = true; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } if (rc.hasBuildRequirements(RobotType.GUARD) && rc.isCoreReady() && !built && numNearbyGuards < 1) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.GUARD)) { rc.build(dirToBuild, RobotType.GUARD); built = true; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } if (rc.hasBuildRequirements(RobotType.SOLDIER) && rc.isCoreReady() && !built) { Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, RobotType.SOLDIER)) { rc.build(dirToBuild, RobotType.SOLDIER); built = true; break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } // if archon has nothing to do, tell soldiers to come to it's location /*if (rc.getRoundNum() > 500 && rc.isCoreReady() && rc.isWeaponReady()) { rc.broadcastMessageSignal(-100, 0, 70 * 70); }*/ } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
protected boolean mineHazard(MapLocation location) { Team mine = rc.senseMine(location); return (mine == Team.NEUTRAL || mine == myTeam.opponent()); }
public static void run(RobotController rc) { int myAttackRange = 0; Random rand = new Random(rc.getID()); Team myTeam = rc.getTeam(); Team enemyTeam = myTeam.opponent(); try { // Any code here gets executed exactly once at the beginning of the game. myAttackRange = rc.getType().attackRadiusSquared; } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { MapLocation myLoc = rc.getLocation(); // If this robot type can attack, check for enemies within range and attack one // guards only attack ZOMBIES RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, Team.ZOMBIE); RobotInfo[] friendliesWithinRange = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, myTeam); int closestArchonDist = 60; MapLocation closestArchonLoc = null; for (RobotInfo f : friendliesWithinRange) { if (f.type == RobotType.ARCHON) { MapLocation curArchonLoc = f.location; if (myLoc.distanceSquaredTo(curArchonLoc) < closestArchonDist) { closestArchonDist = myLoc.distanceSquaredTo(curArchonLoc); closestArchonLoc = curArchonLoc; } } } if (closestArchonLoc != null) { if (enemiesWithinRange.length > 0) { RobotInfo nearestEnemyToArchon = enemiesWithinRange[0]; int nearestEnemyToArchonDist = nearestEnemyToArchon.location.distanceSquaredTo(closestArchonLoc); for (RobotInfo e : enemiesWithinRange) { if (e.location.distanceSquaredTo(closestArchonLoc) < nearestEnemyToArchonDist) { nearestEnemyToArchonDist = e.location.distanceSquaredTo(closestArchonLoc); nearestEnemyToArchon = e; } } // only attack/move towards if it's within 24 range of the archon if (nearestEnemyToArchonDist <= 24) { // Check if weapon is ready if (rc.isWeaponReady() && rc.canAttackLocation(nearestEnemyToArchon.location)) { rc.attackLocation(nearestEnemyToArchon.location); } else { // otherwise try to move towards the enemy if (rc.isCoreReady()) { // try to move towards enemy Direction dirToMove = myLoc.directionTo(nearestEnemyToArchon.location); if (rc.canMove(dirToMove)) { rc.move(dirToMove); } else if (rc.canMove(dirToMove.rotateLeft())) { rc.move(dirToMove.rotateLeft()); } else if (rc.canMove(dirToMove.rotateRight())) { rc.move(dirToMove.rotateRight()); } } } } } else { // if no enemies, it should try to circle nearest archon if (rc.isCoreReady()) { Direction dirToMove = myLoc.directionTo(closestArchonLoc); if (rc.canMove(dirToMove)) { rc.move(dirToMove); } else if (rc.canMove(dirToMove.rotateLeft())) { rc.move(dirToMove.rotateLeft()); } else if (rc.canMove(dirToMove.rotateRight())) { rc.move(dirToMove.rotateRight()); } } } } else { rc.setIndicatorString(1, "no archon"); // if no archons nearby, move randomly and attack if (enemiesWithinRange.length > 0) { RobotInfo nearestEnemy = enemiesWithinRange[0]; int nearestEnemyDist = nearestEnemy.location.distanceSquaredTo(myLoc); for (RobotInfo e : enemiesWithinRange) { if (e.location.distanceSquaredTo(myLoc) < nearestEnemyDist) { nearestEnemyDist = e.location.distanceSquaredTo(myLoc); nearestEnemy = e; } } if (rc.isWeaponReady() && rc.canAttackLocation(nearestEnemy.location)) { rc.attackLocation(nearestEnemy.location); } else { // otherwise try to move towards the enemy if (rc.isCoreReady()) { // try to move towards enemy Direction dirToMove = myLoc.directionTo(nearestEnemy.location); if (rc.canMove(dirToMove)) { rc.move(dirToMove); } else if (rc.canMove(dirToMove.rotateLeft())) { rc.move(dirToMove.rotateLeft()); } else if (rc.canMove(dirToMove.rotateRight())) { rc.move(dirToMove.rotateRight()); } } } } else if (rc.isCoreReady()) { Direction dirToMove = RobotPlayer.directions[(rand.nextInt(8))]; for (int i = 0; i < 8; i++) { if (rc.canMove(dirToMove)) { rc.move(dirToMove); break; } else { dirToMove = dirToMove.rotateLeft(); } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
public static void run(RobotController rc) { sightRadius = RobotType.SOLDIER.sensorRadiusSquared; attackRadius = RobotType.SOLDIER.attackRadiusSquared; myTeam = rc.getTeam(); enemyTeam = myTeam.opponent(); while (true) { try { numEnemySoldiers = 0; totalEnemySoldierHealth = 0; myLoc = rc.getLocation(); nearbyAllies = rc.senseNearbyRobots(sightRadius, myTeam); nearbyEnemies = rc.senseHostileRobots(myLoc, sightRadius); newArchonLoc = null; // clear bad locations resetLocations(rc); // read messages and get destination readMessages(rc); // heal if need (and set the archon destination to go to) setRetreatingStatus(rc, nearbyEnemies); // Remove turret locations that you can see are not there. // Does NOT remove turret locations due to broadcasts. Already done in read messages. removeTurretLocations(rc); if (newArchonLoc != null) { nearestArchonLocation = newArchonLoc; } rc.setIndicatorString(2, "Round: " + rc.getRoundNum() + ", rushing: " + rush); // Reset rushing if turns since rush is > 20 and see no more enemies. if (rush && myLoc.distanceSquaredTo(rushLocation) <= 100) turnsSinceRush++; if (turnsSinceRush > 20) { if (rc.senseNearbyRobots(sightRadius, enemyTeam).length == 0) { turnsSinceRush = 0; rush = false; } } // When rushing, be mad aggressive. if (rush) { rushMicro(rc, nearbyEnemies); } // When retreating, retreat else if (healing) { if (rc.isCoreReady()) { if (nearestArchonLocation != null) { if (myLoc.distanceSquaredTo(nearestArchonLocation) > 13) { bugging.enemyAvoidMove(nearbyEnemies); // Get away from archons that are not too close together. } else if (myLoc.distanceSquaredTo(nearestArchonLocation) <= 2) { Direction radialDir = nearestArchonLocation.directionTo(myLoc); Direction awayDir = Movement.getBestMoveableDirection(radialDir, rc, 2); if (awayDir != Direction.NONE) { rc.move(awayDir); } } } } // Make sure to attack people even when retreating. // Prioritize the closest enemy. Then the closest zombie. if (rc.isWeaponReady()) { // Attack the closest enemy. If there is not one, then attack the closest zombie int closestDist = 10000; RobotInfo target = null; for (RobotInfo hostile : nearbyEnemies) { int dist = myLoc.distanceSquaredTo(hostile.location); if (rc.canAttackLocation(hostile.location)) { // There is already is a target if (target != null) { if (target.team == enemyTeam) { // Target is already enemy, so prioritize the closest if (hostile.team == enemyTeam) { if (dist < closestDist) { target = hostile; closestDist = dist; } } // If hostile is not an enemy, not worth considering. } else { // Target is not on enemy team, so hostile is best choice! if (hostile.team == enemyTeam) { target = hostile; closestDist = dist; // Both are zombies, so just pick the closest. } else { if (dist < closestDist) { target = hostile; closestDist = dist; } } } // Set a target when there is not one. } else { target = hostile; closestDist = dist; } } } // We know that if there is a target, we can attack it. if (target != null) { rc.attackLocation(target.location); } } } // When viper infected and will die from the infection, do special micro else if (isViperInfected(rc) && (rc.getHealth() < rc.getViperInfectedTurns() * 2 || rc.getRoundNum() > 2100)) { viperInfectedMicro(rc); } // if there are enemies in range, we should focus on attack and micro else if (nearbyEnemies.length > 0) { if (shouldLure(rc, nearbyEnemies, nearbyAllies)) luringMicro(rc); else micro(rc); } else { // otherwise, we should always be moving somewhere moveSoldier(rc); } Clock.yield(); } catch (Exception e) { e.printStackTrace(); } } }
/** * run() is the method that is called when a robot is instantiated in the Battlecode world. If * this method returns, the robot dies! */ @SuppressWarnings("unused") public static void run(RobotController rc) { // You can instantiate variables here. Direction[] directions = { Direction.NORTH, Direction.NORTH_EAST, Direction.EAST, Direction.SOUTH_EAST, Direction.SOUTH, Direction.SOUTH_WEST, Direction.WEST, Direction.NORTH_WEST }; RobotType[] robotTypes = { RobotType.SCOUT, RobotType.SOLDIER, RobotType.SOLDIER, RobotType.SOLDIER, RobotType.GUARD, RobotType.GUARD, RobotType.VIPER, RobotType.TURRET }; Random rand = new Random(rc.getID()); int myAttackRange = 0; Team myTeam = rc.getTeam(); Team enemyTeam = myTeam.opponent(); if (rc.getType() == RobotType.ARCHON) { try { // Any code here gets executed exactly once at the beginning of the game. } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { /* // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { int fate = rand.nextInt(1000); // Check if this ARCHON's core is ready if (fate % 10 == 2) { // Send a message signal containing the data (6370, 6147) rc.broadcastMessageSignal(6370, 6147, 80); } Signal[] signals = rc.emptySignalQueue(); if (signals.length > 0) { // Set an indicator string that can be viewed in the client rc.setIndicatorString(0, "I received a signal this turn!"); } else { rc.setIndicatorString(0, "I don't any signal buddies"); } if (rc.isCoreReady()) { if (fate < 800) { // Choose a random direction to try to move in Direction dirToMove = directions[fate % 8]; // Check the rubble in that direction if (rc.senseRubble(rc.getLocation().add(dirToMove)) >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) { // Too much rubble, so I should clear it rc.clearRubble(dirToMove); // Check if I can move in this direction } else if (rc.canMove(dirToMove)) { // Move rc.move(dirToMove); } } else { // Choose a random unit to build RobotType typeToBuild = robotTypes[fate % 8]; // Check for sufficient parts if (rc.hasBuildRequirements(typeToBuild)) { // Choose a random direction to try to build in Direction dirToBuild = directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, typeToBuild)) { rc.build(dirToBuild, typeToBuild); break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } */ } } else if (rc.getType() != RobotType.TURRET) { try { // Any code here gets executed exactly once at the beginning of the game. myAttackRange = rc.getType().attackRadiusSquared; } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { int fate = rand.nextInt(1000); if (fate % 5 == 3) { // Send a normal signal rc.broadcastSignal(80); } boolean shouldAttack = false; // If this robot type can attack, check for enemies within range and attack one if (myAttackRange > 0) { RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam); RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE); if (enemiesWithinRange.length > 0) { shouldAttack = true; // Check if weapon is ready if (rc.isWeaponReady()) { rc.attackLocation( enemiesWithinRange[rand.nextInt(enemiesWithinRange.length)].location); } } else if (zombiesWithinRange.length > 0) { shouldAttack = true; // Check if weapon is ready if (rc.isWeaponReady()) { rc.attackLocation( zombiesWithinRange[rand.nextInt(zombiesWithinRange.length)].location); } } } if (!shouldAttack) { if (rc.isCoreReady()) { if (fate < 600) { // Choose a random direction to try to move in Direction dirToMove = directions[fate % 8]; // Check the rubble in that direction if (rc.senseRubble(rc.getLocation().add(dirToMove)) >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) { // Too much rubble, so I should clear it rc.clearRubble(dirToMove); // Check if I can move in this direction } else if (rc.canMove(dirToMove)) { // Move rc.move(dirToMove); } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } else if (rc.getType() == RobotType.TURRET) { try { myAttackRange = rc.getType().attackRadiusSquared; } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { // If this robot type can attack, check for enemies within range and attack one if (rc.isWeaponReady()) { RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam); RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE); if (enemiesWithinRange.length > 0) { for (RobotInfo enemy : enemiesWithinRange) { // Check whether the enemy is in a valid attack range (turrets have a minimum range) if (rc.canAttackLocation(enemy.location)) { rc.attackLocation(enemy.location); break; } } } else if (zombiesWithinRange.length > 0) { for (RobotInfo zombie : zombiesWithinRange) { if (rc.canAttackLocation(zombie.location)) { rc.attackLocation(zombie.location); break; } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } }
public static void run(RobotController rc) { int myAttackRange = 0; Random rand = new Random(rc.getID()); Team myTeam = rc.getTeam(); Team enemyTeam = myTeam.opponent(); try { // Any code here gets executed exactly once at the beginning of the game. myAttackRange = rc.getType().attackRadiusSquared; } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { int fate = rand.nextInt(1000); if (fate % 5 == 3) { // Send a normal signal rc.broadcastSignal(80); } boolean shouldAttack = false; // If this robot type can attack, check for enemies within range and attack one if (myAttackRange > 0) { RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam); RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE); if (enemiesWithinRange.length > 0) { shouldAttack = true; // Check if weapon is ready if (rc.isWeaponReady()) { rc.attackLocation( enemiesWithinRange[rand.nextInt(enemiesWithinRange.length)].location); } } else if (zombiesWithinRange.length > 0) { shouldAttack = true; // Check if weapon is ready if (rc.isWeaponReady()) { rc.attackLocation( zombiesWithinRange[rand.nextInt(zombiesWithinRange.length)].location); } } } if (!shouldAttack) { if (rc.isCoreReady()) { if (fate < 600) { // Choose a random direction to try to move in Direction dirToMove = RobotPlayer.directions[fate % 8]; // Check the rubble in that direction if (rc.senseRubble(rc.getLocation().add(dirToMove)) >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) { // Too much rubble, so I should clear it rc.clearRubble(dirToMove); // Check if I can move in this direction } else if (rc.canMove(dirToMove)) { // Move rc.move(dirToMove); } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }