Skip to content

Commit 9107408

Browse files
author
bytekeeper
committed
No more MutableInt.
1 parent 99ad9e0 commit 9107408

File tree

5 files changed

+131
-109
lines changed

5 files changed

+131
-109
lines changed

src/main/java/bwem/BWMap.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import bwapi.*;
1616
import bwem.util.BwemExt;
1717
import bwem.util.CheckMode;
18-
import bwem.util.MutableInt;
1918
import bwem.util.Pred;
2019

2120
import java.util.*;
@@ -212,18 +211,13 @@ public Area getMainArea(final TilePosition topLeft, final TilePosition size) {
212211
return null;
213212
}
214213

215-
private CPPath getPath(Position a, Position b, MutableInt pLength) {
216-
return graph.getPath(a, b, pLength);
217-
}
218-
219214
public int getPathLength(Position a, Position b) {
220-
MutableInt pLength = new MutableInt();
221-
getPath(a, b, pLength);
222-
return pLength.intValue();
215+
return graph.getPathingResult(a, b).map(PathingResult::getLength).orElse(-1);
223216
}
224217

218+
// TODO: This might be a bad method: What is the difference between no path and "same area"?
225219
public CPPath getPath(Position a, Position b) {
226-
return getPath(a, b, null);
220+
return graph.getPath(a, b).orElse(CPPath.EMPTY_PATH);
227221
}
228222

229223
public TilePosition breadthFirstSearch(

src/main/java/bwem/CPPath.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* CPPath;
2424
*/
2525
public class CPPath implements Iterable<ChokePoint> {
26+
27+
static final CPPath EMPTY_PATH = new CPPath();
2628
private final List<ChokePoint> chokepoints;
2729

2830
CPPath() {

src/main/java/bwem/Graph.java

Lines changed: 100 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import bwapi.WalkPosition;
1919
import bwem.util.BwemExt;
2020
import bwem.util.CheckMode;
21-
import bwem.util.MutableInt;
2221
import bwem.util.Utils;
2322

2423
import java.util.*;
@@ -146,94 +145,12 @@ public CPPath getPath(ChokePoint cpA, ChokePoint cpB) {
146145
.get(cpB.getIndex());
147146
}
148147

149-
public CPPath getPath(final Position a, final Position b, final MutableInt pLength) {
150-
final Area areaA = getNearestArea(a.toWalkPosition());
151-
final Area areaB = getNearestArea(b.toWalkPosition());
152-
153-
if (areaA.equals(areaB)) {
154-
if (pLength != null) {
155-
pLength.setValue(BwemExt.getApproxDistance(a, b));
156-
}
157-
return new CPPath();
158-
}
159-
160-
if (!areaA.isAccessibleFrom(areaB)) {
161-
if (pLength != null) {
162-
pLength.setValue(-1);
163-
}
164-
return new CPPath();
165-
}
166-
167-
int minDistAB = Integer.MAX_VALUE;
168-
169-
ChokePoint pBestCpA = null;
170-
ChokePoint pBestCpB = null;
171-
172-
for (final ChokePoint cpA : areaA.getChokePoints()) {
173-
if (!cpA.isBlocked()) {
174-
final int distACpA = BwemExt.getApproxDistance(a, cpA.getCenter().toPosition());
175-
for (final ChokePoint cpB : areaB.getChokePoints()) {
176-
if (!cpB.isBlocked()) {
177-
final int distBToCPB = BwemExt.getApproxDistance(b, cpB.getCenter().toPosition());
178-
final int distAToB = distACpA + distBToCPB + distance(cpA, cpB);
179-
if (distAToB < minDistAB) {
180-
minDistAB = distAToB;
181-
pBestCpA = cpA;
182-
pBestCpB = cpB;
183-
}
184-
}
185-
}
186-
}
187-
}
188-
189-
if (minDistAB == Integer.MAX_VALUE) {
190-
throw new IllegalStateException();
191-
}
192-
193-
final CPPath path = getPath(pBestCpA, pBestCpB);
194-
195-
if (pLength != null) {
196-
if (!(path.size() >= 1)) {
197-
throw new IllegalStateException();
198-
}
199-
200-
pLength.setValue(minDistAB);
201-
202-
if (path.size() == 1) {
203-
if (!pBestCpA.equals(pBestCpB)) {
204-
throw new IllegalStateException();
205-
}
206-
207-
final Position cpEnd1 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END1));
208-
final Position cpEnd2 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END2));
209-
if (Utils.intersect(
210-
a.getX(),
211-
a.getY(),
212-
b.getX(),
213-
b.getY(),
214-
cpEnd1.getX(),
215-
cpEnd1.getY(),
216-
cpEnd2.getX(),
217-
cpEnd2.getY())) {
218-
pLength.setValue(BwemExt.getApproxDistance(a, b));
219-
} else {
220-
for (final ChokePoint.Node node :
221-
new ChokePoint.Node[]{ChokePoint.Node.END1, ChokePoint.Node.END2}) {
222-
final Position c = BwemExt.center(pBestCpA.getNodePosition(node));
223-
final int distAToB = BwemExt.getApproxDistance(a, c) + BwemExt.getApproxDistance(b, c);
224-
if (distAToB < pLength.intValue()) {
225-
pLength.setValue(distAToB);
226-
}
227-
}
228-
}
229-
}
230-
}
231-
232-
return getPath(pBestCpA, pBestCpB);
148+
public Optional<PathingResult> getPathingResult(Position a, Position b) {
149+
return new Pathing(a, b).getPathWithLength();
233150
}
234151

