You've already forked Arcturus-Morningstar-Extended
mirror of
https://github.com/duckietm/Arcturus-Morningstar-Extended.git
synced 2026-06-21 07:46:18 +00:00
@@ -221,22 +221,8 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
|
||||
|
||||
this.bannedHabbos = new TIntObjectHashMap<>();
|
||||
|
||||
try (Connection connection = Emulator.getDatabase().getDataSource()
|
||||
.getConnection(); PreparedStatement statement = connection.prepareStatement(
|
||||
"SELECT * FROM room_promotions WHERE room_id = ? AND end_timestamp > ? LIMIT 1")) {
|
||||
if (this.promoted) {
|
||||
statement.setInt(1, this.id);
|
||||
statement.setInt(2, Emulator.getIntUnixTimestamp());
|
||||
|
||||
try (ResultSet promotionSet = statement.executeQuery()) {
|
||||
this.promoted = false;
|
||||
if (promotionSet.next()) {
|
||||
this.promoted = true;
|
||||
this.promotion = new RoomPromotion(this, promotionSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
|
||||
// Load bans eagerly (needed for entry check before loadData)
|
||||
this.loadBans(connection);
|
||||
} catch (SQLException e) {
|
||||
LOGGER.error("Caught SQL exception", e);
|
||||
@@ -489,7 +475,26 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
|
||||
LOGGER.error("Caught exception loading layout", e);
|
||||
}
|
||||
|
||||
// Phase 2: Load items and rights in parallel (independent operations)
|
||||
if (this.promoted) {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try (Connection promoConnection = Emulator.getDatabase().getDataSource().getConnection();
|
||||
PreparedStatement stmt = promoConnection.prepareStatement(
|
||||
"SELECT * FROM room_promotions WHERE room_id = ? AND end_timestamp > ? LIMIT 1")) {
|
||||
stmt.setInt(1, this.id);
|
||||
stmt.setInt(2, Emulator.getIntUnixTimestamp());
|
||||
try (ResultSet promoSet = stmt.executeQuery()) {
|
||||
this.promoted = false;
|
||||
if (promoSet.next()) {
|
||||
this.promoted = true;
|
||||
this.promotion = new RoomPromotion(this, promoSet);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Caught exception loading promotion", e);
|
||||
}
|
||||
}, Emulator.getThreading().getService());
|
||||
}
|
||||
|
||||
CompletableFuture<Void> itemsFuture = CompletableFuture.runAsync(() -> {
|
||||
try (Connection itemConnection = Emulator.getDatabase().getDataSource().getConnection()) {
|
||||
this.loadItems(itemConnection);
|
||||
@@ -514,21 +519,7 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
|
||||
}
|
||||
}, Emulator.getThreading().getService());
|
||||
|
||||
// Wait for items to be loaded before loading wired data (wired depends on items)
|
||||
try {
|
||||
itemsFuture.join();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error waiting for items to load", e);
|
||||
}
|
||||
|
||||
// Phase 3: Load heightmap after items are loaded (depends on items for stack heights)
|
||||
try {
|
||||
this.loadHeightmap();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Caught exception loading heightmap", e);
|
||||
}
|
||||
|
||||
// Phase 4: Load bots, pets, and wired data in parallel (all depend on layout + items)
|
||||
// Bots and pets only need layout for positioning - start them now
|
||||
CompletableFuture<Void> botsFuture = CompletableFuture.runAsync(() -> {
|
||||
try (Connection botsConnection = Emulator.getDatabase().getDataSource().getConnection()) {
|
||||
this.loadBots(botsConnection);
|
||||
@@ -545,6 +536,22 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
|
||||
}
|
||||
}, Emulator.getThreading().getService());
|
||||
|
||||
// Wait for items (needed for heightmap + wired)
|
||||
try {
|
||||
itemsFuture.join();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error waiting for items to load", e);
|
||||
}
|
||||
|
||||
// Phase 3: Heightmap and wired in parallel (both depend on items, not on each other)
|
||||
CompletableFuture<Void> heightmapFuture = CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
this.loadHeightmap();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Caught exception loading heightmap", e);
|
||||
}
|
||||
}, Emulator.getThreading().getService());
|
||||
|
||||
CompletableFuture<Void> wiredFuture = CompletableFuture.runAsync(() -> {
|
||||
try (Connection wiredConnection = Emulator.getDatabase().getDataSource().getConnection()) {
|
||||
this.loadWiredData(wiredConnection);
|
||||
@@ -553,9 +560,9 @@ public class Room implements Comparable<Room>, ISerialize, Runnable {
|
||||
}
|
||||
}, Emulator.getThreading().getService());
|
||||
|
||||
// Wait for all parallel operations to complete
|
||||
// Wait for all remaining operations
|
||||
try {
|
||||
CompletableFuture.allOf(rightsFuture, wordFilterFuture, botsFuture, petsFuture, wiredFuture).join();
|
||||
CompletableFuture.allOf(rightsFuture, wordFilterFuture, botsFuture, petsFuture, heightmapFuture, wiredFuture).join();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error waiting for parallel room data loading", e);
|
||||
}
|
||||
|
||||
@@ -54,6 +54,25 @@ public class RoomLayout {
|
||||
}
|
||||
}
|
||||
|
||||
public RoomLayout(RoomManager.RoomLayoutData data, Room room) {
|
||||
this.room = room;
|
||||
try {
|
||||
this.name = data.name;
|
||||
this.doorX = (short) data.doorX;
|
||||
this.doorY = (short) data.doorY;
|
||||
|
||||
this.doorDirection = data.doorDir;
|
||||
this.heightmap = data.heightmap;
|
||||
|
||||
this.parse();
|
||||
this.pathfinder = new PathfinderImpl(this.room, MAXIMUM_STEP_HEIGHT,
|
||||
Emulator.getConfig().getBoolean("pathfinder.step.allow.falling", true),
|
||||
Emulator.getConfig().getBoolean("pathfinder.retro-style.diagonals", false));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Caught exception", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean squareInSquare(Rectangle outerSquare, Rectangle innerSquare) {
|
||||
if (outerSquare.x > innerSquare.x) {
|
||||
return false;
|
||||
|
||||
@@ -72,6 +72,7 @@ public class RoomManager {
|
||||
public static boolean SHOW_PUBLIC_IN_POPULAR_TAB = false;
|
||||
private final THashMap<Integer, RoomCategory> roomCategories;
|
||||
private final List<String> mapNames;
|
||||
private final ConcurrentHashMap<String, RoomLayoutData> layoutCache;
|
||||
private final ConcurrentHashMap<Integer, Room> activeRooms;
|
||||
private final ConcurrentHashMap<Integer, Set<Integer>> roomsByOwner;
|
||||
private final ArrayList<Class<? extends Game>> gameTypes;
|
||||
@@ -80,6 +81,7 @@ public class RoomManager {
|
||||
long millis = System.currentTimeMillis();
|
||||
this.roomCategories = new THashMap<>();
|
||||
this.mapNames = new ArrayList<>();
|
||||
this.layoutCache = new ConcurrentHashMap<>();
|
||||
this.activeRooms = new ConcurrentHashMap<>();
|
||||
this.roomsByOwner = new ConcurrentHashMap<>();
|
||||
this.loadRoomCategories();
|
||||
@@ -114,9 +116,12 @@ public class RoomManager {
|
||||
|
||||
public void loadRoomModels() {
|
||||
this.mapNames.clear();
|
||||
this.layoutCache.clear();
|
||||
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM room_models")) {
|
||||
while (set.next()) {
|
||||
this.mapNames.add(set.getString("name"));
|
||||
String name = set.getString("name");
|
||||
this.mapNames.add(name);
|
||||
this.layoutCache.put(name, new RoomLayoutData(set));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.error("Caught SQL exception", e);
|
||||
@@ -446,6 +451,12 @@ public class RoomManager {
|
||||
}
|
||||
|
||||
public RoomLayout loadLayout(String name, Room room) {
|
||||
RoomLayoutData cached = this.layoutCache.get(name);
|
||||
if (cached != null) {
|
||||
return new RoomLayout(cached, room);
|
||||
}
|
||||
|
||||
// Fallback to DB if not in cache (should not happen for standard models)
|
||||
RoomLayout layout = null;
|
||||
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM room_models WHERE name = ? LIMIT 1")) {
|
||||
statement.setString(1, name);
|
||||
@@ -1623,4 +1634,24 @@ public class RoomManager {
|
||||
this.duration = duration;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cached layout data from room_models to avoid repeated DB queries.
|
||||
* The raw data is shared; each Room gets its own RoomLayout instance.
|
||||
*/
|
||||
static class RoomLayoutData {
|
||||
final String name;
|
||||
final int doorX;
|
||||
final int doorY;
|
||||
final int doorDir;
|
||||
final String heightmap;
|
||||
|
||||
RoomLayoutData(ResultSet set) throws SQLException {
|
||||
this.name = set.getString("name");
|
||||
this.doorX = set.getInt("door_x");
|
||||
this.doorY = set.getInt("door_y");
|
||||
this.doorDir = set.getInt("door_dir");
|
||||
this.heightmap = set.getString("heightmap");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -517,18 +517,44 @@ public class RoomTileManager {
|
||||
|
||||
/**
|
||||
* Loads the heightmap for the room.
|
||||
* Only updates tiles that have items on them (+ door tile) instead of all tiles,
|
||||
* using getTilesAt() to correctly handle rotated multi-tile furniture.
|
||||
*/
|
||||
public void loadHeightmap() {
|
||||
RoomLayout layout = this.room.getLayout();
|
||||
if (layout != null) {
|
||||
for (short x = 0; x < layout.getMapSizeX(); x++) {
|
||||
for (short y = 0; y < layout.getMapSizeY(); y++) {
|
||||
RoomTile tile = layout.getTile(x, y);
|
||||
if (tile != null) {
|
||||
THashSet<HabboItem> floorItems = this.room.getFloorItems();
|
||||
|
||||
if (floorItems.isEmpty()) {
|
||||
// No items - only update door tile
|
||||
RoomTile doorTile = layout.getDoorTile();
|
||||
if (doorTile != null) {
|
||||
this.updateTile(doorTile);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Collect unique tiles occupied by items (handles rotation)
|
||||
THashSet<RoomTile> tilesToUpdate = new THashSet<>();
|
||||
for (HabboItem item : floorItems) {
|
||||
RoomTile baseTile = layout.getTile(item.getX(), item.getY());
|
||||
if (baseTile != null) {
|
||||
tilesToUpdate.addAll(layout.getTilesAt(baseTile,
|
||||
item.getBaseItem().getWidth(),
|
||||
item.getBaseItem().getLength(),
|
||||
item.getRotation()));
|
||||
}
|
||||
}
|
||||
|
||||
// Always include door tile
|
||||
RoomTile doorTile = layout.getDoorTile();
|
||||
if (doorTile != null) {
|
||||
tilesToUpdate.add(doorTile);
|
||||
}
|
||||
|
||||
for (RoomTile tile : tilesToUpdate) {
|
||||
this.updateTile(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LOGGER.error("Unknown Room Layout for Room (ID: {})", this.room.getId());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user