From 373fb4b3618d555cff7c5921851025b085b356fa Mon Sep 17 00:00:00 2001 From: Alejandro <25-alejandro@users.noreply.git.ngh.codes> Date: Sun, 12 May 2019 06:35:23 -0400 Subject: [PATCH] Add Battle Banzai tile filling --- .../games/battlebanzai/BattleBanzaiGame.java | 85 ++++++++++++++++++- 1 file changed, 83 insertions(+), 2 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 f61002ff..3fbb2a99 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 @@ -21,8 +21,7 @@ import com.eu.habbo.threading.runnables.BattleBanzaiTilesFlicker; import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; -import java.util.Collection; -import java.util.Map; +import java.util.*; public class BattleBanzaiGame extends Game { @@ -269,6 +268,11 @@ public class BattleBanzaiGame extends Game public void tileLocked(GameTeamColors teamColor, HabboItem item, Habbo habbo) + { + this.tileLocked(teamColor, item, habbo, false); + } + + public void tileLocked(GameTeamColors teamColor, HabboItem item, Habbo habbo, boolean doNotCheckFill) { if(item instanceof InteractionBattleBanzaiTile) { @@ -284,8 +288,85 @@ public class BattleBanzaiGame extends Game { AchievementManager.progressAchievement(habbo, Emulator.getGameEnvironment().getAchievementManager().getAchievement("BattleBallTilesLocked")); } + + if (doNotCheckFill) return; + + int x = item.getX(); + int y = item.getY(); + + List> filledAreas = new ArrayList<>(); + filledAreas.add(this.floodFill(x, y - 1, this.lockedTiles.get(teamColor), new ArrayList<>())); + filledAreas.add(this.floodFill(x, y + 1, this.lockedTiles.get(teamColor), new ArrayList<>())); + filledAreas.add(this.floodFill(x - 1, y, this.lockedTiles.get(teamColor), new ArrayList<>())); + filledAreas.add(this.floodFill(x + 1, y, this.lockedTiles.get(teamColor), new ArrayList<>())); + + Optional> largestAreaOfAll = filledAreas.stream().filter(Objects::nonNull).max(Comparator.comparing(List::size)); + + if (largestAreaOfAll.isPresent()) + { + 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); + + 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()); + } + } } + private List floodFill(int x, int y, THashSet lockedTiles, List stack) + { + if (this.isOutOfBounds(x, y)) return null; + + RoomTile tile = this.room.getLayout().getTile((short)x, (short)y); + + if (this.hasLockedTileAtCoordinates(x, y, lockedTiles) || stack.contains(tile)) return stack; + + stack.add(tile); + + List> result = new ArrayList<>(); + result.add(this.floodFill(x, y - 1, lockedTiles, stack)); + result.add(this.floodFill(x, y + 1, lockedTiles, stack)); + result.add(this.floodFill(x - 1, y, lockedTiles, stack)); + result.add(this.floodFill(x + 1, y, lockedTiles, stack)); + + if (result.contains(null)) return null; + + Optional> biggestArea = result.stream().max(Comparator.comparing(List::size)); + + return biggestArea.orElse(null); + + } + + private boolean hasLockedTileAtCoordinates(int x, int y, THashSet lockedTiles) + { + for (HabboItem item: lockedTiles) + { + if (item.getX() == x && item.getY() == y) return true; + } + + return false; + } + + private boolean isOutOfBounds(int x, int y) + { + for (HabboItem item: this.gameTiles.values()) + { + if (item.getX() == x && item.getY() == y) return false; + } + + return true; + } public void refreshCounters() {