Skip to content

Commit e709efc

Browse files
committed
implement some unitset methods in game
1 parent 917dd63 commit e709efc

File tree

4 files changed

+126
-50
lines changed

4 files changed

+126
-50
lines changed

src/main/java/JavaBWAPIBackend/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public class UnitData {
235235
static final int SIZE = 336;
236236
}
237237

238-
public UnitData unit(int id) { return new UnitData(id); }
238+
public UnitData getUnit(int id) { return new UnitData(id); }
239239

240240
private static final int BulletOffset = 0x346fa8;
241241

src/main/java/bwapi/EventHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66

77
class EventHandler implements Client.EventHandler {
88
private final BWListener eventListener;
9-
private final Client.GameData data;
109
private Game game;
1110

1211
public EventHandler(final BWListener eventListener, final Client.GameData data) {
1312
this.eventListener = eventListener;
14-
this.data = data;
13+
game = new Game(data);
1514
}
1615

1716
public void operation(Client.GameData.Event event) {
1817
switch (event.type()) {
1918
case 0: //MatchStart
20-
//recreate a new game instance every onStart instead of a "clear" method in Game
21-
game = new Game(data);
19+
game.reset();
2220
eventListener.onStart();
2321
break;
2422
case 1: //MatchEnd
@@ -48,9 +46,11 @@ public void operation(Client.GameData.Event event) {
4846
eventListener.onUnitEvade(game.getUnit(event.v1()));
4947
break;
5048
case 10: // UnitShow
49+
game.unitShow(event.v1());
5150
eventListener.onUnitShow(game.getUnit(event.v1()));
5251
break;
5352
case 11: //UnitHide
53+
game.unitHide(event.v1());
5454
eventListener.onUnitHide(game.getUnit(event.v1()));
5555
break;
5656
case 12: //UnitCreate

src/main/java/bwapi/Game.java

Lines changed: 92 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,58 @@
1818
import static bwapi.types.UnitType.*;
1919

2020
public class Game {
21-
final GameData gameData;
21+
private final GameData gameData;
2222

23-
///CONSTANT
24-
final Map<Integer, Player> players = new HashMap<>();
25-
final Map<Integer, Region> regions = new HashMap<>();
26-
final Map<Integer, Force> forces = new HashMap<>();
23+
// CONSTANT
24+
private final Map<Integer, Player> players = new HashMap<>();
25+
private final Map<Integer, Region> regions = new HashMap<>();
26+
private final Map<Integer, Force> forces = new HashMap<>();
2727

28-
//CHANGING
28+
private final Set<Unit> staticMinerals = new HashSet<>();
29+
private final Set<Unit> staticGeysers = new HashSet<>();
30+
private final Set<Unit> staticNeutralUnits = new HashSet<>();
31+
32+
// CHANGING
2933
final Map<Integer, Unit> units = new HashMap<>();
34+
final Set<Integer> visibleUnits = new HashSet<>();
3035

3136
final Map<Integer, Bullet> bullets = new HashMap<>();
3237

3338
public Game(GameData gameData) {
3439
this.gameData = gameData;
40+
}
41+
42+
/*
43+
Call this method in EventHander::OnMatchStart
44+
*/
45+
void reset() {
46+
clear();
3547
init();
3648
}
3749

50+
private void clear() {
51+
players.clear();
52+
regions.clear();
53+
forces.clear();
54+
staticMinerals.clear();
55+
staticGeysers.clear();
56+
staticNeutralUnits.clear();
57+
units.clear();
58+
visibleUnits.clear();
59+
bullets.clear();
60+
}
61+
62+
void unitShow(final int id) {
63+
if (!units.containsKey(id)) {
64+
units.put(id, new Unit(gameData.getUnit(id), this));
65+
}
66+
visibleUnits.add(id);
67+
}
68+
69+
void unitHide(final int id) {
70+
visibleUnits.remove(id);
71+
}
72+
3873
private void init() {
3974
for (int id=0; id < gameData.getForceCount(); id++) {
4075
forces.put(id, new Force(gameData.getForce(id), this));
@@ -45,67 +80,79 @@ private void init() {
4580
for (int id=0; id < gameData.regionCount(); id++) {
4681
regions.put(id, new Region(gameData.getRegion(id), this));
4782
}
83+
84+
for (int id=0; id < gameData.getInitialUnitCount(); id++) {
85+
final Unit unit = new Unit(gameData.getUnit(id), this);
86+
87+
units.put(id, unit);
88+
89+
if (unit.getType().isMineralField()) {
90+
staticMinerals.add(unit);
91+
}
92+
if (unit.getType() == Resource_Vespene_Geyser) {
93+
staticGeysers.add(unit);
94+
}
95+
if (unit.getPlayer().equals(neutral())) {
96+
staticNeutralUnits.add(unit);
97+
}
98+
}
4899
}
49100

50-
public Collection<Force> getForces() {
51-
return forces.values();
101+
public Set<Force> getForces() {
102+
return new HashSet<>(forces.values());
52103
}
53104

54-
public Collection<Player> getPlayers() {
55-
return players.values();
105+
public Set<Player> getPlayers() {
106+
return new HashSet<>(players.values());
56107
}
57108

58109

59-
public Collection<Unit> getAllUnits() {
60-
Set<Unit> units = new HashSet<>();
61-
for (int id=0; id < gameData.getInitialUnitCount(); id++) {
62-
units.add(new Unit(gameData.unit(id), this));
110+
public Set<Unit> getAllUnits() {
111+
// simulate current BWAPI behavior
112+
if (getFrameCount() == 0) {
113+
return new HashSet<>(units.values());
63114
}
64-
return units;
65-
//return units.values();
115+
return visibleUnits.stream()
116+
.map(units::get)
117+
.collect(Collectors.toSet());
66118
}
67119

68-
//TODO
69-
public List<Unit> getMinerals() {
70-
return null;
120+
public Set<Unit> getMinerals() {
121+
return getAllUnits().stream()
122+
.filter(u ->u.getType().isMineralField())
123+
.collect(Collectors.toSet());
71124
}
72125

73-
//TODO
74-
public List<Unit> getGeysers() {
75-
return null;
126+
public Set<Unit> getGeysers() {
127+
return getAllUnits().stream()
128+
.filter(u ->u.getType() == Resource_Vespene_Geyser)
129+
.collect(Collectors.toSet());
76130
}
77131

78-
//TODO
79-
public List<Unit> getNeutralUnits() {
80-
return null;
132+
public Set<Unit> getNeutralUnits() {
133+
return getAllUnits().stream()
134+
.filter(u ->u.getPlayer().equals(neutral()))
135+
.collect(Collectors.toSet());
81136
}
82137

83-
//TODO
84-
public List<Unit> getStaticMinerals() {
85-
return null;
138+
public Set<Unit> getStaticMinerals() {
139+
return new HashSet<>(staticMinerals);
86140
}
87141

88-
//TODO
89-
public List<Unit> getStaticGeysers() {
90-
return null;
142+
public Set<Unit> getStaticGeysers() {
143+
return new HashSet<>(staticGeysers);
91144
}
92145

93-
//TODO
94-
public List<Unit> getStaticNeutralUnits() {
95-
return null;
146+
public Set<Unit> getStaticNeutralUnits() {
147+
return new HashSet<>(staticNeutralUnits);
96148
}
97149

98-
99-
public Collection<Bullet> getBullets() {
100-
//TODO cache this in onFrame
101-
final List<Bullet> bullets = new ArrayList<>();
102-
for (int i=0; i < gameData.bulletCount(); i++) {
103-
bullets.add(new Bullet(gameData.bullet(i), this));
104-
}
105-
return bullets;
150+
public Set<Bullet> getBullets() {
151+
// TODO
152+
// for (int i=0; i < gameData.bulletCount(); i++) {bullets.add(new Bullet(gameData.bullet(i), this))};
153+
return null;
106154
}
107155

108-
109156
public Set<Position> getNukeDots() {
110157
return IntStream.range(0, gameData.nukeDotCount())
111158
.mapToObj(id -> new Position(gameData.getNukeDotX(id), gameData.getNukeDotY((id))))
@@ -365,7 +412,7 @@ public boolean canBuildHere(final TilePosition position, final UnitType type, fi
365412
return false;
366413
}
367414

368-
//if the unit is a refinery, we just need to check the set of geysers to see if the position
415+
//if the getUnit is a refinery, we just need to check the set of geysers to see if the position
369416
//matches one of them (and the type is still vespene geyser)
370417
if ( type.isRefinery() ) {
371418
for (final Unit g : getGeysers()) {
@@ -400,7 +447,7 @@ else if (!builder.getType().isFlyingBuilding() && type != Zerg_Nydus_Canal && !t
400447
}
401448
}
402449

403-
// Ground unit dimension check
450+
// Ground getUnit dimension check
404451
if (type != Special_Start_Location) {
405452
final Position targPos = lt.toPosition().add(type.tileSize().toPosition().divide(2));
406453
Set<Unit> unitsInRect = getUnitsInRectangle(lt.toPosition(), rb.toPosition(),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import bwapi.BWClient;
2+
import bwapi.DefaultBWListener;
3+
import bwapi.Game;
4+
5+
public class PlayTestListener extends DefaultBWListener {
6+
final BWClient bwClient;
7+
8+
Game game;
9+
10+
PlayTestListener() {
11+
bwClient = new BWClient(this);
12+
bwClient.startGame();
13+
}
14+
15+
public void onStart() {
16+
game = bwClient.getGame();
17+
System.out.println(game.getAllUnits().size());
18+
System.out.println(game.getStaticMinerals().size());
19+
}
20+
21+
public void onFrame() {
22+
System.out.println(game.getAllUnits().size());
23+
System.out.println(game.getMinerals().size());
24+
}
25+
26+
public static void main(String[] args) {
27+
new PlayTestListener();
28+
}
29+
}

0 commit comments

Comments
 (0)