Skip to content

Commit

Permalink
Clear member cache during onStart
Browse files Browse the repository at this point in the history
- Helps with Issue OpenBW#52: "Unable to run consecutive games without
stopping process"
- It would be better to create an entirely new "BW" object instead
of manually resetting members.
  • Loading branch information
adakitesystems committed Oct 17, 2018
1 parent 53c6633 commit 0d25222
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
96 changes: 53 additions & 43 deletions BWAPI4J/src/main/java/org/openbw/bwapi4j/BW.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,49 +157,6 @@ public BW(
logger.warn("Korean character set not available. Some characters may not be read properly.");
this.charset = StandardCharsets.ISO_8859_1;
}

this.getUnitsFromPlayerCache =
new Cache<>(
() -> {
final Map<Player, List<PlayerUnit>> playerListMap = new HashMap<>();

for (final Unit unit : this.units.values()) {
if (unit instanceof PlayerUnit) {
final PlayerUnit playerUnit = (PlayerUnit) unit;

final Player player = playerUnit.getPlayer();

if (player != null) {
final List<PlayerUnit> units =
playerListMap.computeIfAbsent(player, list -> new ArrayList<>());
units.add(playerUnit);
}
}
}

return playerListMap;
},
this.interactionHandler);
this.getMineralPatchesCache =
new Cache<>(
() ->
this.units
.values()
.stream()
.filter(u -> u instanceof MineralPatch)
.map(u -> (MineralPatch) u)
.collect(Collectors.toList()),
this.interactionHandler);
this.getVespeneGeysersCache =
new Cache<>(
() ->
this.units
.values()
.stream()
.filter(u -> u instanceof VespeneGeyser)
.map(u -> (VespeneGeyser) u)
.collect(Collectors.toList()),
this.interactionHandler);
}

public void startGame() {
Expand Down Expand Up @@ -439,13 +396,66 @@ private void preFrame() {
logger.trace("updated all bullets.");
}

private void resetCache() {
this.getUnitsFromPlayerCache =
new Cache<>(
() -> {
final Map<Player, List<PlayerUnit>> playerListMap = new HashMap<>();

for (final Unit unit : this.units.values()) {
if (unit instanceof PlayerUnit) {
final PlayerUnit playerUnit = (PlayerUnit) unit;

final Player player = playerUnit.getPlayer();

if (player != null) {
final List<PlayerUnit> units =
playerListMap.computeIfAbsent(player, list -> new ArrayList<>());
units.add(playerUnit);
}
}
}

return playerListMap;
},
this.interactionHandler);

this.getMineralPatchesCache =
new Cache<>(
() ->
this.units
.values()
.stream()
.filter(u -> u instanceof MineralPatch)
.map(u -> (MineralPatch) u)
.collect(Collectors.toList()),
this.interactionHandler);

this.getVespeneGeysersCache =
new Cache<>(
() ->
this.units
.values()
.stream()
.filter(u -> u instanceof VespeneGeyser)
.map(u -> (VespeneGeyser) u)
.collect(Collectors.toList()),
this.interactionHandler);

this.interactionHandler.resetCache();

this.bwMap.resetCache();
}

private void onStart() {
try {
logger.trace(" --- onStart called.");
this.players.clear();
this.units.clear();
this.bullets.clear();

resetCache();

initializeTypes();

logger.trace(" --- calling initial preFrame...");
Expand Down
4 changes: 4 additions & 0 deletions BWAPI4J/src/main/java/org/openbw/bwapi4j/BWMapImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class BWMapImpl implements BWMap {
BWMapImpl(final InteractionHandler interactionHandler) {
this.interactionHandler = interactionHandler;
this.startLocations = new ArrayList<>();
resetCache();
}

void resetCache() {
this.getCreepDataCache = new Cache<>(this::getCreepData, this.interactionHandler);
}

Expand Down
10 changes: 7 additions & 3 deletions BWAPI4J/src/main/java/org/openbw/bwapi4j/InteractionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ private enum CacheIndex {
private int apm;
private int apm_including_selects;

private final Cache<List<Player>> getAlliesCache;
private final Cache<List<Player>> getEnemiesCache;
private Cache<List<Player>> getAlliesCache;
private Cache<List<Player>> getEnemiesCache;

private final Cache<List<Position>> getNukeDotsCache;
private Cache<List<Position>> getNukeDotsCache;

InteractionHandler(final BW bw) {
this.bw = bw;
resetCache();
}

void resetCache() {
this.getAlliesCache = new Cache<>(this::allies_from_native, this);
this.getEnemiesCache = new Cache<>(this::enemies_from_native, this);
this.getNukeDotsCache = new Cache<>(this::getNukeDotsData, this);
Expand Down
6 changes: 5 additions & 1 deletion BWAPI4J/src/main/java/org/openbw/bwapi4j/util/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ public class Cache<T> {
private final InteractionHandler interactionHandler;

public Cache(final Callable<T> updateFunction, final InteractionHandler interactionHandler) {
this.lastFrameUpdate = -1;
reset();
this.refreshPeriod = 1;
this.updateFunction = updateFunction;
this.interactionHandler = interactionHandler;
}

public void reset() {
this.lastFrameUpdate = -1;
}

public T get() {
final int currentFrameCount = this.interactionHandler.getFrameCount();

Expand Down

0 comments on commit 0d25222

Please sign in to comment.