Skip to content

Commit

Permalink
Merge pull request #16 from j-mao/distress
Browse files Browse the repository at this point in the history
Add distress calls from structures
  • Loading branch information
ChiCubed authored Jan 30, 2019
2 parents 91d7408 + ef64f11 commit 261ee66
Showing 1 changed file with 75 additions and 11 deletions.
86 changes: 75 additions & 11 deletions newstart/MyRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Integer> karboniteLocs;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1593,6 +1645,12 @@ Action runSpecificTurn() {
sendStructureLocation();
checkTurtleWelfare();

if (me.turn > lastDistress+TIME_BETWEEN_DISTRESSES) {
if (sendDistressIfRequired()) {
lastDistress = me.turn;
}
}

Action myAction = null;

if (myAction == null) {
Expand Down Expand Up @@ -1795,15 +1853,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;
Expand Down Expand Up @@ -1851,20 +1900,33 @@ private class TurtlingRobotController extends MobileRobotController {
private LinkedList<Integer> circleLocs;
private boolean activated;
private int assignedLoc;
private int distressLoc;

TurtlingRobotController() {
super();

circleLocs = new LinkedList<>();
activated = false;
assignedLoc = communications.readAssignedLoc();
distressLoc = Vector.INVALID;
}

@Override
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();

Expand All @@ -1877,7 +1939,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();
Expand Down

0 comments on commit 261ee66

Please sign in to comment.