Skip to content

Commit 4d2bd5b

Browse files
author
bytekeeper
committed
getAllUnits() gets called a lot, even within a single frame: Calculate it once per frame/init
1 parent 33c28b6 commit 4d2bd5b

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/main/java/bwapi/EventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void operation(final ClientData.Event event) {
2727
eventListener.onEnd(event.getV1() != 0);
2828
break;
2929
case MatchFrame:
30-
game.updateUnitPositions(frames);
30+
game.onFrame(frames);
3131
eventListener.onFrame();
3232
frames += 1;
3333
break;

src/main/java/bwapi/Game.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class Game {
4040
private static final int REGION_DATA_SIZE = 5000;
4141

4242
private final Set<Integer> visibleUnits = new HashSet<>();
43+
private List<Unit> allUnits;
4344
private final Client client;
4445
private final GameData gameData;
4546

@@ -147,7 +148,7 @@ void init() {
147148
final int regionCount = gameData.getRegionCount();
148149
regions = new Region[regionCount];
149150
for (int id = 0; id < regionCount; id++) {
150-
regions[id] = new Region(gameData.getRegions(id),this);
151+
regions[id] = new Region(gameData.getRegions(id), this);
151152
}
152153

153154
for (final Region region : regions) {
@@ -161,13 +162,15 @@ void init() {
161162
final List<Unit> staticMinerals = new ArrayList<>();
162163
final List<Unit> staticGeysers = new ArrayList<>();
163164
final List<Unit> staticNeutralUnits = new ArrayList<>();
165+
final List<Unit> allUnits = new ArrayList<>();
164166
for (int id = 0; id < gameData.getInitialUnitCount(); id++) {
165-
final Unit unit = new Unit(gameData.getUnits(id), id,this);
167+
final Unit unit = new Unit(gameData.getUnits(id), id, this);
166168
//skip ghost units
167169
if (unit.getInitialType() == UnitType.Terran_Marine && unit.getInitialHitPoints() == 0) {
168170
continue;
169171
}
170172
this.units[id] = unit;
173+
allUnits.add(unit);
171174

172175
if (unit.getType().isMineralField()) {
173176
staticMinerals.add(unit);
@@ -183,6 +186,7 @@ void init() {
183186
this.staticMinerals = Collections.unmodifiableList(staticMinerals);
184187
this.staticGeysers = Collections.unmodifiableList(staticGeysers);
185188
this.staticNeutralUnits = Collections.unmodifiableList(staticNeutralUnits);
189+
this.allUnits = Collections.unmodifiableList(allUnits);
186190

187191
randomSeed = gameData.getRandomSeed();
188192

@@ -257,7 +261,10 @@ void unitHide(final int id) {
257261
visibleUnits.remove(id);
258262
}
259263

260-
void updateUnitPositions(final int frame) {
264+
void onFrame(final int frame) {
265+
allUnits = Collections.unmodifiableList(visibleUnits.stream()
266+
.map(i -> units[i])
267+
.collect(Collectors.toList()));
261268
getAllUnits().forEach(u -> u.updatePosition(frame));
262269
}
263270

@@ -301,14 +308,7 @@ public List<Player> getPlayers() {
301308
}
302309

303310
public List<Unit> getAllUnits() {
304-
if (getFrameCount() == 0) {
305-
return Arrays.stream(units)
306-
.filter(Objects::nonNull)
307-
.collect(Collectors.toList());
308-
}
309-
return visibleUnits.stream()
310-
.map(i -> units[i])
311-
.collect(Collectors.toList());
311+
return allUnits;
312312
}
313313

314314
public List<Unit> getMinerals() {

0 commit comments

Comments
 (0)