Skip to content

Commit e7401d7

Browse files
Merge pull request #14 from CoffeeCoder1015/MicroSoldiersMoppers
Soldier logic & Mopper logic micros done-ish
2 parents 7d115f0 + 584cd97 commit e7401d7

File tree

4 files changed

+146
-3
lines changed

4 files changed

+146
-3
lines changed

java/src/s2/Mopper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public void run() throws GameActionException {
4444
MapLocation enemyLoc = enemy.getLocation();
4545

4646
// Check if the enemy lies in the swing range for the current direction
47-
if (isInSwingRange(curLoc, enemyLoc, dir)) {
47+
if (rc.isActionReady() && isInSwingRange(curLoc, enemyLoc, dir)) {
48+
4849
enemyCount++;
4950
}
5051
}
51-
5252
// Update the best direction if this one has more enemies
5353
if (enemyCount > maxEnemiesInDirection) {
5454
maxEnemiesInDirection = enemyCount;

java/src/s2/Soldier.java

+16
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,32 @@ public class Soldier implements GenericRobotContoller {
1515
Pathing pathing_engine;
1616
boolean buildPaintTowerNext = false;
1717

18+
private TowerEngager towerEngager; // Declare TowerEngager instance
19+
1820
MapLocation currentLocation;
1921

2022
public Soldier(RobotController handler) throws GameActionException {
2123
rc = handler;
2224
pathing_engine = new Pathing(handler);
2325
SRP_pattern = rc.getResourcePattern();
26+
towerEngager = new TowerEngager(handler); // Initialize TowerEngager
2427
MoneyPattern = rc.getTowerPattern(UnitType.LEVEL_ONE_MONEY_TOWER);
2528
PaintPattern = rc.getTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER);
2629
}
2730

2831
public void run() throws GameActionException {
32+
33+
//START OF SOLDIER TOWER PRODDING CODE#######################################################################
34+
//towerEngager.engageEnemyTower();
35+
36+
if (towerEngager.engageEnemyTower()) {
37+
return; // Skip further logic and end the turn if tower engagement was successful
38+
}
39+
40+
//END OF SOLDIER TOWER PRODDING CODE#######################################################################
41+
42+
43+
2944
//get our current location at the start of each run
3045
currentLocation = rc.getLocation();
3146
if (shouldBuildSRP()) {
@@ -172,6 +187,7 @@ private boolean shouldBuildSRP() throws GameActionException{
172187
return true;
173188
}
174189

190+
175191
private void buildSRP() throws GameActionException{
176192
isBuildingSRP = true;
177193
MapInfo[] key_squares = rc.senseNearbyMapInfos(8);

java/src/s2/Tower.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void run() throws GameActionException {
7676
if (spawn_count[rtype] >= target_count[rtype]) {
7777
rtype++;
7878
}
79-
if (rtype == 2) {
79+
if (rtype > 2) {
8080
rtype = 0;
8181
spawn_count[0] = 0;
8282
spawn_count[1] = 0;

java/src/s2/TowerEngager.java

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//Includes attacking Logic and Sequence.
2+
3+
package s2;
4+
5+
import java.util.HashSet;
6+
7+
import battlecode.common.*;
8+
//import s2.generics.GenericFunc;
9+
10+
//import java.util.Random;
11+
12+
13+
public class TowerEngager {
14+
15+
16+
HashSet<MapLocation> enemyLocations = new HashSet<MapLocation>();
17+
18+
19+
RobotController rc;
20+
21+
public TowerEngager(RobotController handler) {
22+
this.rc = handler;
23+
}
24+
25+
public boolean engageEnemyTower() throws GameActionException {
26+
//System.out.println("Running tower engagement logic...");
27+
28+
// Sense nearby map info in a certain radius
29+
MapInfo[] nearbyMapInfo = rc.senseNearbyMapInfos(-1);
30+
31+
MapLocation enemyTowerLocation = null;
32+
33+
// Identify the location of an enemy tower
34+
int minDistance = Integer.MAX_VALUE; // Initialize with a large value
35+
for (MapInfo mapInfo : nearbyMapInfo) {
36+
if (mapInfo.hasRuin()) {
37+
RobotInfo robot = rc.senseRobotAtLocation(mapInfo.getMapLocation());
38+
if (robot != null && robot.getTeam() != rc.getTeam()) {
39+
int currentDistance = rc.getLocation().distanceSquaredTo(mapInfo.getMapLocation());
40+
if (currentDistance < minDistance) {
41+
System.out.println("We found something Boss! A tower?");
42+
minDistance = currentDistance; // Update the minimum distance
43+
enemyTowerLocation = mapInfo.getMapLocation(); // Update the closest tower
44+
}
45+
}
46+
}
47+
48+
}
49+
50+
// Add the closest tower to the HashSet if one was found
51+
if (enemyTowerLocation != null) {
52+
enemyLocations.add(enemyTowerLocation);
53+
}
54+
55+
56+
if (enemyTowerLocation == null) {
57+
//System.out.println("No enemy tower detected in range.");
58+
return false; // Exit the function if no enemy tower is found
59+
}
60+
61+
System.out.println("Enemy tower detected at: " + enemyTowerLocation);
62+
63+
// Check if the enemy tower is already a ruin
64+
if (enemyLocations.contains(enemyTowerLocation) && rc.senseRobotAtLocation(enemyTowerLocation) == null) {
65+
System.out.println("Enemy tower has been destroyed (turned into a ruin).");
66+
enemyLocations.remove(enemyTowerLocation); // Remove the tower's location from the HashSet
67+
return false; // Exit as there is no active enemy tower at this location
68+
}
69+
70+
71+
// Get the current robot's location
72+
MapLocation currentLocation = rc.getLocation();
73+
74+
// Calculate a direction towards the enemy tower
75+
Direction towardsTower = currentLocation.directionTo(enemyTowerLocation);
76+
77+
// Move one step towards the enemy tower if possible
78+
// Check if the robot is outside the tower's attack range but can still see it
79+
System.out.println("Debug Before Cooldown Check: Movement=" + rc.getMovementCooldownTurns() + ", Action=" + rc.getActionCooldownTurns());
80+
if (rc.getMovementCooldownTurns() > 0 || rc.getActionCooldownTurns() > 0) { //IMPORTANT RUNNING WITH 0 IS OPTIMAL BUT SHOUDL TWEAK LATER
81+
System.out.println("Movement cooldown: " + rc.getMovementCooldownTurns());
82+
System.out.println("Cooldowns too high, skipping engagement. Action cooldown is:" + rc.getActionCooldownTurns());
83+
return true;
84+
}
85+
86+
87+
if (currentLocation.distanceSquaredTo(enemyTowerLocation) > 9 &&
88+
currentLocation.distanceSquaredTo(enemyTowerLocation) <= 16 &&
89+
rc.getMovementCooldownTurns() == 0 && rc.getActionCooldownTurns() == 0) {
90+
if (rc.canMove(towardsTower)) {
91+
rc.move(towardsTower);
92+
System.out.println("Moved towards enemy tower at: " + enemyTowerLocation);
93+
94+
//Attacking sequence added here:
95+
// Attack the tower after moving closer
96+
if (rc.canAttack(enemyTowerLocation)) {
97+
rc.attack(enemyTowerLocation); // Attack the tower
98+
System.out.println("Attacked enemy tower after moving closer: " + enemyTowerLocation);
99+
}
100+
101+
return true; // Exit after taking one step
102+
}
103+
}
104+
105+
// If the robot is within the tower's attack range, move one step away
106+
if (currentLocation.distanceSquaredTo(enemyTowerLocation) <= 9) {
107+
Direction awayFromTower = enemyTowerLocation.directionTo(currentLocation);
108+
if (rc.canMove(awayFromTower)) {
109+
110+
//Attack before leaving sequence here:
111+
112+
if (rc.canAttack(enemyTowerLocation)) {
113+
rc.attack(enemyTowerLocation); // Attack the tower
114+
System.out.println("Attacked enemy tower before retreating: " + enemyTowerLocation);
115+
}
116+
117+
rc.move(awayFromTower);
118+
System.out.println("Moved away from enemy tower to avoid attack.");
119+
return true; // Move away from the tower successfully
120+
}
121+
}
122+
123+
return false;
124+
125+
}
126+
}
127+

0 commit comments

Comments
 (0)