Skip to content

Commit 6c651fb

Browse files
committed
Merge branch 'engine-testing-3' into schema
2 parents 511f67d + aae3ba3 commit 6c651fb

13 files changed

Lines changed: 141 additions & 49 deletions

File tree

engine/src/main/battlecode/common/MapLocation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ public final int distanceSquaredTo(MapLocation location) {
113113
* location.
114114
*
115115
* @param location the location to compute the squared distance to
116-
* @return the ceiling of the squared distance to the given location
116+
* @return the squared distance to the given location
117117
*
118118
* @battlecode.doc.costlymethod
119119
*/
120-
public final int bottomLeftDistanceSquaredTo(MapLocation location) {
120+
public final float bottomLeftDistanceSquaredTo(MapLocation location) {
121121
double dx = this.x + 0.5 - location.x;
122122
double dy = this.y + 0.5 - location.y;
123123

124-
return (int) Math.ceil(dx * dx + dy * dy);
124+
return (float) (dx * dx + dy * dy);
125125
}
126126

127127
/**

engine/src/main/battlecode/schema/PlaceTrap.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public final class PlaceTrap extends Struct {
2929

3030
public int loc() { return bb.getShort(bb_pos + 0) & 0xFFFF; }
3131
public byte team() { return bb.get(bb_pos + 2); }
32+
public boolean isRatTrapType() { return 0!=bb.get(bb_pos + 3); }
3233

33-
public static int createPlaceTrap(FlatBufferBuilder builder, int loc, byte team) {
34+
public static int createPlaceTrap(FlatBufferBuilder builder, int loc, byte team, boolean isRatTrapType) {
3435
builder.prep(2, 4);
35-
builder.pad(1);
36+
builder.putBoolean(isRatTrapType);
3637
builder.putByte(team);
3738
builder.putShort((short) loc);
3839
return builder.offset();

engine/src/main/battlecode/server/GameMaker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,15 +620,15 @@ public void addBecomeRatKingAction(int id) {
620620
public void addPlaceTrapAction(int trapID, MapLocation loc, Team team, TrapType type) {
621621
applyToBuilders((builder) -> {
622622
byte teamID = TeamMapping.id(team);
623-
int action = PlaceTrap.createPlaceTrap(builder, locationToInt(loc), teamID);
623+
int action = PlaceTrap.createPlaceTrap(builder, locationToInt(loc), teamID, type==TrapType.RAT_TRAP);
624624
builder.addAction(action, Action.PlaceTrap);
625625
});
626626
}
627627

628-
public void addRemoveTrapAction(int trapID, Team team) {
628+
public void addRemoveTrapAction(MapLocation loc, Team team) {
629629
applyToBuilders((builder) -> {
630630
byte teamID = TeamMapping.id(team);
631-
int action = RemoveTrap.createRemoveTrap(builder, trapID, teamID);
631+
int action = RemoveTrap.createRemoveTrap(builder, locationToInt(loc), teamID);
632632
builder.addAction(action, Action.RemoveTrap);
633633
});
634634
}

engine/src/main/battlecode/world/GameWorld.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public class GameWorld {
5858
private ArrayList<CheeseMine> cheeseMines;
5959
private CheeseMine[] cheeseMineLocs;
6060

61+
// bfs map
62+
private Direction[][] bfs_map;
63+
6164
private int numCats;
6265

6366
private int[][] sharedArray;
@@ -167,6 +170,65 @@ public GameWorld(LiveMap gm, RobotControlProvider cp, GameMaker.MatchMaker match
167170
spawnRobot(robotInfo.ID, robotInfo.type, newLocation, robotInfo.direction, robotInfo.chirality,
168171
robotInfo.team);
169172
}
173+
174+
// cat bfs map
175+
this.bfs_map = new Direction[width*height][width*height];
176+
177+
for (int target_x=0; target_x < width; target_x++){
178+
for (int target_y=0; target_y < width; target_y++){
179+
MapLocation source = new MapLocation(target_x, target_y);
180+
bfsFromTarget(source);
181+
}
182+
}
183+
184+
185+
}
186+
187+
public void bfsFromTarget(MapLocation target){
188+
// bfs form target to all possible sources, set source direction to target
189+
190+
Queue<MapLocation> queue = new LinkedList<MapLocation>();
191+
queue.add(target);
192+
193+
this.bfs_map[locationToIndex(target)][locationToIndex(target)] = Direction.CENTER;
194+
195+
while(!queue.isEmpty()){
196+
MapLocation nextLoc = queue.poll();
197+
198+
// check neighbors
199+
for (Direction d : Direction.allDirections()){
200+
if (d == Direction.CENTER)
201+
continue;
202+
203+
MapLocation neighbor = nextLoc.add(d);
204+
205+
if (this.gameMap.onTheMap(neighbor) && this.bfs_map[locationToIndex(neighbor)][locationToIndex(target)] != null){
206+
// visited already
207+
continue;
208+
}
209+
210+
// check this path works for all cat locations
211+
boolean validPath = true;
212+
Direction[] dirsFromCenterLoc = {Direction.CENTER, Direction.NORTH, Direction.NORTHEAST, Direction.EAST};
213+
for (Direction dirFromCenter : dirsFromCenterLoc){
214+
MapLocation neighborCorner = neighbor.add(dirFromCenter);
215+
if (!this.gameMap.onTheMap(neighborCorner) || this.getWall(neighborCorner)){
216+
// location not on map or has walls
217+
validPath = false;
218+
break;
219+
}
220+
}
221+
if (validPath){
222+
Direction reverseDirection = d.opposite();
223+
this.bfs_map[locationToIndex(neighbor)][locationToIndex(target)] = reverseDirection;
224+
queue.add(neighbor);
225+
}
226+
}
227+
}
228+
}
229+
230+
public Direction getBfsDir(MapLocation from, MapLocation to){
231+
return this.bfs_map[locationToIndex(from)][locationToIndex(to)];
170232
}
171233

172234
/**

engine/src/main/battlecode/world/InternalRobot.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public class InternalRobot implements Comparable<InternalRobot> {
6969

7070
private int currentWaypoint;
7171
private CatStateType catState;
72-
private MapLocation[] catWaypoints;
72+
private MapLocation[] rawCatWaypoints;
73+
private MapLocation[] adjustedCatWaypoints;
7374
private MapLocation catTargetLoc;
7475
private int catTurns;
7576
private RobotInfo catTarget;
@@ -126,14 +127,36 @@ public InternalRobot(GameWorld gw, int id, Team team, UnitType type, MapLocation
126127

127128
// set waypoints
128129
int[] waypointIndexLocations = gw.getGameMap().getCatWaypointsByID(this.ID);
129-
catWaypoints = new MapLocation[waypointIndexLocations.length];
130-
for (int i = 0; i < waypointIndexLocations.length; i++)
131-
catWaypoints[i] = this.gameWorld.indexToLocation(waypointIndexLocations[i]);
130+
rawCatWaypoints = new MapLocation[waypointIndexLocations.length];
131+
adjustedCatWaypoints = new MapLocation[waypointIndexLocations.length];
132+
for (int i = 0; i < waypointIndexLocations.length; i++){
133+
rawCatWaypoints[i] = this.gameWorld.indexToLocation(waypointIndexLocations[i]);
134+
if (this.chirality == 0){
135+
adjustedCatWaypoints[i] = rawCatWaypoints[i];
136+
}
137+
else{
138+
MapSymmetry symmetry = this.gameWorld.getGameMap().getSymmetry();
139+
switch (symmetry) {
140+
case VERTICAL:
141+
adjustedCatWaypoints[i] = rawCatWaypoints[i].add(Direction.EAST.opposite());
142+
break;
143+
case HORIZONTAL:
144+
adjustedCatWaypoints[i] = rawCatWaypoints[i].add(Direction.NORTH.opposite());
145+
break;
146+
case ROTATIONAL:
147+
adjustedCatWaypoints[i] = rawCatWaypoints[i].add(Direction.NORTHEAST.opposite());
148+
break;
149+
default:
150+
throw new RuntimeException("Invalid symmetry");
151+
}
152+
}
153+
}
132154

133-
this.catTargetLoc = this.catWaypoints[0];
155+
this.catTargetLoc = this.adjustedCatWaypoints[0];
134156

135157
} else {
136-
this.catWaypoints = new MapLocation[0];
158+
this.rawCatWaypoints = new MapLocation[0];
159+
this.adjustedCatWaypoints = new MapLocation[0];
137160
this.catTargetLoc = null;
138161
}
139162

@@ -1114,21 +1137,10 @@ public void processEndOfTurn() {
11141137
switch (this.catState) {
11151138
case EXPLORE:
11161139

1117-
MapLocation waypoint = catWaypoints[currentWaypoint];
1118-
1119-
if (this.location.equals(waypoint)) {
1120-
currentWaypoint = (currentWaypoint + 1) % catWaypoints.length;
1121-
}
1122-
1123-
this.dir = this.location.directionTo(catWaypoints[currentWaypoint]);
1124-
11251140
// try seeing nearby rats
11261141
Message squeak = getFrontMessage();
11271142
RobotInfo[] nearbyRobots = this.controller.senseNearbyRobots();
11281143

1129-
// this.gameWorld.getAllRobotsWithinConeRadiusSquared(this.location,
1130-
// this.dir, getVisionConeAngle(), getVisionRadiusSquared(), team);
1131-
11321144
boolean ratVisible = false;
11331145
RobotInfo rat = null;
11341146

@@ -1148,21 +1160,26 @@ public void processEndOfTurn() {
11481160
this.catTargetLoc = squeak.getSource();
11491161
this.catState = CatStateType.CHASE;
11501162
} else {
1151-
this.catTargetLoc = waypoint;
1163+
MapLocation waypoint = adjustedCatWaypoints[currentWaypoint];
1164+
1165+
if (this.location.equals(waypoint)) {
1166+
currentWaypoint = (currentWaypoint + 1) % adjustedCatWaypoints.length;
1167+
}
1168+
this.catTargetLoc = adjustedCatWaypoints[currentWaypoint];
11521169
}
11531170

1154-
Direction toWaypoint = this.location.directionTo(this.catTargetLoc);
1155-
this.dir = this.location.directionTo(this.catTargetLoc);
1171+
this.dir = this.gameWorld.getBfsDir(this.location, this.catTargetLoc);
11561172

1157-
if (this.controller.canMove(toWaypoint)) {
1173+
if (this.controller.canMove(this.dir)) {
1174+
System.out.println("TRYING TO MOVE");
11581175
try {
1159-
this.controller.move(toWaypoint);
1176+
this.controller.move(this.dir);
11601177
} catch (GameActionException e) {
11611178
}
11621179

11631180
} else {
11641181
for (MapLocation partLoc : this.getAllPartLocations()) {
1165-
MapLocation nextLoc = partLoc.add(toWaypoint);
1182+
MapLocation nextLoc = partLoc.add(this.dir);
11661183

11671184
if (this.controller.canRemoveDirt(nextLoc)) {
11681185
System.out.println("stuck more here cuz of dirt " + this.gameWorld.currentRound);
@@ -1192,8 +1209,7 @@ public void processEndOfTurn() {
11921209
case CHASE:
11931210
System.out.println("CAT " + this.ID + "Entering Chase");
11941211

1195-
Direction toTarget = this.location.directionTo(this.catTargetLoc);
1196-
this.dir = toTarget;
1212+
this.dir = this.gameWorld.getBfsDir(this.location, this.catTargetLoc);
11971213

11981214
if (this.location.equals(this.catTargetLoc)) {
11991215
this.catState = CatStateType.SEARCH;
@@ -1290,7 +1306,7 @@ public void processEndOfTurn() {
12901306

12911307
}
12921308

1293-
this.dir = this.location.directionTo(this.catTargetLoc);
1309+
this.dir = this.gameWorld.getBfsDir(this.location, this.catTargetLoc);
12941310

12951311
// pounce towards target if possible
12961312
pounceTraj = canPounce(this.catTargetLoc);

engine/src/main/battlecode/world/RobotControllerImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ private void assertCanActLocation(MapLocation loc, int maxRadiusSquared) throws
213213
// assumes maxRadiusSquared <= visionRadiusSquared.
214214
// This handles the angle checking, so we only check distance.
215215
assertCanSenseLocation(loc);
216-
int distance = (this.getType().usesBottomLeftLocationForDistance())
216+
float distance = (this.getType().usesBottomLeftLocationForDistance())
217217
? (getLocation().bottomLeftDistanceSquaredTo(loc))
218218
: (getLocation().distanceSquaredTo(loc));
219219

220-
int addDistance = (this.getType().size > 1)
221-
? (int) Math.ceil((this.getType().size / (2.0) + Math.sqrt((double) maxRadiusSquared))
220+
float addDistance = (this.getType().size > 1)
221+
? (float) Math.ceil((this.getType().size / (2.0) + Math.sqrt((double) maxRadiusSquared))
222222
* (this.getType().size / 2.0 + Math.sqrt((double) maxRadiusSquared)))
223223
: maxRadiusSquared;
224224

@@ -371,7 +371,7 @@ public void removeRatTrap(MapLocation loc) throws GameActionException {
371371
assertCanRemoveRatTrap(loc);
372372
Trap trap = this.gameWorld.getTrap(loc);
373373
this.gameWorld.removeTrap(loc);
374-
this.gameWorld.getMatchMaker().addRemoveTrapAction(trap.getId(), trap.getTeam());
374+
this.gameWorld.getMatchMaker().addRemoveTrapAction(trap.getLocation(), trap.getTeam());
375375
}
376376

377377
@Override
@@ -405,7 +405,7 @@ public void removeCatTrap(MapLocation loc) throws GameActionException {
405405
assertCanRemoveCatTrap(loc);
406406
Trap trap = this.gameWorld.getTrap(loc);
407407
this.gameWorld.removeTrap(loc);
408-
this.gameWorld.getMatchMaker().addRemoveTrapAction(trap.getId(), trap.getTeam());
408+
this.gameWorld.getMatchMaker().addRemoveTrapAction(trap.getLocation(), trap.getTeam());
409409
}
410410

411411
@Override

schema/battlecode.fbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ struct CatPounce {
168168
struct PlaceTrap {
169169
loc: ushort;
170170
team: byte;
171+
isRatTrapType: bool;
171172
}
172173

173174
/// Remove a trap at location

schema/java/battlecode/schema/PlaceTrap.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public final class PlaceTrap extends Struct {
2929

3030
public int loc() { return bb.getShort(bb_pos + 0) & 0xFFFF; }
3131
public byte team() { return bb.get(bb_pos + 2); }
32+
public boolean isRatTrapType() { return 0!=bb.get(bb_pos + 3); }
3233

33-
public static int createPlaceTrap(FlatBufferBuilder builder, int loc, byte team) {
34+
public static int createPlaceTrap(FlatBufferBuilder builder, int loc, byte team, boolean isRatTrapType) {
3435
builder.prep(2, 4);
35-
builder.pad(1);
36+
builder.putBoolean(isRatTrapType);
3637
builder.putByte(team);
3738
builder.putShort((short) loc);
3839
return builder.offset();

schema/js/battlecode/schema/place-trap.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export declare class PlaceTrap {
88
__init(i: number, bb: flatbuffers.ByteBuffer): PlaceTrap;
99
loc(): number;
1010
team(): number;
11+
isRatTrapType(): boolean;
1112
static sizeOf(): number;
12-
static createPlaceTrap(builder: flatbuffers.Builder, loc: number, team: number): flatbuffers.Offset;
13+
static createPlaceTrap(builder: flatbuffers.Builder, loc: number, team: number, isRatTrapType: boolean): flatbuffers.Offset;
1314
}

schema/js/battlecode/schema/place-trap.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ var PlaceTrap = /** @class */ (function () {
2121
PlaceTrap.prototype.team = function () {
2222
return this.bb.readInt8(this.bb_pos + 2);
2323
};
24+
PlaceTrap.prototype.isRatTrapType = function () {
25+
return !!this.bb.readInt8(this.bb_pos + 3);
26+
};
2427
PlaceTrap.sizeOf = function () {
2528
return 4;
2629
};
27-
PlaceTrap.createPlaceTrap = function (builder, loc, team) {
30+
PlaceTrap.createPlaceTrap = function (builder, loc, team, isRatTrapType) {
2831
builder.prep(2, 4);
29-
builder.pad(1);
32+
builder.writeInt8(Number(Boolean(isRatTrapType)));
3033
builder.writeInt8(team);
3134
builder.writeInt16(loc);
3235
return builder.offset();

0 commit comments

Comments
 (0)