From 908b5605b3821cd2fbf757a0ccebd2fdcdb3cb44 Mon Sep 17 00:00:00 2001 From: Yordi Date: Thu, 17 Oct 2024 16:03:56 +0000 Subject: [PATCH] Improvement/bot limit walking distance --- sqlupdates/Update 3_5_3 to 3_5_4.sql | 3 + .../com/eu/habbo/habbohotel/bots/Bot.java | 68 +++++++++++++++---- .../eu/habbo/habbohotel/bots/BotManager.java | 2 +- .../com/eu/habbo/habbohotel/rooms/Room.java | 31 +++++++++ .../com/eu/habbo/plugin/PluginManager.java | 3 +- 5 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 sqlupdates/Update 3_5_3 to 3_5_4.sql diff --git a/sqlupdates/Update 3_5_3 to 3_5_4.sql b/sqlupdates/Update 3_5_3 to 3_5_4.sql new file mode 100644 index 00000000..e6b0a748 --- /dev/null +++ b/sqlupdates/Update 3_5_3 to 3_5_4.sql @@ -0,0 +1,3 @@ +--New bot walking settings +INSERT INTO `emulator_settings` (`key`, `value`) VALUES ('hotel.bot.limit.walking.distance', '1'); +INSERT INTO `emulator_settings` (`key`, `value`) VALUES ('hotel.bot.limit.walking.distance.radius', '5'); \ No newline at end of file diff --git a/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java b/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java index 995a7af1..13914fc4 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java @@ -27,6 +27,8 @@ public class Bot implements Runnable { public static final String NO_CHAT_SET = "${bot.skill.chatter.configuration.text.placeholder}"; public static String[] PLACEMENT_MESSAGES = "Yo!;Hello I'm a real party animal!;Hello!".split(";"); + public static boolean BOT_LIMIT_WALKING_DISTANCE = true; + public static int BOT_WALKING_DISTANCE_RADIUS = 5; private final ArrayList chatLines; private transient int id; @@ -137,30 +139,26 @@ public class Bot implements Runnable { @Override public void run() { if (this.needsUpdate) { - try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE bots SET name = ?, motto = ?, figure = ?, gender = ?, user_id = ?, room_id = ?, x = ?, y = ?, z = ?, rot = ?, dance = ?, freeroam = ?, chat_lines = ?, chat_auto = ?, chat_random = ?, chat_delay = ?, effect = ?, bubble_id = ? WHERE id = ?")) { + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE bots SET name = ?, motto = ?, figure = ?, gender = ?, user_id = ?, room_id = ?, dance = ?, freeroam = ?, chat_lines = ?, chat_auto = ?, chat_random = ?, chat_delay = ?, effect = ?, bubble_id = ? WHERE id = ?")) { statement.setString(1, this.name); statement.setString(2, this.motto); statement.setString(3, this.figure); statement.setString(4, this.gender.toString()); statement.setInt(5, this.ownerId); statement.setInt(6, this.room == null ? 0 : this.room.getId()); - statement.setInt(7, this.roomUnit == null ? 0 : this.roomUnit.getX()); - statement.setInt(8, this.roomUnit == null ? 0 : this.roomUnit.getY()); - statement.setDouble(9, this.roomUnit == null ? 0 : this.roomUnit.getZ()); - statement.setInt(10, this.roomUnit == null ? 0 : this.roomUnit.getBodyRotation().getValue()); - statement.setInt(11, this.roomUnit == null ? 0 : this.roomUnit.getDanceType().getType()); - statement.setString(12, this.canWalk ? "1" : "0"); + statement.setInt(7, this.roomUnit == null ? 0 : this.roomUnit.getDanceType().getType()); + statement.setString(8, this.canWalk ? "1" : "0"); StringBuilder text = new StringBuilder(); for (String s : this.chatLines) { text.append(s).append("\r"); } - statement.setString(13, text.toString()); - statement.setString(14, this.chatAuto ? "1" : "0"); - statement.setString(15, this.chatRandom ? "1" : "0"); - statement.setInt(16, this.chatDelay); - statement.setInt(17, this.effect); - statement.setInt(18, this.bubble); - statement.setInt(19, this.id); + statement.setString(9, text.toString()); + statement.setString(10, this.chatAuto ? "1" : "0"); + statement.setString(11, this.chatRandom ? "1" : "0"); + statement.setInt(12, this.chatDelay); + statement.setInt(13, this.effect); + statement.setInt(14, this.bubble); + statement.setInt(15, this.id); statement.execute(); this.needsUpdate = false; } catch (SQLException e) { @@ -174,7 +172,15 @@ public class Bot implements Runnable { if (allowBotsWalk && this.canWalk) { if (!this.roomUnit.isWalking()) { if (this.roomUnit.getWalkTimeOut() < Emulator.getIntUnixTimestamp() && this.followingHabboId == 0) { - this.roomUnit.setGoalLocation(this.room.getRandomWalkableTile()); + this.roomUnit.setGoalLocation( + Bot.BOT_LIMIT_WALKING_DISTANCE + ? this.room.getRandomWalkableTilesAround( + this.getRoomUnit(), + this.room.getLayout().getTile(this.roomUnit.getX(), this.roomUnit.getY()), + Bot.BOT_WALKING_DISTANCE_RADIUS) + : this.room.getRandomWalkableTile() + ); + int timeOut = Emulator.getRandom().nextInt(20) * 2; this.roomUnit.setWalkTimeOut((timeOut < 10 ? 5 : timeOut) + Emulator.getIntUnixTimestamp()); } @@ -497,4 +503,36 @@ public class Bot implements Runnable { this.roomUnit.statusUpdate(true); } + public void onPlaceUpdate() { + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE bots SET name = ?, motto = ?, figure = ?, gender = ?, user_id = ?, room_id = ?, x = ?, y = ?, z = ?, rot = ?, dance = ?, freeroam = ?, chat_lines = ?, chat_auto = ?, chat_random = ?, chat_delay = ?, effect = ?, bubble_id = ? WHERE id = ?")) { + statement.setString(1, this.name); + statement.setString(2, this.motto); + statement.setString(3, this.figure); + statement.setString(4, this.gender.toString()); + statement.setInt(5, this.ownerId); + statement.setInt(6, this.room == null ? 0 : this.room.getId()); + statement.setInt(7, this.roomUnit == null ? 0 : this.roomUnit.getX()); + statement.setInt(8, this.roomUnit == null ? 0 : this.roomUnit.getY()); + statement.setDouble(9, this.roomUnit == null ? 0 : this.roomUnit.getZ()); + statement.setInt(10, this.roomUnit == null ? 0 : this.roomUnit.getBodyRotation().getValue()); + statement.setInt(11, this.roomUnit == null ? 0 : this.roomUnit.getDanceType().getType()); + statement.setString(12, this.canWalk ? "1" : "0"); + StringBuilder text = new StringBuilder(); + for (String s : this.chatLines) { + text.append(s).append("\r"); + } + statement.setString(13, text.toString()); + statement.setString(14, this.chatAuto ? "1" : "0"); + statement.setString(15, this.chatRandom ? "1" : "0"); + statement.setInt(16, this.chatDelay); + statement.setInt(17, this.effect); + statement.setInt(18, this.bubble); + statement.setInt(19, this.id); + statement.execute(); + } catch (SQLException e) { + LOGGER.error("Caught SQL exception", e); + } + } + + } diff --git a/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java b/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java index 2eebcc6f..a21f8ae6 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java @@ -136,7 +136,7 @@ public class BotManager { roomUnit.setCanWalk(room.isAllowBotsWalk()); bot.setRoomUnit(roomUnit); bot.setRoom(room); - bot.needsUpdate(true); + bot.onPlaceUpdate(); room.addBot(bot); Emulator.getThreading().run(bot); room.sendComposer(new RoomUsersComposer(bot).compose()); 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 03a2f242..f60c75e8 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -3849,6 +3849,37 @@ public class Room implements Comparable, ISerialize, Runnable { return null; } + public RoomTile getRandomWalkableTilesAround(RoomUnit roomUnit, RoomTile tile, int radius) { + if (!layout.tileExists(tile.x, tile.y)) { + tile = layout.getTile(roomUnit.getX(), roomUnit.getY()); + this.getBot(roomUnit).needsUpdate(true); + } + + List walkableTiles = new ArrayList<>(); + + int minX = Math.max(0, tile.x - radius); + int minY = Math.max(0, tile.y - radius); + int maxX = Math.min(this.getLayout().getMapSizeX() - 1, tile.x + radius); + int maxY = Math.min(this.getLayout().getMapSizeY() - 1, tile.y + radius); + + for (int x = minX; x <= maxX; x++) { + for (int y = minY; y <= maxY; y++) { + RoomTile candidateTile = this.getLayout().getTile((short) x, (short) y); + + if (candidateTile != null && candidateTile.getState() != RoomTileState.BLOCKED && candidateTile.getState() != RoomTileState.INVALID) { + walkableTiles.add(candidateTile); + } + } + } + + if (walkableTiles.isEmpty()) { + return tile; + } + + Collections.shuffle(walkableTiles); + return walkableTiles.get(0); + } + public Habbo getHabbo(String username) { for (Habbo habbo : this.getHabbos()) { if (habbo.getHabboInfo().getUsername().equalsIgnoreCase(username)) diff --git a/src/main/java/com/eu/habbo/plugin/PluginManager.java b/src/main/java/com/eu/habbo/plugin/PluginManager.java index a7d60270..9d81a0a8 100644 --- a/src/main/java/com/eu/habbo/plugin/PluginManager.java +++ b/src/main/java/com/eu/habbo/plugin/PluginManager.java @@ -25,7 +25,6 @@ import com.eu.habbo.habbohotel.users.clothingvalidation.ClothingValidationManage import com.eu.habbo.habbohotel.users.HabboInventory; import com.eu.habbo.habbohotel.users.HabboManager; import com.eu.habbo.habbohotel.users.subscriptions.SubscriptionHabboClub; -import com.eu.habbo.habbohotel.users.subscriptions.SubscriptionManager; import com.eu.habbo.habbohotel.wired.WiredHandler; import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreManager; import com.eu.habbo.messages.PacketManager; @@ -100,6 +99,8 @@ public class PluginManager { BotManager.MAXIMUM_NAME_LENGTH = Emulator.getConfig().getInt("hotel.bot.max.namelength"); BotManager.MAXIMUM_CHAT_SPEED = Emulator.getConfig().getInt("hotel.bot.max.chatdelay"); Bot.PLACEMENT_MESSAGES = Emulator.getConfig().getValue("hotel.bot.placement.messages", "Yo!;Hello I'm a real party animal!;Hello!").split(";"); + Bot.BOT_LIMIT_WALKING_DISTANCE = Emulator.getConfig().getBoolean("hotel.bot.limit.walking.distance", true); + Bot.BOT_WALKING_DISTANCE_RADIUS = Emulator.getConfig().getInt("hotel.bot.limit.walking.distance.radius", 5); HabboInventory.MAXIMUM_ITEMS = Emulator.getConfig().getInt("hotel.inventory.max.items"); Messenger.MAXIMUM_FRIENDS = Emulator.getConfig().getInt("hotel.users.max.friends", 300);