Skip to content

Commit ecdd1b1

Browse files
Merge pull request #11 from CoffeeCoder1015/Agentattacks
Mopper Logic Fully Functioning #4
2 parents 0a9c3a0 + 83b5cee commit ecdd1b1

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed

java/src/s2/Mopper.java

+91-26
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,104 @@ public class Mopper implements GenericRobotContoller {
88
RobotController rc;
99
Pathing pathing_engine;
1010

11-
public Mopper(RobotController handler) throws GameActionException{
11+
public Mopper(RobotController handler) throws GameActionException {
1212
rc = handler;
1313
pathing_engine = new Pathing(handler);
1414
}
1515

16-
public void run() throws GameActionException{
16+
public void run() throws GameActionException {
1717
System.out.println("Starting mopper logic...");
18-
18+
19+
MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
20+
if (currentTile.getPaint() != null && currentTile.getPaint().isEnemy() && rc.canAttack(rc.getLocation())) {
21+
System.out.println("Attacking enemy paint under itself.OVERRIDE.########################################");
22+
rc.attack(rc.getLocation(), false); // Use primary paint color
23+
return; // Exit this turn after attacking the square under itself
24+
}
25+
26+
// Reset variables
27+
Direction bestDirection = null; // Redeclared here - causing the error
28+
int maxEnemiesInDirection = 0; // Redeclared here - causing the error
29+
1930
// Define all possible directions
2031
Direction[] directions = Direction.values();
21-
22-
// Initialize variables to track the best direction
23-
Direction bestDirection = null;
24-
int maxEnemiesInDirection = 0;
25-
32+
2633
// Get the mopper's current location
2734
MapLocation curLoc = rc.getLocation();
28-
35+
2936
// Scan for all enemies within the circle of radius 2 * sqrt(2)
3037
RobotInfo[] nearbyEnemies = rc.senseNearbyRobots(curLoc, 8, rc.getTeam().opponent());
31-
38+
3239
// Count enemies in each direction based on their relative position
3340
for (Direction dir : directions) {
3441
int enemyCount = 0;
35-
42+
3643
for (RobotInfo enemy : nearbyEnemies) {
3744
MapLocation enemyLoc = enemy.getLocation();
38-
45+
3946
// Check if the enemy lies in the swing range for the current direction
4047
if (isInSwingRange(curLoc, enemyLoc, dir)) {
4148
enemyCount++;
4249
}
4350
}
44-
45-
System.out.println("Direction: " + dir + ", Enemies: " + enemyCount);
46-
51+
4752
// Update the best direction if this one has more enemies
4853
if (enemyCount > maxEnemiesInDirection) {
4954
maxEnemiesInDirection = enemyCount;
5055
bestDirection = dir;
5156
}
5257
}
53-
54-
// Perform the mop swing in the best direction if enemies are found
58+
5559
if (bestDirection != null && maxEnemiesInDirection > 0) {
56-
57-
rc.mopSwing(bestDirection);
60+
// Check if the robot can perform the mop swing
61+
if (rc.isActionReady() && rc.canMopSwing(bestDirection)) {
62+
rc.mopSwing(bestDirection);
63+
} else {
64+
// Handle invalid swing or cooldown
65+
if (!rc.canMopSwing(bestDirection)) {
66+
System.out.println("Mop swing skipped: Can't swing in direction " + bestDirection);
67+
} else {
68+
System.out.println("Mop swing skipped: Action cooldown not expired.");
69+
}
70+
// Pass onto the else block or fallback
71+
}
5872
} else {
59-
73+
bestDirection = findEnemyPaintDirection(curLoc, directions);
74+
75+
if (bestDirection != null) {
76+
if (rc.isActionReady()) { // Check if the robot is ready to act
77+
System.out.println("Clearing enemy paint in direction: " + bestDirection);
78+
79+
// Declare and reset targetLoc
80+
MapLocation targetLoc = null;
81+
82+
// Calculate the target location based on the best direction
83+
targetLoc = curLoc.add(bestDirection);
84+
85+
// Check if attack is possible and perform the attack
86+
if (rc.canAttack(targetLoc)) {
87+
rc.attack(targetLoc, false); // Use primary paint color to clear the tile
88+
}
89+
} else {
90+
System.out.println("Attack skipped: Action cooldown not expired.");
91+
}
92+
} else {
93+
pathing_engine.Move();
94+
}
6095
}
61-
62-
// If no enemies to mop swing, move randomly
63-
pathing_engine.Move();
6496

6597
// Try to paint beneath us as we walk to avoid paint penalties
66-
MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
98+
currentTile = rc.senseMapInfo(rc.getLocation());
6799
if (!currentTile.getPaint().isAlly() && rc.canAttack(rc.getLocation())) {
68-
rc.attack(rc.getLocation());
100+
rc.attack(rc.getLocation(), false); // Use primary paint color
69101
}
70102
}
71103

72104
private boolean isInSwingRange(MapLocation mopperLoc, MapLocation targetLoc, Direction swingDir) {
73105
// Get the relative position of the target
74106
int dx = targetLoc.x - mopperLoc.x;
75107
int dy = targetLoc.y - mopperLoc.y;
76-
108+
77109
// Check based on direction and relative positions
78110
switch (swingDir) {
79111
case NORTH:
@@ -88,4 +120,37 @@ private boolean isInSwingRange(MapLocation mopperLoc, MapLocation targetLoc, Dir
88120
return false;
89121
}
90122
}
123+
124+
private Direction findEnemyPaintDirection(MapLocation curLoc, Direction[] directions) throws GameActionException {
125+
Direction bestDirection = null;
126+
int maxEnemyPaintCount = 0;
127+
128+
// Check each direction for enemy paint
129+
for (Direction dir : directions) {
130+
int enemyPaintCount = 0;
131+
132+
// Scan adjacent tiles in the direction
133+
MapLocation targetLoc = curLoc.add(dir);
134+
135+
// Ensure the target location is on the map
136+
if (rc.onTheMap(targetLoc)) {
137+
MapInfo targetTile = rc.senseMapInfo(targetLoc);
138+
139+
// Count tiles with enemy paint
140+
if (targetTile.getPaint() != null && targetTile.getPaint().isEnemy() && targetTile.isPassable()) {
141+
enemyPaintCount++;
142+
}
143+
144+
145+
// Update the best direction if this one has more enemy paint
146+
if (enemyPaintCount > maxEnemyPaintCount) {
147+
maxEnemyPaintCount = enemyPaintCount;
148+
bestDirection = dir;
149+
}
150+
}
151+
}
152+
153+
return bestDirection;
154+
}
91155
}
156+

java/src/s2/Tower.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void run() throws GameActionException {
6464
if (spawn_count[rtype] >= target_count[rtype]) {
6565
rtype++;
6666
}
67-
if (rtype == 2) {
67+
if (rtype > 2) {
6868
rtype = 0;
6969
spawn_count[0] = 0;
7070
spawn_count[1] = 0;
@@ -74,11 +74,12 @@ public void run() throws GameActionException {
7474
if (chipCount > 10_000) {
7575
target_count[0] = 5;
7676
target_count[1] = 2;
77+
target_count[2] = 1; // Add this line for Moppers
7778
}
7879
if (chipCount < 650) {
7980
target_count[0] = 3;
8081
target_count[1] = 1;
81-
// target_count[2] = 1;
82+
target_count[2] = 1;
8283
}
8384

8485
// Attack logic for Tower

0 commit comments

Comments
 (0)