Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/games/battlebanzai/BattleBanzaiGame.java

422 lines
16 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.games.battlebanzai;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.achievements.AchievementManager;
2019-03-18 02:22:00 +01:00
import com.eu.habbo.habbohotel.games.*;
2019-05-04 22:41:18 +02:00
import com.eu.habbo.habbohotel.items.interactions.games.InteractionGameTimer;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.items.interactions.games.battlebanzai.InteractionBattleBanzaiSphere;
import com.eu.habbo.habbohotel.items.interactions.games.battlebanzai.InteractionBattleBanzaiTile;
import com.eu.habbo.habbohotel.items.interactions.games.battlebanzai.gates.InteractionBattleBanzaiGate;
import com.eu.habbo.habbohotel.items.interactions.games.battlebanzai.scoreboards.InteractionBattleBanzaiScoreboard;
import com.eu.habbo.habbohotel.rooms.Room;
2018-11-17 14:28:00 +01:00
import com.eu.habbo.habbohotel.rooms.RoomTile;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.RoomUserAction;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboItem;
import com.eu.habbo.messages.outgoing.rooms.users.RoomUserActionComposer;
import com.eu.habbo.threading.runnables.BattleBanzaiTilesFlicker;
import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
2019-05-12 12:35:23 +02:00
import java.util.*;
2019-05-18 21:03:11 +02:00
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class BattleBanzaiGame extends Game {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(BattleBanzaiGame.class);
2018-07-08 23:32:00 +02:00
2019-06-10 18:06:22 +02:00
public static final int effectId = 32;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-05-12 20:54:39 +02:00
public static final int POINTS_HIJACK_TILE = Emulator.getConfig().getInt("hotel.banzai.points.tile.steal", 0);
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-05-12 20:54:39 +02:00
public static final int POINTS_FILL_TILE = Emulator.getConfig().getInt("hotel.banzai.points.tile.fill", 0);
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-05-12 20:54:39 +02:00
public static final int POINTS_LOCK_TILE = Emulator.getConfig().getInt("hotel.banzai.points.tile.lock", 1);
2018-07-06 15:30:00 +02:00
2019-05-18 21:03:11 +02:00
private static final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(Emulator.getConfig().getInt("hotel.banzai.fill.threads", 2));
2018-07-06 15:30:00 +02:00
private final THashMap<GameTeamColors, THashSet<HabboItem>> lockedTiles;
2019-03-18 02:22:00 +01:00
private final THashMap<Integer, HabboItem> gameTiles;
2019-05-26 20:14:53 +02:00
private int tileCount;
private int countDown;
private int countDown2;
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public BattleBanzaiGame(Room room) {
2018-07-06 15:30:00 +02:00
super(BattleBanzaiGameTeam.class, BattleBanzaiGamePlayer.class, room, true);
2018-09-28 21:25:00 +02:00
this.lockedTiles = new THashMap<>();
2019-03-18 02:22:00 +01:00
this.gameTiles = new THashMap<>();
2018-07-06 15:30:00 +02:00
room.setAllowEffects(true);
}
@Override
2019-05-26 20:14:53 +02:00
public void initialise() {
if (!this.state.equals(GameState.IDLE))
2018-07-06 15:30:00 +02:00
return;
/* The first countdown is activated for the first two seconds emitting only the blue light (second interaction),
the second, after another two seconds, completely activates the sphere (third interaction).
*/
2018-07-06 15:30:00 +02:00
this.countDown = 3;
this.countDown2 = 2;
2018-07-06 15:30:00 +02:00
this.resetMap();
2019-05-26 20:14:53 +02:00
synchronized (this.teams) {
for (GameTeam t : this.teams.values()) {
2018-07-06 15:30:00 +02:00
t.initialise();
}
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionBattleBanzaiSphere.class)) {
2018-07-06 15:30:00 +02:00
item.setExtradata("1");
this.room.updateItemState(item);
}
this.start();
}
@Override
2019-05-26 20:14:53 +02:00
public boolean addHabbo(Habbo habbo, GameTeamColors teamColor) {
2018-07-06 15:30:00 +02:00
return super.addHabbo(habbo, teamColor);
}
@Override
2019-05-26 20:14:53 +02:00
public void start() {
if (!this.state.equals(GameState.IDLE))
2018-07-06 15:30:00 +02:00
return;
super.start();
2018-11-17 14:28:00 +01:00
this.refreshGates();
2018-07-06 15:30:00 +02:00
Emulator.getThreading().run(this, 0);
}
@Override
2019-05-26 20:14:53 +02:00
public void run() {
try {
2019-03-18 02:22:00 +01:00
if (this.state.equals(GameState.IDLE))
2018-07-06 15:30:00 +02:00
return;
2019-05-26 20:14:53 +02:00
if (this.countDown > 0) {
2018-07-06 15:30:00 +02:00
this.countDown--;
2019-05-26 20:14:53 +02:00
if (this.countDown == 0) {
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionBattleBanzaiSphere.class)) {
item.setExtradata("1");
2018-07-06 15:30:00 +02:00
this.room.updateItemState(item);
if(this.countDown2 > 0) {
this.countDown2--;
if(this.countDown2 == 0) {
item.setExtradata("2");
this.room.updateItemState(item);
}
}
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
if (this.countDown > 1) {
2018-07-06 15:30:00 +02:00
Emulator.getThreading().run(this, 500);
return;
}
}
2019-05-04 22:41:18 +02:00
Emulator.getThreading().run(this, 1000);
2019-03-18 02:22:00 +01:00
2019-05-04 22:41:18 +02:00
if (this.state.equals(GameState.PAUSED)) return;
2018-07-06 15:30:00 +02:00
2019-05-04 22:41:18 +02:00
int total = 0;
2019-05-26 20:14:53 +02:00
synchronized (this.lockedTiles) {
for (Map.Entry<GameTeamColors, THashSet<HabboItem>> set : this.lockedTiles.entrySet()) {
2019-05-04 22:41:18 +02:00
total += set.getValue().size();
2018-07-06 15:30:00 +02:00
}
2019-05-04 22:41:18 +02:00
}
2018-07-06 15:30:00 +02:00
2019-05-04 22:41:18 +02:00
GameTeam highestScore = null;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
synchronized (this.teams) {
for (Map.Entry<GameTeamColors, GameTeam> set : this.teams.entrySet()) {
if (highestScore == null || highestScore.getTotalScore() < set.getValue().getTotalScore()) {
2019-05-04 22:41:18 +02:00
highestScore = set.getValue();
2018-07-06 15:30:00 +02:00
}
}
2019-05-04 22:41:18 +02:00
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (highestScore != null) {
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionBattleBanzaiSphere.class)) {
2019-06-10 18:06:22 +02:00
item.setExtradata((highestScore.teamColor.type + 2) + "");
2019-05-04 22:41:18 +02:00
this.room.updateItemState(item);
2018-07-06 15:30:00 +02:00
}
2019-05-04 22:41:18 +02:00
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (total >= this.tileCount && this.tileCount != 0) {
for (InteractionGameTimer timer : room.getRoomSpecialTypes().getGameTimers().values()) {
2019-06-03 22:49:17 +02:00
if (timer.isRunning()) {
timer.endGame(room);
}
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught exception", e);
2019-05-04 22:41:18 +02:00
}
}
2018-07-06 15:30:00 +02:00
2019-05-04 22:41:18 +02:00
@Override
public void onEnd() {
GameTeam winningTeam = null;
2018-07-06 15:30:00 +02:00
boolean singleTeamGame = this.teams.values().stream().filter(t -> t.getMembers().size() > 0).count() == 1;
2019-05-26 20:14:53 +02:00
for (GameTeam team : this.teams.values()) {
if (!singleTeamGame) {
for (GamePlayer player : team.getMembers()) {
if (player.getScoreAchievementValue() > 0) {
AchievementManager.progressAchievement(player.getHabbo(), Emulator.getGameEnvironment().getAchievementManager().getAchievement("BattleBallPlayer"));
}
2018-07-06 15:30:00 +02:00
}
2019-05-04 22:41:18 +02:00
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (winningTeam == null || team.getTotalScore() > winningTeam.getTotalScore()) {
2019-05-04 22:41:18 +02:00
winningTeam = team;
2018-07-06 15:30:00 +02:00
}
}
2019-05-04 22:41:18 +02:00
2019-05-26 20:14:53 +02:00
if (winningTeam != null) {
if (!singleTeamGame) {
for (GamePlayer player : winningTeam.getMembers()) {
if (player.getScoreAchievementValue() > 0) {
this.room.sendComposer(new RoomUserActionComposer(player.getHabbo().getRoomUnit(), RoomUserAction.WAVE).compose());
AchievementManager.progressAchievement(player.getHabbo(), Emulator.getGameEnvironment().getAchievementManager().getAchievement("BattleBallWinner"));
}
2019-05-04 22:41:18 +02:00
}
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionBattleBanzaiSphere.class)) {
2019-06-10 18:06:22 +02:00
item.setExtradata((6 + winningTeam.teamColor.type) + "");
2019-05-04 22:41:18 +02:00
this.room.updateItemState(item);
}
synchronized (this.lockedTiles) {
Emulator.getThreading().run(new BattleBanzaiTilesFlicker(this.lockedTiles.get(winningTeam.teamColor), winningTeam.teamColor, this.room));
}
2018-07-06 15:30:00 +02:00
}
2019-05-04 22:41:18 +02:00
super.onEnd();
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void stop() {
2018-07-06 15:30:00 +02:00
super.stop();
2018-11-17 14:28:00 +01:00
this.refreshGates();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (HabboItem tile : this.gameTiles.values()) {
if (tile.getExtradata().equals("1")) {
2019-03-18 02:22:00 +01:00
tile.setExtradata("0");
2019-05-04 22:41:18 +02:00
this.room.updateItem(tile);
2019-03-18 02:22:00 +01:00
}
}
synchronized (this.lockedTiles) {
this.lockedTiles.clear();
}
2018-07-06 15:30:00 +02:00
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
private synchronized void resetMap() {
this.tileCount = 0;
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.room.getFloorItems()) {
if (item instanceof InteractionBattleBanzaiTile) {
2018-07-06 15:30:00 +02:00
item.setExtradata("1");
this.room.updateItemState(item);
this.tileCount++;
2019-03-18 02:22:00 +01:00
this.gameTiles.put(item.getId(), item);
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
if (item instanceof InteractionBattleBanzaiScoreboard) {
2018-07-06 15:30:00 +02:00
item.setExtradata("0");
this.room.updateItemState(item);
}
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void tileLocked(GameTeamColors teamColor, HabboItem item, Habbo habbo) {
2019-05-12 12:35:23 +02:00
this.tileLocked(teamColor, item, habbo, false);
}
2019-05-26 20:14:53 +02:00
public void tileLocked(GameTeamColors teamColor, HabboItem item, Habbo habbo, boolean doNotCheckFill) {
synchronized (this.lockedTiles) {
if (item instanceof InteractionBattleBanzaiTile) {
if (!this.lockedTiles.containsKey(teamColor)) {
this.lockedTiles.put(teamColor, new THashSet<>());
}
2018-07-06 15:30:00 +02:00
this.lockedTiles.get(teamColor).add(item);
}
2018-07-06 15:30:00 +02:00
if (habbo != null) {
AchievementManager.progressAchievement(habbo, Emulator.getGameEnvironment().getAchievementManager().getAchievement("BattleBallTilesLocked"));
}
2019-05-12 12:35:23 +02:00
if (doNotCheckFill) return;
2019-05-12 12:35:23 +02:00
final int x = item.getX();
final int y = item.getY();
2019-05-12 12:35:23 +02:00
final List<List<RoomTile>> filledAreas = new ArrayList<>();
final THashSet<HabboItem> lockedTiles = new THashSet<>(this.lockedTiles.get(teamColor));
2019-05-12 12:35:23 +02:00
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));
2019-05-12 12:35:23 +02:00
Optional<List<RoomTile>> largestAreaOfAll = filledAreas.stream().filter(Objects::nonNull).max(Comparator.comparing(List::size));
2019-05-18 21:03:11 +02:00
if (largestAreaOfAll.isPresent()) {
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();
2019-05-12 12:35:23 +02:00
tileItem.ifPresent(habboItem -> {
this.tileLocked(teamColor, habboItem, habbo, true);
2019-05-12 12:35:23 +02:00
habboItem.setExtradata((2 + (teamColor.type * 3)) + "");
this.room.updateItem(habboItem);
});
}
2019-05-12 12:35:23 +02:00
this.refreshCounters(teamColor);
if (habbo != null) {
habbo.getHabboInfo().getGamePlayer().addScore(BattleBanzaiGame.POINTS_LOCK_TILE * largestAreaOfAll.get().size());
}
2019-05-18 21:03:11 +02:00
}
});
}
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
private List<RoomTile> floodFill(int x, int y, THashSet<HabboItem> lockedTiles, List<RoomTile> stack, GameTeamColors color) {
if (this.isOutOfBounds(x, y) || this.isForeignLockedTile(x, y, color)) return null;
2019-05-12 12:35:23 +02:00
2019-05-26 20:14:53 +02:00
RoomTile tile = this.room.getLayout().getTile((short) x, (short) y);
2019-05-12 12:35:23 +02:00
if (this.hasLockedTileAtCoordinates(x, y, lockedTiles) || stack.contains(tile)) return stack;
stack.add(tile);
List<List<RoomTile>> result = new ArrayList<>();
2019-05-18 20:25:07 +02:00
result.add(this.floodFill(x, y - 1, lockedTiles, stack, color));
result.add(this.floodFill(x, y + 1, lockedTiles, stack, color));
result.add(this.floodFill(x - 1, y, lockedTiles, stack, color));
result.add(this.floodFill(x + 1, y, lockedTiles, stack, color));
2019-05-12 12:35:23 +02:00
if (result.contains(null)) return null;
Optional<List<RoomTile>> biggestArea = result.stream().max(Comparator.comparing(List::size));
return biggestArea.orElse(null);
}
2019-05-26 20:14:53 +02:00
private boolean hasLockedTileAtCoordinates(int x, int y, THashSet<HabboItem> lockedTiles) {
for (HabboItem item : lockedTiles) {
2019-05-12 12:35:23 +02:00
if (item.getX() == x && item.getY() == y) return true;
}
return false;
}
2019-05-26 20:14:53 +02:00
private boolean isOutOfBounds(int x, int y) {
for (HabboItem item : this.gameTiles.values()) {
2019-05-12 12:35:23 +02:00
if (item.getX() == x && item.getY() == y) return false;
}
return true;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
private boolean isForeignLockedTile(int x, int y, GameTeamColors color) {
for (HashMap.Entry<GameTeamColors, THashSet<HabboItem>> lockedTilesForColor : this.lockedTiles.entrySet()) {
if (lockedTilesForColor.getKey() == color) continue;
for (HabboItem item : lockedTilesForColor.getValue()) {
if (item.getX() == x && item.getY() == y) return true;
}
}
return false;
}
2019-05-26 20:14:53 +02:00
public void refreshCounters() {
for (GameTeam team : this.teams.values()) {
if (team.getMembers().isEmpty())
2018-07-06 15:30:00 +02:00
continue;
this.refreshCounters(team.teamColor);
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void refreshCounters(GameTeamColors teamColors) {
if (!this.teams.containsKey(teamColors)) return;
2018-07-06 15:30:00 +02:00
int totalScore = this.teams.get(teamColors).getTotalScore();
THashMap<Integer, InteractionBattleBanzaiScoreboard> scoreBoards = this.room.getRoomSpecialTypes().getBattleBanzaiScoreboards(teamColors);
2019-05-26 20:14:53 +02:00
for (InteractionBattleBanzaiScoreboard scoreboard : scoreBoards.values()) {
if (scoreboard.getExtradata().isEmpty()) {
2018-07-06 15:30:00 +02:00
scoreboard.setExtradata("0");
}
int oldScore = Integer.valueOf(scoreboard.getExtradata());
2019-05-26 20:14:53 +02:00
if (oldScore == totalScore)
2018-07-06 15:30:00 +02:00
continue;
scoreboard.setExtradata(totalScore + "");
this.room.updateItemState(scoreboard);
}
}
2018-11-17 14:28:00 +01:00
2019-05-26 20:14:53 +02:00
private void refreshGates() {
2019-03-18 02:22:00 +01:00
Collection<InteractionBattleBanzaiGate> gates = this.room.getRoomSpecialTypes().getBattleBanzaiGates().values();
THashSet<RoomTile> tilesToUpdate = new THashSet<>(gates.size());
2019-05-26 20:14:53 +02:00
for (HabboItem item : gates) {
2018-11-17 14:28:00 +01:00
tilesToUpdate.add(this.room.getLayout().getTile(item.getX(), item.getY()));
}
this.room.updateTiles(tilesToUpdate);
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public void markTile(Habbo habbo, InteractionBattleBanzaiTile tile, int state) {
2019-03-18 02:22:00 +01:00
if (!this.gameTiles.contains(tile.getId())) return;
int check = state - (habbo.getHabboInfo().getGamePlayer().getTeamColor().type * 3);
2019-06-10 18:06:22 +02:00
if (check == 0 || check == 1) {
2019-03-18 02:22:00 +01:00
state++;
2019-05-26 20:14:53 +02:00
if (state % 3 == 2) {
2019-03-18 02:22:00 +01:00
habbo.getHabboInfo().getGamePlayer().addScore(BattleBanzaiGame.POINTS_LOCK_TILE);
this.tileLocked(habbo.getHabboInfo().getGamePlayer().getTeamColor(), tile, habbo);
2019-05-26 20:14:53 +02:00
} else {
2019-03-18 02:22:00 +01:00
habbo.getHabboInfo().getGamePlayer().addScore(BattleBanzaiGame.POINTS_FILL_TILE);
}
2019-05-26 20:14:53 +02:00
} else {
2019-06-10 18:06:22 +02:00
state = habbo.getHabboInfo().getGamePlayer().getTeamColor().type * 3;
2019-03-18 02:22:00 +01:00
habbo.getHabboInfo().getGamePlayer().addScore(BattleBanzaiGame.POINTS_HIJACK_TILE);
}
this.refreshCounters(habbo.getHabboInfo().getGamePlayer().getTeamColor());
tile.setExtradata(state + "");
this.room.updateItem(tile);
}
2018-07-06 15:30:00 +02:00
}