I'm trying to write a program that generates a random maze around the spawn point. Unfortunately, I've run into a few errors that I can't seem to figure out. The main one is that the X and Y axis walls are being buggy for no reason that I can locate, being on and off at nonsensical times. This does not happen with the walls on the Z axis nor does it happen in the actual map data, only in the map that it creates. I have checked the maze data and that works fine. This shows that there is some error in my interpretation of the maze into blocks, but I cannot find it.
Also there is something annoying that I have found in Notch's programming that is messing up my program as well. For some reason he mixes up the axes so that Y is the vertical and Z is one of the flat axes. Could anybody help with these problems?
Note: I used the level editing class provided with the wiki.
Here is my source code:
//MazeGen.java
//By VDOgamez
//Creates a maze around the spawn point
import java.util.*;
public class MazeGen {
private LevelEditor le;
public MazeGen(String fileName, int x, int y, int z) {
le = new LevelEditor();
le.load(fileName);
generate(x, y, z);
le.save("maze_" + fileName);
}
//Represents a cell in the maze
private class Cell {
//The coordinates of the cell
private int x;
private int y;
private int z;
//Which sides are walled (True means there is a wall)
private boolean up;
private boolean down;
private boolean north;
private boolean south;
private boolean east;
private boolean west;
public Cell(int xpos, int ypos, int zpos) {
x = xpos;
y = ypos;
z = zpos;
up = true;
down = true;
north = true;
south = true;
east = true;
west = true;
}
}
private class Maze {
private int length;
private int width;
private int height;
private Cell start;
private Cell end;
private Cell[][][] maze;
public Maze(int x, int y, int z) {
length = x;
width = y;
height = z;
maze = new Cell[x][y][z];
//Initializes the maze grid
for(int i = 0; i visited = new HashSet();
Set unvisited = new HashSet();
//Wilson's algorithm requires a random walk from a random cell to the already
//built part of the maze, which is then carved out. This stores information for the path.
int[][][] dirs = new int[length][width][height];
//Initializing
for(int i = 0; i 0) {
//Picks a random cell
int rand = (int)(Math.random() * unvisited.size());
Cell startCell = (Cell)unvisited.toArray()[rand];
Cell cell = startCell;
//Finds a path from the cell to the maze
while(!visited.contains(cell)) {
x = cell.x;
y = cell.y;
z = cell.z;
int dir;
boolean validDir = false;
//Picks a valid direction
do {
dir = (int)(Math.random() * 6 + 1);
if(dir == 1 && z 0)
validDir = true;
if(dir == 3 && y 0)
validDir = true;
if(dir == 5 && x 0)
validDir = true;
}
while(!validDir);
//Sets the direction data
dirs[x][y][z] = dir;
//Moves in the selected direction
if(dir == 1)
z++;
if(dir == 2)
z--;
if(dir == 3)
y++;
if(dir == 4)
y--;
if(dir == 5)
x++;
if(dir == 6)
x--;
cell = maze[x][y][z];
}
cell = startCell;
//Carves along the selected path
while(!visited.contains(cell)) {
//Adds carved cells to the maze
unvisited.remove(cell);
visited.add(cell);
//Debug, prints out maze information
for(int i = 0; i < length; i++) {
for(int j = 0; j < width; j++) {
for(int k = 0; k < height; k++) {
System.out.println(maze[i][j][k].x + "," + maze[i][j][k].y +
"," + maze[i][j][k].z);
System.out.println(maze[i][j][k].up + "," + maze[i][j][k].down +
"," + maze[i][j][k].north + "," + maze[i][j][k].south + "," +
maze[i][j][k].east + "," + maze[i][j][k].west);
}
}
}
}
}
//Generates the block pattern in the actual level from the generated maze
//NOT WORKING PROPERLY!!!
private void generate(int len, int wid, int hei) {
//Gets the spawn point to build around
int x = le.level.xSpawn - 1;
int y = le.level.ySpawn - 1;
int z = le.level.zSpawn - 1;
//Generates the maze
Maze ref = new Maze(len, wid, hei);
//Build the walls for each cell
for(int i = 0; i < len; i++) {
for(int j = 0; j < wid; j++) {
for(int k = 0; k < hei; k++) {
//Add the area that all cells have around them, leaving open all 6 walls,
//also filling in water in the middle. There are redundancies, but they do not affect anything
//(To my knowledge)
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 2, Blocks.waterstill);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k + 3, Blocks.blackrock);
//Sets optional walls for each tile; if no wall, water is used.
if(ref.maze[i][j][k].up)
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 3, Blocks.blackrock);
else
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 3, Blocks.waterstill);
if(ref.maze[i][j][k].down)
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k, Blocks.blackrock);
else
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k, Blocks.waterstill);
if(ref.maze[i][j][k].north) {
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 2, Blocks.waterstill);
}
if(ref.maze[i][j][k].south) {
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 2, Blocks.waterstill);
}
if(ref.maze[i][j][k].east) {
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 2, Blocks.waterstill);
}
if(ref.maze[i][j][k].west) {
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 2, Blocks.waterstill);
}
}
}
}
}
public static void main(String[] args) {
MazeGen gen = new MazeGen("maze.dat", 5, 5, 5);
}
}
Rollback Post to RevisionRollBack
I like making tools for Minecraft. Here are a few of the ones that I have made so far:
MazeGen (Version 1.0) --- An automatic maze generation tool ModelGen (Version 1.0) --- A 3D model import tool
I know Java syntax but I don't use it so I might of missed something. The first thing I would start with is checking that you are writing to the right part of the map file. being even one line off can cuase it to miss the data some times and not others. Espiacialy if the spacing is an odd number. Also redundincies rarely affect don't something.(At least in my experiences)
Rollback Post to RevisionRollBack
Blargha! Blargha! Blargha!
I wouldn't listen to a thing I say. I'm probably insane.
Also there is something annoying that I have found in Notch's programming that is messing up my program as well. For some reason he mixes up the axes so that Y is the vertical and Z is one of the flat axes. Could anybody help with these problems?
Note: I used the level editing class provided with the wiki.
Here is my source code:
//MazeGen.java
//By VDOgamez
//Creates a maze around the spawn point
import java.util.*;
public class MazeGen {
private LevelEditor le;
public MazeGen(String fileName, int x, int y, int z) {
le = new LevelEditor();
le.load(fileName);
generate(x, y, z);
le.save("maze_" + fileName);
}
//Represents a cell in the maze
private class Cell {
//The coordinates of the cell
private int x;
private int y;
private int z;
//Which sides are walled (True means there is a wall)
private boolean up;
private boolean down;
private boolean north;
private boolean south;
private boolean east;
private boolean west;
public Cell(int xpos, int ypos, int zpos) {
x = xpos;
y = ypos;
z = zpos;
up = true;
down = true;
north = true;
south = true;
east = true;
west = true;
}
}
private class Maze {
private int length;
private int width;
private int height;
private Cell start;
private Cell end;
private Cell[][][] maze;
public Maze(int x, int y, int z) {
length = x;
width = y;
height = z;
maze = new Cell[x][y][z];
//Initializes the maze grid
for(int i = 0; i visited = new HashSet();
Set unvisited = new HashSet();
//Wilson's algorithm requires a random walk from a random cell to the already
//built part of the maze, which is then carved out. This stores information for the path.
int[][][] dirs = new int[length][width][height];
//Initializing
for(int i = 0; i 0) {
//Picks a random cell
int rand = (int)(Math.random() * unvisited.size());
Cell startCell = (Cell)unvisited.toArray()[rand];
Cell cell = startCell;
//Finds a path from the cell to the maze
while(!visited.contains(cell)) {
x = cell.x;
y = cell.y;
z = cell.z;
int dir;
boolean validDir = false;
//Picks a valid direction
do {
dir = (int)(Math.random() * 6 + 1);
if(dir == 1 && z 0)
validDir = true;
if(dir == 3 && y 0)
validDir = true;
if(dir == 5 && x 0)
validDir = true;
}
while(!validDir);
//Sets the direction data
dirs[x][y][z] = dir;
//Moves in the selected direction
if(dir == 1)
z++;
if(dir == 2)
z--;
if(dir == 3)
y++;
if(dir == 4)
y--;
if(dir == 5)
x++;
if(dir == 6)
x--;
cell = maze[x][y][z];
}
cell = startCell;
//Carves along the selected path
while(!visited.contains(cell)) {
//Adds carved cells to the maze
unvisited.remove(cell);
visited.add(cell);
x = cell.x;
y = cell.y;
z = cell.z;
int dir = dirs[x][y][z];
//Checks for direction to carve and opens walls
if(dir == 1)
cell.up = false;
if(dir == 2)
cell.down = false;
if(dir == 3)
cell.north = false;
if(dir == 4)
cell.south = false;
if(dir == 5)
cell.east = false;
if(dir == 6)
cell.west = false;
if(dir == 1)
z++;
if(dir == 2)
z--;
if(dir == 3)
y++;
if(dir == 4)
y--;
if(dir == 5)
x++;
if(dir == 6)
x--;
cell = maze[x][y][z];
if(dir == 1)
cell.down = false;
if(dir == 2)
cell.up = false;
if(dir == 3)
cell.south = false;
if(dir == 4)
cell.north = false;
if(dir == 5)
cell.west = false;
if(dir == 6)
cell.east = false;
}
}
//start.down = false;
end.up = false;
//Debug, prints out maze information
for(int i = 0; i < length; i++) {
for(int j = 0; j < width; j++) {
for(int k = 0; k < height; k++) {
System.out.println(maze[i][j][k].x + "," + maze[i][j][k].y +
"," + maze[i][j][k].z);
System.out.println(maze[i][j][k].up + "," + maze[i][j][k].down +
"," + maze[i][j][k].north + "," + maze[i][j][k].south + "," +
maze[i][j][k].east + "," + maze[i][j][k].west);
}
}
}
}
}
//Generates the block pattern in the actual level from the generated maze
//NOT WORKING PROPERLY!!!
private void generate(int len, int wid, int hei) {
//Gets the spawn point to build around
int x = le.level.xSpawn - 1;
int y = le.level.ySpawn - 1;
int z = le.level.zSpawn - 1;
//Generates the maze
Maze ref = new Maze(len, wid, hei);
//Build the walls for each cell
for(int i = 0; i < len; i++) {
for(int j = 0; j < wid; j++) {
for(int k = 0; k < hei; k++) {
//Add the area that all cells have around them, leaving open all 6 walls,
//also filling in water in the middle. There are redundancies, but they do not affect anything
//(To my knowledge)
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 2, Blocks.waterstill);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k + 2, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 2, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 3, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 2, z + 3 * k + 3, Blocks.blackrock);
//Sets optional walls for each tile; if no wall, water is used.
if(ref.maze[i][j][k].up)
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 3, Blocks.blackrock);
else
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k + 3, Blocks.waterstill);
if(ref.maze[i][j][k].down)
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k, Blocks.blackrock);
else
le.setTile(x + 2 * i + 1, y + 2 * j + 1, z + 3 * k, Blocks.waterstill);
if(ref.maze[i][j][k].north) {
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i + 2, y + 2 * j + 1, z + 3 * k + 2, Blocks.waterstill);
}
if(ref.maze[i][j][k].south) {
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i, y + 2 * j + 1, z + 3 * k + 2, Blocks.waterstill);
}
if(ref.maze[i][j][k].east) {
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i + 1, y + 2 * j + 2, z + 3 * k + 2, Blocks.waterstill);
}
if(ref.maze[i][j][k].west) {
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 1, Blocks.blackrock);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 2, Blocks.blackrock);
}
else {
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 1, Blocks.waterstill);
le.setTile(x + 2 * i + 1, y + 2 * j, z + 3 * k + 2, Blocks.waterstill);
}
}
}
}
}
public static void main(String[] args) {
MazeGen gen = new MazeGen("maze.dat", 5, 5, 5);
}
}
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool
I wouldn't listen to a thing I say. I'm probably insane.
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool
I wouldn't listen to a thing I say. I'm probably insane.
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool
MazeGen (Version 1.0) --- An automatic maze generation tool
ModelGen (Version 1.0) --- A 3D model import tool