@Override public void initialize() throws GameActionException { if (rc.getRoundNum() != 0) { return; } Signal[] signals = rc.emptySignalQueue(); rc.broadcastSignal(GameConstants.MAP_MAX_HEIGHT * GameConstants.MAP_MAX_HEIGHT); for (Signal s : signals) { if (s.getTeam() == myTeam) { heiarchy++; } } rc.setIndicatorString(1, "I am the " + heiarchy + ": " + rc.getRoundNum()); if (heiarchy == -1) { goalLocation = rc.getLocation(); rc.broadcastMessageSignal(1337, 0, 100 * 100); leaderId = rc.getID(); leaderLocation = rc.getLocation(); } else { heiarchy -= 1; for (Signal s : signals) { if (s.getMessage() != null) { if (s.getMessage()[0] == 1337) { leaderId = s.getID(); leaderLocation = s.getLocation(); goalLocation = leaderLocation; break; } } } } }
private static Direction getScoutTargetDirection(RobotController rc) { Signal[] signals = rc.emptySignalQueue(); for (Signal signal : signals) { if (signal.getMessage() != null && signal.getTeam().equals(myTeam)) { if (signal.getMessage()[0] == SENDING_TARGET_DIRECTION) { return SIGNAL_TO_DIRECTION.get(signal.getMessage()[1]); } } } return Direction.NONE; }
public void getSignals() { Signal[] queue = rc.emptySignalQueue(); for (Signal signal : queue) { if (signal.getTeam() == myTeam) { if (signal.getMessage() != null) { if (signal.getMessage()[0] == 0xdead && signal.getMessage()[1] == 0xbeef) { heiarchy--; continue; } MessageSignal msgSig = new MessageSignal(signal); switch (msgSig.getMessageType()) { case ROBOT: if (msgSig.getPingedTeam() == Team.NEUTRAL) { rc.setIndicatorString(0, "Found neutral"); if (msgSig.getPingedLocation().distanceSquaredTo(rc.getLocation()) < 40) { goalLocation = msgSig.getPingedLocation(); } } if (msgSig.getPingedType() == RobotType.ARCHON && msgSig.getPingedTeam() == myTeam.opponent()) { rc.setIndicatorString(0, "Found enemy Archon"); foundEnemyArchon = true; sentGoal = false; enemyArchon = msgSig.getPingedLocation(); } if (msgSig.getPingedType() == RobotType.ZOMBIEDEN) { rc.setIndicatorString(2, "Found Zombie Den"); knownZombieDenLocations.add(msgSig.getPingedLocation()); foundZombieDen = true; } break; case PARTS: break; default: break; } } } } }
/** * Message-processing for non-archons Currently handles messages: Change of mode Setting * turtle-corner location * * @param rc * @throws GameActionException */ private static void processFighterSignals(RobotController rc) throws GameActionException { int cornerX = Integer.MIN_VALUE; int cornerY = Integer.MIN_VALUE; RobotType type = rc.getType(); Signal[] signals = rc.emptySignalQueue(); for (Signal s : signals) { if (s.getTeam().equals(myTeam) && s.getMessage() != null) { final int[] message = s.getMessage(); if (message[0] == SENDING_MODE) { currentMode = message[1]; } else if (message[0] == SENDING_TURTLE_X) { cornerX = message[1]; } else if (message[0] == SENDING_TURTLE_Y) { cornerY = message[1]; } } // when a soldier finds a zombie den, it signals. Other soldiers that receive // the message then should move toward the den to help kill it. Ideally, // this should make it easier to remove zombie dens near the turtle corner if (type == RobotType.SOLDIER && s.getTeam().equals(myTeam) && s.getMessage() == null) { if (rc.getLocation().distanceSquaredTo(s.getLocation()) < rc.getType().sensorRadiusSquared * 2.5 && rc.isCoreReady()) { moveTowards(rc, rc.getLocation().directionTo(s.getLocation())); } } // for turrets to attack broadcasting enemies outside of sight range if (type == RobotType.TURRET && !s.getTeam().equals(myTeam) && rc.isWeaponReady() && rc.canAttackLocation(s.getLocation())) { rc.attackLocation(s.getLocation()); } } if (cornerX > Integer.MIN_VALUE && cornerY > Integer.MIN_VALUE) { turtleCorner = new MapLocation(cornerX, cornerY); } }
/** * To be run by archons during the intro phase (and others? change it a bit?) to handle and gather * information from incoming message signals * * @param rc */ private static void processIntroMessageSignals(RobotController rc) { int cornerX = Integer.MIN_VALUE; int cornerY = Integer.MIN_VALUE; int denX = Integer.MIN_VALUE; int denY = Integer.MIN_VALUE; Signal[] signals = rc.emptySignalQueue(); for (Signal s : signals) { if (s.getTeam().equals(myTeam) && s.getMessage() != null) { final int[] message = s.getMessage(); if (message[0] == SENDING_CORNER_X) { cornerX = message[1]; } else if (message[0] == SENDING_CORNER_Y) { cornerY = message[1]; } else if (message[0] == SENDING_DEN_X) { denX = message[1]; } else if (message[0] == SENDING_DEN_Y) { denY = message[1]; } } } if (cornerX > Integer.MIN_VALUE && cornerY > Integer.MIN_VALUE) { MapLocation newCorner = new MapLocation(cornerX, cornerY); if (!SCOUTED_CORNERS.contains(newCorner)) { SCOUTED_CORNERS.add(newCorner); rc.setIndicatorString(0, "Added new corner: " + newCorner); rc.setIndicatorString(2, "Current Mode" + currentMode); } rc.setIndicatorString(1, SCOUTED_CORNERS + ""); } if (denX > Integer.MIN_VALUE && denY > Integer.MIN_VALUE) { MapLocation newDen = new MapLocation(denX, denY); if (!ZOMBIE_DEN_LOCATIONS.contains(newDen)) { ZOMBIE_DEN_LOCATIONS.add(newDen); } } }
private RobotData getZombieDen() { for (Signal s : roundSignals) { if (s.getTeam() != team) continue; int[] message = s.getMessage(); if (message == null) continue; MessageParser parser = new MessageParser(message[0], message[1], currentLocation); if (parser.getMessageType() == MessageType.ZOMBIE) { RobotData robotData = parser.getRobotData(); if (robotData.type == RobotType.ZOMBIEDEN) { return robotData; } } } return null; }
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(); } } }
public static void run() throws GameActionException { rc = RobotPlayer.rc; rand = new Random(rc.getID()); // build scouts right away buildRobot(RobotType.SCOUT); while (true) { /* * INPUT */ if (rc.getLocation().equals(goal)) { goal = null; // you made it to the goal past10Locations = new ArrayList<MapLocation>(); // delete the slug trail after you reach your goal } // sense locations around you nearbyMapLocations = MapLocation.getAllMapLocationsWithinRadiusSq( rc.getLocation(), rc.getType().sensorRadiusSquared); // parts locations nearbyPartsLocations = rc.sensePartLocations(RobotType.ARCHON.sensorRadiusSquared); // find the nearest mapLocation with the most parts double maxParts = 0; MapLocation nearbyLocationWithMostParts = null; for (MapLocation loc : nearbyPartsLocations) { // add to locationsWithParts arraylist if (locationsWithParts.contains(loc) == false) { locationsWithParts.add(loc); } // find the location with the most parts double partsAtLoc = rc.senseParts(loc); if (partsAtLoc > maxParts) { maxParts = partsAtLoc; nearbyLocationWithMostParts = loc; } } // read signals Signal[] signals = rc.emptySignalQueue(); for (Signal signal : signals) { // check if the signal has parts at the location int[] message = signal.getMessage(); if (message != null && message[0] == Utility.PARTS_CODE) { // add that location to the locationsWithParts arraylist locationsWithParts.add(signal.getLocation()); } } // sense robots MapLocation myLoc = rc.getLocation(); robots = rc.senseNearbyRobots(); foes = new ArrayList<RobotInfo>(); foesWithinAttackRange = new ArrayList<RobotInfo>(); for (RobotInfo robot : robots) { if (robot.team == Team.ZOMBIE || robot.team == rc.getTeam().opponent()) // if the robot is a foe { foes.add(robot); if (myLoc.distanceSquaredTo(robot.location) < robot.type.attackRadiusSquared) { foesWithinAttackRange.add(robot); } } } int nearbyFoes = foes.size(); int nearbyFoesInAttackRange = foesWithinAttackRange.size(); /*//check stats double health = rc.getHealth(); int infectedTurns = rc.getInfectedTurns(); int robotsAlive = rc.getRobotCount(); */ /* * OUPUT */ // what to do if (nearbyFoes == 0) // if there are no foes in sight { if (rc.getTeamParts() >= RobotType.TURRET.partCost) // build if you can { buildRobots(); } else { if (maxParts > 0 && goal == null) // if there are parts nearby { // make that the goal goal = nearbyLocationWithMostParts; } else if (goal == null) // if there aren't and there is no goal { // build something or find new parts // 80% build, 20% new parts if (locationsWithParts.size() > 0 && rand.nextFloat() > .8) { goal = locationsWithParts.get(0); locationsWithParts.remove(0); goalIsASafeLocation = false; } // calculate the next goal - maybe a new parts location you got via signal } else if (goal != null) // if there is a goal, move there { moveToLocation(goal); } } } else // there are foes nearby { // message for help! if (Math.random() < probSignal) { rc.broadcastSignal(archonInTroubleSignalRadiusSquared); } if (nearbyFoesInAttackRange > 0) { goal = findSaferLocation(); rc.setIndicatorString(0, "" + goal.x + " " + goal.y); goalIsASafeLocation = true; moveToLocation(goal); } } Clock.yield(); } }
public MessageSignal(Signal signal) { this.signal = signal; message = signal.getMessage(); }