add a kinda broken maze command

This commit is contained in:
Chayapak 2023-04-06 15:01:41 +07:00
parent 55a9d6a746
commit 742edc9cc2
5 changed files with 283 additions and 0 deletions

View file

@ -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<Bot> 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();
}

View file

@ -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<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<x> <y> <z> <width> <long>");
return usages;
}
public List<String> alias() {
final List<String> 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");
}
}

View file

@ -50,6 +50,7 @@ public class CommandHandlerPlugin {
registerCommand(new WeatherCommand());
registerCommand(new ServerInfoCommand());
registerCommand(new BotUserCommand());
registerCommand(new GenerateMazeCommand());
}
public void registerCommand (Command command) {

View file

@ -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++;
}
}
}
}
}

View file

@ -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<int[]> 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<int[]> getNeighbors(int x, int y) {
List<int[]> 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;
}
}