diff --git a/sqlupdates/3_5_0 to 4_0_0.sql b/sqlupdates/3_5_0 to 4_0_0.sql index 7b308684..03c319a2 100644 --- a/sqlupdates/3_5_0 to 4_0_0.sql +++ b/sqlupdates/3_5_0 to 4_0_0.sql @@ -14,4 +14,8 @@ ALTER TABLE `pet_commands_data` CHANGE `cost_happyness` `cost_happiness` int(11) ALTER TABLE `users_pets` CHANGE `happyness` `happiness` int(11) NOT NULL DEFAULT '100'; -UPDATE `items_base` SET `interaction_type` = 'spinning_bottle', `interaction_modes_count` = '8' WHERE `item_name` = 'bottle'; \ No newline at end of file +UPDATE `items_base` SET `interaction_type` = 'spinning_bottle', `interaction_modes_count` = '8' WHERE `item_name` = 'bottle'; + +--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'); 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 ed58bbfa..b10086cf 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java @@ -54,7 +54,6 @@ public class Bot implements Runnable { private transient boolean canWalk = true; - private boolean needsUpdate; @@ -114,7 +113,6 @@ public class Bot implements Runnable { this.type = bot.getType(); this.effect = bot.getEffect(); this.bubble = bot.getBubbleId(); - this.needsUpdate = false; } @@ -136,31 +134,28 @@ 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 = ?")) { + 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 = ?, 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"); + statement.setInt(7, this.roomUnit == null ? 0 : this.roomUnit.getBodyRotation().getValue()); + statement.setInt(8, this.roomUnit == null ? 0 : this.roomUnit.getDanceType().getType()); + statement.setString(9, 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(10, text.toString()); + statement.setString(11, this.chatAuto ? "1" : "0"); + statement.setString(12, this.chatRandom ? "1" : "0"); + statement.setInt(13, this.chatDelay); + statement.setInt(14, this.effect); + statement.setInt(15, this.bubble); + statement.setInt(16, this.id); statement.execute(); this.needsUpdate = false; } catch (SQLException e) { @@ -174,7 +169,8 @@ 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(Emulator.getConfig().getBoolean("hotel.bot.limit.walking.distance", true) ? this.room.getLayout().getRandomWalkableTilesAround(this.roomUnit, this.room.getLayout().getTile(this.roomUnit.getBotStartLocation().getX(), this.roomUnit.getBotStartLocation().getY()), this.room, Emulator.getConfig().getInt("hotel.bot.limit.walking.distance.radius", 5)) : this.room.getRandomWalkableTile()); + int timeOut = Emulator.getRandom().nextInt(20) * 2; this.roomUnit.setWalkTimeOut((timeOut < 10 ? 5 : timeOut) + Emulator.getIntUnixTimestamp()); } @@ -484,4 +480,35 @@ 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) { + log.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 be39101f..461c306f 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java @@ -131,7 +131,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/RoomLayout.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java index 5236e11b..379fa44b 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java @@ -7,9 +7,7 @@ import lombok.extern.slf4j.Slf4j; import java.awt.*; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Deque; -import java.util.LinkedList; +import java.util.*; import java.util.List; @Slf4j @@ -590,6 +588,30 @@ public class RoomLayout { return availableTiles; } + public RoomTile getRandomWalkableTilesAround(RoomUnit roomUnit, RoomTile tile, Room room, int radius) { + if(!this.tileExists(tile.getX(), tile.getY())) { + tile = this.getTile(roomUnit.getX(), roomUnit.getY()); + room.getBot(roomUnit).needsUpdate(true); + } + + List newTiles = new ArrayList<>(); + + int minX = Math.max(0, tile.getX() - radius); + int minY = Math.max(0, tile.getY() - radius); + int maxX = Math.min(room.getLayout().getMapSizeX(), tile.getX() + radius); + int maxY = Math.min(room.getLayout().getMapSizeY(), tile.getY() + radius); + + for (int x = minX; x <= maxX; x++) { + for (int y = minY; y <= maxY; y++) { + RoomTile tile2 = room.getLayout().getTile((short) x, (short) y); + if (tile2 != null && tile.getState() != RoomTileState.BLOCKED && tile.getState() != RoomTileState.INVALID) + newTiles.add(tile2); + } + } + Collections.shuffle(newTiles); + return newTiles.get(0); + } + public boolean fitsOnMap(RoomTile tile, int width, int length, int rotation) { if (tile != null) { if (rotation == 0 || rotation == 4) { diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnit.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnit.java index c0437373..91d778cf 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnit.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnit.java @@ -79,6 +79,9 @@ public class RoomUnit { @Getter private RoomTile startLocation; @Getter + @Setter + private RoomTile botStartLocation; + @Getter private RoomTile previousLocation; @Getter @Setter @@ -501,6 +504,7 @@ public class RoomUnit { setPreviousLocation(location); setCurrentLocation(location); this.goalLocation = location; + this.botStartLocation = location; } return this; }