Skip to content

Commit 999279d

Browse files
committed
Add partial fake bwta
1 parent 9bdc460 commit 999279d

File tree

5 files changed

+369
-0
lines changed

5 files changed

+369
-0
lines changed

src/main/java/bwta/BWTA.java

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package bwta;
2+
3+
import bwapi.Game;
4+
import bwapi.Player;
5+
import bwapi.Position;
6+
import bwapi.TilePosition;
7+
import bwem.BWEM;
8+
import bwem.Base;
9+
import bwem.ChokePoint;
10+
import bwem.area.Area;
11+
import bwem.tile.Tile;
12+
import org.apache.commons.lang3.mutable.MutableInt;
13+
14+
import java.util.*;
15+
import java.util.stream.Collectors;
16+
17+
public class BWTA {
18+
private static BWEM bwem;
19+
private static Game game;
20+
static Map<Area, Region> regionMap;
21+
static Map<ChokePoint, Chokepoint> chokeMap;
22+
static Map<Base, BaseLocation> baseMap;
23+
private static Set<Region> regions;
24+
private static Set<Chokepoint> chokepoints;
25+
private static Set<BaseLocation> baseLocations;
26+
27+
public static void readMap(final Game gamePointer) {
28+
game = gamePointer;
29+
bwem = new BWEM(game);
30+
}
31+
32+
private static boolean walkableTile(final int x, final int y) {
33+
for (int sx=0; sx < 4; sx++) {
34+
for (int sy=0; sy < 4; sy++) {
35+
if (!game.isWalkable(x+sx, y+sx)) {
36+
return false;
37+
}
38+
}
39+
}
40+
return true;
41+
}
42+
43+
public static void analyze() {
44+
bwem.initialize();
45+
46+
regionMap = new HashMap<>();
47+
for (final Area a : bwem.getMap().getAreas()) {
48+
regionMap.put(a, new Region(a));
49+
}
50+
regions = new HashSet<>(regionMap.values());
51+
52+
chokeMap = new HashMap<>();
53+
for (final ChokePoint c : bwem.getMap().getChokePoints()) {
54+
chokeMap.put(c, new Chokepoint(c));
55+
}
56+
chokepoints = new HashSet<>(chokeMap.values());
57+
58+
baseMap = new HashMap<>();
59+
for (final Base b : bwem.getMap().getBases()) {
60+
baseMap.put(b, new BaseLocation(b));
61+
}
62+
baseLocations = new HashSet<>(baseMap.values());
63+
}
64+
65+
public static Set<Region> getRegions() {
66+
return regions;
67+
}
68+
69+
public static Set<Chokepoint> getChokepoints() {
70+
return chokepoints;
71+
}
72+
73+
public static Set<BaseLocation> getBaseLocations() {
74+
return baseLocations;
75+
}
76+
77+
public static Set<BaseLocation> getStartLocations() {
78+
return getBaseLocations().stream()
79+
.filter(BaseLocation::isStartLocation)
80+
.collect(Collectors.toSet());
81+
}
82+
83+
public static BaseLocation getStartLocation(final Player player) {
84+
return getNearestBaseLocation(player.getStartLocation());
85+
}
86+
87+
public static Region getRegion(final TilePosition tileposition) {
88+
return regionMap.get(bwem.getMap().getNearestArea(tileposition));
89+
}
90+
91+
public static Region getRegion(final Position position) {
92+
return regionMap.get(bwem.getMap().getNearestArea(position.toWalkPosition()));
93+
}
94+
95+
public static Chokepoint getNearestChokepoint(final TilePosition tileposition) {
96+
return getNearestChokepoint(tileposition.toPosition());
97+
}
98+
99+
public static Chokepoint getNearestChokepoint(final Position position) {
100+
return chokepoints.stream().min((a, b) -> (int) (a.getCenter().getDistance(position) - b.getCenter().getDistance(position))).get();
101+
}
102+
103+
public static BaseLocation getNearestBaseLocation(final TilePosition tileposition) {
104+
return baseLocations.stream().min((a, b) -> (int) (a.getTilePosition().getDistance(tileposition) - b.getTilePosition().getDistance(tileposition))).get();
105+
}
106+
107+
public static BaseLocation getNearestBaseLocation(final Position position) {
108+
return baseLocations.stream().min((a, b) ->(int) (a.getPosition().getDistance(position) - b.getPosition().getDistance(position))).get();
109+
}
110+
111+
public static boolean isConnected(final TilePosition a, final TilePosition b) {
112+
return bwem.getMap().getNearestArea(a).isAccessibleFrom(bwem.getMap().getNearestArea(b));
113+
}
114+
115+
116+
public static double getGroundDistance(final TilePosition start, final TilePosition end) {
117+
final MutableInt L = new MutableInt();
118+
bwem.getMap().getPath(start.toPosition(), end.toPosition(), L);
119+
return L.intValue();
120+
}
121+
122+
public static List<TilePosition> getShortestPath(final TilePosition start, final TilePosition end) {
123+
final List<TilePosition> path = new ArrayList<>();
124+
125+
final Iterator<ChokePoint> it = bwem.getMap().getPath(start.toPosition(), end.toPosition()).iterator();
126+
127+
ChokePoint curr = null;
128+
while (it.hasNext()) {
129+
final ChokePoint next = it.next();
130+
if (curr != null) {
131+
final TilePosition t0 = curr.getCenter().toTilePosition();
132+
final TilePosition t1 = next.getCenter().toTilePosition();
133+
//trace a ray
134+
int dx = Math.abs(t1.x - t0.x);
135+
int dy = Math.abs(t1.y - t0.y);
136+
int x = t0.x;
137+
int y = t0.y;
138+
int n = 1 + dx + dy;
139+
final int x_inc = (t1.x > t0.x) ? 1 : -1;
140+
final int y_inc = (t1.x > t0.x) ? 1 : -1;
141+
int error = dx - dy;
142+
dx *= 2;
143+
dy *= 2;
144+
145+
for (; n > 0; --n) {
146+
path.add(new TilePosition(x, y));
147+
148+
if (error > 0) {
149+
x += x_inc;
150+
error -= dy;
151+
}
152+
else {
153+
y += y_inc;
154+
error += dx;
155+
}
156+
}
157+
}
158+
curr = next;
159+
}
160+
return path;
161+
}
162+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package bwta;
2+
3+
import bwapi.Position;
4+
import bwapi.TilePosition;
5+
import bwapi.Unit;
6+
import bwem.Base;
7+
8+
import java.util.Set;
9+
import java.util.stream.Collectors;
10+
11+
public class BaseLocation {
12+
private final Base base;
13+
private final Position position;
14+
private final TilePosition tilePosition;
15+
private final int minerals;
16+
private final int gas;
17+
private final Set<Unit> mineralSet;
18+
private final Set<Unit> geyserSet;
19+
private final boolean island;
20+
private final boolean mineralOnly;
21+
private final boolean startLocation;
22+
23+
24+
BaseLocation(final Base base) {
25+
this.base = base;
26+
this.position = base.getCenter();
27+
this.tilePosition = base.getLocation();
28+
this.minerals = 1;
29+
this.gas = 1;
30+
this.mineralSet = base.getMinerals().stream().map(m -> m.getUnit()).collect(Collectors.toSet());
31+
this.geyserSet = base.getGeysers().stream().map(g -> g.getUnit()).collect(Collectors.toSet());
32+
this.island = base.getArea().getAccessibleNeighbors().isEmpty();
33+
this.mineralOnly = !mineralSet.isEmpty() && geyserSet.isEmpty();
34+
this.startLocation = base.isStartingLocation();
35+
}
36+
37+
public Position getPosition() {
38+
return position;
39+
}
40+
41+
public TilePosition getTilePosition() {
42+
return tilePosition;
43+
}
44+
45+
public Region getRegion() {
46+
return BWTA.regionMap.get(base.getArea());
47+
}
48+
49+
public int minerals() {
50+
return minerals;
51+
}
52+
53+
public int gas() {
54+
return gas;
55+
}
56+
57+
public Set<Unit> getMinerals() {
58+
return mineralSet;
59+
}
60+
61+
public Set<Unit> getGeysers() {
62+
return geyserSet;
63+
}
64+
65+
public double getGroundDistance(final BaseLocation other) {
66+
return BWTA.getGroundDistance(tilePosition, other.tilePosition);
67+
}
68+
69+
public double getAirDistance(final BaseLocation other) {
70+
return position.getDistance(other.position);
71+
}
72+
73+
public boolean isIsland() {
74+
return island;
75+
}
76+
77+
public boolean isMineralOnly() {
78+
return mineralOnly;
79+
}
80+
81+
public boolean isStartLocation() {
82+
return startLocation;
83+
}
84+
85+
@Override
86+
public boolean equals(final Object o) {
87+
if (!(o instanceof BaseLocation)) {
88+
return false;
89+
}
90+
return base.equals(((BaseLocation) o).base);
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return base.hashCode();
96+
}
97+
}

src/main/java/bwta/Chokepoint.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package bwta;
2+
3+
import bwapi.Position;
4+
import bwem.ChokePoint;
5+
import org.apache.commons.lang3.tuple.ImmutablePair;
6+
import org.apache.commons.lang3.tuple.Pair;
7+
8+
9+
public class Chokepoint {
10+
private final ChokePoint chokePoint;
11+
private final Pair<Position, Position> sides;
12+
private final Position center;
13+
private final double width;
14+
15+
Chokepoint(final ChokePoint chokePoint) {
16+
this.chokePoint = chokePoint;
17+
this.sides = new ImmutablePair<>(chokePoint.getGeometry().get(1).toPosition(), chokePoint.getGeometry().get(2).toPosition());
18+
this.center = chokePoint.getGeometry().get(0).toPosition();
19+
this.width = sides.getLeft().getDistance(sides.getRight());
20+
}
21+
22+
public Pair<Region, Region> getRegions() {
23+
return new ImmutablePair<>(BWTA.regionMap.get(chokePoint.getAreas().getLeft()), BWTA.regionMap.get(chokePoint.getAreas().getRight()));
24+
}
25+
26+
public Pair<Position, Position> getSides() {
27+
return sides;
28+
}
29+
30+
public Position getCenter() {
31+
return center;
32+
}
33+
34+
public double getWidth() {
35+
return width;
36+
}
37+
38+
@Override
39+
public boolean equals(final Object o) {
40+
if (!(o instanceof Chokepoint)) {
41+
return false;
42+
}
43+
return this.chokePoint.equals(((Chokepoint) o).chokePoint);
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return chokePoint.hashCode();
49+
}
50+
}

src/main/java/bwta/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## BWTA
2+
3+
fake stripped down BWTA with methods pointing to their respective BWEM values (except Polygons)

src/main/java/bwta/Region.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package bwta;
2+
3+
import bwapi.Position;
4+
import bwem.area.Area;
5+
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
public class Region {
10+
private final Area area;
11+
private final Position center;
12+
13+
Region(final Area area) {
14+
this.area = area;
15+
this.center = area.getWalkPositionWithHighestAltitude().toPosition();
16+
}
17+
18+
19+
public Position getCenter() {
20+
return center;
21+
}
22+
23+
public Set<Chokepoint> getChokepoints() {
24+
return area.getChokePoints().stream()
25+
.map(c -> BWTA.chokeMap.get(c))
26+
.collect(Collectors.toSet());
27+
}
28+
29+
public Set<BaseLocation> getBaseLocations() {
30+
return area.getBases().stream()
31+
.map(b -> BWTA.baseMap.get(b))
32+
.collect(Collectors.toSet());
33+
}
34+
35+
public boolean isReachable(final Region region) {
36+
return area.isAccessibleFrom(region.area);
37+
}
38+
39+
public Set<Region> getReachableRegions() {
40+
return area.getAccessibleNeighbors().stream()
41+
.map(a -> BWTA.regionMap.get(a))
42+
.collect(Collectors.toSet());
43+
}
44+
45+
@Override
46+
public boolean equals(final Object o) {
47+
if (!(o instanceof Region)) {
48+
return false;
49+
}
50+
return area.equals(((Region) o).area);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return area.hashCode();
56+
}
57+
}

0 commit comments

Comments
 (0)