Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 91 additions & 26 deletions java/src/s2/Mopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,104 @@ public class Mopper implements GenericRobotContoller {
RobotController rc;
Pathing pathing_engine;

public Mopper(RobotController handler) throws GameActionException{
public Mopper(RobotController handler) throws GameActionException {
rc = handler;
pathing_engine = new Pathing(handler);
}

public void run() throws GameActionException{
public void run() throws GameActionException {
System.out.println("Starting mopper logic...");


MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
if (currentTile.getPaint() != null && currentTile.getPaint().isEnemy() && rc.canAttack(rc.getLocation())) {
System.out.println("Attacking enemy paint under itself.OVERRIDE.########################################");
rc.attack(rc.getLocation(), false); // Use primary paint color
return; // Exit this turn after attacking the square under itself
}

// Reset variables
Direction bestDirection = null; // Redeclared here - causing the error
int maxEnemiesInDirection = 0; // Redeclared here - causing the error

// Define all possible directions
Direction[] directions = Direction.values();

// Initialize variables to track the best direction
Direction bestDirection = null;
int maxEnemiesInDirection = 0;


// Get the mopper's current location
MapLocation curLoc = rc.getLocation();

// Scan for all enemies within the circle of radius 2 * sqrt(2)
RobotInfo[] nearbyEnemies = rc.senseNearbyRobots(curLoc, 8, rc.getTeam().opponent());

// Count enemies in each direction based on their relative position
for (Direction dir : directions) {
int enemyCount = 0;

for (RobotInfo enemy : nearbyEnemies) {
MapLocation enemyLoc = enemy.getLocation();

// Check if the enemy lies in the swing range for the current direction
if (isInSwingRange(curLoc, enemyLoc, dir)) {
enemyCount++;
}
}

System.out.println("Direction: " + dir + ", Enemies: " + enemyCount);


// Update the best direction if this one has more enemies
if (enemyCount > maxEnemiesInDirection) {
maxEnemiesInDirection = enemyCount;
bestDirection = dir;
}
}

// Perform the mop swing in the best direction if enemies are found

if (bestDirection != null && maxEnemiesInDirection > 0) {

rc.mopSwing(bestDirection);
// Check if the robot can perform the mop swing
if (rc.isActionReady() && rc.canMopSwing(bestDirection)) {
rc.mopSwing(bestDirection);
} else {
// Handle invalid swing or cooldown
if (!rc.canMopSwing(bestDirection)) {
System.out.println("Mop swing skipped: Can't swing in direction " + bestDirection);
} else {
System.out.println("Mop swing skipped: Action cooldown not expired.");
}
// Pass onto the else block or fallback
}
} else {

bestDirection = findEnemyPaintDirection(curLoc, directions);

if (bestDirection != null) {
if (rc.isActionReady()) { // Check if the robot is ready to act
System.out.println("Clearing enemy paint in direction: " + bestDirection);

// Declare and reset targetLoc
MapLocation targetLoc = null;

// Calculate the target location based on the best direction
targetLoc = curLoc.add(bestDirection);

// Check if attack is possible and perform the attack
if (rc.canAttack(targetLoc)) {
rc.attack(targetLoc, false); // Use primary paint color to clear the tile
}
} else {
System.out.println("Attack skipped: Action cooldown not expired.");
}
} else {
pathing_engine.Move();
}
}

// If no enemies to mop swing, move randomly
pathing_engine.Move();

// Try to paint beneath us as we walk to avoid paint penalties
MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
currentTile = rc.senseMapInfo(rc.getLocation());
if (!currentTile.getPaint().isAlly() && rc.canAttack(rc.getLocation())) {
rc.attack(rc.getLocation());
rc.attack(rc.getLocation(), false); // Use primary paint color
}
}

private boolean isInSwingRange(MapLocation mopperLoc, MapLocation targetLoc, Direction swingDir) {
// Get the relative position of the target
int dx = targetLoc.x - mopperLoc.x;
int dy = targetLoc.y - mopperLoc.y;

// Check based on direction and relative positions
switch (swingDir) {
case NORTH:
Expand All @@ -88,4 +120,37 @@ private boolean isInSwingRange(MapLocation mopperLoc, MapLocation targetLoc, Dir
return false;
}
}

private Direction findEnemyPaintDirection(MapLocation curLoc, Direction[] directions) throws GameActionException {
Direction bestDirection = null;
int maxEnemyPaintCount = 0;

// Check each direction for enemy paint
for (Direction dir : directions) {
int enemyPaintCount = 0;

// Scan adjacent tiles in the direction
MapLocation targetLoc = curLoc.add(dir);

// Ensure the target location is on the map
if (rc.onTheMap(targetLoc)) {
MapInfo targetTile = rc.senseMapInfo(targetLoc);

// Count tiles with enemy paint
if (targetTile.getPaint() != null && targetTile.getPaint().isEnemy() && targetTile.isPassable()) {
enemyPaintCount++;
}


// Update the best direction if this one has more enemy paint
if (enemyPaintCount > maxEnemyPaintCount) {
maxEnemyPaintCount = enemyPaintCount;
bestDirection = dir;
}
}
}

return bestDirection;
}
}

5 changes: 3 additions & 2 deletions java/src/s2/Tower.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void run() throws GameActionException {
if (spawn_count[rtype] >= target_count[rtype]) {
rtype++;
}
if (rtype == 2) {
if (rtype > 2) {
rtype = 0;
spawn_count[0] = 0;
spawn_count[1] = 0;
Expand All @@ -74,11 +74,12 @@ public void run() throws GameActionException {
if (chipCount > 10_000) {
target_count[0] = 5;
target_count[1] = 2;
target_count[2] = 1; // Add this line for Moppers
}
if (chipCount < 650) {
target_count[0] = 3;
target_count[1] = 1;
// target_count[2] = 1;
target_count[2] = 1;
}

// Attack logic for Tower
Expand Down
Loading