From 196c1ef23d073382a19a245f2e933cca76dcba21 Mon Sep 17 00:00:00 2001 From: Mike <76-Mike@users.noreply.git.krews.org> Date: Mon, 4 May 2020 05:41:28 +0200 Subject: [PATCH 1/3] Optimize water mask recalculations. --- .../items/interactions/InteractionWater.java | 282 +++++++++++------- .../com/eu/habbo/habbohotel/rooms/Room.java | 1 - .../eu/habbo/habbohotel/users/HabboItem.java | 18 ++ 3 files changed, 186 insertions(+), 115 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java index 9af96347..b85bd494 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java @@ -1,43 +1,43 @@ package com.eu.habbo.habbohotel.items.interactions; -import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.bots.Bot; import com.eu.habbo.habbohotel.items.Item; import com.eu.habbo.habbohotel.pets.Pet; -import com.eu.habbo.habbohotel.rooms.Room; -import com.eu.habbo.habbohotel.rooms.RoomTile; -import com.eu.habbo.habbohotel.rooms.RoomUnit; -import com.eu.habbo.habbohotel.rooms.RoomUnitStatus; +import com.eu.habbo.habbohotel.rooms.*; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboItem; -import gnu.trove.list.array.TIntArrayList; -import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; import org.apache.commons.math3.util.Pair; +import java.awt.*; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; public class InteractionWater extends InteractionDefault { + + private static final String DEEP_WATER_NAME = "bw_water_2"; + private final boolean isDeepWater; + public InteractionWater(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); + this.isDeepWater = baseItem.getName().equalsIgnoreCase(DEEP_WATER_NAME); } public InteractionWater(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) { super(id, userId, item, extradata, limitedStack, limitedSells); + this.isDeepWater = false; } @Override public void onMove(Room room, RoomTile oldLocation, RoomTile newLocation) { super.onMove(room, oldLocation, newLocation); - - this.recalculate(room); + this.updateWaters(room, oldLocation); } @Override public void onPickUp(Room room) { - this.recalculate(room); + this.updateWaters(room, null); Object[] empty = new Object[]{}; for (Habbo habbo : room.getHabbosOnItem(this)) { @@ -58,91 +58,10 @@ public class InteractionWater extends InteractionDefault { @Override public void onPlace(Room room) { - this.recalculate(room); + this.updateWaters(room, null); super.onPlace(room); } - public void refreshWaters(Room room) { - if (room == null) { - room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()); - } - - int _1 = 0; - int _2 = 0; - int _3 = 0; - int _4 = 0; - int _5 = 0; - int _6 = 0; - int _7 = 0; - int _8 = 0; - int _9 = 0; - int _10 = 0; - int _11 = 0; - int _12 = 0; - - for (HabboItem item : room.getRoomSpecialTypes().getItemsOfType(InteractionWaterItem.class)) { - ((InteractionWaterItem) item).update(); - } - - if (!this.getBaseItem().getName().equalsIgnoreCase("bw_water_2")) { - if (room.waterTiles.containsKey(this.getX() - 1) && room.waterTiles.get(this.getX() - 1).contains(this.getY() - 1)) - _1 = 1; - if (room.waterTiles.containsKey(this.getX()) && room.waterTiles.get(this.getX()).contains(this.getY() - 1)) - _2 = 1; - if (room.waterTiles.containsKey(this.getX() + 1) && room.waterTiles.get(this.getX() + 1).contains(this.getY() - 1)) - _3 = 1; - if (room.waterTiles.containsKey(this.getX() + 2) && room.waterTiles.get(this.getX() + 2).contains(this.getY() - 1)) - _4 = 1; - if (room.waterTiles.containsKey(this.getX() - 1) && room.waterTiles.get(this.getX() - 1).contains(this.getY())) - _5 = 1; - if (room.waterTiles.containsKey(this.getX() + 2) && room.waterTiles.get(this.getX() + 2).contains(this.getY())) - _6 = 1; - if (room.waterTiles.containsKey(this.getX() - 1) && room.waterTiles.get(this.getX() - 1).contains(this.getY() + 1)) - _7 = 1; - if (room.waterTiles.containsKey(this.getX() + 2) && room.waterTiles.get(this.getX() + 2).contains(this.getY() + 1)) - _8 = 1; - if (room.waterTiles.containsKey(this.getX() - 1) && room.waterTiles.get(this.getX() - 1).contains(this.getY() + 2)) - _9 = 1; - if (room.waterTiles.containsKey(this.getX()) && room.waterTiles.get(this.getX()).contains(this.getY() + 2)) - _10 = 1; - if (room.waterTiles.containsKey(this.getX() + 1) && room.waterTiles.get(this.getX() + 1).contains(this.getY() + 2)) - _11 = 1; - if (room.waterTiles.containsKey(this.getX() + 2) && room.waterTiles.get(this.getX() + 2).contains(this.getY() + 2)) - _12 = 1; - } - - //if (_1 == 0 && room.getLayout().isVoidTile((short)(this.getX() -1), (short) (this.getY() -1))) _1 = 1; - if (_2 == 0 && room.getLayout().isVoidTile(this.getX(), (short) (this.getY() - 1))) _2 = 1; - if (_3 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 1), (short) (this.getY() - 1))) _3 = 1; - //if (_4 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), (short) (this.getY() - 1))) _4 = 1; - if (_5 == 0 && room.getLayout().isVoidTile((short) (this.getX() - 1), this.getY())) _5 = 1; - if (_6 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), this.getY())) _6 = 1; - if (_7 == 0 && room.getLayout().isVoidTile((short) (this.getX() - 1), (short) (this.getY() + 1))) _7 = 1; - if (_8 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), (short) (this.getY() + 1))) _8 = 1; - //if (_9 == 0 && room.getLayout().isVoidTile((short)(this.getX() -1), (short) (this.getY() + 2))) _9 = 1; - if (_10 == 0 && room.getLayout().isVoidTile(this.getX(), (short) (this.getY() + 2))) _10 = 1; - if (_11 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 1), (short) (this.getY() + 2))) _11 = 1; - //if (_12 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), (short) (this.getY() + 2))) _12 = 1; - - int result = 0; - result |= _1 << 11; - result |= _2 << 10; - result |= _3 << 9; - result |= _4 << 8; - result |= _5 << 7; - result |= _6 << 6; - result |= _7 << 5; - result |= _8 << 4; - result |= _9 << 3; - result |= _10 << 2; - result |= _11 << 1; - result |= _12; - - this.setExtradata(result + ""); - this.needsUpdate(true); - room.updateItem(this); - } - @Override public void onWalkOn(RoomUnit roomUnit, Room room, Object[] objects) throws Exception { super.onWalkOn(roomUnit, room, objects); @@ -169,28 +88,6 @@ public class InteractionWater extends InteractionDefault { pet.getRoomUnit().removeStatus(RoomUnitStatus.SWIM); } - private void recalculate(Room room) { - THashMap tiles = new THashMap<>(); - - for (HabboItem item : room.getRoomSpecialTypes().getItemsOfType(InteractionWater.class)) { - for (short i = 0; i < item.getBaseItem().getLength(); i++) { - for (short j = 0; j < item.getBaseItem().getWidth(); j++) { - if (!tiles.containsKey((short) (item.getX() + i))) { - tiles.put((short) (item.getX() + i), new TIntArrayList()); - } - - tiles.get((short) (item.getX() + i)).add(item.getY() + j); - } - } - } - - room.waterTiles = tiles; - - for (HabboItem item : room.getRoomSpecialTypes().getItemsOfType(InteractionWater.class)) { - ((InteractionWater) item).refreshWaters(room); - } - } - @Override public boolean allowWiredResetState() { return false; @@ -222,4 +119,161 @@ public class InteractionWater extends InteractionDefault { return pet == null || pet.getPetData().canSwim; } + + private void updateWaters(Room room, RoomTile oldLocation) { + // Update ourself. + this.updateWater(room); + + // Find targets containing furni to update. + Rectangle target = this.getRectangle(1, 1); + Rectangle targetOld = null; + + if (oldLocation != null) { + targetOld = RoomLayout.getRectangle( + oldLocation.x - 1, + oldLocation.y - 1, + this.getBaseItem().getWidth() + 2, + this.getBaseItem().getLength() + 2, + this.getRotation()); + } + + // Update neighbouring water. + for (HabboItem item : room.getRoomSpecialTypes().getItemsOfType(InteractionWater.class)) { + // We already updated ourself. + if (item == this) { + continue; + } + + // Check if found water furni is touching or intersecting our water furni. + // Check the same for the old location + Rectangle itemRectangle = item.getRectangle(); + + if (target.intersects(itemRectangle) || (targetOld != null && targetOld.intersects(itemRectangle))) { + ((InteractionWater) item).updateWater(room); + } + } + + // Update water items we might have missed in the old location. + if (targetOld != null) { + for (HabboItem item : room.getRoomSpecialTypes().getItemsOfType(InteractionWaterItem.class)) { + if (targetOld.intersects(item.getRectangle())) { + ((InteractionWaterItem) item).update(); + } + } + } + } + + private void updateWater(Room room) { + Rectangle target = this.getRectangle(); + + // Only update water item furnis that are intersecting with us. + for (HabboItem item : room.getRoomSpecialTypes().getItemsOfType(InteractionWaterItem.class)) { + if (target.intersects(item.getRectangle())) { + ((InteractionWaterItem) item).update(); + } + } + + // Prepare bits for cutting off water. + byte _1 = 0; + byte _2 = 0; + byte _3 = 0; + byte _4 = 0; + byte _5 = 0; + byte _6 = 0; + byte _7 = 0; + byte _8 = 0; + byte _9 = 0; + byte _10 = 0; + byte _11 = 0; + byte _12 = 0; + + // Check if we are touching a water tile. + if (this.isValidForMask(room, this.getX() - 1, this.getY() - 1, this.getZ())) { + _1 = 1; + } + if (this.isValidForMask(room, this.getX(), this.getY() - 1, this.getZ())) { + _2 = 1; + } + if (this.isValidForMask(room, this.getX() + 1, this.getY() - 1, this.getZ())) { + _3 = 1; + } + if (this.isValidForMask(room, this.getX() + 2, this.getY() - 1, this.getZ())) { + _4 = 1; + } + if (this.isValidForMask(room, this.getX() - 1, this.getY(), this.getZ())) { + _5 = 1; + } + if (this.isValidForMask(room, this.getX() + 2, this.getY(), this.getZ())) { + _6 = 1; + } + if (this.isValidForMask(room, this.getX() - 1, this.getY() + 1, this.getZ())) { + _7 = 1; + } + if (this.isValidForMask(room, this.getX() + 2, this.getY() + 1, this.getZ())) { + _8 = 1; + } + if (this.isValidForMask(room, this.getX() - 1, this.getY() + 2, this.getZ())) { + _9 = 1; + } + if (this.isValidForMask(room, this.getX(), this.getY() + 2, this.getZ())) { + _10 = 1; + } + if (this.isValidForMask(room, this.getX() + 1, this.getY() + 2, this.getZ())) { + _11 = 1; + } + if (this.isValidForMask(room, this.getX() + 2, this.getY() + 2, this.getZ())) { + _12 = 1; + } + + // Check if we are touching invalid tiles. + // if (_1 == 0 && room.getLayout().isVoidTile((short)(this.getX() -1), (short) (this.getY() -1))) _1 = 1; + if (_2 == 0 && room.getLayout().isVoidTile(this.getX(), (short) (this.getY() - 1))) _2 = 1; + if (_3 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 1), (short) (this.getY() - 1))) _3 = 1; + // if (_4 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), (short) (this.getY() - 1))) _4 = 1; + if (_5 == 0 && room.getLayout().isVoidTile((short) (this.getX() - 1), this.getY())) _5 = 1; + if (_6 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), this.getY())) _6 = 1; + if (_7 == 0 && room.getLayout().isVoidTile((short) (this.getX() - 1), (short) (this.getY() + 1))) _7 = 1; + if (_8 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), (short) (this.getY() + 1))) _8 = 1; + // if (_9 == 0 && room.getLayout().isVoidTile((short)(this.getX() -1), (short) (this.getY() + 2))) _9 = 1; + if (_10 == 0 && room.getLayout().isVoidTile(this.getX(), (short) (this.getY() + 2))) _10 = 1; + if (_11 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 1), (short) (this.getY() + 2))) _11 = 1; + // if (_12 == 0 && room.getLayout().isVoidTile((short) (this.getX() + 2), (short) (this.getY() + 2))) _12 = 1; + + // Update water. + int result = (_1 << 11) + | (_2 << 10) + | (_3 << 9) + | (_4 << 8) + | (_5 << 7) + | (_6 << 6) + | (_7 << 5) + | (_8 << 4) + | (_9 << 3) + | (_10 << 2) + | (_11 << 1) + | _12; + + String updatedData = String.valueOf(result); + + if (!this.getExtradata().equals(updatedData)) { + this.setExtradata(updatedData); + this.needsUpdate(true); + room.updateItem(this); + } + } + + private boolean isValidForMask(Room room, int x, int y, double z) { + for (HabboItem item : room.getItemsAt(x, y, z)) { + if (item instanceof InteractionWater) { + // Only allow masking if both are deepwater or both not. + // This allows deepwater and normal water to look nice. + if (((InteractionWater) item).isDeepWater == this.isDeepWater) { + return true; + } + } + } + + return false; + } + } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index c8de883e..2c834c3c 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -141,7 +141,6 @@ public class Room implements Comparable, ISerialize, Runnable { //Use appropriately. Could potentially cause memory leaks when used incorrectly. public volatile boolean preventUnloading = false; public volatile boolean preventUncaching = false; - public THashMap waterTiles; public ConcurrentSet scheduledComposers = new ConcurrentSet<>(); public ConcurrentSet scheduledTasks = new ConcurrentSet<>(); public String wordQuiz = ""; diff --git a/src/main/java/com/eu/habbo/habbohotel/users/HabboItem.java b/src/main/java/com/eu/habbo/habbohotel/users/HabboItem.java index f689c504..6a37c611 100644 --- a/src/main/java/com/eu/habbo/habbohotel/users/HabboItem.java +++ b/src/main/java/com/eu/habbo/habbohotel/users/HabboItem.java @@ -512,4 +512,22 @@ public abstract class HabboItem implements Runnable, IEventTriggers { public boolean canOverrideTile(RoomUnit unit, Room room, RoomTile tile) { return false; } + + public Rectangle getRectangle() { + return RoomLayout.getRectangle( + this.getX(), + this.getY(), + this.getBaseItem().getWidth(), + this.getBaseItem().getLength(), + this.getRotation()); + } + + public Rectangle getRectangle(int marginX, int marginY) { + return RoomLayout.getRectangle( + this.getX() - marginX, + this.getY() - marginY, + this.getBaseItem().getWidth() + (marginX * 2), + this.getBaseItem().getLength() + (marginY * 2), + this.getRotation()); + } } From 87fdba280a815b3688d8b6b59a50e1f7baa80629 Mon Sep 17 00:00:00 2001 From: Mike <76-Mike@users.noreply.git.krews.org> Date: Mon, 4 May 2020 05:53:28 +0200 Subject: [PATCH 2/3] Optimize water item update. --- .../interactions/InteractionWaterItem.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWaterItem.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWaterItem.java index a9840636..0332c717 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWaterItem.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWaterItem.java @@ -42,12 +42,15 @@ public class InteractionWaterItem extends InteractionDefault { public void update() { Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()); - if (room == null) + if (room == null) { return; + } - Rectangle rectangle = RoomLayout.getRectangle(this.getX(), this.getY(), this.getBaseItem().getWidth(), this.getBaseItem().getLength(), this.getRotation()); + Rectangle rectangle = this.getRectangle(); + // Check if every tile of the furni is in water. boolean foundWater = true; + for (short x = (short) rectangle.x; x < rectangle.getWidth() + rectangle.x && foundWater; x++) { for (short y = (short) rectangle.y; y < rectangle.getHeight() + rectangle.y && foundWater; y++) { boolean tile = false; @@ -66,16 +69,14 @@ public class InteractionWaterItem extends InteractionDefault { } } - if (foundWater) { - this.setExtradata("1"); + // Update data if changed. + String updatedData = foundWater ? "1" : "0"; + + if (!this.getExtradata().equals(updatedData)) { + this.setExtradata(updatedData); this.needsUpdate(true); room.updateItem(this); - return; } - - this.setExtradata("0"); - this.needsUpdate(true); - room.updateItem(this); } @Override From 9a9b986948e71799f9c61147d9893ad5b08183e0 Mon Sep 17 00:00:00 2001 From: Mike <76-Mike@users.noreply.git.krews.org> Date: Mon, 4 May 2020 06:18:12 +0200 Subject: [PATCH 3/3] Improve corner clipping and remove picked up furni from mask calculation --- .../items/interactions/InteractionWater.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java index b85bd494..d697ee4a 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWater.java @@ -17,16 +17,20 @@ import java.util.List; public class InteractionWater extends InteractionDefault { private static final String DEEP_WATER_NAME = "bw_water_2"; + private final boolean isDeepWater; + private boolean isInRoom; public InteractionWater(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); this.isDeepWater = baseItem.getName().equalsIgnoreCase(DEEP_WATER_NAME); + this.isInRoom = this.getRoomId() != 0; } public InteractionWater(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) { super(id, userId, item, extradata, limitedStack, limitedSells); this.isDeepWater = false; + this.isInRoom = this.getRoomId() != 0; } @Override @@ -37,6 +41,7 @@ public class InteractionWater extends InteractionDefault { @Override public void onPickUp(Room room) { + this.isInRoom = false; this.updateWaters(room, null); Object[] empty = new Object[]{}; @@ -58,6 +63,7 @@ public class InteractionWater extends InteractionDefault { @Override public void onPlace(Room room) { + this.isInRoom = true; this.updateWaters(room, null); super.onPlace(room); } @@ -188,7 +194,7 @@ public class InteractionWater extends InteractionDefault { byte _12 = 0; // Check if we are touching a water tile. - if (this.isValidForMask(room, this.getX() - 1, this.getY() - 1, this.getZ())) { + if (this.isValidForMask(room, this.getX() - 1, this.getY() - 1, this.getZ(), true)) { _1 = 1; } if (this.isValidForMask(room, this.getX(), this.getY() - 1, this.getZ())) { @@ -197,7 +203,7 @@ public class InteractionWater extends InteractionDefault { if (this.isValidForMask(room, this.getX() + 1, this.getY() - 1, this.getZ())) { _3 = 1; } - if (this.isValidForMask(room, this.getX() + 2, this.getY() - 1, this.getZ())) { + if (this.isValidForMask(room, this.getX() + 2, this.getY() - 1, this.getZ(), true)) { _4 = 1; } if (this.isValidForMask(room, this.getX() - 1, this.getY(), this.getZ())) { @@ -212,7 +218,7 @@ public class InteractionWater extends InteractionDefault { if (this.isValidForMask(room, this.getX() + 2, this.getY() + 1, this.getZ())) { _8 = 1; } - if (this.isValidForMask(room, this.getX() - 1, this.getY() + 2, this.getZ())) { + if (this.isValidForMask(room, this.getX() - 1, this.getY() + 2, this.getZ(), true)) { _9 = 1; } if (this.isValidForMask(room, this.getX(), this.getY() + 2, this.getZ())) { @@ -221,7 +227,7 @@ public class InteractionWater extends InteractionDefault { if (this.isValidForMask(room, this.getX() + 1, this.getY() + 2, this.getZ())) { _11 = 1; } - if (this.isValidForMask(room, this.getX() + 2, this.getY() + 2, this.getZ())) { + if (this.isValidForMask(room, this.getX() + 2, this.getY() + 2, this.getZ(), true)) { _12 = 1; } @@ -263,11 +269,24 @@ public class InteractionWater extends InteractionDefault { } private boolean isValidForMask(Room room, int x, int y, double z) { + return this.isValidForMask(room, x, y, z, false); + } + + private boolean isValidForMask(Room room, int x, int y, double z, boolean corner) { for (HabboItem item : room.getItemsAt(x, y, z)) { if (item instanceof InteractionWater) { - // Only allow masking if both are deepwater or both not. + InteractionWater water = (InteractionWater) item; + + // Take out picked up water from the recalculation. + if (!water.isInRoom) { + continue; + } + + // Allow: + // - masking if both are deepwater or both not. + // - corners too because otherwise causes ugly clipping issues. // This allows deepwater and normal water to look nice. - if (((InteractionWater) item).isDeepWater == this.isDeepWater) { + if (corner && !this.isDeepWater || water.isDeepWater == this.isDeepWater) { return true; } }