Skip to content

Commit eefd1de

Browse files
committed
fix wrong brackets in buildingPlacer code
1 parent aa936a5 commit eefd1de

File tree

4 files changed

+84
-47
lines changed

4 files changed

+84
-47
lines changed

src/main/java/bwapi/BuildingPlacer.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
7474
desiredPosition = pTargRegion.getCenter().toTilePosition();
7575
}
7676
// Find the best position
77-
int bestDistance;
78-
int fallbackDistance;
79-
TilePosition bestPosition;
80-
TilePosition fallbackPosition;
77+
int bestDistance = 999999;
78+
int fallbackDistance = 999999;
79+
TilePosition bestPosition = TilePosition.None;
80+
TilePosition fallbackPosition = TilePosition.None;
8181

82-
bestDistance = fallbackDistance = 999999;
83-
bestPosition = fallbackPosition = TilePosition.None;
8482
for (int passCount = 0; passCount < (pTargRegion != null ? 2 : 1); ++passCount) {
8583
for (int y = 0; y < MAX_RANGE; ++y) {
8684
for (int x = 0; x < MAX_RANGE; ++x) {
@@ -90,7 +88,7 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
9088
}
9189
final TilePosition currentPosition = new TilePosition(x, y).add(centerPosition);
9290
//Broodwar->getGroundDistance( desiredPosition, currentPosition );
93-
final int currentDistance = desiredPosition.toPosition().getApproxDistance(currentPosition.toPosition());
91+
final int currentDistance = desiredPosition.getApproxDistance(currentPosition);
9492
if (currentDistance < bestDistance) {
9593
if (currentDistance <= maxRange) {
9694
bestDistance = currentDistance;
@@ -101,21 +99,21 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
10199
}
102100
}
103101
}
104-
// Break pass if position is found
105-
if (bestPosition != TilePosition.None) {
106-
break;
107-
}
102+
}
103+
// Break pass if position is found
104+
if (!bestPosition.equals(TilePosition.None)) {
105+
break;
106+
}
108107

109-
// Break if an alternative position was found
110-
if (fallbackPosition != TilePosition.None) {
111-
bestPosition = fallbackPosition;
112-
break;
113-
}
108+
// Break if an alternative position was found
109+
if (!fallbackPosition.equals(TilePosition.None)) {
110+
bestPosition = fallbackPosition;
111+
break;
112+
}
114113

115-
// If we were really targetting a region, and couldn't find a position above
116-
if (pTargRegion != null) { // Then fallback to the default build position
117-
desiredPosition = centerPosition;
118-
}
114+
// If we were really targetting a region, and couldn't find a position above
115+
if (pTargRegion != null) { // Then fallback to the default build position
116+
desiredPosition = centerPosition;
119117
}
120118
}
121119

@@ -195,7 +193,7 @@ private static void AssignBuildableLocations(final PlacementReserve reserve, fin
195193
}
196194

