@@ -8,72 +8,104 @@ public class Mopper implements GenericRobotContoller {
8
8
RobotController rc ;
9
9
Pathing pathing_engine ;
10
10
11
- public Mopper (RobotController handler ) throws GameActionException {
11
+ public Mopper (RobotController handler ) throws GameActionException {
12
12
rc = handler ;
13
13
pathing_engine = new Pathing (handler );
14
14
}
15
15
16
- public void run () throws GameActionException {
16
+ public void run () throws GameActionException {
17
17
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
+
19
30
// Define all possible directions
20
31
Direction [] directions = Direction .values ();
21
-
22
- // Initialize variables to track the best direction
23
- Direction bestDirection = null ;
24
- int maxEnemiesInDirection = 0 ;
25
-
32
+
26
33
// Get the mopper's current location
27
34
MapLocation curLoc = rc .getLocation ();
28
-
35
+
29
36
// Scan for all enemies within the circle of radius 2 * sqrt(2)
30
37
RobotInfo [] nearbyEnemies = rc .senseNearbyRobots (curLoc , 8 , rc .getTeam ().opponent ());
31
-
38
+
32
39
// Count enemies in each direction based on their relative position
33
40
for (Direction dir : directions ) {
34
41
int enemyCount = 0 ;
35
-
42
+
36
43
for (RobotInfo enemy : nearbyEnemies ) {
37
44
MapLocation enemyLoc = enemy .getLocation ();
38
-
45
+
39
46
// Check if the enemy lies in the swing range for the current direction
40
47
if (isInSwingRange (curLoc , enemyLoc , dir )) {
41
48
enemyCount ++;
42
49
}
43
50
}
44
-
45
- System .out .println ("Direction: " + dir + ", Enemies: " + enemyCount );
46
-
51
+
47
52
// Update the best direction if this one has more enemies
48
53
if (enemyCount > maxEnemiesInDirection ) {
49
54
maxEnemiesInDirection = enemyCount ;
50
55
bestDirection = dir ;
51
56
}
52
57
}
53
-
54
- // Perform the mop swing in the best direction if enemies are found
58
+
55
59
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
+ }
58
72
} 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
+ }
60
95
}
61
-
62
- // If no enemies to mop swing, move randomly
63
- pathing_engine .Move ();
64
96
65
97
// 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 ());
67
99
if (!currentTile .getPaint ().isAlly () && rc .canAttack (rc .getLocation ())) {
68
- rc .attack (rc .getLocation ());
100
+ rc .attack (rc .getLocation (), false ); // Use primary paint color
69
101
}
70
102
}
71
103
72
104
private boolean isInSwingRange (MapLocation mopperLoc , MapLocation targetLoc , Direction swingDir ) {
73
105
// Get the relative position of the target
74
106
int dx = targetLoc .x - mopperLoc .x ;
75
107
int dy = targetLoc .y - mopperLoc .y ;
76
-
108
+
77
109
// Check based on direction and relative positions
78
110
switch (swingDir ) {
79
111
case NORTH :
@@ -88,4 +120,37 @@ private boolean isInSwingRange(MapLocation mopperLoc, MapLocation targetLoc, Dir
88
120
return false ;
89
121
}
90
122
}
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
+ }
91
155
}
156
+
0 commit comments