NicoFIDI
Member
Hello, as a result of try and error I finally made a random maze generator, it's not perfect and the discussion I want to start it's about some other ways to generate a maze.
The code has 2 parts a base and the polish
The base it's straight fordward and the easiest one to understand
Then to make the maze more maze-like i added the polish witch it's easy to esplain and hard to code:
The code conects the walls isle that aren't conected with the exteiror walls, with an added wall.
This it's the code:
I wanted to open a discussion about other ways to make random mazes.
Finally this is how the algorithm works with his polish
The code has 2 parts a base and the polish
The base it's straight fordward and the easiest one to understand
Code:
/// ds_grid_mazeify(grid, wallValue, floorValue, startX, startY, polish)
var blueprint = argument[0];
var wallValue = argument[1];
var floorValue = argument[2];
var startX = argument[3];
var startY = argument[4];
var polish = argument[5];
var maze_width = ds_grid_width (blueprint);
var maze_height = ds_grid_height(blueprint);
enum MazePolishOption {
no_polish,
walls,
floors,
both
}
ds_grid_clear(blueprint, floorValue);
for (var xx = 0; xx < maze_width ; xx++) {
for (var yy = 0; yy < maze_height; yy++) {
var borderX = xx == 0 || xx == maze_width-1;
var borderY = yy == 0 || yy == maze_height-1;
var startPosition = (xx == startX) && (yy == startY);
if ((borderX || borderY)) {
if (!startPosition)
blueprint[# xx,yy] = wallValue;
} else {
var oddPosition = (xx mod 2 == (startX-1) mod 2) && (yy mod 2 == (startY-1) mod 2)
if (oddPosition) {
blueprint[# xx,yy] = wallValue;
var wallDirection = irandom(3) * 90;
blueprint[# xx + dcos(wallDirection),yy + dsin(wallDirection)] = wallValue;
}
}
}
}
if (polish == MazePolishOption.walls || polish == MazePolishOption.both)
ds_grid_mazeify_conect_value(blueprint,wallValue);
if (polish == MazePolishOption.floors || polish == MazePolishOption.both)
ds_grid_mazeify_conect_value(blueprint,floorValue);
Then to make the maze more maze-like i added the polish witch it's easy to esplain and hard to code:
The code conects the walls isle that aren't conected with the exteiror walls, with an added wall.
This it's the code:
Code:
/// ds_grid_mazeify_conect_value(grid, value)
var blueprints = argument[0];
var content = argument[1];
var maze_width = ds_grid_width(blueprints);
var maze_height = ds_grid_height(blueprints);
var used = ds_map_create();
var chain = ds_list_create();
var chained = ds_grid_create(maze_width, maze_height);
var itsChained = true;
ds_grid_clear(chained, false);
for (var xx = 0; xx < maze_width; xx++) {
for (var yy = 0; yy < maze_height ; yy++) {
var isContent = blueprints[# xx,yy] == content;
var itsUsed = ds_map_exists(used, xx*10000+yy);
if (isContent && !itsUsed) {
var queue = ds_queue_create();
ds_queue_enqueue(queue,xx,yy);
while(!ds_queue_empty(queue)) {
var xact = ds_queue_dequeue(queue);
var yact = ds_queue_dequeue(queue);
ds_list_add(chain,xact,yact);
used[? xact*10000+yact] = false;
for (var dir = 0; dir < 360; dir += 90) {
var xnew = xact + dcos(dir);
var ynew = yact + dsin(dir);
var xvalido = between(xnew,0,maze_width-1);
var yvalido = between(ynew,0,maze_height-1);
if (!itsChained) { itsChained = !(xvalido && yvalido); }
var posUsed = ds_map_exists(used, xnew*10000+ynew);
used[? xnew*10000+ynew] = false;
if (xvalido && yvalido && !posUsed) {
if (blueprints[# xnew,ynew] == content) {
ds_queue_enqueue(queue, xnew, ynew);
}
}
}
}
ds_queue_destroy(queue);
while (!itsChained) {
var imax = ds_list_size(chain)/2;
var i = irandom(imax-1);
var xact = chain[| 2*i+0];
var yact = chain[| 2*i+1];
var desv = irandom(3) * 90;
for (var dir = 0; dir < 360 && !itsChained; dir += 90) {
var deltax = dcos(dir+desv);
var deltay = dsin(dir+desv);
if (chained[# xact+2*deltax,yact+2*deltay]) {
blueprints[# xact+deltax,yact+deltay] = content;
ds_list_add(chain,xact+deltax,yact+deltay);
itsChained = true;
}
}
}
for (var i = 0; i < ds_list_size(chain)/2; i++) {
var xnew = chain[| 2*i+0];
var ynew = chain[| 2*i+1];
chained[# xnew,ynew] = itsChained;
}
itsChained = false;
ds_list_clear(chain);
}
}}
ds_map_destroy(used);
ds_list_destroy(chain);
ds_grid_destroy(chained);
I wanted to open a discussion about other ways to make random mazes.
Finally this is how the algorithm works with his polish