Skip to content

Commit 6318ff4

Browse files
Merge pull request #45 from battlecode/new-sensing-functions
Add overloads for sensing functions
2 parents 8fbe69a + 106aa86 commit 6318ff4

File tree

2 files changed

+174
-12
lines changed

2 files changed

+174
-12
lines changed

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

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,21 @@ public strictfp interface RobotController {
340340
*/
341341
int senseGold(MapLocation loc) throws GameActionException;
342342

343+
/**
344+
* Return all locations that contain a nonzero amount of lead.
345+
*
346+
* @return all locations within vision radius that contain a nonzero amount of lead
347+
*
348+
* @battlecode.doc.costlymethod
349+
*/
350+
MapLocation[] senseNearbyLocationsWithLead();
351+
343352
/**
344353
* Return all locations that contain a nonzero amount of lead, within a
345354
* specified radius of your robot location.
346355
* If radiusSquared is larger than the robot's vision radius, uses the robot's
347-
* vision radius instead.
348-
*
349-
* Checks that radiusSquared is non-negative.
356+
* vision radius instead. If -1 is passed, all locations with vision radius
357+
* are returned.
350358
*
351359
* @param radiusSquared the squared radius of all locations to be returned
352360
* @return all locations that contain a nonzero amount of lead within the radius
@@ -357,12 +365,69 @@ public strictfp interface RobotController {
357365
MapLocation[] senseNearbyLocationsWithLead(int radiusSquared) throws GameActionException;
358366

359367
/**
360-
* Return all locations that contain a nonzero amount of gold, within a
368+
* Return all locations that contain a nonzero amount of lead, within a
369+
* specified radius of a center location.
370+
* If radiusSquared is larger than the robot's vision radius, uses the robot's
371+
* vision radius instead. If -1 is passed, all locations with vision radius
372+
* are returned.
373+
*
374+
* @param center the center of the search area
375+
* @param radiusSquared the squared radius of all locations to be returned
376+
* @return all locations that contain a nonzero amount of lead within the radius
377+
* @throws GameActionException if the radius is negative
378+
*
379+
* @battlecode.doc.costlymethod
380+
*/
381+
MapLocation[] senseNearbyLocationsWithLead(MapLocation center, int radiusSquared) throws GameActionException;
382+
383+
/**
384+
* Return all locations that contain at least a certain amount of lead, within a
361385
* specified radius of your robot location.
362386
* If radiusSquared is larger than the robot's vision radius, uses the robot's
363-
* vision radius instead.
387+
* vision radius instead. If -1 is passed, all locations with vision radius
388+
* are returned.
364389
*
365-
* Checks that radiusSquared is non-negative.
390+
* @param radiusSquared the squared radius of all locations to be returned
391+
* @param minLead the minimum amount of lead
392+
* @return all locations that contain at least minLead lead within the radius
393+
* @throws GameActionException if the radius is negative
394+
*
395+
* @battlecode.doc.costlymethod
396+
*/
397+
MapLocation[] senseNearbyLocationsWithLead(int radiusSquared, int minLead) throws GameActionException;
398+
399+
/**
400+
* Return all locations that contain at least a certain amount of lead, within a
401+
* specified radius of a center location.
402+
* If radiusSquared is larger than the robot's vision radius, uses the robot's
403+
* vision radius instead. If -1 is passed, all locations with vision radius
404+
* are returned.
405+
*
406+
* @param center the center of the search area
407+
* @param radiusSquared the squared radius of all locations to be returned
408+
* @param minLead the minimum amount of lead
409+
* @return all locations that contain at least minLead lead within the radius
410+
* @throws GameActionException if the radius is negative
411+
*
412+
* @battlecode.doc.costlymethod
413+
*/
414+
MapLocation[] senseNearbyLocationsWithLead(MapLocation center, int radiusSquared, int minLead) throws GameActionException;
415+
416+
/**
417+
* Return all locations that contain a nonzero amount of gold.
418+
*
419+
* @return all locations within vision radius that contain a nonzero amount of gold
420+
*
421+
* @battlecode.doc.costlymethod
422+
*/
423+
MapLocation[] senseNearbyLocationsWithGold();
424+
425+
/**
426+
* Return all locations that contain a nonzero amount of gold, within a
427+
* specified radius of your robot location.
428+
* If radiusSquared is larger than the robot's vision radius, uses the robot's
429+
* vision radius instead. If -1 is passed, all locations with vision radius
430+
* are returned.
366431
*
367432
* @param radiusSquared the squared radius of all locations to be returned
368433
* @return all locations that contain a nonzero amount of gold within the radius
@@ -372,6 +437,55 @@ public strictfp interface RobotController {
372437
*/
373438
MapLocation[] senseNearbyLocationsWithGold(int radiusSquared) throws GameActionException;
374439

440+
/**
441+
* Return all locations that contain a nonzero amount of gold, within a
442+
* specified radius of a center location.
443+
* If radiusSquared is larger than the robot's vision radius, uses the robot's
444+
* vision radius instead. If -1 is passed, all locations with vision radius
445+
* are returned.
446+
*
447+
* @param center the center of the search area
448+
* @param radiusSquared the squared radius of all locations to be returned
449+
* @return all locations that contain a nonzero amount of gold within the radius
450+
* @throws GameActionException if the radius is negative
451+
*
452+
* @battlecode.doc.costlymethod
453+
*/
454+
MapLocation[] senseNearbyLocationsWithGold(MapLocation center, int radiusSquared) throws GameActionException;
455+
456+
/**
457+
* Return all locations that contain at least a certain amount of gold, within a
458+
* specified radius of your robot location.
459+
* If radiusSquared is larger than the robot's vision radius, uses the robot's
460+
* vision radius instead. If -1 is passed, all locations with vision radius
461+
* are returned.
462+
*
463+
* @param radiusSquared the squared radius of all locations to be returned
464+
* @param minGold the minimum amount of gold
465+
* @return all locations that contain at least minGold gold within the radius
466+
* @throws GameActionException if the radius is negative
467+
*
468+
* @battlecode.doc.costlymethod
469+
*/
470+
MapLocation[] senseNearbyLocationsWithGold(int radiusSquared, int minGold) throws GameActionException;
471+
472+
/**
473+
* Return all locations that contain at least a certain amount of gold, within a
474+
* specified radius of a center location.
475+
* If radiusSquared is larger than the robot's vision radius, uses the robot's
476+
* vision radius instead. If -1 is passed, all locations with vision radius
477+
* are returned.
478+
*
479+
* @param center the center of the search area
480+
* @param radiusSquared the squared radius of all locations to be returned
481+
* @param minGold the minimum amount of gold
482+
* @return all locations that contain at least minGold gold within the radius
483+
* @throws GameActionException if the radius is negative
484+
*
485+
* @battlecode.doc.costlymethod
486+
*/
487+
MapLocation[] senseNearbyLocationsWithGold(MapLocation center, int radiusSquared, int minGold) throws GameActionException;
488+
375489
/**
376490
* Returns the location adjacent to current location in the given direction.
377491
*

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,31 +290,79 @@ public int senseGold(MapLocation loc) throws GameActionException {
290290
return this.gameWorld.getGold(loc);
291291
}
292292

293+
@Override
294+
public MapLocation[] senseNearbyLocationsWithLead() {
295+
try {
296+
return senseNearbyLocationsWithLead(getLocation(), -1, 1);
297+
} catch (GameActionException willNeverHappen) {
298+
throw new RuntimeException("impossible", willNeverHappen);
299+
}
300+
}
301+
293302
@Override
294303
public MapLocation[] senseNearbyLocationsWithLead(int radiusSquared) throws GameActionException {
304+
return senseNearbyLocationsWithLead(getLocation(), radiusSquared, 1);
305+
}
306+
307+
@Override
308+
public MapLocation[] senseNearbyLocationsWithLead(MapLocation center, int radiusSquared) throws GameActionException {
309+
return senseNearbyLocationsWithLead(center, radiusSquared, 1);
310+
}
311+
312+
@Override
313+
public MapLocation[] senseNearbyLocationsWithLead(int radiusSquared, int minLead) throws GameActionException {
314+
return senseNearbyLocationsWithLead(getLocation(), radiusSquared, minLead);
315+
}
316+
317+
@Override
318+
public MapLocation[] senseNearbyLocationsWithLead(MapLocation center, int radiusSquared, int minLead) throws GameActionException {
319+
radiusSquared = (radiusSquared == -1) ? getType().visionRadiusSquared : Math.min(radiusSquared, getType().visionRadiusSquared);
295320
if (radiusSquared < 0)
296321
throw new GameActionException(CANT_DO_THAT,
297322
"Radius squared must be non-negative.");
298-
radiusSquared = Math.min(radiusSquared, getType().visionRadiusSquared);
299323
ArrayList<MapLocation> locations = new ArrayList<>();
300-
for (MapLocation m : this.gameWorld.getAllLocationsWithinRadiusSquared(getLocation(), radiusSquared)) {
301-
if (this.gameWorld.getLead(m) > 0) {
324+
for (MapLocation m : this.gameWorld.getAllLocationsWithinRadiusSquared(center, radiusSquared)) {
325+
if (this.gameWorld.getLead(m) >= minLead) {
302326
locations.add(m);
303327
}
304328
}
305329
MapLocation[] result = new MapLocation[locations.size()];
306330
return locations.toArray(result);
307331
}
308332

333+
@Override
334+
public MapLocation[] senseNearbyLocationsWithGold() {
335+
try {
336+
return senseNearbyLocationsWithGold(getLocation(), -1, 1);
337+
} catch (GameActionException willNeverHappen) {
338+
throw new RuntimeException("impossible", willNeverHappen);
339+
}
340+
}
341+
309342
@Override
310343
public MapLocation[] senseNearbyLocationsWithGold(int radiusSquared) throws GameActionException {
344+
return senseNearbyLocationsWithGold(getLocation(), radiusSquared, 1);
345+
}
346+
347+
@Override
348+
public MapLocation[] senseNearbyLocationsWithGold(MapLocation center, int radiusSquared) throws GameActionException {
349+
return senseNearbyLocationsWithGold(center, radiusSquared, 1);
350+
}
351+
352+
@Override
353+
public MapLocation[] senseNearbyLocationsWithGold(int radiusSquared, int minGold) throws GameActionException {
354+
return senseNearbyLocationsWithGold(getLocation(), radiusSquared, minGold);
355+
}
356+
357+
@Override
358+
public MapLocation[] senseNearbyLocationsWithGold(MapLocation center, int radiusSquared, int minGold) throws GameActionException {
359+
radiusSquared = (radiusSquared == -1) ? getType().visionRadiusSquared : Math.min(radiusSquared, getType().visionRadiusSquared);
311360
if (radiusSquared < 0)
312361
throw new GameActionException(CANT_DO_THAT,
313362
"Radius squared must be non-negative.");
314-
radiusSquared = Math.min(radiusSquared, getType().visionRadiusSquared);
315363
ArrayList<MapLocation> locations = new ArrayList<>();
316-
for (MapLocation m : this.gameWorld.getAllLocationsWithinRadiusSquared(getLocation(), radiusSquared)) {
317-
if (this.gameWorld.getGold(m) > 0) {
364+
for (MapLocation m : this.gameWorld.getAllLocationsWithinRadiusSquared(center, radiusSquared)) {
365+
if (this.gameWorld.getGold(m) >= minGold) {
318366
locations.add(m);
319367
}
320368
}

0 commit comments

Comments
 (0)