From 62c274c5119d2522205109c10975a7996f7cf757 Mon Sep 17 00:00:00 2001 From: Adakite Systems Date: Wed, 17 Oct 2018 12:40:13 -0600 Subject: [PATCH] Clear member cache during onStart - Helps with Issue #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. --- .../src/main/java/org/openbw/bwapi4j/BW.java | 96 ++++++++++--------- .../java/org/openbw/bwapi4j/BWMapImpl.java | 4 + .../openbw/bwapi4j/InteractionHandler.java | 10 +- 3 files changed, 64 insertions(+), 46 deletions(-) diff --git a/BWAPI4J/src/main/java/org/openbw/bwapi4j/BW.java b/BWAPI4J/src/main/java/org/openbw/bwapi4j/BW.java index 3b092705..e304fd03 100644 --- a/BWAPI4J/src/main/java/org/openbw/bwapi4j/BW.java +++ b/BWAPI4J/src/main/java/org/openbw/bwapi4j/BW.java @@ -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> 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 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() { @@ -439,6 +396,57 @@ private void preFrame() { logger.trace("updated all bullets."); } + private void resetCache() { + this.getUnitsFromPlayerCache = + new Cache<>( + () -> { + final Map> 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 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."); @@ -446,6 +454,8 @@ private void onStart() { this.units.clear(); this.bullets.clear(); + resetCache(); + initializeTypes(); logger.trace(" --- calling initial preFrame..."); diff --git a/BWAPI4J/src/main/java/org/openbw/bwapi4j/BWMapImpl.java b/BWAPI4J/src/main/java/org/openbw/bwapi4j/BWMapImpl.java index d809efd1..028236df 100644 --- a/BWAPI4J/src/main/java/org/openbw/bwapi4j/BWMapImpl.java +++ b/BWAPI4J/src/main/java/org/openbw/bwapi4j/BWMapImpl.java @@ -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); } diff --git a/BWAPI4J/src/main/java/org/openbw/bwapi4j/InteractionHandler.java b/BWAPI4J/src/main/java/org/openbw/bwapi4j/InteractionHandler.java index 2444615a..19a7f371 100644 --- a/BWAPI4J/src/main/java/org/openbw/bwapi4j/InteractionHandler.java +++ b/BWAPI4J/src/main/java/org/openbw/bwapi4j/InteractionHandler.java @@ -82,13 +82,17 @@ private enum CacheIndex { private int apm; private int apm_including_selects; - private final Cache> getAlliesCache; - private final Cache> getEnemiesCache; + private Cache> getAlliesCache; + private Cache> getEnemiesCache; - private final Cache> getNukeDotsCache; + private Cache> 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);