Skip to content

Commit fe411b4

Browse files
committed
Merge branch 'bwem-fixes' into develop
2 parents 59d0ef4 + c6fd7e1 commit fe411b4

21 files changed

+208
-154
lines changed

src/main/java/bwem/Area.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ public abstract class Area {
3939
int highGroundTileCount = 0;
4040
int veryHighGroundTileCount = 0;
4141

42-
Area(final AreaId areaId, final WalkPosition top, final int miniTileCount) {
42+
protected final BWMap map;
43+
44+
45+
Area(final AreaId areaId, final WalkPosition top, final int miniTileCount, final BWMap map) {
4346
this.id = areaId;
4447
this.walkPositionWithHighestAltitude = top;
4548
this.miniTileCount = miniTileCount;
49+
this.map = map;
4650
}
4751

4852
public AreaId getId() {
@@ -102,7 +106,7 @@ public List<ChokePoint> getChokePoints() {
102106
public List<ChokePoint> getChokePoints(final Area area) {
103107
final List<ChokePoint> ret = this.chokePointsByArea.get(area);
104108
if (ret == null) {
105-
throw new IllegalArgumentException();
109+
map.asserter.throwIllegalStateException("");
106110
}
107111
return ret;
108112
}

src/main/java/bwem/AreaInitializer.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,19 @@ final class AreaInitializer extends Area {
3030
private static final StaticMarkable staticMarkable = new StaticMarkable();
3131
private final Markable markable;
3232

33-
private final BWMap map;
34-
3533
AreaInitializer(
3634
final BWMap map, final AreaId areaId, final WalkPosition top, final int miniTileCount) {
37-
super(areaId, top, miniTileCount);
38-
39-
this.map = map;
35+
super(areaId, top, miniTileCount, map);
4036

4137
this.markable = new Markable(staticMarkable);
4238

4339
if (!(areaId.intValue() > 0)) {
44-
throw new IllegalArgumentException();
40+
map.asserter.throwIllegalStateException("");
4541
}
4642

4743
final MiniTile topMiniTile = this.map.getData().getMiniTile(top);
4844
if (!(topMiniTile.getAreaId().equals(areaId))) {
49-
throw new IllegalStateException(
45+
map.asserter.throwIllegalStateException(
5046
"assert failed: topMiniTile.AreaId().equals(areaId): expected: "
5147
+ topMiniTile.getAreaId().intValue()
5248
+ ", actual: "
@@ -66,7 +62,7 @@ Markable getMarkable() {
6662

6763
void addChokePoints(final Area area, final List<ChokePoint> chokePoints) {
6864
if (!(super.chokePointsByArea.get(area) == null && chokePoints != null)) {
69-
throw new IllegalArgumentException();
65+
map.asserter.throwIllegalStateException("");
7066
}
7167

7268
super.chokePointsByArea.put(area, chokePoints);
@@ -76,14 +72,14 @@ void addChokePoints(final Area area, final List<ChokePoint> chokePoints) {
7672

7773
void addMineral(final Mineral mineral) {
7874
if (!(mineral != null && !super.minerals.contains(mineral))) {
79-
throw new IllegalStateException();
75+
map.asserter.throwIllegalStateException("");
8076
}
8177
super.minerals.add(mineral);
8278
}
8379

8480
void addGeyser(final Geyser geyser) {
8581
if (!(geyser != null && !super.geysers.contains(geyser))) {
86-
throw new IllegalStateException();
82+
map.asserter.throwIllegalStateException("");
8783
}
8884
super.geysers.add(geyser);
8985
}
@@ -121,7 +117,7 @@ void setGroupId(int gid) {
121117

122118
int[] computeDistances(final ChokePoint startCP, final List<ChokePoint> targetCPs) {
123119
if (targetCPs.contains(startCP)) {
124-
throw new IllegalStateException();
120+
map.asserter.throwIllegalStateException("");
125121
}
126122

127123
final TilePosition start =
@@ -168,7 +164,7 @@ private int[] computeDistances(final TilePosition start, final List<TilePosition
168164
final TilePosition current = distanceAndTilePosition.getRight();
169165
final Tile currentTile = this.map.getData().getTile(current, CheckMode.NO_CHECK);
170166
if (!(currentTile.getInternalData() == currentDist)) {
171-
throw new IllegalStateException(
167+
map.asserter.throwIllegalStateException(
172168
"currentTile.InternalData().intValue()="
173169
+ currentTile.getInternalData()
174170
+ ", currentDist="
@@ -216,7 +212,7 @@ private int[] computeDistances(final TilePosition start, final List<TilePosition
216212
toVisit.remove(
217213
new Pair<>(nextTile.getInternalData(), next));
218214
if (!removed) {
219-
throw new IllegalStateException();
215+
map.asserter.throwIllegalStateException("");
220216
}
221217
nextTile.setInternalData(newNextDist);
222218
toVisit.offer(new Pair<>(newNextDist, next));
@@ -232,7 +228,7 @@ private int[] computeDistances(final TilePosition start, final List<TilePosition
232228
}
233229

234230
if (!(remainingTargets == 0)) {
235-
throw new IllegalStateException();
231+
map.asserter.throwIllegalStateException("");
236232
}
237233

238234
for (final Pair<Integer, TilePosition> distanceAndTilePosition : toVisit) {
@@ -452,7 +448,7 @@ void createBases(final TerrainData terrainData) {
452448
break;
453449
}
454450

455-
super.bases.add(new Base(this, bestLocation, assignedResources, blockingMinerals));
451+
super.bases.add(new Base(this, bestLocation, assignedResources, blockingMinerals, map.asserter));
456452
}
457453
}
458454

@@ -532,7 +528,7 @@ private boolean validateBaseLocation(
532528

533529
public void onMineralDestroyed(final Mineral mineral) {
534530
if (mineral == null) {
535-
throw new IllegalArgumentException();
531+
map.asserter.throwIllegalStateException("");
536532
}
537533

538534
this.minerals.remove(mineral);

src/main/java/bwem/Asserter.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package bwem;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.util.Arrays;
6+
import java.util.stream.Collectors;
7+
8+
class Asserter {
9+
private boolean failOnError = true;
10+
private OutputStream outStream = System.err;
11+
12+
void throwIllegalStateException(String message) {
13+
final IllegalStateException exception = new IllegalStateException(message);
14+
if (failOnError) {
15+
throw exception;
16+
}
17+
else {
18+
try {
19+
throw exception;
20+
} catch (IllegalStateException e) {
21+
if (outStream != null) {
22+
try {
23+
outStream.write(Arrays
24+
.stream(e.getStackTrace())
25+
.map(s -> s.toString() + "\n")
26+
.collect(Collectors.joining())
27+
.getBytes());
28+
} catch (IOException ex) {
29+
ex.printStackTrace();
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
void setFailOnError(boolean failOnError) {
37+
this.failOnError = failOnError;
38+
}
39+
40+
void setFailOutputStream(OutputStream outputStream) {
41+
this.outStream = outputStream;
42+
}
43+
}

src/main/java/bwem/BWEM.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@
1414

1515
import bwapi.Game;
1616

17+
import java.io.OutputStream;
18+
19+
/**
20+
* BWEM Broodwar Map analysis library by Igor Dimitrijevic.
21+
* Ported to Java by the OpenBW Team
22+
*
23+
* By default BWEM throws when an invalid state is encountered.
24+
* But if you know what you are doing, you can skip these throws by setting
25+
* {@link #setFailOnError} to `false`.
26+
* These errors will then be outputted to {@link System.err}, but this can also be changed
27+
* with {@link #setFailOutputStream} (if you set it to `null` the errors will be completely ignored).
28+
*/
1729
public final class BWEM {
1830
private final BWMap map;
31+
private final Asserter asserter;
1932

2033
public BWEM(final Game game) {
21-
this.map = new BWMapInitializer(game);
34+
this.asserter = new Asserter();
35+
this.map = new BWMapInitializer(game, asserter);
2236
}
2337

2438
/**
@@ -39,4 +53,12 @@ public void initialize() {
3953
((BWMapInitializer) this.map).initialize();
4054
this.map.assignStartingLocationsToSuitableBases();
4155
}
56+
57+
public void setFailOnError(boolean failOnError) {
58+
asserter.setFailOnError(failOnError);
59+
}
60+
61+
public void setFailOutputStream(OutputStream outputStream) {
62+
asserter.setFailOutputStream(outputStream);
63+
}
4264
}

src/main/java/bwem/BWMap.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ public abstract class BWMap {
3535
TerrainData terrainData = null;
3636
NeutralData neutralData = null;
3737
Altitude highestAltitude;
38+
final Asserter asserter;
3839

39-
BWMap(final Game game) {
40+
BWMap(final Game game, final Asserter asserter) {
4041
this.game = game;
4142
this.players = game.getPlayers();
4243
this.mineralPatches = game.getMinerals();
4344
this.vespeneGeysers = game.getGeysers();
4445
this.units = game.getAllUnits();
4546
this.graph = new Graph(this);
4647
this.neighboringAreaChooser = new NeighboringAreaChooser();
48+
this.asserter = asserter;
4749
}
4850

4951
public TerrainData getData() {
@@ -89,7 +91,7 @@ public void assignStartingLocationsToSuitableBases() {
8991
}
9092

9193
if (atLeastOneFailed) {
92-
throw new IllegalStateException("At least one starting location was not assigned to a base.");
94+
asserter.throwIllegalStateException("At least one starting location was not assigned to a base.");
9395
}
9496
}
9597

@@ -143,7 +145,7 @@ private void onMineralDestroyed(Unit u) {
143145
return;
144146
}
145147
}
146-
throw new IllegalArgumentException("unit is not a Mineral");
148+
asserter.throwIllegalStateException("unit is not a Mineral");
147149
}
148150

149151
/**
@@ -271,8 +273,8 @@ public TilePosition breadthFirstSearch(
271273
}
272274

273275
// TODO: Are we supposed to return start or not?
274-
throw new IllegalStateException();
275-
// return start;
276+
asserter.throwIllegalStateException("");
277+
return start;
276278
}
277279

278280
public TilePosition breadthFirstSearch(TilePosition start, Pred<Tile, TilePosition> findCond, Pred<Tile, TilePosition> visitCond) {
@@ -330,8 +332,8 @@ public WalkPosition breadthFirstSearch(
330332
}
331333

332334
// TODO: Are we supposed to return start or not?
333-
throw new IllegalStateException();
334-
// return start;
335+
asserter.throwIllegalStateException("");
336+
return start;
335337
}
336338

337339
public WalkPosition breadthFirstSearch(
@@ -368,7 +370,7 @@ List<Unit> filterNeutralPlayerUnits(
368370
void setAreaIdInTile(final TilePosition t) {
369371
final Tile tile = getData().getTile(t);
370372
if (!(tile.getAreaId().intValue() == 0)) { // initialized to 0
371-
throw new IllegalStateException();
373+
asserter.throwIllegalStateException("");
372374
}
373375

374376
for (int dy = 0; dy < 4; ++dy) {

src/main/java/bwem/BWMapInitializer.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.List;
2121

2222
class BWMapInitializer extends BWMap {
23-
BWMapInitializer(final Game game) {
24-
super(game);
23+
BWMapInitializer(final Game game, final Asserter asserter) {
24+
super(game, asserter);
2525
}
2626

2727
void initialize() {
@@ -77,7 +77,8 @@ private void initializeTerrainData(
7777
final TileData tileData =
7878
new TileData(
7979
mapData.getTileSize().getX() * mapData.getTileSize().getY(),
80-
mapData.getWalkSize().getX() * mapData.getWalkSize().getY());
80+
mapData.getWalkSize().getX() * mapData.getWalkSize().getY(),
81+
asserter);
8182
super.terrainData = new TerrainData(mapData, tileData);
8283
}
8384

@@ -212,7 +213,7 @@ private Altitude setAltitudesAndGetUpdatedHighestAltitude(
212213
if (miniTile.isAltitudeMissing()) {
213214
if (updatedHighestAltitude != null
214215
&& updatedHighestAltitude.intValue() > altitude.intValue()) {
215-
throw new IllegalStateException();
216+
asserter.throwIllegalStateException("");
216217
}
217218
updatedHighestAltitude = altitude;
218219
current.setRight(altitude);
@@ -449,15 +450,15 @@ private List<Pair<WalkPosition, MiniTile>> getSortedMiniTilesByDescendingAltitud
449450
private List<TempAreaInfo> computeTempAreas(
450451
final List<Pair<WalkPosition, MiniTile>> miniTilesByDescendingAltitude) {
451452
final List<TempAreaInfo> tempAreaList = new ArrayList<>();
452-
tempAreaList.add(new TempAreaInfo()); // tempAreaList[0] left unused, as AreaIds are > 0
453+
tempAreaList.add(new TempAreaInfo(asserter)); // tempAreaList[0] left unused, as AreaIds are > 0
453454

454455
for (final Pair<WalkPosition, MiniTile> current : miniTilesByDescendingAltitude) {
455456
final WalkPosition pos = new WalkPosition(current.getLeft().getX(), current.getLeft().getY());
456457
final MiniTile cur = current.getRight();
457458

458459
final Pair<AreaId, AreaId> neighboringAreas = findNeighboringAreas(pos);
459460
if (neighboringAreas.getLeft() == null) { // no neighboring area : creates of a new area
460-
tempAreaList.add(new TempAreaInfo(new AreaId(tempAreaList.size()), cur, pos));
461+
tempAreaList.add(new TempAreaInfo(new AreaId(tempAreaList.size()), cur, pos, asserter));
461462
} else if (neighboringAreas.getRight()
462463
== null) { // one neighboring area : adds cur to the existing area
463464
tempAreaList.get(neighboringAreas.getLeft().intValue()).add(cur);
@@ -569,7 +570,7 @@ private void createAreas(final List<TempAreaInfo> tempAreaList, final int areaMi
569570
if (tempArea.isValid()) {
570571
if (tempArea.getSize() >= areaMinMiniTiles) {
571572
if (!(newAreaId <= tempArea.getId().intValue())) {
572-
throw new IllegalStateException();
573+
asserter.throwIllegalStateException("");
573574
}
574575
if (newAreaId != tempArea.getId().intValue()) {
575576
replaceAreaIds(tempArea.getWalkPositionWithHighestAltitude(), new AreaId(newAreaId));
@@ -620,7 +621,7 @@ private void setAreaIdAndLowestAltitudeInTiles() {
620621

621622
void onBlockingNeutralDestroyed(Neutral pBlocking) {
622623
if (!(pBlocking != null && pBlocking.isBlocking())) {
623-
throw new IllegalArgumentException();
624+
asserter.throwIllegalStateException("");
624625
}
625626

626627
for (Area pArea : pBlocking.getBlockedAreas())

src/main/java/bwem/Base.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,23 @@ public final class Base {
3737
private Position center;
3838
private boolean isStartingLocation = false;
3939

40+
private final Asserter asserter;
41+
4042
Base(
4143
final Area area,
4244
final TilePosition location,
4345
final List<Resource> assignedResources,
44-
final List<Mineral> blockingMinerals) {
46+
final List<Mineral> blockingMinerals,
47+
final Asserter asserter) {
4548
this.area = area;
4649
this.location = location;
4750
this.center = BwemExt.centerOfBuilding(location, UnitType.Terran_Command_Center.tileSize());
4851
this.blockingMinerals = blockingMinerals;
52+
this.asserter = asserter;
4953

5054
// bwem_assert(!AssignedResources.empty());
5155
if (assignedResources.isEmpty()) {
52-
throw new IllegalArgumentException();
56+
asserter.throwIllegalStateException("");
5357
}
5458

5559
for (final Resource assignedResource : assignedResources) {
@@ -141,7 +145,7 @@ void assignStartingLocation(final TilePosition actualLocation) {
141145
public void onMineralDestroyed(final Mineral mineral) {
142146
// bwem_assert(pMineral);
143147
if (mineral == null) {
144-
throw new IllegalArgumentException();
148+
asserter.throwIllegalStateException("");
145149
}
146150

147151
this.minerals.remove(mineral);

0 commit comments

Comments
 (0)