From 742edc9cc2b99f6666d87fe187988588f0b95d62 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:01:41 +0700 Subject: [PATCH] add a kinda broken maze command --- .../chipmunk/chayapak/chomens_bot/Bot.java | 2 + .../commands/GenerateMazeCommand.java | 61 ++++++++ .../plugins/CommandHandlerPlugin.java | 1 + .../chomens_bot/plugins/MazePlugin.java | 141 ++++++++++++++++++ .../chomens_bot/util/MazeGenerator.java | 78 ++++++++++ 5 files changed, 283 insertions(+) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GenerateMazeCommand.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MazePlugin.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/util/MazeGenerator.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index 5d99550..dd4d009 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -58,6 +58,7 @@ public class Bot { @Getter private BruhifyPlugin bruhify; @Getter private GrepLogPlugin grepLog; @Getter private CloopPlugin cloop; + @Getter private MazePlugin maze; public Bot (String host, int port, String _username, boolean kaboom, String serverName, List allBots, Configuration config) { this.host = host; @@ -95,6 +96,7 @@ public class Bot { this.bruhify = new BruhifyPlugin(this); this.grepLog = new GrepLogPlugin(this); this.cloop = new CloopPlugin(this); + this.maze = new MazePlugin(this); reconnect(); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GenerateMazeCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GenerateMazeCommand.java new file mode 100644 index 0000000..f4380a7 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/GenerateMazeCommand.java @@ -0,0 +1,61 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import land.chipmunk.chayapak.chomens_bot.util.MazeGenerator; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class GenerateMazeCommand implements Command { + public String name() { return "generatemaze"; } + + public String description() { + return "Generates a maze"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(" "); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("genmaze"); + aliases.add("mazegen"); + + return aliases; + } + + public int trustLevel() { + return 0; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot(); + + try { + final int x = Integer.parseInt(args[0]); + final int y = Integer.parseInt(args[1]); + final int z = Integer.parseInt(args[2]); + + final int width = Integer.parseInt(args[3]); + final int height = Integer.parseInt(args[4]); + + final MazeGenerator generator = new MazeGenerator(width, height); + + generator.generateMaze(); + + bot.maze().generate(generator, x, y, z); + } catch (NumberFormatException e) { + return Component.text("Invalid position/size").color(NamedTextColor.RED); + } + + return Component.text("success"); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index f077b9a..aacac76 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -50,6 +50,7 @@ public class CommandHandlerPlugin { registerCommand(new WeatherCommand()); registerCommand(new ServerInfoCommand()); registerCommand(new BotUserCommand()); + registerCommand(new GenerateMazeCommand()); } public void registerCommand (Command command) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MazePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MazePlugin.java new file mode 100644 index 0000000..bc059b5 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MazePlugin.java @@ -0,0 +1,141 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.util.MazeGenerator; + +public class MazePlugin { + private final Bot bot; + + public MazePlugin (Bot bot) { + this.bot = bot; + } + + // also totally didn't ask chatgpt for this too (but modified a bit) + public void generate (MazeGenerator generator, int startX, int startY, int startZ) { + final int[][] maze = generator.maze(); + + int x = startX; + int z = startZ; + + // Find the starting and ending positions of the maze + int startRow = 0; + int startCol = 0; + int endRow = maze.length - 1; + int endCol = maze[0].length - 1; + while (maze[startRow][startCol] != 0) { + startCol++; + if (startCol == maze[0].length) { + startCol = 0; + startRow++; + } + } + while (maze[endRow][endCol] != 0) { + endCol--; + if (endCol < 0) { + endCol = maze[0].length - 1; + endRow--; + } + } + + final String command = "minecraft:fill %s %s %s %s %s %s %s"; + + // lazy fix for the sus border issue + bot.core().run( + String.format( + command, + x + generator.width(), + startY, + z, + x + generator.width(), + startY + 3, + z + generator.height(), + "minecraft:stone" + ) + ); + + bot.core().run( + String.format( + command, + x, + startY, + z + generator.height(), + x + generator.width(), + startY + 3, + z + generator.height(), + "minecraft:stone" + ) + ); + + // fill the floor + bot.core().run( + String.format( + command, + x, + startY - 1, + z, + x + generator.width(), + startY - 1, + z + generator.height(), + "minecraft:stone_bricks replace minecraft:air" + ) + ); + + // actually build the maze + for (int row = 0; row < generator.height(); row++) { + for (int col = 0; col < generator.width(); col++) { + if (maze[row][col] == 1) { + // makes the wall + bot.core().run( + String.format( + command, + x, + startY, + z, + x, + startY + 3, + z, + "minecraft:stone" + ) + ); + } else if ((row == startRow && col == startCol)) { + // Set a marker block for the start position + bot.core().run( + String.format( + command, + x, + startY - 1, + z, + x, + startY - 1, + z, + "minecraft:glowstone" + ) + ); + } else if ((row == endRow && col == endCol)) { + // Set a marker block for the end position + bot.core().run( + String.format( + command, + x, + startY - 1, + z, + x, + startY - 1, + z, + "minecraft:lime_concrete" + ) + ); + } + + // Increment the x-coordinate + x++; + + // If we've reached the end of the x-axis, reset x and increment z + if (x == startX + maze[0].length) { + x = startX; + z++; + } + } + } + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MazeGenerator.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MazeGenerator.java new file mode 100644 index 0000000..27c0a48 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/MazeGenerator.java @@ -0,0 +1,78 @@ +package land.chipmunk.chayapak.chomens_bot.util; + +import lombok.Getter; + +import java.util.*; + +// totally didn't ask chatgpt for this lmao +public class MazeGenerator { + @Getter private final int width; + @Getter private final int height; + @Getter private final int[][] maze; + private final Random rand; + + public MazeGenerator(int width, int height) { + this.width = width; + this.height = height; + this.maze = new int[height][width]; + this.rand = new Random(); + } + + public void generateMaze() { + // Set all cells to walls + for (int row = 0; row < height; row++) { + Arrays.fill(maze[row], 1); + } + + // Create a starting point + int startX = rand.nextInt(width); + int startY = rand.nextInt(height); + maze[startY][startX] = 0; + + // Recursive backtracking algorithm + backtrack(startX, startY); + } + + private void backtrack(int x, int y) { + // Get a list of neighboring cells + List neighbors = getNeighbors(x, y); + + // Shuffle the list of neighbors + Collections.shuffle(neighbors, rand); + + for (int[] neighbor : neighbors) { + int nx = neighbor[0]; + int ny = neighbor[1]; + + // Check if the neighboring cell is a wall + if (maze[ny][nx] == 1) { + // Remove the wall between the current cell and the neighboring cell + maze[(y + ny) / 2][(x + nx) / 2] = 0; + maze[ny][nx] = 0; + + // Recursively backtrack from the neighboring cell + backtrack(nx, ny); + } + } + } + + private List getNeighbors(int x, int y) { + List neighbors = new ArrayList<>(); + + if (x > 1) { + neighbors.add(new int[]{x - 2, y}); + } + if (y > 1) { + neighbors.add(new int[]{x, y - 2}); + } + if (x < width - 2) { + neighbors.add(new int[]{x + 2, y}); + } + if (y < height - 2) { + neighbors.add(new int[]{x, y + 2}); + } + + return neighbors; + } +} +