From 85169405499e447b2a8b3f860f43b52a01749933 Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 11:52:47 -0500 Subject: [PATCH 1/8] Slight Micro Improvement --- templates/Attack.java.jinja2 | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/templates/Attack.java.jinja2 b/templates/Attack.java.jinja2 index 169da67..a7640b3 100644 --- a/templates/Attack.java.jinja2 +++ b/templates/Attack.java.jinja2 @@ -226,12 +226,12 @@ public class Attack { {% endfor %} } - {% for i in range(0, 9) -%} + {# {% for i in range(0, 9) -%} {% set dir = shortDirections[i] %} {% debug('minDistToEnemy_' ~ dir, 'targetLoc_' ~ dir ~ '.x', 'canMove_' ~ dir, 'targetLoc_' ~ dir ~ '.y', 'penalty_' ~ dir, 'healthDmgAttackRange_' ~ dir, 'myHealthDmg_' ~ dir, 'towerHealth_' ~ dir, 'canBeHit_' ~ dir, 'canLandHit_' ~ dir) %} - {% endfor %} + {% endfor %} #} boolean bestWins = false; Direction bestDir = {{ directions[0] }}; @@ -242,12 +242,12 @@ public class Attack { {{ chooseBest(type, i) | indent(4) }} {% endfor %} - {% set dir = 'best' %} + {# {% set dir = 'best' %} {% debug('minDistToEnemy_' ~ dir, 'targetLoc_' ~ dir ~ '.x', 'canMove_' ~ dir, 'targetLoc_' ~ dir ~ '.y', 'penalty_' ~ dir, 'healthDmgAttackRange_' ~ dir, 'myHealthDmg_' ~ dir, 'towerHealth_' ~ dir, 'canBeHit_' ~ dir, 'canLandHit_' ~ dir) %} System.out.println("Chose: " + bestDir); - System.out.println("\n"); + System.out.println("\n"); #} if (rc.canMove(bestDir)) { Pathing.move(bestDir); @@ -303,10 +303,16 @@ public class Attack { } } - // If I can't land a hit at either square, prefer the one which is closer. - // This is necessary if we're far away, but if we can already land a hit, - // Then there's really no difference between these two squares, so we should go for penalty instead. + // If I'm not in action radius, and I can't land a hit at either square, + // Prefer the one which prepares me to hit a low-health tower. + // If those are the same, chose the square that gets me closer to the enemy. + // Otherwise, there's no difference between the squares and we should go for penalty. else if (!canLandHit_C && !canLandHit_{{best}} && !canLandHit_{{dir}}) { + if (towerHealth_{{best}} != towerHealth_{{dir}}) { + bestWins = towerHealth_{{best}} < towerHealth_{{dir}}; + break {{label}}; + } + if (minDistToEnemy_{{best}} != minDistToEnemy_{{dir}}) { bestWins = minDistToEnemy_{{best}} < minDistToEnemy_{{dir}}; break {{label}}; @@ -369,7 +375,7 @@ public class Attack { {% else %} myHealthDmg_{{dir}} = {{type}}AttackStrength; {% endif %} - towerHealth_{{dir}} = Math.min(towerHealth_{{dir}}, robot.health); + canLandHit_{{dir}} = true; } if (d <= UnitType.{{tower}}.actionRadiusSquared) { @@ -377,8 +383,10 @@ public class Attack { healthDmgAttackRange_{{dir}} += UnitType.{{tower}}.attackStrength; canBeHit_{{dir}} = true; } - - if (d < minDistToEnemy_{{dir}}) minDistToEnemy_{{dir}} = d; + if (d <= minDistToEnemy_{{dir}}) { + towerHealth_{{dir}} = robot.health; + minDistToEnemy_{{dir}} = d; + } break {{label}}; } {% endfor %} From c40425e85fcbd50073f22dd1a3a600793b03bc2d Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 14:58:16 -0500 Subject: [PATCH 2/8] Revert "Slight Micro Improvement" This reverts commit 85169405499e447b2a8b3f860f43b52a01749933. --- templates/Attack.java.jinja2 | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/templates/Attack.java.jinja2 b/templates/Attack.java.jinja2 index a7640b3..169da67 100644 --- a/templates/Attack.java.jinja2 +++ b/templates/Attack.java.jinja2 @@ -226,12 +226,12 @@ public class Attack { {% endfor %} } - {# {% for i in range(0, 9) -%} + {% for i in range(0, 9) -%} {% set dir = shortDirections[i] %} {% debug('minDistToEnemy_' ~ dir, 'targetLoc_' ~ dir ~ '.x', 'canMove_' ~ dir, 'targetLoc_' ~ dir ~ '.y', 'penalty_' ~ dir, 'healthDmgAttackRange_' ~ dir, 'myHealthDmg_' ~ dir, 'towerHealth_' ~ dir, 'canBeHit_' ~ dir, 'canLandHit_' ~ dir) %} - {% endfor %} #} + {% endfor %} boolean bestWins = false; Direction bestDir = {{ directions[0] }}; @@ -242,12 +242,12 @@ public class Attack { {{ chooseBest(type, i) | indent(4) }} {% endfor %} - {# {% set dir = 'best' %} + {% set dir = 'best' %} {% debug('minDistToEnemy_' ~ dir, 'targetLoc_' ~ dir ~ '.x', 'canMove_' ~ dir, 'targetLoc_' ~ dir ~ '.y', 'penalty_' ~ dir, 'healthDmgAttackRange_' ~ dir, 'myHealthDmg_' ~ dir, 'towerHealth_' ~ dir, 'canBeHit_' ~ dir, 'canLandHit_' ~ dir) %} System.out.println("Chose: " + bestDir); - System.out.println("\n"); #} + System.out.println("\n"); if (rc.canMove(bestDir)) { Pathing.move(bestDir); @@ -303,16 +303,10 @@ public class Attack { } } - // If I'm not in action radius, and I can't land a hit at either square, - // Prefer the one which prepares me to hit a low-health tower. - // If those are the same, chose the square that gets me closer to the enemy. - // Otherwise, there's no difference between the squares and we should go for penalty. + // If I can't land a hit at either square, prefer the one which is closer. + // This is necessary if we're far away, but if we can already land a hit, + // Then there's really no difference between these two squares, so we should go for penalty instead. else if (!canLandHit_C && !canLandHit_{{best}} && !canLandHit_{{dir}}) { - if (towerHealth_{{best}} != towerHealth_{{dir}}) { - bestWins = towerHealth_{{best}} < towerHealth_{{dir}}; - break {{label}}; - } - if (minDistToEnemy_{{best}} != minDistToEnemy_{{dir}}) { bestWins = minDistToEnemy_{{best}} < minDistToEnemy_{{dir}}; break {{label}}; @@ -375,7 +369,7 @@ public class Attack { {% else %} myHealthDmg_{{dir}} = {{type}}AttackStrength; {% endif %} - + towerHealth_{{dir}} = Math.min(towerHealth_{{dir}}, robot.health); canLandHit_{{dir}} = true; } if (d <= UnitType.{{tower}}.actionRadiusSquared) { @@ -383,10 +377,8 @@ public class Attack { healthDmgAttackRange_{{dir}} += UnitType.{{tower}}.attackStrength; canBeHit_{{dir}} = true; } - if (d <= minDistToEnemy_{{dir}}) { - towerHealth_{{dir}} = robot.health; - minDistToEnemy_{{dir}} = d; - } + + if (d < minDistToEnemy_{{dir}}) minDistToEnemy_{{dir}} = d; break {{label}}; } {% endfor %} From f148b73a335f116c56475c35ac4ca589f2099033 Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 15:06:45 -0500 Subject: [PATCH 3/8] Initial Commit := 92-60 --- templates/Soldier.java.jinja2 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/templates/Soldier.java.jinja2 b/templates/Soldier.java.jinja2 index 5c1e809..573d745 100644 --- a/templates/Soldier.java.jinja2 +++ b/templates/Soldier.java.jinja2 @@ -305,13 +305,14 @@ public class Soldier { } public static boolean isHarasser() throws GameActionException { - if (Math.max(rc.getMapHeight(), rc.getMapWidth()) > 40) return false; + // if (Math.max(rc.getMapHeight(), rc.getMapWidth()) > 40) return false; if (rc.getRoundNum() < 50) { - if (rc.getRoundNum() > 2) return false; + if (rc.getRoundNum() > 5) return false; if (RefuelManager.home == null) return false; if (!rc.canSenseLocation(RefuelManager.home)) return false; RobotInfo r = rc.senseRobotAtLocation(RefuelManager.home); - if (r == null || r.getType() != UnitType.LEVEL_TWO_PAINT_TOWER) return false; + if (r == null) return false; + if (r.getType() == UnitType.LEVEL_ONE_MONEY_TOWER) return false; return true; } else return false; @@ -421,6 +422,7 @@ public class Soldier { } public static void runTurn() throws GameActionException { + rc.setIndicatorString("Harasser := " + harrasser); if (Attack.shouldSoldierMicro()) { rc.setIndicatorString("Attacking"); Attack.soldierAttackMicro(); @@ -473,23 +475,26 @@ public class Soldier { RefuelManager.refuel(); return; } - else if (rc.getNumberTowers() < 25 && markedResource == null) { if (buildTower != null) { TowerBuild.makeTower(buildTower); return; } } - else if (harrasser) { + + if (harrasser) { + rc.setIndicatorString("Harassing"); goHarrass(); return; } + if (returnLoc != null) { if (myloc.distanceSquaredTo(returnLoc) <= 5) { returnLoc = null; } else { Pathing.pathTo(returnLoc); + return; } } @@ -504,7 +509,7 @@ public class Soldier { if (moved) return; - //rc.setIndicatorString("Exploring"); + rc.setIndicatorString("Exploring"); Explore.explore(near); } From 899a5868a4baab6384e2c6849fb3eb85ebdf3f40 Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 15:17:17 -0500 Subject: [PATCH 4/8] Refactor --- templates/Harass.java.jinja2 | 99 +++++++++++++++++++++++++++++++ templates/RobotPlayer.java.jinja2 | 2 + templates/Soldier.java.jinja2 | 94 ++--------------------------- 3 files changed, 106 insertions(+), 89 deletions(-) create mode 100644 templates/Harass.java.jinja2 diff --git a/templates/Harass.java.jinja2 b/templates/Harass.java.jinja2 new file mode 100644 index 0000000..e9531d5 --- /dev/null +++ b/templates/Harass.java.jinja2 @@ -0,0 +1,99 @@ +package current; +import battlecode.common.*; + +public class Harass { + RobotInfo friend = null; + public static RobotController rc; + public static boolean wasHarasser = false; + public static boolean isHarasser = false; + public static boolean reachedH = false; + public static boolean reachedR = false; + public static boolean reachedV = false; + public static boolean seenCenter = false; + public static MapLocation center = null; + public static void init(RobotController rc) throws GameActionException { + Harass.rc = rc; + center = new MapLocation(rc.getMapWidth() / 2, rc.getMapHeight() / 2); + } + + public static void initTurn() throws GameActionException { + if (!isHarasser && !wasHarasser) { + isHarasser = decideHarasser(); + if (isHarasser) wasHarasser = true; + } + // We'd do this with messages later on. + } + + public static boolean decideHarasser() throws GameActionException { + if (rc.getRoundNum() >= 50) return false; + if (rc.getRoundNum() > 5) return false; + if (RefuelManager.home == null) return false; + if (!rc.canSenseLocation(RefuelManager.home)) return false; + RobotInfo r = rc.senseRobotAtLocation(RefuelManager.home); + if (r == null) return false; + if (r.getType() == UnitType.LEVEL_ONE_MONEY_TOWER) return false; + return true; + } + + public static boolean shouldHarass() { + return isHarasser; + } + + public static void harass() throws GameActionException { + MapLocation myloc = rc.getLocation(); + int sym = SymmetryChecker.HSYM + SymmetryChecker.RSYM + SymmetryChecker.VSYM; + if (sym > 1 && !seenCenter) { + if (rc.getLocation().distanceSquaredTo(center) <= 9) seenCenter = true; + else { + Pathing.pathTo(center); + return; + } + } + + int closest = 1000000000; + MapLocation best = null; + MapLocation target; + MapLocation ruin = RefuelManager.firstHome; + if (SymmetryChecker.RSYM != 0 && !reachedR) { + target = new MapLocation(rc.getMapWidth() - ruin.x - 1, rc.getMapHeight() - ruin.y - 1); + if (myloc.distanceSquaredTo(target) <= 9) { + reachedR = true; + } + else { + int d = rc.getLocation().distanceSquaredTo(target); + if (d < closest) { + best = target; + closest = d; + } + } + } + if (SymmetryChecker.HSYM != 0 && !reachedH) { + target = new MapLocation(ruin.x, rc.getMapHeight() - ruin.y - 1); + if (myloc.distanceSquaredTo(target) <= 9) { + reachedH = true; + } + else { + int d = rc.getLocation().distanceSquaredTo(target); + if (d < closest) { + best = target; + closest = d; + } + } + } + if (SymmetryChecker.VSYM != 0 && !reachedV) { + target = new MapLocation(rc.getMapWidth() - ruin.x - 1, ruin.y); + if (myloc.distanceSquaredTo(target) <= 9) { + reachedV = true; + } + else { + int d = rc.getLocation().distanceSquaredTo(target); + if (d < closest) { + best = target; + closest = d; + } + } + } + if (best != null) Pathing.pathTo(best); + else isHarasser = false; + } +} \ No newline at end of file diff --git a/templates/RobotPlayer.java.jinja2 b/templates/RobotPlayer.java.jinja2 index b241eb5..21fd458 100644 --- a/templates/RobotPlayer.java.jinja2 +++ b/templates/RobotPlayer.java.jinja2 @@ -18,6 +18,7 @@ public class RobotPlayer { RefuelManager.init(rc); TowerBuild.init(rc); SquareManager.init(rc); + Harass.init(rc); Comms.init(rc); {%- macro generate_main_loop(classname) -%} while (true) { @@ -26,6 +27,7 @@ public class RobotPlayer { Globals.run(); SquareManager.run(); Attack.initTurn(); + Harass.initTurn(); {{ classname }}.run(); SymmetryChecker.run(); Comms.run(); diff --git a/templates/Soldier.java.jinja2 b/templates/Soldier.java.jinja2 index 573d745..18abf5c 100644 --- a/templates/Soldier.java.jinja2 +++ b/templates/Soldier.java.jinja2 @@ -21,14 +21,6 @@ public class Soldier { public static boolean moved = false; public static MapLocation returnLoc = null; public static boolean reallyGoing = false; - public static boolean seenCenter = false; - public static boolean harrasser = false; - public static MapLocation center = null; - public static boolean reachedH = false; - public static boolean reachedR = false; - public static boolean reachedV = false; - public static boolean wasHarrasser = false; - public static MapLocation chunkGoal = null; public static int[] dy = {0, 0, 4, -4}; @@ -40,7 +32,6 @@ public class Soldier { public static void init(RobotController rc) { Soldier.rc = rc; resourcePat = rc.getResourcePattern(); - center = new MapLocation(rc.getMapWidth() / 2, rc.getMapHeight() / 2); } public static boolean shouldUseSecond(MapLocation loc, MapLocation center) throws GameActionException { @@ -290,6 +281,7 @@ public class Soldier { moved = false; RefuelManager.setHome(); + Harass.initTurn(); boolean lowHealth = (myPaint <= (paintCapacity >> 2)); if (lowHealth != shouldGoHome) { shouldGoHome = lowHealth; @@ -297,25 +289,6 @@ public class Soldier { if (buildTower != null && shouldGoHome) returnLoc = buildTower; RefuelManager.reset(); } - - if (!harrasser && !wasHarrasser) { - harrasser = isHarasser(); - if (harrasser) wasHarrasser = true; - } - } - - public static boolean isHarasser() throws GameActionException { - // if (Math.max(rc.getMapHeight(), rc.getMapWidth()) > 40) return false; - if (rc.getRoundNum() < 50) { - if (rc.getRoundNum() > 5) return false; - if (RefuelManager.home == null) return false; - if (!rc.canSenseLocation(RefuelManager.home)) return false; - RobotInfo r = rc.senseRobotAtLocation(RefuelManager.home); - if (r == null) return false; - if (r.getType() == UnitType.LEVEL_ONE_MONEY_TOWER) return false; - return true; - } - else return false; } public static boolean getGoodColor(MapLocation m) throws GameActionException { @@ -363,66 +336,9 @@ public class Soldier { } } } - - public static void goHarrass() throws GameActionException { - int sym = SymmetryChecker.HSYM + SymmetryChecker.RSYM + SymmetryChecker.VSYM; - if (sym > 1 && !seenCenter) { - if (rc.getLocation().distanceSquaredTo(center) <= 9) seenCenter = true; - else { - Pathing.pathTo(center); - return; - } - } - - int closest = 1000000000; - MapLocation best = null; - MapLocation target; - MapLocation ruin = RefuelManager.firstHome; - if (SymmetryChecker.RSYM != 0 && !reachedR) { - target = new MapLocation(rc.getMapWidth() - ruin.x - 1, rc.getMapHeight() - ruin.y - 1); - if (myloc.distanceSquaredTo(target) <= 9) { - reachedR = true; - } - else { - int d = rc.getLocation().distanceSquaredTo(target); - if (d < closest) { - best = target; - closest = d; - } - } - } - if (SymmetryChecker.HSYM != 0 && !reachedH) { - target = new MapLocation(ruin.x, rc.getMapHeight() - ruin.y - 1); - if (myloc.distanceSquaredTo(target) <= 9) { - reachedH = true; - } - else { - int d = rc.getLocation().distanceSquaredTo(target); - if (d < closest) { - best = target; - closest = d; - } - } - } - if (SymmetryChecker.VSYM != 0 && !reachedV) { - target = new MapLocation(rc.getMapWidth() - ruin.x - 1, ruin.y); - if (myloc.distanceSquaredTo(target) <= 9) { - reachedV = true; - } - else { - int d = rc.getLocation().distanceSquaredTo(target); - if (d < closest) { - best = target; - closest = d; - } - } - } - if (best != null) Pathing.pathTo(best); - else harrasser = false; - } public static void runTurn() throws GameActionException { - rc.setIndicatorString("Harasser := " + harrasser); + rc.setIndicatorString("Harasser := " + Harass.isHarasser); if (Attack.shouldSoldierMicro()) { rc.setIndicatorString("Attacking"); Attack.soldierAttackMicro(); @@ -482,9 +398,9 @@ public class Soldier { } } - if (harrasser) { + if (Harass.shouldHarass()) { rc.setIndicatorString("Harassing"); - goHarrass(); + Harass.harass(); return; } @@ -517,7 +433,7 @@ public class Soldier { // Lay paint where I am first. myloc = rc.getLocation(); - if (canChangeColor(myloc) && rc.canAttack(myloc) && rc.getPaint() >= 50 && okToTile(myloc) && !harrasser){ + if (canChangeColor(myloc) && rc.canAttack(myloc) && rc.getPaint() >= 50 && okToTile(myloc) && !Harass.isHarasser) { rc.attack(myloc, getGoodColor(myloc)); } From c8c5c505823048f8701f1a533553e422dcbe187b Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 15:28:58 -0500 Subject: [PATCH 5/8] Stop recomputing things --- templates/Cache.java.jinja2 | 22 ++++++++++++++++++++++ templates/Mopper.java.jinja2 | 4 ++-- templates/RefuelManager.java.jinja2 | 2 +- templates/RobotPlayer.java.jinja2 | 2 ++ templates/Soldier.java.jinja2 | 4 ++-- templates/TileLoader.java.jinja2 | 4 ++-- 6 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 templates/Cache.java.jinja2 diff --git a/templates/Cache.java.jinja2 b/templates/Cache.java.jinja2 new file mode 100644 index 0000000..acf3b7f --- /dev/null +++ b/templates/Cache.java.jinja2 @@ -0,0 +1,22 @@ +package current; +import java.util.Random; +import battlecode.common.*; + +public class Cache { + public static RobotController rc; + public static RobotInfo[] friends; + public static RobotInfo[] enemies; + public static MapLocation[] ruins; + public static MapInfo[] mapInfos; + + public static void init(RobotController rc) { + Cache.rc = rc; + } + + public static void run() throws GameActionException { + friends = rc.senseNearbyRobots(-1, rc.getTeam()); + enemies = rc.senseNearbyRobots(-1, rc.getTeam().opponent()); + ruins = rc.senseNearbyRuins(-1); + mapInfos = rc.senseNearbyMapInfos(-1); + } +} \ No newline at end of file diff --git a/templates/Mopper.java.jinja2 b/templates/Mopper.java.jinja2 index 00f0f6a..c937dcc 100644 --- a/templates/Mopper.java.jinja2 +++ b/templates/Mopper.java.jinja2 @@ -27,7 +27,7 @@ public class Mopper { public static MapLocation mopTarget = null; public static void computeMopTarget() throws GameActionException { RobotInfo enemyTower = null; - MapLocation[] ruins = rc.senseNearbyRuins(-1); + MapLocation[] ruins = Cache.ruins; for (MapLocation loc: ruins) { RobotInfo r = rc.senseRobotAtLocation(loc); if (r != null && r.team != rc.getTeam()) { @@ -115,7 +115,7 @@ public class Mopper { public static void initTurn() throws GameActionException { myloc = rc.getLocation(); myPaint = rc.getPaint(); - near = rc.senseNearbyMapInfos(); + near = Cache.mapInfos; computeMopTarget(); RefuelManager.setHome(); boolean lowHealth = (myPaint <= (paintCapacity >> 2)); diff --git a/templates/RefuelManager.java.jinja2 b/templates/RefuelManager.java.jinja2 index 8cf2c54..5a4f363 100644 --- a/templates/RefuelManager.java.jinja2 +++ b/templates/RefuelManager.java.jinja2 @@ -50,7 +50,7 @@ public class RefuelManager { RobotInfo robot = rc.senseRobotAtLocation(home); homeState = getTowerState(robot); } - MapLocation[] ruins = rc.senseNearbyRuins(-1); + MapLocation[] ruins = Cache.ruins; for (MapLocation ruin: ruins) { RobotInfo robot = rc.senseRobotAtLocation(ruin); if (robot != null && robot.team != rc.getTeam()) continue; diff --git a/templates/RobotPlayer.java.jinja2 b/templates/RobotPlayer.java.jinja2 index 21fd458..c25e142 100644 --- a/templates/RobotPlayer.java.jinja2 +++ b/templates/RobotPlayer.java.jinja2 @@ -4,6 +4,7 @@ public class RobotPlayer { @SuppressWarnings("unused") public static void run(RobotController rc) throws GameActionException { {# This is technically unnecessary but this is the easiest way to get a reference to the controller #} + Cache.init(rc); Globals.init(rc); Mopper.init(rc); Pathing.init(rc); @@ -23,6 +24,7 @@ public class RobotPlayer { {%- macro generate_main_loop(classname) -%} while (true) { try { + Cache.run(); TileLoader.load(); Globals.run(); SquareManager.run(); diff --git a/templates/Soldier.java.jinja2 b/templates/Soldier.java.jinja2 index 18abf5c..666e4dc 100644 --- a/templates/Soldier.java.jinja2 +++ b/templates/Soldier.java.jinja2 @@ -275,7 +275,7 @@ public class Soldier { } public static void initTurn() throws GameActionException { - near = rc.senseNearbyMapInfos(); + near = Cache.mapInfos; myloc = rc.getLocation(); myPaint = rc.getPaint(); moved = false; @@ -346,7 +346,7 @@ public class Soldier { } // Opportunistically steal paint from adjacent towers. - MapLocation[] ruins = rc.senseNearbyRuins(-1); + MapLocation[] ruins = Cache.ruins; for (MapLocation tower: ruins) { RobotInfo r = rc.senseRobotAtLocation(tower); if (r == null || r.team != rc.getTeam()) continue; diff --git a/templates/TileLoader.java.jinja2 b/templates/TileLoader.java.jinja2 index c8ce1f2..33ce460 100644 --- a/templates/TileLoader.java.jinja2 +++ b/templates/TileLoader.java.jinja2 @@ -64,7 +64,7 @@ public class TileLoader { int myY = rc.getLocation().y; int myX = rc.getLocation().x; long lastSquare = (1L << (rc.getMapWidth() - 1)); - MapLocation[] ruins = rc.senseNearbyRuins(-1); + MapLocation[] ruins = Cache.ruins; for (int i = ruins.length; --i >= 0; ) { MapLocation ruin = ruins[i]; switch (ruin.y) { @@ -91,7 +91,7 @@ public class TileLoader { // We measure from the top right corner since that will always // Have non-negative coordinates, meaning this hashCode magic will make sense. offset = rc.getLocation().translate({{CORNER_OFFSET}}, {{CORNER_OFFSET}}).hashCode(); - mapInfos = rc.senseNearbyMapInfos(); + mapInfos = Cache.mapInfos; // The seen masks are known statically. local_seen8 = 0b001111100; From 601d40988c7bc9059a66e395c528e5fe17623b71 Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 16:26:41 -0500 Subject: [PATCH 6/8] Switch everything to use cache --- templates/Attack.java.jinja2 | 10 +++++----- templates/Comms.java.jinja2 | 12 ++++++------ templates/Globals.java.jinja2 | 4 ---- templates/MopperAttack.java.jinja2 | 4 ++-- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/templates/Attack.java.jinja2 b/templates/Attack.java.jinja2 index 169da67..fa47269 100644 --- a/templates/Attack.java.jinja2 +++ b/templates/Attack.java.jinja2 @@ -200,7 +200,7 @@ public class Attack { {%- endfor -%} boolean friendNearby = false; - for (RobotInfo r: Globals.friends) { + for (RobotInfo r: Cache.friends) { if (r.type == UnitType.SOLDIER) { friendNearby = true; break; @@ -212,15 +212,15 @@ public class Attack { boolean actionReady = (rc.isActionReady() && (rc.getPaint() >= 5) && !shouldWaitForFriend); int cooldownTurns = rc.getActionCooldownTurns(); {# {% debug('actionReady', 'cooldownTurns') %} #} - for (int i = Globals.friends.length; --i >= 0; ) { - RobotInfo robot = Globals.friends[i]; + for (int i = Cache.friends.length; --i >= 0; ) { + RobotInfo robot = Cache.friends[i]; {% for i in range(9) -%} {{ addAlly(type, i) | indent(8) -}} {% endfor %} } - for (int i = Globals.enemies.length; --i >= 0; ) { - RobotInfo robot = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0; ) { + RobotInfo robot = Cache.enemies[i]; {% for i in range(9) -%} {{ addEnemy(type, i) | indent(8) -}} {% endfor %} diff --git a/templates/Comms.java.jinja2 b/templates/Comms.java.jinja2 index 42b8cde..f301e6d 100644 --- a/templates/Comms.java.jinja2 +++ b/templates/Comms.java.jinja2 @@ -31,16 +31,16 @@ public class Comms { if (rc.canBroadcastMessage()) { rc.broadcastMessage(send); } - for (int i = Globals.friends.length; --i >= 0;) { - if (rc.canSendMessage(Globals.friends[i].location, send)) { - rc.sendMessage(Globals.friends[i].location, send); + for (int i = Cache.friends.length; --i >= 0;) { + if (rc.canSendMessage(Cache.friends[i].location, send)) { + rc.sendMessage(Cache.friends[i].location, send); } } } else { - for (int i = Globals.friends.length; --i >= 0;) if (Globals.isTower(Globals.friends[i].getType())) { - if (rc.canSendMessage(Globals.friends[i].location, send)) { - rc.sendMessage(Globals.friends[i].location, send); + for (int i = Cache.friends.length; --i >= 0;) if (Globals.isTower(Cache.friends[i].getType())) { + if (rc.canSendMessage(Cache.friends[i].location, send)) { + rc.sendMessage(Cache.friends[i].location, send); } } } diff --git a/templates/Globals.java.jinja2 b/templates/Globals.java.jinja2 index bf631b3..3895636 100644 --- a/templates/Globals.java.jinja2 +++ b/templates/Globals.java.jinja2 @@ -17,11 +17,7 @@ public class Globals { born = rc.getRoundNum(); } - public static RobotInfo[] friends; - public static RobotInfo[] enemies; public static void run() throws GameActionException { - friends = rc.senseNearbyRobots(-1, rc.getTeam()); - enemies = rc.senseNearbyRobots(-1, rc.getTeam().opponent()); roundNum = rc.getRoundNum(); updateTowerTracker(); } diff --git a/templates/MopperAttack.java.jinja2 b/templates/MopperAttack.java.jinja2 index 06ca2c3..9f96203 100644 --- a/templates/MopperAttack.java.jinja2 +++ b/templates/MopperAttack.java.jinja2 @@ -126,8 +126,8 @@ public class MopperAttack { } boolean actionReady = rc.isActionReady() || shouldChase; - for (int i = Globals.friends.length; --i >= 0; ) { - RobotInfo robot = Globals.friends[i]; + for (int i = Cache.friends.length; --i >= 0; ) { + RobotInfo robot = Cache.friends[i]; {% for i in range(9) -%} {{ addAlly(i) | indent(8) -}} {% endfor %} From c23bbe6070eef3fb27fa6f04d1de4597f9451448 Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 16:43:35 -0500 Subject: [PATCH 7/8] More things I forgot --- templates/Attack.java.jinja2 | 16 ++++++++-------- templates/MopperAttack.java.jinja2 | 24 ++++++++++++------------ templates/Soldier.java.jinja2 | 2 +- templates/Tower.java.jinja2 | 8 ++++---- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/templates/Attack.java.jinja2 b/templates/Attack.java.jinja2 index fa47269..fe939bd 100644 --- a/templates/Attack.java.jinja2 +++ b/templates/Attack.java.jinja2 @@ -28,8 +28,8 @@ public class Attack { public static void initTurn() throws GameActionException { myloc = rc.getLocation(); boolean shouldComputeReachSoon = false; - for (int i = Globals.enemies.length; --i >= 0; ) { - RobotInfo enemy = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0; ) { + RobotInfo enemy = Cache.enemies[i]; switch (enemy.type) { case SOLDIER, MOPPER, SPLASHER: continue; default: @@ -110,8 +110,8 @@ public class Attack { public static boolean shouldSoldierMicro() throws GameActionException { - for (int i = Globals.enemies.length; --i >= 0; ) { - RobotInfo enemy = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0; ) { + RobotInfo enemy = Cache.enemies[i]; switch (enemy.type) { case SOLDIER, MOPPER, SPLASHER: continue; default: if (canHitSoon(enemy.location)) return true; @@ -121,8 +121,8 @@ public class Attack { } public static void soldierTryAttack() throws GameActionException { - for (int i = Globals.enemies.length; --i >= 0;) { - RobotInfo enemy = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0;) { + RobotInfo enemy = Cache.enemies[i]; switch (enemy.type) { case SOLDIER, SPLASHER, MOPPER: continue; default: { @@ -138,8 +138,8 @@ public class Attack { public static boolean shouldSplasherMicro() throws GameActionException { // Only activate micro if you can attack a tower. - for (int i = Globals.enemies.length; --i >= 0; ) { - RobotInfo enemy = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0; ) { + RobotInfo enemy = Cache.enemies[i]; switch (enemy.type) { case SOLDIER, MOPPER, SPLASHER: continue; default: if (canHitSoon(enemy.location)) return true; diff --git a/templates/MopperAttack.java.jinja2 b/templates/MopperAttack.java.jinja2 index 9f96203..53a69c7 100644 --- a/templates/MopperAttack.java.jinja2 +++ b/templates/MopperAttack.java.jinja2 @@ -22,9 +22,9 @@ public class MopperAttack { } public static boolean shouldMopperMicro() throws GameActionException { - for (int i = Globals.enemies.length; --i >= 0; ) { - if (Globals.enemies[i].paintAmount == 0) continue; - switch (Globals.enemies[i].type) { + for (int i = Cache.enemies.length; --i >= 0; ) { + if (Cache.enemies[i].paintAmount == 0) continue; + switch (Cache.enemies[i].type) { case SOLDIER, MOPPER, SPLASHER: return true; default: return false; } @@ -34,7 +34,7 @@ public class MopperAttack { public static void mopperTryAttack() throws GameActionException { if ((rc.getPaint() < (UnitType.MOPPER.paintCapacity >> 1)) - || (Globals.enemies.length == 1)) { + || (Cache.enemies.length == 1)) { mopperHealAttack(); } else { mopperMopAttack(); @@ -42,11 +42,11 @@ public class MopperAttack { } public static void mopperHealAttack() throws GameActionException { - for (int i = Globals.enemies.length; --i >= 0; ) { - switch (Globals.enemies[i].type) { + for (int i = Cache.enemies.length; --i >= 0; ) { + switch (Cache.enemies[i].type) { case SOLDIER, MOPPER, SPLASHER: { - if (rc.canAttack(Globals.enemies[i].location)) { - rc.attack(Globals.enemies[i].location); + if (rc.canAttack(Cache.enemies[i].location)) { + rc.attack(Cache.enemies[i].location); return; } } @@ -117,8 +117,8 @@ public class MopperAttack { {%- endfor -%} boolean shouldChase = true; - for (int i = Globals.enemies.length; --i >= 0; ) { - RobotInfo robot = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0; ) { + RobotInfo robot = Cache.enemies[i]; switch (robot.type) { case MOPPER -> shouldChase = false; default -> {} @@ -133,8 +133,8 @@ public class MopperAttack { {% endfor %} } - for (int i = Globals.enemies.length; --i >= 0; ) { - RobotInfo robot = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0; ) { + RobotInfo robot = Cache.enemies[i]; {% for i in range(9) -%} {{ addEnemy(i) | indent(8) -}} {% endfor %} diff --git a/templates/Soldier.java.jinja2 b/templates/Soldier.java.jinja2 index 666e4dc..68617c1 100644 --- a/templates/Soldier.java.jinja2 +++ b/templates/Soldier.java.jinja2 @@ -148,7 +148,7 @@ public class Soldier { else Pathing.pathTo(chunkGoal); } - if (Globals.enemies.length > 0) return; + if (Cache.enemies.length > 0) return; //this just checks that nothing is like directly in the way for (int i = 3; --i >= -2; ){ diff --git a/templates/Tower.java.jinja2 b/templates/Tower.java.jinja2 index 84b9526..1857960 100644 --- a/templates/Tower.java.jinja2 +++ b/templates/Tower.java.jinja2 @@ -27,7 +27,7 @@ public class Tower { myloc = rc.getLocation(); threatLoc = null; shouldDefend = false; - for (RobotInfo r: Globals.enemies) { + for (RobotInfo r: Cache.enemies) { switch (r.type) { case SOLDIER, SPLASHER: { threatLoc = r.location; @@ -74,7 +74,7 @@ public class Tower { public static void runTurn() throws GameActionException { towerTryAttack(); - if (Globals.enemies.length > 0) + if (Cache.enemies.length > 0) enemyLastSeen = rc.getRoundNum(); UnitType t; @@ -148,8 +148,8 @@ public class Tower { int smallestSoldierHealth = 100000000; MapLocation bestNonSoldierLoc = null; int smallestNonSoldierHealth = 10000000; - for (int i = Globals.enemies.length; --i >= 0;) { - RobotInfo enemy = Globals.enemies[i]; + for (int i = Cache.enemies.length; --i >= 0;) { + RobotInfo enemy = Cache.enemies[i]; if (enemy.paintAmount == 0) continue; MapLocation loc = enemy.location; if (rc.canAttack(loc)) { From b39210000b87c05baa4bd4434b1e92be08185b98 Mon Sep 17 00:00:00 2001 From: CyrilSharma Date: Wed, 29 Jan 2025 17:30:59 -0500 Subject: [PATCH 8/8] Regression, but in the right direction maybe --- templates/Comms.java.jinja2 | 43 ++++++++++++++++++++++++++++++++++-- templates/Harass.java.jinja2 | 21 ++++++++++++++---- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/templates/Comms.java.jinja2 b/templates/Comms.java.jinja2 index f301e6d..183061b 100644 --- a/templates/Comms.java.jinja2 +++ b/templates/Comms.java.jinja2 @@ -2,12 +2,18 @@ package current; import battlecode.common.*; public class Comms { + public static MapLocation myloc; public static RobotController rc; public static void init(RobotController rc) throws GameActionException { Comms.rc = rc; } + public static int totalHarassersMade = 0; public static void run() throws GameActionException { + myloc = rc.getLocation(); + if (rc.getRoundNum() % 20 == 0) { + totalHarassersMade = 0; + } updateSym(); broadcastSym(); } @@ -19,21 +25,37 @@ public class Comms { SymmetryChecker.RSYM &= (msg & 1); SymmetryChecker.HSYM &= ((msg & 2) >> 1); SymmetryChecker.VSYM &= ((msg & 4) >> 2); + if (((msg & 8) >> 3) != 0) + Harass.isHarasser = true; } } public static void broadcastSym() throws GameActionException { + int harassersMade = 0; int R = SymmetryChecker.RSYM; int H = SymmetryChecker.HSYM; int V = SymmetryChecker.VSYM; int send = R + 2 * H + 4 * V; + int harassMessage = send + 8; + boolean shouldMakeHarassers = shouldMakeHarassers(); if (Globals.isTower(rc.getType())) { if (rc.canBroadcastMessage()) { rc.broadcastMessage(send); } for (int i = Cache.friends.length; --i >= 0;) { - if (rc.canSendMessage(Cache.friends[i].location, send)) { - rc.sendMessage(Cache.friends[i].location, send); + RobotInfo friend = Cache.friends[i]; + if ((shouldMakeHarassers) + && (friend.type == UnitType.SOLDIER) + && (myloc.distanceSquaredTo(friend.location) <= 9) + && (harassersMade < 2)) { + if (rc.canSendMessage(friend.location, harassMessage)) { + rc.sendMessage(friend.location, harassMessage); + ++harassersMade; + } + } else { + if (rc.canSendMessage(friend.location, send)) { + rc.sendMessage(friend.location, send); + } } } } @@ -45,4 +67,21 @@ public class Comms { } } } + + + public static boolean shouldMakeHarassers() throws GameActionException { + if (rc.getRoundNum() % 20 < 15) return false; + if (totalHarassersMade >= 2) return false; + int soldierCount = 0; + for (int i = Cache.friends.length; --i >= 0;) { + RobotInfo r = Cache.friends[i]; + if ((r.type == UnitType.SOLDIER) + && (rc.canSendMessage(r.location, 0)) + && (myloc.distanceSquaredTo(r.location) <= 9)) { + ++soldierCount; + } + } + if (soldierCount >= 2) return true; + return false; + } } \ No newline at end of file diff --git a/templates/Harass.java.jinja2 b/templates/Harass.java.jinja2 index e9531d5..df022d7 100644 --- a/templates/Harass.java.jinja2 +++ b/templates/Harass.java.jinja2 @@ -2,7 +2,7 @@ package current; import battlecode.common.*; public class Harass { - RobotInfo friend = null; + public static RobotInfo friend = null; public static RobotController rc; public static boolean wasHarasser = false; public static boolean isHarasser = false; @@ -21,7 +21,18 @@ public class Harass { isHarasser = decideHarasser(); if (isHarasser) wasHarasser = true; } - // We'd do this with messages later on. + + // This should be static... + friend = null; + MapLocation myloc = rc.getLocation(); + for (RobotInfo r: Cache.friends) { + if (r.type == UnitType.SOLDIER) { + if (r.location.distanceSquaredTo(myloc) <= 36) { + friend = r; + break; + } + } + } } public static boolean decideHarasser() throws GameActionException { @@ -31,11 +42,13 @@ public class Harass { if (!rc.canSenseLocation(RefuelManager.home)) return false; RobotInfo r = rc.senseRobotAtLocation(RefuelManager.home); if (r == null) return false; - if (r.getType() == UnitType.LEVEL_ONE_MONEY_TOWER) return false; - return true; + if (r.getType() == UnitType.LEVEL_ONE_PAINT_TOWER) return true; + if (r.getType() == UnitType.LEVEL_TWO_PAINT_TOWER) return true; + return false; } public static boolean shouldHarass() { + // return (isHarasser) && (friend != null); return isHarasser; }