Skip to content

Commit 03d4629

Browse files
author
Jasper Geurtz
committed
Fix UnitCommand.assignTarget bounds clamping and document equals/hashCode difference
1 parent 7b64d19 commit 03d4629

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎src/main/java/bwapi/Point.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public abstract class Point<T extends Point<T>> implements Comparable<Point<T>>
55

66
public final int x;
77
public final int y;
8-
private final int scalar;
8+
final int scalar;
99

1010
protected Point(final int x, final int y, final int type) {
1111
this.x = x;

‎src/main/java/bwapi/UnitCommand.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ private UnitCommand(final Unit unit, final UnitCommandType type) {
2020
}
2121

2222
private <T extends Point<T>> void assignTarget(Point<T> target) {
23-
this.x = target.x;
24-
this.y = target.y;
23+
final int maxW = (unit != null && unit.game != null && unit.game.mapPixelWidth() > 0
24+
? unit.game.mapPixelWidth()
25+
: 256 * 32) / target.scalar - 1;
26+
final int maxH = (unit != null && unit.game != null && unit.game.mapPixelHeight() > 0
27+
? unit.game.mapPixelHeight()
28+
: 256 * 32) / target.scalar - 1;
29+
this.x = Math.min(Math.max(0, target.x), maxW);
30+
this.y = Math.min(Math.max(0, target.y), maxH);
2531
}
2632

2733
public static UnitCommand attack(final Unit unit, final Position target) {
@@ -410,6 +416,7 @@ public boolean isQueued() {
410416
&& extra != 0;
411417
}
412418

419+
// Note: C++ BWAPI excludes `unit` when comparing UnitCommands, but JBWAPI includes `unit` in equals and hashCode.
413420
@Override
414421
public boolean equals(Object o) {
415422
if (this == o) return true;

0 commit comments

Comments
 (0)