Skip to content

Commit 0507796

Browse files
author
Jasper Geurtz
committed
Add missing Unit::getClosestUnit
1 parent 4a5efd0 commit 0507796

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎src/main/java/bwapi/Unit.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,40 @@ public List<Unit> getUnitsInWeaponRange(final WeaponType weapon, final UnitFilte
13281328
});
13291329
}
13301330

1331+
public Unit getClosestUnit() {
1332+
return getClosestUnit(u -> true, 999999);
1333+
}
1334+
1335+
public Unit getClosestUnit(final UnitFilter pred) {
1336+
return getClosestUnit(pred, 999999);
1337+
}
1338+
1339+
public Unit getClosestUnit(final int radius) {
1340+
return getClosestUnit(u -> true, radius);
1341+
}
1342+
1343+
/**
1344+
* Retrieves the closest unit to this one.
1345+
*
1346+
* @param pred (optional)
1347+
* A predicate used to determine which units are considered.
1348+
* If omitted or {@code null}, the closest unit owned by any
1349+
* player will be returned.
1350+
* @param radius (optional)
1351+
* The maximum radius to search for the closest unit.
1352+
* Limiting the radius can improve performance.
1353+
* If omitted, the entire map is searched.
1354+
* @return The closest matching unit, or {@code null} if no unit is found.
1355+
* @see #getUnitsInRadius(int, UnitFilter)
1356+
* @see Game#getUnitsInRadius
1357+
* @see Game#getUnitsInRectangle
1358+
*/
1359+
public Unit getClosestUnit(final UnitFilter pred, final int radius) {
1360+
return getUnitsInRadius(radius, pred == null ? u -> true : pred).stream()
1361+
.min(Comparator.comparingInt(this::getDistance))
1362+
.orElse(null);
1363+
}
1364+
13311365
/**
13321366
* Checks if the current unit is housing a @Nuke. This is only available
13331367
* for @Silos.

0 commit comments

Comments
 (0)