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
4 changes: 2 additions & 2 deletions java/src/s2/Mopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void run() throws GameActionException {
MapLocation enemyLoc = enemy.getLocation();

// Check if the enemy lies in the swing range for the current direction
if (isInSwingRange(curLoc, enemyLoc, dir)) {
if (rc.isActionReady() && isInSwingRange(curLoc, enemyLoc, dir)) {

enemyCount++;
}
}

// Update the best direction if this one has more enemies
if (enemyCount > maxEnemiesInDirection) {
maxEnemiesInDirection = enemyCount;
Expand Down
16 changes: 16 additions & 0 deletions java/src/s2/Soldier.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ public class Soldier implements GenericRobotContoller {
Pathing pathing_engine;
boolean buildPaintTowerNext = false;

private TowerEngager towerEngager; // Declare TowerEngager instance

MapLocation currentLocation;

public Soldier(RobotController handler) throws GameActionException {
rc = handler;
pathing_engine = new Pathing(handler);
SRP_pattern = rc.getResourcePattern();
towerEngager = new TowerEngager(handler); // Initialize TowerEngager
MoneyPattern = rc.getTowerPattern(UnitType.LEVEL_ONE_MONEY_TOWER);
PaintPattern = rc.getTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER);
}

public void run() throws GameActionException {

//START OF SOLDIER TOWER PRODDING CODE#######################################################################
//towerEngager.engageEnemyTower();

if (towerEngager.engageEnemyTower()) {
return; // Skip further logic and end the turn if tower engagement was successful
}

//END OF SOLDIER TOWER PRODDING CODE#######################################################################



//get our current location at the start of each run
currentLocation = rc.getLocation();
if (shouldBuildSRP()) {
Expand Down Expand Up @@ -172,6 +187,7 @@ private boolean shouldBuildSRP() throws GameActionException{
return true;
}


private void buildSRP() throws GameActionException{
isBuildingSRP = true;
MapInfo[] key_squares = rc.senseNearbyMapInfos(8);
Expand Down
2 changes: 1 addition & 1 deletion java/src/s2/Tower.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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 Down
127 changes: 127 additions & 0 deletions java/src/s2/TowerEngager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//Includes attacking Logic and Sequence.

package s2;

import java.util.HashSet;

import battlecode.common.*;
//import s2.generics.GenericFunc;

//import java.util.Random;


public class TowerEngager {


HashSet<MapLocation> enemyLocations = new HashSet<MapLocation>();


RobotController rc;

public TowerEngager(RobotController handler) {
this.rc = handler;
}

public boolean engageEnemyTower() throws GameActionException {
//System.out.println("Running tower engagement logic...");

// Sense nearby map info in a certain radius
MapInfo[] nearbyMapInfo = rc.senseNearbyMapInfos(-1);

MapLocation enemyTowerLocation = null;

// Identify the location of an enemy tower
int minDistance = Integer.MAX_VALUE; // Initialize with a large value
for (MapInfo mapInfo : nearbyMapInfo) {
if (mapInfo.hasRuin()) {
RobotInfo robot = rc.senseRobotAtLocation(mapInfo.getMapLocation());
if (robot != null && robot.getTeam() != rc.getTeam()) {
int currentDistance = rc.getLocation().distanceSquaredTo(mapInfo.getMapLocation());
if (currentDistance < minDistance) {
System.out.println("We found something Boss! A tower?");
minDistance = currentDistance; // Update the minimum distance
enemyTowerLocation = mapInfo.getMapLocation(); // Update the closest tower
}
}
}

}

// Add the closest tower to the HashSet if one was found
if (enemyTowerLocation != null) {
enemyLocations.add(enemyTowerLocation);
}


if (enemyTowerLocation == null) {
//System.out.println("No enemy tower detected in range.");
return false; // Exit the function if no enemy tower is found
}

System.out.println("Enemy tower detected at: " + enemyTowerLocation);

// Check if the enemy tower is already a ruin
if (enemyLocations.contains(enemyTowerLocation) && rc.senseRobotAtLocation(enemyTowerLocation) == null) {
System.out.println("Enemy tower has been destroyed (turned into a ruin).");
enemyLocations.remove(enemyTowerLocation); // Remove the tower's location from the HashSet
return false; // Exit as there is no active enemy tower at this location
}


// Get the current robot's location
MapLocation currentLocation = rc.getLocation();

// Calculate a direction towards the enemy tower
Direction towardsTower = currentLocation.directionTo(enemyTowerLocation);

// Move one step towards the enemy tower if possible
// Check if the robot is outside the tower's attack range but can still see it
System.out.println("Debug Before Cooldown Check: Movement=" + rc.getMovementCooldownTurns() + ", Action=" + rc.getActionCooldownTurns());
if (rc.getMovementCooldownTurns() > 0 || rc.getActionCooldownTurns() > 0) { //IMPORTANT RUNNING WITH 0 IS OPTIMAL BUT SHOUDL TWEAK LATER
System.out.println("Movement cooldown: " + rc.getMovementCooldownTurns());
System.out.println("Cooldowns too high, skipping engagement. Action cooldown is:" + rc.getActionCooldownTurns());
return true;
}


if (currentLocation.distanceSquaredTo(enemyTowerLocation) > 9 &&
currentLocation.distanceSquaredTo(enemyTowerLocation) <= 16 &&
rc.getMovementCooldownTurns() == 0 && rc.getActionCooldownTurns() == 0) {
if (rc.canMove(towardsTower)) {
rc.move(towardsTower);
System.out.println("Moved towards enemy tower at: " + enemyTowerLocation);

//Attacking sequence added here:
// Attack the tower after moving closer
if (rc.canAttack(enemyTowerLocation)) {
rc.attack(enemyTowerLocation); // Attack the tower
System.out.println("Attacked enemy tower after moving closer: " + enemyTowerLocation);
}

return true; // Exit after taking one step
}
}

// If the robot is within the tower's attack range, move one step away
if (currentLocation.distanceSquaredTo(enemyTowerLocation) <= 9) {
Direction awayFromTower = enemyTowerLocation.directionTo(currentLocation);
if (rc.canMove(awayFromTower)) {

//Attack before leaving sequence here:

if (rc.canAttack(enemyTowerLocation)) {
rc.attack(enemyTowerLocation); // Attack the tower
System.out.println("Attacked enemy tower before retreating: " + enemyTowerLocation);
}

rc.move(awayFromTower);
System.out.println("Moved away from enemy tower to avoid attack.");
return true; // Move away from the tower successfully
}
}

return false;

}
}

Loading