Skip to content

Commit 0a9c3a0

Browse files
Merge pull request #8 from CoffeeCoder1015/soldierClean
#6 Moved SRP building into seperate function for clarity and resovled tesselation issue
2 parents d3d920a + 36f4c3d commit 0a9c3a0

File tree

1 file changed

+76
-66
lines changed

1 file changed

+76
-66
lines changed

java/src/s2/Soldier.java

+76-66
Original file line numberDiff line numberDiff line change
@@ -13,78 +13,21 @@ public class Soldier implements GenericRobotContoller {
1313
Pathing pathing_engine;
1414
boolean buildPaintTowerNext = false;
1515

16+
MapLocation currentLocation;
17+
1618
public Soldier(RobotController handler) throws GameActionException {
1719
rc = handler;
1820
pathing_engine = new Pathing(handler);
1921
SRP_pattern = rc.getResourcePattern();
2022
}
2123

2224
public void run() throws GameActionException {
23-
if (SRP_built == false && cant_find_tower_for > 40) {
24-
boolean early_exit = false;
25-
if (!rc.canMarkResourcePattern(rc.getLocation())) {
26-
early_exit = true;
27-
}
28-
if (rc.getChips() < 700) {
29-
early_exit = true;
30-
}
31-
if (!isBuildingSRP && early_exit == false) {
32-
MapInfo[] info = rc.senseNearbyMapInfos();
33-
for (MapInfo mapInfo : info) {
34-
if (mapInfo.isResourcePatternCenter()) {
35-
early_exit = true;
36-
break;
37-
}
38-
39-
// Locations within SRP range
40-
boolean noRobot = rc.senseRobotAtLocation(mapInfo.getMapLocation()) == null;
41-
boolean in_range = mapInfo.getMapLocation().isWithinDistanceSquared(rc.getLocation(), 1);
42-
if (in_range) {
43-
// boolean isPaintedbyAlly =mapInfo.getPaint().isAlly();
44-
boolean hasTower = mapInfo.hasRuin() && !noRobot;
45-
if (mapInfo.isWall() || hasTower) {
46-
early_exit = true;
47-
break;
48-
}
49-
}
50-
51-
boolean noTower = mapInfo.hasRuin() && noRobot;
52-
if (noTower) {
53-
early_exit = true;
54-
break;
55-
}
56-
}
57-
}
58-
if (!early_exit && rc.isActionReady()) {
59-
isBuildingSRP = true;
60-
MapLocation curr_loc = rc.getLocation();
61-
MapInfo[] key_squares = rc.senseNearbyMapInfos(8);
62-
if (rc.senseMapInfo(rc.getLocation()).getPaint() != PaintType.ALLY_SECONDARY) {
63-
rc.attack(curr_loc, true);
64-
}else{
65-
for (MapInfo mapInfo : key_squares) {
66-
MapLocation relative_loc = mapInfo.getMapLocation().translate(-curr_loc.x, -curr_loc.y);
67-
rc.setIndicatorDot(curr_loc, 0,0,255);
68-
// System.out.println(relative_loc);
69-
boolean color = SRP_pattern[relative_loc.x+2][-(relative_loc.y-2)];
70-
PaintType correct_paint = PaintType.ALLY_PRIMARY;
71-
if (color) {
72-
correct_paint = PaintType.ALLY_SECONDARY;
73-
}
74-
if (mapInfo.getPaint() != correct_paint) {
75-
rc.attack(mapInfo.getMapLocation(),color);
76-
break;
77-
}
78-
}
79-
}
80-
if (rc.canCompleteResourcePattern(curr_loc)) {
81-
rc.completeResourcePattern(curr_loc);
82-
rc.setTimelineMarker("Built SRP", 255,0,0);
83-
SRP_built = true;
84-
isBuildingSRP = false;
85-
}
86-
}
25+
//get our current location at the start of each run
26+
currentLocation = rc.getLocation();
27+
if (shouldBuildSRP()) {
28+
buildSRP();
8729
}
30+
8831
boolean found = false;
8932
if (!isBuildingSRP) {
9033
found = buildRuins();
@@ -125,7 +68,7 @@ private boolean buildRuins() throws GameActionException {
12568
for (MapInfo tile : nearbyTiles) {
12669
// Make sure the ruin is not already complete (has no tower on it)
12770
if (tile.hasRuin() && rc.senseRobotAtLocation(tile.getMapLocation()) == null) {
128-
int checkDist = tile.getMapLocation().distanceSquaredTo(rc.getLocation());
71+
int checkDist = tile.getMapLocation().distanceSquaredTo(currentLocation);
12972
if (checkDist < curDist) {
13073
curDist = checkDist;
13174
curRuin = tile;
@@ -136,7 +79,7 @@ private boolean buildRuins() throws GameActionException {
13679

13780
if (curRuin != null) {
13881
MapLocation targetLoc = curRuin.getMapLocation();
139-
Direction dir = rc.getLocation().directionTo(targetLoc);
82+
Direction dir = currentLocation.directionTo(targetLoc);
14083
if (rc.canMove(dir))
14184
rc.move(dir);
14285
// Mark the pattern we need to draw to build a tower here if we haven't already.
@@ -185,4 +128,71 @@ private boolean buildRuins() throws GameActionException {
185128
return false;
186129
}
187130

131+
private boolean shouldBuildSRP() throws GameActionException{
132+
/* early exit conditions */
133+
if (SRP_built || cant_find_tower_for < 40 || !rc.canMarkResourcePattern(currentLocation) || rc.getChips() < 700 || !rc.isActionReady()) {
134+
return false;
135+
}
136+
/* bad location exit */
137+
if (!isBuildingSRP) { // not already doing it
138+
MapInfo[] info = rc.senseNearbyMapInfos(-1);
139+
for (MapInfo mapInfo : info) {
140+
MapLocation tileLocation = mapInfo.getMapLocation();
141+
if (mapInfo.isResourcePatternCenter()) { // will overlap already built SRP
142+
// Resource centers at the very edge of the vision radius
143+
// that are on the same x or y axis as the current are overlappable without issue
144+
// relative locations (0,4) (-4,0) (0,-4) (4,0)
145+
MapLocation relativeLocation = tileLocation.translate(-currentLocation.x, -currentLocation.y);
146+
int x = relativeLocation.x;
147+
int y = relativeLocation.y;
148+
boolean case1 = x == 0 && (y == 4 || y == -4);
149+
boolean case2 = y == 0 && (x == 4 || x == -4);
150+
if (!(case1 || case2)) {
151+
return false;
152+
}
153+
}
154+
155+
// Has wall or tower blocking
156+
// Locations within SRP range
157+
boolean noRobot = rc.senseRobotAtLocation(tileLocation) == null;
158+
// cant build if there is uncompleted ruin
159+
// this makes sure SRPs wont overlap onto TowerPatterns
160+
boolean noTower = mapInfo.hasRuin() && noRobot;
161+
if (noTower) {
162+
return false;
163+
}
164+
}
165+
}
166+
return true;
167+
}
168+
169+
private void buildSRP() throws GameActionException{
170+
isBuildingSRP = true;
171+
MapInfo[] key_squares = rc.senseNearbyMapInfos(8);
172+
if (rc.senseMapInfo(currentLocation).getPaint() != PaintType.ALLY_SECONDARY) {
173+
rc.attack(currentLocation, true);
174+
} else {
175+
for (MapInfo mapInfo : key_squares) {
176+
MapLocation relative_loc = mapInfo.getMapLocation().translate(-currentLocation.x, -currentLocation.y);
177+
rc.setIndicatorDot(currentLocation, 0, 0, 255);
178+
// System.out.println(relative_loc);
179+
boolean color = SRP_pattern[relative_loc.x + 2][-(relative_loc.y - 2)];
180+
PaintType correct_paint = PaintType.ALLY_PRIMARY;
181+
if (color) {
182+
correct_paint = PaintType.ALLY_SECONDARY;
183+
}
184+
if (mapInfo.getPaint() != correct_paint) {
185+
rc.attack(mapInfo.getMapLocation(), color);
186+
break;
187+
}
188+
}
189+
}
190+
if (rc.canCompleteResourcePattern(currentLocation)) {
191+
rc.completeResourcePattern(currentLocation);
192+
rc.setTimelineMarker("Built SRP", 255, 0, 0);
193+
SRP_built = true;
194+
isBuildingSRP = false;
195+
}
196+
}
197+
188198
}

0 commit comments

Comments
 (0)