235-
public CPPath getPath(Position a, Position b) {
236-
return getPath(a, b, null);
152+
public Optional<CPPath> getPath(final Position a, final Position b) {
153+
return new Pathing(a, b).getPath();
237154
}
238155

239156
public List<Base> getBases() {
@@ -762,4 +679,100 @@ private void setPath(final ChokePoint cpA, final ChokePoint cpB, final CPPath pa
762679
private boolean isValid(AreaId id) {
763680
return (1 <= id.intValue() && id.intValue() <= getAreaCount());
764681
}
682+
683+
private class Pathing {
684+
private final Position a;
685+
private final Position b;
686+
private final Area areaA;
687+
private final Area areaB;
688+
private final CPPath path;
689+
private ChokePoint pBestCpA;
690+
private ChokePoint pBestCpB;
691+
private int minDistAB;
692+
693+
Pathing(Position a, Position b) {
694+
this.a = a;
695+
this.b = b;
696+
areaA = getNearestArea(a.toWalkPosition());
697+
areaB = getNearestArea(b.toWalkPosition());
698+
699+
if (areaA.equals(areaB)) {
700+
path = CPPath.EMPTY_PATH;
701+
return;
702+
}
703+
704+
if (!areaA.isAccessibleFrom(areaB)) {
705+
path = null;
706+
return;
707+
}
708+
709+
minDistAB = Integer.MAX_VALUE;
710+
711+
for (final ChokePoint cpA : areaA.getChokePoints()) {
712+
if (!cpA.isBlocked()) {
713+
final int distACpA = BwemExt.getApproxDistance(a, cpA.getCenter().toPosition());
714+
for (final ChokePoint cpB : areaB.getChokePoints()) {
715+
if (!cpB.isBlocked()) {
716+
final int distBToCPB = BwemExt.getApproxDistance(b, cpB.getCenter().toPosition());
717+
final int distAToB = distACpA + distBToCPB + distance(cpA, cpB);
718+
if (distAToB < minDistAB) {
719+
minDistAB = distAToB;
720+
pBestCpA = cpA;
721+
pBestCpB = cpB;
722+
}
723+
}
724+
}
725+
}
726+
}
727+
728+
if (minDistAB == Integer.MAX_VALUE) {
729+
throw new IllegalStateException();
730+
}
731+
732+
path = Graph.this.getPath(pBestCpA, pBestCpB);
733+
}
734+
735+
Optional<PathingResult> getPathWithLength() {
736+
if (path == null) {
737+
return Optional.empty();
738+
}
739+
if (areaA.equals(areaB)) {
740+
return Optional.of(new PathingResult(path, BwemExt.getApproxDistance(a, b)));
741+
}
742+
743+
if (path.size() == 1) {
744+
if (!pBestCpA.equals(pBestCpB)) {
745+
throw new IllegalStateException();
746+
}
747+
748+
final Position cpEnd1 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END1));
749+
final Position cpEnd2 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END2));
750+
if (Utils.intersect(
751+
a.getX(),
752+
a.getY(),
753+
b.getX(),
754+
b.getY(),
755+
cpEnd1.getX(),
756+
cpEnd1.getY(),
757+
cpEnd2.getX(),
758+
cpEnd2.getY())) {
759+
return Optional.of(new PathingResult(path, BwemExt.getApproxDistance(a, b)));
760+
} else {
761+
int pLength = minDistAB;
762+
for (final ChokePoint.Node node :
763+
new ChokePoint.Node[]{ChokePoint.Node.END1, ChokePoint.Node.END2}) {
764+
Position c = BwemExt.center(pBestCpA.getNodePosition(node));
765+
int distAToB = BwemExt.getApproxDistance(a, c) + BwemExt.getApproxDistance(b, c);
766+
pLength = Math.min(pLength, distAToB);
767+
}
768+
return Optional.of(new PathingResult(path, pLength));
769+
}
770+
}
771+
return Optional.of(new PathingResult(path, minDistAB));
772+
}
773+
774+
Optional<CPPath> getPath() {
775+
return Optional.ofNullable(path);
776+
}
777+
}
765778
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bwem;
2+
3+
/**
4+
* Result of a path query including the approximated length of the path.
5+
*/
6+
public class PathingResult {
7+
8+
private final CPPath cpPath;
9+
private final int approxDistance;
10+
11+
PathingResult(CPPath cpPath, int approxDistance) {
12+
this.cpPath = cpPath;
13+
this.approxDistance = approxDistance;
14+
}
15+
16+
public CPPath getCPPath() {
17+
return cpPath;
18+
}
19+
20+
/**
21+
* Returns the approximate length of the path.
22+
*/
23+
public int getLength() {
24+
return approxDistance;
25+
}
26+
}

src/main/java/bwem/util/MutableInt.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)