Skip to content

Commit 58b7786

Browse files
committed
Set<Unit> -> List<Unit>
1 parent 1320667 commit 58b7786

File tree

7 files changed

+107
-105
lines changed

7 files changed

+107
-105
lines changed

src/main/java/bwapi/Game.java

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ public class Game {
3939

4040
private static final int REGION_DATA_SIZE = 5000;
4141

42-
private final Set<Unit> staticMinerals = new HashSet<>();
43-
private final Set<Unit> staticGeysers = new HashSet<>();
44-
private final Set<Unit> staticNeutralUnits = new HashSet<>();
4542
private final Set<Integer> visibleUnits = new HashSet<>();
4643
private final Client client;
4744
private final GameData gameData;
45+
46+
private List<Unit> staticMinerals;
47+
private List<Unit> staticGeysers;
48+
private List<Unit> staticNeutralUnits;
49+
4850
// CONSTANT
4951
private Player[] players;
5052
private Region[] regions;
5153
private Force[] forces;
5254
private Bullet[] bullets;
53-
private Set<Force> forceSet;
54-
private Set<Player> playerSet;
55-
private Set<Region> regionSet;
55+
private List<Force> forceSet;
56+
private List<Player> playerSet;
57+
private List<Region> regionSet;
5658
// CHANGING
5759
private Unit[] units;
5860
//CACHED
@@ -89,7 +91,7 @@ public Game(Client client) {
8991
this.gameData = client.data();
9092
}
9193

92-
private static boolean hasPower(final int x, final int y, final UnitType unitType, final Set<Unit> pylons) {
94+
private static boolean hasPower(final int x, final int y, final UnitType unitType, final List<Unit> pylons) {
9395
if (unitType.id >= 0 && unitType.id < UnitType.None.id && (!unitType.requiresPsi() || !unitType.isBuilding())) {
9496
return true;
9597
}
@@ -118,9 +120,6 @@ private static boolean hasPower(final int x, final int y, final UnitType unitTyp
118120
Call this method in EventHander::OnMatchStart
119121
*/
120122
void init() {
121-
staticMinerals.clear();
122-
staticGeysers.clear();
123-
staticNeutralUnits.clear();
124123
visibleUnits.clear();
125124

126125
final int forceCount = gameData.getForceCount();
@@ -129,15 +128,15 @@ void init() {
129128
forces[id] = new Force(gameData.getForces(id), id, this);
130129
}
131130

132-
forceSet = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(forces)));
131+
forceSet = Collections.unmodifiableList(Arrays.asList(forces));
133132

134133
final int playerCount = gameData.getPlayerCount();
135134
players = new Player[playerCount];
136135
for (int id = 0; id < playerCount; id++) {
137136
players[id] = new Player(gameData.getPlayers(id), id, this);
138137
}
139138

140-
playerSet = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(players)));
139+
playerSet = Collections.unmodifiableList(Arrays.asList(players));
141140

142141
final int bulletCount = 100;
143142
bullets = new Bullet[bulletCount];
@@ -155,9 +154,13 @@ void init() {
155154
region.updateNeighbours();
156155
}
157156

158-
regionSet = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(regions)));
157+
regionSet = Collections.unmodifiableList(Arrays.asList(regions));
159158

160159
units = new Unit[10000];
160+
161+
final List<Unit> staticMinerals = new ArrayList<>();
162+
final List<Unit> staticGeysers = new ArrayList<>();
163+
final List<Unit> staticNeutralUnits = new ArrayList<>();
161164
for (int id = 0; id < gameData.getInitialUnitCount(); id++) {
162165
final Unit unit = new Unit(gameData.getUnits(id), id,this);
163166
//skip ghost units
@@ -177,7 +180,12 @@ void init() {
177180
}
178181
}
179182

183+
this.staticMinerals = Collections.unmodifiableList(staticMinerals);
184+
this.staticGeysers = Collections.unmodifiableList(staticGeysers);
185+
this.staticNeutralUnits = Collections.unmodifiableList(staticNeutralUnits);
186+
180187
randomSeed = gameData.getRandomSeed();
188+
181189
revision = gameData.getRevision();
182190
debug = gameData.isDebug();
183191
self = players[gameData.getSelf()];
@@ -284,73 +292,65 @@ void addShape(final ShapeType type, final CoordinateType coordType, final int x1
284292
shape.setIsSolid(isSolid);
285293
}
286294