197195
private static void RemoveDisconnected(final PlacementReserve reserve, final TilePosition desiredPosition, final Game game) {
198-
final TilePosition start = desiredPosition.subtract(new TilePosition(MAX_RANGE, MAX_RANGE)).divide(2);
196+
final TilePosition start = desiredPosition.subtract(new TilePosition(MAX_RANGE, MAX_RANGE).divide(2));
199197

200198
// Assign 0 to all locations that aren't connected
201199
reserve.iterate((pr, x, y) -> {
@@ -206,7 +204,7 @@ private static void RemoveDisconnected(final PlacementReserve reserve, final Til
206204
}
207205

208206
private static void ReserveGroundHeight(final PlacementReserve reserve, final TilePosition desiredPosition, final Game game) {
209-
final TilePosition start = desiredPosition.subtract(new TilePosition(MAX_RANGE, MAX_RANGE)).divide(2);
207+
final TilePosition start = desiredPosition.subtract(new TilePosition(MAX_RANGE, MAX_RANGE).divide(2));
210208

211209
// Exclude locations with a different ground height, but restore a backup in case there are no more build locations
212210
reserve.backup();
@@ -447,11 +445,15 @@ boolean hasValidSpace() {
447445
}
448446

449447
void backup() {
450-
System.arraycopy(data, 0, save, 0, data.length);
448+
for (int i = 0; i < MAX_RANGE; i++) {
449+
System.arraycopy(data[i], 0, save[i], 0, MAX_RANGE);
450+
}
451451
}
452452

453453
void restore() {
454-
System.arraycopy(save, 0, data, 0, save.length);
454+
for (int i = 0; i < MAX_RANGE; i++) {
455+
System.arraycopy(save[i], 0, data[i], 0, MAX_RANGE);
456+
}
455457
}
456458

457459
void restoreIfInvalid() {

src/main/java/bwapi/Point.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ protected double getDistance(final int x, final int y) {
3535
return Math.sqrt(dx * dx + dy * dy);
3636
}
3737

38+
private static int getApproxDistance(final int x1, final int y1, final int x2, final int y2) {
39+
int max = Math.abs(x1 - x2);
40+
int min = Math.abs(y1 - y2);
41+
if (max < min) {
42+
final int temp = min;
43+
min = max;
44+
max = temp;
45+
}
46+
47+
if (min <= (max >> 2)) {
48+
return max;
49+
}
50+
51+
final int minCalc = (3 * min) >> 3;
52+
return (minCalc >> 5) + minCalc + max - (max >> 4) - (max >> 6);
53+
}
54+
55+
public int getApproxDistance(final Point point) {
56+
return getApproxDistance(x, y, point.x, point.y);
57+
}
58+
59+
3860
public boolean equals(final Object o) {
3961
if (o != null && this.getClass().equals(o.getClass())) {
4062
final Point point = (Point) o;

src/main/java/bwapi/Position.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,8 @@ public Position(final int x, final int y) {
1313
super(x, y, SIZE_IN_PIXELS);
1414
}
1515

16-
Position(ClientData.Position position) {
17-
this(position.getX(), position.getY());
18-
}
19-
20-
private static int getApproxDistance(final int x1, final int y1, final int x2, final int y2) {
21-
int max = Math.abs(x1 - x2);
22-
int min = Math.abs(y1 - y2);
23-
if (max < min) {
24-
final int temp = min;
25-
min = max;
26-
max = temp;
27-
}
28-
29-
if (min <= (max >> 2)) {
30-
return max;
31-
}
32-
33-
final int minCalc = (3 * min) >> 3;
34-
return (minCalc >> 5) + minCalc + max - (max >> 4) - (max >> 6);
35-
}
36-
37-
public int getApproxDistance(final Position position) {
38-
return getApproxDistance(x, y, position.x, position.y);
16+
Position(ClientData.Position position) {
17+
this(position.getX(), position.getY());
3918
}
4019

4120
public TilePosition toTilePosition() {

src/test/java/BuildingPlacer.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import bwapi.*;
2+
import static bwapi.Color.*;
3+
import static bwapi.UnitType.*;
4+
5+
public class BuildingPlacer extends DefaultBWListener {
6+
7+
final BWClient bwClient;
8+
9+
Game game;
10+
11+
BuildingPlacer() {
12+
bwClient = new BWClient(this);
13+
bwClient.startGame();
14+
}
15+
@Override
16+
public void onStart() {
17+
game = bwClient.getGame();
18+
System.out.println("seed:" + game.getRandomSeed());
19+
System.out.println("StartPosition: " + game.self().getStartLocation());
20+
}
21+
22+
@Override
23+
public void onFrame() {
24+
TilePosition start = game.self().getStartLocation();
25+
game.drawBoxMap(start.toPosition(), start.add(new TilePosition(1, 1)).toPosition(), Red);
26+
TilePosition tp = game.getBuildLocation(Protoss_Pylon, start );
27+
System.out.println("chosen: " + tp);
28+
game.drawBoxMap(tp.toPosition(), tp.add(new TilePosition(1, 1)).toPosition(), Purple);
29+
}
30+
31+
public static void main(String[] args) {
32+
new BuildingPlacer();
33+
}
34+
}

0 commit comments

Comments
 (0)