Make BB tile filling threaded

This commit is contained in:
Alejandro 2019-05-18 22:03:11 +03:00
parent 3b10d8abc3
commit 25058296c2

View File

@ -23,6 +23,8 @@ import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.THashSet;
import java.util.*; import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class BattleBanzaiGame extends Game 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); 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 tileCount;
private int countDown; private int countDown;
@ -292,38 +296,41 @@ public class BattleBanzaiGame extends Game
if (doNotCheckFill) return; if (doNotCheckFill) return;
int x = item.getX(); final int x = item.getX();
int y = item.getY(); final int y = item.getY();
List<List<RoomTile>> filledAreas = new ArrayList<>(); final List<List<RoomTile>> filledAreas = new ArrayList<>();
THashSet<HabboItem> lockedTiles = new THashSet<>(this.lockedTiles.get(teamColor)); final THashSet<HabboItem> 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));
Optional<List<RoomTile>> 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()) Optional<List<RoomTile>> largestAreaOfAll = filledAreas.stream().filter(Objects::nonNull).max(Comparator.comparing(List::size));
{
for (RoomTile tile: largestAreaOfAll.get()) if (largestAreaOfAll.isPresent())
{ {
Optional<HabboItem> 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<HabboItem> tileItem = this.gameTiles.values().stream().filter(i -> i.getX() == tile.x && i.getY() == tile.y && i instanceof InteractionBattleBanzaiTile).findAny();
tileItem.ifPresent(habboItem -> { tileItem.ifPresent(habboItem -> {
this.tileLocked(teamColor, habboItem, habbo, true); this.tileLocked(teamColor, habboItem, habbo, true);
habboItem.setExtradata((2 + (teamColor.type * 3) + 3) + ""); habboItem.setExtradata((2 + (teamColor.type * 3) + 3) + "");
this.room.updateItem(habboItem); 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<RoomTile> floodFill(int x, int y, THashSet<HabboItem> lockedTiles, List<RoomTile> stack, GameTeamColors color) private List<RoomTile> floodFill(int x, int y, THashSet<HabboItem> lockedTiles, List<RoomTile> stack, GameTeamColors color)