Here's some code to generate a maze (https://en.wikipedia.org/wiki/Maze_generation_algorithm):
I'll post some more code later, explaining how this can be used for creating a map for the wolf3d library.
Code:
set window "maze", 640, 480
set redraw off
randomize time()
maze = GenerateMaze(24, 24)
side = 16
set color 0, 0, 0
cls
for x = 0 to sizeof(maze) - 1
for y = 0 to sizeof(maze[0]) - 1
dx = x*side
dy = y*side
set color 255, 255, 255
if not maze[x][y].l draw line dx, dy, dx, dy + side - 1
if not maze[x][y].r draw line dx + side - 1, dy, dx + side - 1, dy + side - 1
if not maze[x][y].u draw line dx, dy, dx + side - 1, dy
if not maze[x][y].d draw line dx, dy + side - 1, dx + side - 1, dy + side - 1
next
next
redraw
while not keydown(KEY_ESCAPE, true) fwait 60
' GenerateMaze
' ------------
' Return an array of the size w*h, where every element has four fields, l, r, u and d, telling if
' there's a way left, right, up and down.
function GenerateMaze(w, h)
maze = fill([vis: false, l: false, r: false, u: false, d: false], w, h)
GenerateMazeRec(maze, rnd(sizeof(maze)), rnd(sizeof(maze[0])), 0)
return maze
function GenerateMazeRec(maze, x, y, dir)
if x < 0 or x >= sizeof(maze) or y < 0 or y >= sizeof(maze[0]) return false
if maze[x][y].vis return false
maze[x][y].vis = true
if dir = 1
maze[x][y].r = true
maze[x + 1][y].l = true
elseif dir = 2
maze[x][y].l = true
maze[x - 1][y].r = true
elseif dir = 3
maze[x][y].d = true
maze[x][y + 1].u = true
elseif dir = 4
maze[x][y].u = true
maze[x][y - 1].d = true
endif
visit = [1, 2, 3, 4]
while sizeof(visit)
index = rnd(sizeof(visit))
if visit[index] = 1 GenerateMazeRec(maze, x - 1, y, 1)
elseif visit[index] = 2 GenerateMazeRec(maze, x + 1, y, 2)
elseif visit[index] = 3 GenerateMazeRec(maze, x, y - 1, 3)
else GenerateMazeRec(maze, x, y + 1, 4)
free key visit, index
wend
return true
endfunc
endfunc
I'll post some more code later, explaining how this can be used for creating a map for the wolf3d library.