287-
public Set<Force> getForces() {
295+
public List<Force> getForces() {
288296
return forceSet;
289297
}
290298

291-
public Set<Player> getPlayers() {
299+
public List<Player> getPlayers() {
292300
return playerSet;
293301
}
294302

295-
public Set<Unit> getAllUnits() {
303+
public List<Unit> getAllUnits() {
296304
if (getFrameCount() == 0) {
297-
final HashSet<Unit> us = new HashSet<>();
298-
for (final Unit u : units) {
299-
if (u != null) {
300-
us.add(u);
301-
}
302-
}
303-
return us;
305+
return Arrays.stream(units)
306+
.filter(Objects::nonNull)
307+
.collect(Collectors.toList());
304308
}
305309
return visibleUnits.stream()
306310
.map(i -> units[i])
307-
.collect(Collectors.toSet());
311+
.collect(Collectors.toList());
308312
}
309313

310-
public Set<Unit> getMinerals() {
314+
public List<Unit> getMinerals() {
311315
return getAllUnits().stream()
312316
.filter(u -> u.getType().isMineralField())
313-
.collect(Collectors.toSet());
317+
.collect(Collectors.toList());
314318
}
315319

316-
public Set<Unit> getGeysers() {
320+
public List<Unit> getGeysers() {
317321
return getAllUnits().stream()
318322
.filter(u -> u.getType() == Resource_Vespene_Geyser)
319-
.collect(Collectors.toSet());
323+
.collect(Collectors.toList());
320324
}
321325

322-
public Set<Unit> getNeutralUnits() {
326+
public List<Unit> getNeutralUnits() {
323327
return getAllUnits().stream()
324328
.filter(u -> u.getPlayer().equals(neutral()))
325-
.collect(Collectors.toSet());
329+
.collect(Collectors.toList());
326330
}
327331

328-
public Set<Unit> getStaticMinerals() {
329-
return new HashSet<>(staticMinerals);
332+
public List<Unit> getStaticMinerals() {
333+
return staticMinerals;
330334
}
331335

332-
public Set<Unit> getStaticGeysers() {
333-
return new HashSet<>(staticGeysers);
336+
public List<Unit> getStaticGeysers() {
337+
return staticGeysers;
334338
}
335339

336-
public Set<Unit> getStaticNeutralUnits() {
337-
return new HashSet<>(staticNeutralUnits);
340+
public List<Unit> getStaticNeutralUnits() {
341+
return staticNeutralUnits;
338342
}
339343

340-
public Set<Bullet> getBullets() {
341-
final Set<Bullet> bs = new HashSet<>();
342-
for (final Bullet bullet : bullets) {
343-
if (bullet.exists()) {
344-
bs.add(bullet);
345-
}
346-
}
347-
return bs;
344+
public List<Bullet> getBullets() {
345+
return Arrays.stream(bullets)
346+
.filter(Bullet::exists)
347+
.collect(Collectors.toList());
348348
}
349349

350-
public Set<Position> getNukeDots() {
350+
public List<Position> getNukeDots() {
351351
return IntStream.range(0, gameData.getNukeDotCount())
352352
.mapToObj(id -> new Position(gameData.getNukeDots(id)))
353-
.collect(Collectors.toSet());
353+
.collect(Collectors.toList());
354354
}
355355

356356
public Force getForce(final int forceID) {
@@ -436,50 +436,52 @@ public void enableFlag(final Flag flag) {
436436
addCommand(EnableFlag, flag.value, 1);
437437
}
438438

439-
public Set<Unit> getUnitsOnTile(final int tileX, final int tileY) {
439+
public List<Unit> getUnitsOnTile(final int tileX, final int tileY) {
440440
return getAllUnits().stream().filter(u -> {
441441
final TilePosition tp = u.getTilePosition();
442442
return tp.x == tileX && tp.y == tileY;
443-
}).collect(Collectors.toSet());
443+
}).collect(Collectors.toList());
444444
}
445445

446-
public Set<Unit> getUnitsOnTile(final TilePosition tile) {
446+
public List<Unit> getUnitsOnTile(final TilePosition tile) {
447447
return getUnitsOnTile(tile.x, tile.y);
448448
}
449449

450-
public Set<Unit> getUnitsInRectangle(final int left, final int top, final int right, final int bottom) {
450+
public List<Unit> getUnitsInRectangle(final int left, final int top, final int right, final int bottom) {
451451
return getUnitsInRectangle(left, top, right, bottom, u -> true);
452452
}
453453

454-
public Set<Unit> getUnitsInRectangle(final int left, final int top, final int right, final int bottom, final UnitFilter filter) {
454+
public List<Unit> getUnitsInRectangle(final int left, final int top, final int right, final int bottom, final UnitFilter filter) {
455455
return getAllUnits().stream().filter(u -> {
456456
final Position p = u.getPosition();
457457
return left <= p.x && top <= p.y && p.x < right && p.y < bottom && filter.operation(u);
458-
}).collect(Collectors.toSet());
458+
}).collect(Collectors.toList());
459459
}
460460

461-
public Set<Unit> getUnitsInRectangle(final Position leftTop, final Position rightBottom) {
461+
public List<Unit> getUnitsInRectangle(final Position leftTop, final Position rightBottom) {
462462
return getUnitsInRectangle(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y, u -> true);
463463
}
464464

465-
public Set<Unit> getUnitsInRectangle(final Position leftTop, final Position rightBottom, final UnitFilter filter) {
465+
public List<Unit> getUnitsInRectangle(final Position leftTop, final Position rightBottom, final UnitFilter filter) {
466466
return getUnitsInRectangle(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y, filter);
467467
}
468468

469-
public Set<Unit> getUnitsInRadius(final int x, final int y, final int radius) {
469+
public List<Unit> getUnitsInRadius(final int x, final int y, final int radius) {
470470
return getUnitsInRadius(x, y, radius, u -> true);
471471
}
472472

473-
public Set<Unit> getUnitsInRadius(final int x, final int y, final int radius, final UnitFilter filter) {
473+
public List<Unit> getUnitsInRadius(final int x, final int y, final int radius, final UnitFilter filter) {
474474
return getUnitsInRadius(new Position(x, y), radius, filter);
475475
}
476476

477-
public Set<Unit> getUnitsInRadius(final Position center, final int radius) {
477+
public List<Unit> getUnitsInRadius(final Position center, final int radius) {
478478
return getUnitsInRadius(center, radius, u -> true);
479479
}
480480

481-
public Set<Unit> getUnitsInRadius(final Position center, final int radius, final UnitFilter filter) {
482-
return getAllUnits().stream().filter(u -> center.getApproxDistance(u.getPosition()) <= radius && filter.operation(u)).collect(Collectors.toSet());
481+
public List<Unit> getUnitsInRadius(final Position center, final int radius, final UnitFilter filter) {
482+
return getAllUnits().stream()
483+
.filter(u -> center.getApproxDistance(u.getPosition()) <= radius && filter.operation(u))
484+
.collect(Collectors.toList());
483485
}
484486

485487
public Unit getClosestUnitInRectangle(final Position center, final int left, final int top, final int right, final int bottom) {
@@ -622,7 +624,7 @@ public boolean hasPowerPrecise(final Position position, final UnitType unitType)
622624
if (!position.isValid(this)) {
623625
return false;
624626
}
625-
return hasPower(position.x, position.y, unitType, self().getUnits().stream().filter(u -> u.getType() == Protoss_Pylon).collect(Collectors.toSet()));
627+
return hasPower(position.x, position.y, unitType, self().getUnits().stream().filter(u -> u.getType() == Protoss_Pylon).collect(Collectors.toList()));
626628
}
627629

628630
public boolean hasPower(final int tileX, final int tileY) {
@@ -715,7 +717,7 @@ public boolean canBuildHere(final TilePosition position, final UnitType type, fi
715717
// Ground getUnit dimension check
716718
if (type != Special_Start_Location) {
717719
final Position targPos = lt.toPosition().add(type.tileSize().toPosition().divide(2));
718-
final Set<Unit> unitsInRect = getUnitsInRectangle(lt.toPosition(), rb.toPosition(),
720+
final List<Unit> unitsInRect = getUnitsInRectangle(lt.toPosition(), rb.toPosition(),
719721
u -> !u.isFlying() && !u.isLoaded() && builder != null || type == Zerg_Nydus_Canal
720722
&& u.getLeft() <= targPos.x + type.dimensionRight()
721723
&& u.getTop() <= targPos.y + type.dimensionDown()
@@ -1070,10 +1072,10 @@ public boolean issueCommand(final Collection<Unit> units, final UnitCommand comm
10701072
.contains(false);
10711073
}
10721074

1073-
public Set<Unit> getSelectedUnits() {
1075+
public List<Unit> getSelectedUnits() {
10741076
return IntStream.range(0, gameData.getSelectedUnitCount())
10751077
.mapToObj(i -> units[gameData.getSelectedUnits(i)])
1076-
.collect(Collectors.toSet());
1078+
.collect(Collectors.toList());
10771079
}
10781080

10791081
public Player self() {
@@ -1088,26 +1090,26 @@ public Player neutral() {
10881090
return neutral;
10891091
}
10901092

1091-
public Set<Player> allies() {
1093+
public List<Player> allies() {
10921094
final Player self = self();
10931095
return getPlayers().stream()
10941096
.filter(self::isAlly)
1095-
.collect(Collectors.toSet());
1097+
.collect(Collectors.toList());
10961098
}
10971099

10981100
//TODO FIX in 4.3.0
1099-
public Set<Player> enemies() {
1101+
public List<Player> enemies() {
11001102
final Player self = self();
11011103
return getPlayers().stream()
11021104
.filter(p -> !(p.isNeutral() || self.isAlly(p)))
1103-
.collect(Collectors.toSet());
1105+
.collect(Collectors.toList());
11041106
}
11051107

11061108
//TODO FIX in 4.3.0
1107-
public Set<Player> observers() {
1109+
public List<Player> observers() {
11081110
return getPlayers().stream()
11091111
.filter(Player::isObserver)
1110-
.collect(Collectors.toSet());
1112+
.collect(Collectors.toList());
11111113
}
11121114

11131115
public void drawText(final CoordinateType ctype, final int x, final int y, final String cstr_format) {
@@ -1513,7 +1515,7 @@ public int countdownTimer() {
15131515
return gameData.getCountdownTimer();
15141516
}
15151517

1516-
public Set<Region> getAllRegions() {
1518+
public List<Region> getAllRegions() {
15171519
return regionSet;
15181520
}
15191521

src/main/java/bwapi/Region.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33

44
import bwapi.ClientData.RegionData;
5-
import java.util.HashSet;
6-
import java.util.Set;
5+
import java.util.*;
76

87
public class Region {
98
private final RegionData regionData;
109
private final Game game;
1110

12-
private final Set<Region> neighbours = new HashSet<>();
1311
private final int id;
1412
private final int regionGroupID;
1513
private final Position center;
@@ -23,6 +21,8 @@ public class Region {
2321
private Region closestAccessibleRegion;
2422
private Region closestInaccessibleRegion;
2523

24+
private List<Region> neighbours;
25+
2626
Region(final RegionData regionData, final Game game) {
2727
this.regionData = regionData;
2828
this.game = game;
@@ -42,6 +42,7 @@ void updateNeighbours() {
4242
int accessibleBestDist = Integer.MAX_VALUE;
4343
int inaccessibleBestDist = Integer.MAX_VALUE;
4444

45+
final List<Region> neighbours = new ArrayList<>();
4546
for (int i = 0; i < regionData.getNeighborCount(); i++) {
4647
final Region region = game.getRegion(regionData.getNeighbors(i));
4748
neighbours.add(region);
@@ -56,6 +57,7 @@ void updateNeighbours() {
5657
inaccessibleBestDist = d;
5758
}
5859
}
60+
this.neighbours = Collections.unmodifiableList(neighbours);
5961
}
6062

6163
public int getID() {
@@ -82,8 +84,8 @@ public boolean isAccessible() {
8284
return accessible;
8385
}
8486

85-
public Set<Region> getNeighbors() {
86-
return new HashSet<>(neighbours);
87+
public List<Region> getNeighbors() {
88+
return neighbours;
8789
}
8890

8991
public int getBoundsLeft() {
@@ -114,7 +116,7 @@ public int getDistance(final Region other) {
114116
return getCenter().getApproxDistance(other.getCenter());
115117
}
116118

117-
public Set<Unit> getUnits() {
119+
public List<Unit> getUnits() {
118120
return game.getUnitsInRectangle(getBoundsLeft(), getBoundsTop(), getBoundsRight(), getBoundsBottom(),
119121
u -> equals(u.getRegion()));
120122
}

0 commit comments

Comments
 (0)