Skip to content

Commit fb31ad8

Browse files
author
bytekeeper
committed
Fixed getUnitsInRectangle, added a unit test. Also simplified canBuildHere.
1 parent 9c8cab0 commit fb31ad8

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
<version>2.23.4</version>
7575
<scope>test</scope>
7676
</dependency>
77-
</dependencies>
7877

78+
<dependency>
79+
<groupId>org.assertj</groupId>
80+
<artifactId>assertj-core</artifactId>
81+
<version>3.11.1</version>
82+
<scope>test</scope>
83+
</dependency>
84+
</dependencies>
7985
</project>

src/main/java/bwapi/Game.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,9 @@ public List<Unit> getUnitsInRectangle(final int left, final int top, final int r
454454
}
455455

456456
public List<Unit> getUnitsInRectangle(final int left, final int top, final int right, final int bottom, final UnitFilter filter) {
457-
return getAllUnits().stream().filter(u -> {
458-
final Position p = u.getPosition();
459-
return left <= p.x && top <= p.y && p.x < right && p.y < bottom && filter.operation(u);
460-
}).collect(Collectors.toList());
457+
return getAllUnits().stream().filter(u ->
458+
left <= u.getRight() && top <= u.getBottom() && right >= u.getLeft() && bottom >= u.getTop() && filter.operation(u))
459+
.collect(Collectors.toList());
461460
}
462461

463462
public List<Unit> getUnitsInRectangle(final Position leftTop, final Position rightBottom) {
@@ -719,12 +718,10 @@ public boolean canBuildHere(final TilePosition position, final UnitType type, fi
719718
// Ground getUnit dimension check
720719
if (type != Special_Start_Location) {
721720
final Position targPos = lt.toPosition().add(type.tileSize().toPosition().divide(2));
722-
final List<Unit> unitsInRect = getUnitsInRectangle(lt.toPosition(), rb.toPosition(),
723-
u -> !u.isFlying() && !u.isLoaded() && (builder != u || type == Zerg_Nydus_Canal)
724-
&& u.getLeft() <= targPos.x + type.dimensionRight()
725-
&& u.getTop() <= targPos.y + type.dimensionDown()
726-
&& u.getRight() >= targPos.x - type.dimensionLeft()
727-
&& u.getBottom() >= targPos.y - type.dimensionUp());
721+
final List<Unit> unitsInRect = getUnitsInRectangle(
722+
targPos.subtract(new Position(type.dimensionLeft(), type.dimensionUp())),
723+
targPos.add(new Position(type.dimensionRight(), type.dimensionDown())),
724+
u -> !u.isFlying() && !u.isLoaded() && (builder != u || type == Zerg_Nydus_Canal));
728725

729726
for (final Unit u : unitsInRect) {
730727
// Addons can be placed over units that can move, pushing them out of the way

src/test/java/bwapi/GameTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package bwapi;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.BDDMockito.given;
5+
import static org.mockito.Mockito.mock;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import org.junit.Before;
10+
import org.junit.experimental.theories.DataPoints;
11+
import org.junit.experimental.theories.FromDataPoints;
12+
import org.junit.experimental.theories.Theories;
13+
import org.junit.experimental.theories.Theory;
14+
import org.junit.runner.RunWith;
15+
16+
@RunWith(Theories.class)
17+
public class GameTest {
18+
19+
private List<Unit> allUnits = new ArrayList<>();
20+
private Game sut = new Game(mock(Client.class)) {
21+
@Override
22+
public List<Unit> getAllUnits() {
23+
return allUnits;
24+
}
25+
};
26+
private Unit dummy;
27+
28+
@DataPoints("overlapping")
29+
public static final Pair[] overlapping = {
30+
new Pair<>(new Position(15, 35), new Position(20, 40)),
31+
new Pair<>(new Position(15, 32), new Position(25, 38)),
32+
new Pair<>(new Position(15, 28), new Position(25, 42)),
33+
new Pair<>(new Position(5, 35), new Position(15, 40)),
34+
new Pair<>(new Position(0, 32), new Position(15, 38)),
35+
new Pair<>(new Position(0, 28), new Position(15, 42)),
36+
new Pair<>(new Position(12, 25), new Position(22, 35)),
37+
new Pair<>(new Position(15, 38), new Position(28, 42)),
38+
new Pair<>(new Position(5, 20), new Position(25, 45))
39+
};
40+
41+
@DataPoints("non-overlapping")
42+
public static final Pair[] nonOverlapping = {
43+
new Pair<>(new Position(0, 0), new Position(200, 20)),
44+
new Pair<>(new Position(50, 0), new Position(55, 200)),
45+
new Pair<>(new Position(0, 0), new Position(5, 200)),
46+
new Pair<>(new Position(0, 45), new Position(20, 50))
47+
};
48+
49+
@Before
50+
public void setup() {
51+
dummy = mock(Unit.class);
52+
given(dummy.getLeft()).willReturn(10);
53+
given(dummy.getRight()).willReturn(20);
54+
given(dummy.getTop()).willReturn(30);
55+
given(dummy.getBottom()).willReturn(40);
56+
}
57+
58+
@Theory
59+
public void shouldFindOverlappingUnits(
60+
@FromDataPoints("overlapping") Pair<Position, Position> rect) {
61+
// GIVEN
62+
allUnits.add(dummy);
63+
64+
// WHEN
65+
List<Unit> unitsInRectangle = sut
66+
.getUnitsInRectangle(rect.getLeft(), rect.getRight(), unused -> true);
67+
68+
// THEN
69+
assertThat(unitsInRectangle).contains(dummy);
70+
}
71+
72+
@Theory
73+
public void shouldNotFindNonOverlappingUnits(
74+
@FromDataPoints("non-overlapping") Pair<Position, Position> rect) {
75+
// GIVEN
76+
allUnits.add(dummy);
77+
78+
// WHEN
79+
List<Unit> unitsInRectangle = sut
80+
.getUnitsInRectangle(rect.getLeft(), rect.getRight(), unused -> true);
81+
82+
// THEN
83+
assertThat(unitsInRectangle).doesNotContain(dummy);
84+
}
85+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import bwapi.DefaultBWListener;
55
import bwapi.Game;
66

7-
class GameTest extends DefaultBWListener {
7+
class GameTestRunner extends DefaultBWListener {
88
final BWClient bwClient;
99

1010
Game game;
1111

12-
GameTest() {
12+
GameTestRunner() {
1313
bwClient = new BWClient(this);
1414
bwClient.startGame();
1515
}
@@ -23,6 +23,6 @@ public void onStart() {
2323
}
2424

2525
public static void main(String[] args) {
26-
new GameTest();
26+
new GameTestRunner();
2727
}
2828
}

0 commit comments

Comments
 (0)