|
| 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