Skip to content

Commit 49934b3

Browse files
committed
bwem cleanup
1 parent 19b9cd9 commit 49934b3

File tree

6 files changed

+102
-42
lines changed

6 files changed

+102
-42
lines changed

src/main/java/bwem/Graph.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,10 @@ private class Pathing {
711711

712712
for (final ChokePoint cpA : areaA.getChokePoints()) {
713713
if (!cpA.isBlocked()) {
714-
final int distACpA = BwemExt.getApproxDistance(a, cpA.getCenter().toPosition());
714+
final int distACpA = a.getApproxDistance(cpA.getCenter().toPosition());
715715
for (final ChokePoint cpB : areaB.getChokePoints()) {
716716
if (!cpB.isBlocked()) {
717-
final int distBToCPB = BwemExt.getApproxDistance(b, cpB.getCenter().toPosition());
717+
final int distBToCPB = b.getApproxDistance(cpB.getCenter().toPosition());
718718
final int distAToB = distACpA + distBToCPB + distance(cpA, cpB);
719719
if (distAToB < minDistAB) {
720720
minDistAB = distAToB;
@@ -738,7 +738,7 @@ Optional<PathingResult> getPathWithLength() {
738738
return Optional.empty();
739739
}
740740
if (areaA.equals(areaB)) {
741-
return Optional.of(new PathingResult(path, BwemExt.getApproxDistance(a, b)));
741+
return Optional.of(new PathingResult(path, a.getApproxDistance(b)));
742742
}
743743

744744
if (path.size() == 1) {
@@ -757,13 +757,13 @@ Optional<PathingResult> getPathWithLength() {
757757
cpEnd1.getY(),
758758
cpEnd2.getX(),
759759
cpEnd2.getY())) {
760-
return Optional.of(new PathingResult(path, BwemExt.getApproxDistance(a, b)));
760+
return Optional.of(new PathingResult(path, a.getApproxDistance(b)));
761761
} else {
762762
int pLength = minDistAB;
763763
for (final ChokePoint.Node node :
764764
new ChokePoint.Node[]{ChokePoint.Node.END1, ChokePoint.Node.END2}) {
765765
Position c = BwemExt.center(pBestCpA.getNodePosition(node));
766-
int distAToB = BwemExt.getApproxDistance(a, c) + BwemExt.getApproxDistance(b, c);
766+
int distAToB = a.getApproxDistance(c) + b.getApproxDistance(c);
767767
pLength = Math.min(pLength, distAToB);
768768
}
769769
return Optional.of(new PathingResult(path, pLength));

src/main/java/bwem/LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Original work Copyright (c) 2015, 2017, Igor Dimitrijevic
22
// Modified work Copyright (c) 2017-2018 OpenBW Team
3-
// Modified work Copyright (c) 2018 Jasper
3+
// Modified work Copyright (c) 2018- JavaBWAPI
44

55
// (MIT/X11 License)
66
// Copyright (c) 2015, 2017, Igor Dimitrijevic

src/main/java/bwem/MiniTile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ boolean isAltitudeMissing() {
158158
}
159159

160160
boolean isAreaIdMissing() {
161-
return (this.areaId.intValue() == -1);
161+
return this.areaId.equals(UNINITIALIZED);
162162
}
163163

164164
void replaceAreaId(final AreaId areaId) {

src/main/java/bwem/Neutral.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public Neutral getLastStacked() {
143143
}
144144

145145
private boolean isSameUnitTypeAs(Neutral neutral) {
146-
return this.getUnit().getType().equals(neutral.getUnit().getType());
146+
return this.getUnit().getType() == neutral.getUnit().getType();
147147
}
148148

149149
private void putOnTiles() {

src/main/java/bwem/util/BwemExt.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -298,38 +298,4 @@ public static boolean adjoins8SomeLakeOrNeutral(final WalkPosition p, final BWMa
298298

299299
return false;
300300
}
301-
302-
303-
// ----------------------------------------------------------------------
304-
// TODO: Add these functions to main BWAPI4J target type source files?
305-
// ----------------------------------------------------------------------
306-
private static int getApproxDistance(int x0, int y0, int x1, int y1) {
307-
int min = Math.abs(x0 - x1);
308-
int max = Math.abs(y0 - y1);
309-
if (max < min) {
310-
int minTmp = min;
311-
min = max;
312-
max = minTmp;
313-
}
314-
315-
if (min < (max >> 2)) {
316-
return max;
317-
}
318-
319-
int minCalc = (3 * min) >> 3;
320-
return (minCalc >> 5) + minCalc + max - (max >> 4) - (max >> 6);
321-
}
322-
323-
public static int getApproxDistance(TilePosition source, TilePosition target) {
324-
return getApproxDistance(source.getX(), source.getY(), target.getX(), target.getY());
325-
}
326-
327-
public static int getApproxDistance(WalkPosition source, WalkPosition target) {
328-
return getApproxDistance(source.getX(), source.getY(), target.getX(), target.getY());
329-
}
330-
331-
public static int getApproxDistance(Position source, Position target) {
332-
return getApproxDistance(source.getX(), source.getY(), target.getX(), target.getY());
333-
}
334-
// ----------------------------------------------------------------------
335301
}

src/test/java/MapTester.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import bwapi.*;
2+
import bwem.BWEM;
3+
4+
import java.util.Arrays;
5+
import java.util.stream.Collectors;
6+
7+
class MapTester extends DefaultBWListener {
8+
private static BWClient client;
9+
10+
public void onStart() {
11+
Game game = MapTester.client.getGame();
12+
13+
final String mapHash = game.mapHash();
14+
15+
MapData mapData = null;
16+
try {
17+
BWEM bwem = new BWEM(game);
18+
bwem.initialize();
19+
bwem.getMap().assignStartingLocationsToSuitableBases();
20+
mapData = new MapData(
21+
game.mapName(),
22+
game.getStartLocations().size(),
23+
bwem.getMap().getAreas().size(),
24+
bwem.getMap().getBases().size(),
25+
bwem.getMap().getChokePoints().size()
26+
);
27+
}
28+
catch (Exception e) {
29+
//TODO Write to file here
30+
System.err.println(e);
31+
System.out.println(
32+
Arrays
33+
.stream(e.getStackTrace())
34+
.map(StackTraceElement::toString)
35+
.collect(Collectors.joining("\n"))
36+
);
37+
}
38+
39+
// if (MapKnowledge.knowledge.containsKey(mapHash)) {
40+
// MapData expected = MapKnowledge.knowledge.get(mapHash);
41+
// if ( ! expected.equals(mapData)) {
42+
// //TODO Write to file here
43+
// System.out.println("Expected: " + expected + ", but got: " + mapData);
44+
// }
45+
// else {
46+
// // Success, (probably) nothing broke.
47+
// }
48+
49+
System.out.println("New map [" + mapHash + "]: " + mapData);
50+
game.leaveGame();
51+
}
52+
53+
public static void main(String[] args) {
54+
client = new BWClient(new MapTester());
55+
client.startGame();
56+
}
57+
58+
class MapData {
59+
private final String name;
60+
private final int startLocationsAmount;
61+
private final int areaAmount;
62+
private final int basesAmount;
63+
private final int chokeAmount;
64+
65+
MapData(String name, int startLocationsAmount, int areaAmount, int basesAmount, int chokeAmount) {
66+
this.name = name;
67+
this.startLocationsAmount = startLocationsAmount;
68+
this.areaAmount = areaAmount;
69+
this.basesAmount = basesAmount;
70+
this.chokeAmount = chokeAmount;
71+
}
72+
73+
public boolean equals(Object o) {
74+
if (this == o) return true;
75+
if (o == null || getClass() != o.getClass()) return false;
76+
MapData mapData = (MapData) o;
77+
return startLocationsAmount == mapData.startLocationsAmount &&
78+
areaAmount == mapData.areaAmount &&
79+
basesAmount == mapData.basesAmount &&
80+
chokeAmount == mapData.chokeAmount &&
81+
name.equals(mapData.name);
82+
}
83+
84+
public String toString() {
85+
return "MapData{" +
86+
"name='" + name + '\'' +
87+
", startLocationsAmount=" + startLocationsAmount +
88+
", areaAmount=" + areaAmount +
89+
", basesAmount=" + basesAmount +
90+
", chokeAmount=" + chokeAmount +
91+
'}';
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)