From 25058296c2eca13f3e4f97f6196d31fbbb0a3cc3 Mon Sep 17 00:00:00 2001 From: Alejandro <25-alejandro@users.noreply.git.krews.org> Date: Sat, 18 May 2019 22:03:11 +0300 Subject: [PATCH] Make BB tile filling threaded --- .../games/battlebanzai/BattleBanzaiGame.java | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/games/battlebanzai/BattleBanzaiGame.java b/src/main/java/com/eu/habbo/habbohotel/games/battlebanzai/BattleBanzaiGame.java index 01650d44..97850d70 100644 --- a/src/main/java/com/eu/habbo/habbohotel/games/battlebanzai/BattleBanzaiGame.java +++ b/src/main/java/com/eu/habbo/habbohotel/games/battlebanzai/BattleBanzaiGame.java @@ -23,6 +23,8 @@ import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; public class BattleBanzaiGame extends Game { @@ -38,6 +40,8 @@ public class BattleBanzaiGame extends Game public static final int POINTS_LOCK_TILE = Emulator.getConfig().getInt("hotel.banzai.points.tile.lock", 1); + private static final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(Emulator.getConfig().getInt("hotel.banzai.fill.threads", 2)); + private int tileCount; private int countDown; @@ -292,38 +296,41 @@ public class BattleBanzaiGame extends Game if (doNotCheckFill) return; - int x = item.getX(); - int y = item.getY(); + final int x = item.getX(); + final int y = item.getY(); - List> filledAreas = new ArrayList<>(); - THashSet lockedTiles = new THashSet<>(this.lockedTiles.get(teamColor)); - filledAreas.add(this.floodFill(x, y - 1, lockedTiles, new ArrayList<>(), teamColor)); - filledAreas.add(this.floodFill(x, y + 1, lockedTiles, new ArrayList<>(), teamColor)); - filledAreas.add(this.floodFill(x - 1, y, lockedTiles, new ArrayList<>(), teamColor)); - filledAreas.add(this.floodFill(x + 1, y, lockedTiles, new ArrayList<>(), teamColor)); + final List> filledAreas = new ArrayList<>(); + final THashSet lockedTiles = new THashSet<>(this.lockedTiles.get(teamColor)); - Optional> largestAreaOfAll = filledAreas.stream().filter(Objects::nonNull).max(Comparator.comparing(List::size)); + executor.execute(() -> { + filledAreas.add(this.floodFill(x, y - 1, lockedTiles, new ArrayList<>(), teamColor)); + filledAreas.add(this.floodFill(x, y + 1, lockedTiles, new ArrayList<>(), teamColor)); + filledAreas.add(this.floodFill(x - 1, y, lockedTiles, new ArrayList<>(), teamColor)); + filledAreas.add(this.floodFill(x + 1, y, lockedTiles, new ArrayList<>(), teamColor)); - if (largestAreaOfAll.isPresent()) - { - for (RoomTile tile: largestAreaOfAll.get()) + Optional> largestAreaOfAll = filledAreas.stream().filter(Objects::nonNull).max(Comparator.comparing(List::size)); + + if (largestAreaOfAll.isPresent()) { - Optional tileItem = this.gameTiles.values().stream().filter(i -> i.getX() == tile.x && i.getY() == tile.y && i instanceof InteractionBattleBanzaiTile).findAny(); + for (RoomTile tile: largestAreaOfAll.get()) + { + Optional tileItem = this.gameTiles.values().stream().filter(i -> i.getX() == tile.x && i.getY() == tile.y && i instanceof InteractionBattleBanzaiTile).findAny(); - tileItem.ifPresent(habboItem -> { - this.tileLocked(teamColor, habboItem, habbo, true); + tileItem.ifPresent(habboItem -> { + this.tileLocked(teamColor, habboItem, habbo, true); - habboItem.setExtradata((2 + (teamColor.type * 3) + 3) + ""); - this.room.updateItem(habboItem); - }); + habboItem.setExtradata((2 + (teamColor.type * 3) + 3) + ""); + this.room.updateItem(habboItem); + }); + } + + this.refreshCounters(teamColor); + if (habbo != null) + { + habbo.getHabboInfo().getGamePlayer().addScore(BattleBanzaiGame.POINTS_LOCK_TILE * largestAreaOfAll.get().size()); + } } - - this.refreshCounters(teamColor); - if (habbo != null) - { - habbo.getHabboInfo().getGamePlayer().addScore(BattleBanzaiGame.POINTS_LOCK_TILE * largestAreaOfAll.get().size()); - } - } + }); } private List floodFill(int x, int y, THashSet lockedTiles, List stack, GameTeamColors color)