Skip to content

Commit c0512d8

Browse files
authored
Merge pull request #104 from dgant/codex/chokepoint-identity-upstream
BWEM Bugfix: Different chokes considered equal
2 parents 68871f1 + 2fcd599 commit c0512d8

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

‎src/main/java/bwem/ChokePoint.java‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public final class ChokePoint {
4343
private final Graph graph;
4444
private final boolean isPseudo;
4545
private final int index;
46+
private final String mapHash;
4647
private final Pair<Area, Area> areas;
4748
private final WalkPosition[] nodes;
4849
private final List<Pair<WalkPosition, WalkPosition>> nodesInArea;
@@ -64,6 +65,7 @@ public final class ChokePoint {
6465

6566
this.graph = graph;
6667
this.index = index;
68+
this.mapHash = graph.getMap().game.mapHash();
6769
this.areas = new Pair<>(area1, area2);
6870
this.geometry = geometry;
6971

@@ -313,25 +315,13 @@ public boolean equals(final Object object) {
313315
return false;
314316
} else {
315317
final ChokePoint that = (ChokePoint) object;
316-
final boolean lel = this.areas.getLeft().equals(that.areas.getLeft());
317-
final boolean ler = this.areas.getLeft().equals(that.areas.getRight());
318-
final boolean rer = this.areas.getRight().equals(that.areas.getRight());
319-
final boolean rel = this.areas.getRight().equals(that.areas.getLeft());
320-
return lel && rer
321-
|| ler && rel; /* true if area pairs are an exact match or if one pair is reversed. */
318+
return this.index == that.index && Objects.equals(this.mapHash, that.mapHash);
322319
}
323320
}
324321

325322
@Override
326323
public int hashCode() {
327-
int idLeft = areas.getLeft().getId().intValue();
328-
int idRight = areas.getRight().getId().intValue();
329-
if (idLeft > idRight) {
330-
final int idLeftTmp = idLeft;
331-
idLeft = idRight;
332-
idRight = idLeftTmp;
333-
}
334-
return Objects.hash(idLeft, idRight);
324+
return Objects.hash(this.index, this.mapHash);
335325
}
336326

337327
/**

‎src/test/java/bwapi/BWEMTest.java‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package bwapi;
22

3+
import bwem.Area;
34
import bwem.BWEM;
45
import bwem.BWMap;
6+
import bwem.ChokePoint;
57
import org.junit.Test;
68

79
import java.io.IOException;
10+
import java.util.Collections;
811
import java.util.HashMap;
12+
import java.util.HashSet;
913
import java.util.Map;
1014

1115
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertTrue;
1217

1318
public class BWEMTest {
1419
static class BWEMMap{
@@ -66,13 +71,44 @@ public int hashCode() {
6671
mapData.put("(4)Roadrunner.scx", new BWEMMap(26, 12, 35));
6772
}
6873

74+
@Test
75+
public void chokePointsFromSameMapAreEqual() throws IOException {
76+
BWEM first = new BWEM(GameBuilder.createGame("(2)Destination.scx"));
77+
first.initialize();
78+
BWEM second = new BWEM(GameBuilder.createGame("(2)Destination.scx"));
79+
second.initialize();
80+
assertEquals(first.getMap().getChokePoints(), second.getMap().getChokePoints());
81+
assertEquals(new HashSet<>(first.getMap().getChokePoints()), new HashSet<>(second.getMap().getChokePoints()));
82+
}
83+
84+
@Test
85+
public void chokePointsFromDifferentMapsAreDistinct() throws IOException {
86+
BWEM first = new BWEM(GameBuilder.createGame("(2)Benzene.scx"));
87+
first.initialize();
88+
BWEM second = new BWEM(GameBuilder.createGame("(2)Destination.scx"));
89+
second.initialize();
90+
assertTrue(Collections.disjoint(first.getMap().getChokePoints(), second.getMap().getChokePoints()));
91+
}
92+
6993
@Test
7094
public void checkSSCAITMaps() throws IOException {
7195
for (String mapName : mapData.keySet()) {
7296
Game game = GameBuilder.createGame(mapName);
7397
BWEM bwem = new BWEM(game);
7498
bwem.initialize();
75-
assertEquals(new BWEMMap(bwem.getMap()), mapData.get(mapName));
99+
BWMap map = bwem.getMap();
100+
assertEquals(new BWEMMap(map), mapData.get(mapName));
101+
assertEquals(mapName, map.getChokePoints().size(), new HashSet<>(map.getChokePoints()).size());
102+
103+
for (Area area : map.getAreas()) {
104+
for (ChokePoint firstChokePoint : area.getChokePoints()) {
105+
for (ChokePoint secondChokePoint : area.getChokePoints()) {
106+
if (!firstChokePoint.isBlocked() && !secondChokePoint.isBlocked()) {
107+
assertTrue(mapName, firstChokePoint.accessibleFrom(secondChokePoint));
108+
}
109+
}
110+
}
111+
}
76112
}
77113
}
78114
}

0 commit comments

Comments
 (0)