From ef64f11b74b5d310e58430fb56737e24b9ef352c Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 31 Jan 2019 02:14:22 +1100 Subject: [PATCH] Add distress calls from structures --- newstart/MyRobot.java | 86 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/newstart/MyRobot.java b/newstart/MyRobot.java index 9f963e3..0a4786b 100644 --- a/newstart/MyRobot.java +++ b/newstart/MyRobot.java @@ -150,6 +150,7 @@ private abstract class Communicator { static final int ATTACK = 0x3000; static final int SHED_RADIUS = 0x4000; static final int CIRCLE_SUCCESS = 0x5000; + static final int DISTRESS = 0x6000; static final int CASTLELOC = 0x8000; // Bitmasks for castle communications @@ -645,6 +646,9 @@ boolean checkIsArmed(int id) { protected final double CIRCLE_BUILD_REDUCTION_BASE; protected final double CIRCLE_BUILD_REDUCTION_MEDIAN; + protected static final int TIME_BETWEEN_DISTRESSES = 10; + protected int lastDistress; + protected boolean circleInitiated; protected int lastCircleTurn; @@ -691,6 +695,29 @@ protected void sendAssignedLoc(int location) { myUnitWelfareChecker.recordNewAssignment(location); } + protected boolean sendDistressIfRequired() { + int closestEnemy = Vector.INVALID; + int furthestFriendDist = 0; + for (Robot r: visibleRobots) { + if (isVisible(r)) { + int theirLoc = Vector.makeMapLocation(r.x, r.y); + if (r.team != me.team) { + if (closestEnemy == Vector.INVALID || + Vector.distanceSquared(myLoc, theirLoc) < Vector.distanceSquared(myLoc, closestEnemy)) { + closestEnemy = theirLoc; + } + } else if (isArmed(r.unit)) { + furthestFriendDist = Math.max(furthestFriendDist, Vector.distanceSquared(myLoc, theirLoc)); + } + } + } + if (closestEnemy != Vector.INVALID) { + communications.sendRadio(Vector.compress(closestEnemy) | Communicator.DISTRESS, furthestFriendDist); + return true; + } + return false; + } + protected BuildAction buildInResponseToNearbyEnemies() { int friendlyCrusaders = 0; int friendlyProphets = 0; @@ -1017,6 +1044,27 @@ protected AttackAction tryToAttack() { return null; } + + protected int checkForDistress() { + for (Robot r: visibleRobots) { + if (communications.isRadioing(r) && Vector.makeMapLocation(r.x, r.y) == myHome) { + int msg = communications.readRadio(r); + if ((msg & 0xf000) == (Communicator.DISTRESS & 0xf000)) { + return Vector.makeMapLocationFromCompressed(msg & 0x0fff); + } + } + } + return Vector.INVALID; + } + + protected boolean enemyUnitVisible() { + for (Robot r: visibleRobots) { + if (isVisible(r) && r.team != me.team) { + return true; + } + } + return false; + } } private class CastleController extends StructureController { @@ -1027,7 +1075,6 @@ private class CastleController extends StructureController { */ private static final int OCCUPY_FARAWAY_RESOURCE_THRESHOLD = 3; private static final int INVADE_ENEMY_FARMS_TURN_THRESHOLD = 150; - private static final int MAX_CLUSTERS = 32; private LinkedList karboniteLocs; @@ -1127,6 +1174,11 @@ Action runSpecificTurn() { if (myAction == null) { myAction = tryToAttack(); + if (myAction != null && me.turn > lastDistress+TIME_BETWEEN_DISTRESSES) { + if (sendDistressIfRequired()) { + lastDistress = me.turn; + } + } } if (myAction == null && me.turn >= SPAM_CRUSADER_TURN_THRESHOLD) { @@ -1592,6 +1644,12 @@ Action runSpecificTurn() { sendStructureLocation(); checkTurtleWelfare(); + if (me.turn > lastDistress+TIME_BETWEEN_DISTRESSES) { + if (sendDistressIfRequired()) { + lastDistress = me.turn; + } + } + Action myAction = null; if (myAction == null) { @@ -1792,15 +1850,6 @@ Action runSpecificTurn() { return myAction; } - private boolean enemyUnitVisible() { - for (Robot r: visibleRobots) { - if (isVisible(r) && r.team != me.team) { - return true; - } - } - return false; - } - private int karboniteLimit() { if (farmHalfQty > 0) { return SPECS.UNITS[me.unit].KARBONITE_CAPACITY >> 1; @@ -1848,6 +1897,7 @@ private class TurtlingRobotController extends MobileRobotController { private LinkedList circleLocs; private boolean activated; private int assignedLoc; + private int distressLoc; TurtlingRobotController() { super(); @@ -1855,6 +1905,7 @@ private class TurtlingRobotController extends MobileRobotController { circleLocs = new LinkedList<>(); activated = false; assignedLoc = communications.readAssignedLoc(); + distressLoc = Vector.INVALID; } @Override @@ -1862,6 +1913,17 @@ Action runSpecificTurn() { checkForCircleAssignments(); + if (distressLoc == Vector.INVALID) { + distressLoc = checkForDistress(); + } + + if (distressLoc != Vector.INVALID && + Vector.get(distressLoc, visibleRobotMap) != MAP_INVISIBLE && + !enemyUnitVisible()) { + + distressLoc = Vector.INVALID; + } + Action myAction = null; sendMyAssignedLoc(); @@ -1874,7 +1936,9 @@ Action runSpecificTurn() { int newLoc = Vector.add(myLoc, dir); if (dir == Vector.INVALID || !isOccupiable(newLoc)) { myBfsSolver.solve(myLoc, SPECS.UNITS[me.unit].SPEED, SPECS.UNITS[me.unit].SPEED, - (location) -> { return location == assignedLoc; }, + (location) -> { + return location == distressLoc || (distressLoc == Vector.INVALID && location == assignedLoc); + }, (location) -> { return isOccupiable(location) || location == assignedLoc; } ); dir = myBfsSolver.nextStep();