local grid = {}
local n = {}
local path = {}
local nn = {}
local r
local len = 0
local is_stepping = false
for x = 1, 50 do
grid[x] = {}
path[x] = {}
for y = 1, 50 do
grid[x][y] = false
path[x][y] = false
end
end
n={{x=1,y=1}}
local function maze_gen() --actual prims alg, generates 1 cell everytime the func is ran
if #n>0 then
r = math.random(1,#n)
if not grid[n[r].x][n[r].y] then
grid[n[r].x][n[r].y] = true path[n[r].x][n[r].y] = true
if n[r].y>1 then if not grid[n[r].x][n[r].y-1] then table.insert(nn,{x=n[r].x,y=n[r].y-1}) end end
if n[r].y<50 then if not grid[n[r].x][n[r].y+1] then table.insert(nn,{x=n[r].x,y=n[r].y+1}) end end
if n[r].x>1 then if not grid[n[r].x-1][n[r].y] then table.insert(nn,{x=n[r].x-1,y=n[r].y}) end end
if n[r].x<50 then if not grid[n[r].x+1][n[r].y] then table.insert(nn,{x=n[r].x+1,y=n[r].y}) end end
if #nn>0 then
for i=1,#nn do
if nn[i].y>1 then if n[r].x~=nn[i].x and n[r].y~=nn[i].y-1 and path[nn[i].x][nn[i].y-1] then len=len+1 grid[nn[i].x][nn[i].y-1]=true end end
if nn[i].y<50 then if n[r].x~=nn[i].x and n[r].y~=nn[i].y+1 and path[nn[i].x][nn[i].y+1] then len=len+1 grid[nn[i].x][nn[i].y+1]=true end end
if nn[i].x>1 then if n[r].x~=nn[i].x-1 and n[r].y~=nn[i].y and path[nn[i].x-1][nn[i].y] then len=len+1 grid[nn[i].x-1][nn[i].y]=true end end
if nn[i].x<50 then if n[r].x~=nn[i].x+1 and n[r].y~=nn[i].y and path[nn[i].x+1][nn[i].y] then len=len+1 grid[nn[i].x+1][nn[i].y]=true end end
if nn[i].y>1 and nn[i].x>1 then if n[r].x~=nn[i].x-1 and n[r].y~=nn[i].y-1 and path[nn[i].x-1][nn[i].y-1] then len=len+1 grid[nn[i].x-1][nn[i].y-1]=true end end
if nn[i].y<50 and nn[i].x>1 then if n[r].x~=nn[i].x-1 and n[r].y~=nn[i].y+1 and path[nn[i].x-1][nn[i].y+1] then len=len+1 grid[nn[i].x-1][nn[i].y+1]=true end end
if nn[i].x<50 and nn[i].y>1 then if n[r].x~=nn[i].x+1 and n[r].y~=nn[i].y-1 and path[nn[i].x+1][nn[i].y-1] then len=len+1 grid[nn[i].x+1][nn[i].y-1]=true end end
if nn[i].x<50 and nn[i].y<50 then if n[r].x~=nn[i].x+1 and n[r].y~=nn[i].y+1 and path[nn[i].x+1][nn[i].y+1] then len=len+1 grid[nn[i].x+1][nn[i].y+1]=true end end
if len ~= 0 then
grid[nn[i].x][nn[i].y]=true
end
table.insert(n,nn[i])
len = 0
end
end
nn = {}
end
table.remove(n,r)
end
end
while #n>0 do
maze_gen()
end
add_hook("draw2d","box's homework",
function()
set_color(0,0,0,1)
draw_quad(0,0,520,520)
draw_text("boxprims.lua by yoyo - press h to regenerate maze, press n to generate step by step",550,100,1)
for j=1,50 do
for i=1,50 do
set_color(1,1,1,1)
if path[j][i] then
draw_quad(j*10,i*10,10,10)
end
if is_stepping then
set_color(0,0,1,0.3)
if grid[j][i] then
draw_quad(j*10,i*10,10,10)
end
end
end
end
if is_stepping then
set_color(1,0,0,0.3)
for i=1,#n do
draw_quad(n[i].x*10,n[i].y*10,10,10)
end
maze_gen() maze_gen() maze_gen() --step the algorithm forward 3 times
if #n==0 then is_stepping = false end
end
end
)
add_hook("key_down","box's homework",function(k)
if k==104 then
run_cmd("ls boxprims.lua")
elseif k==110 then
if not is_stepping then
is_stepping = true
if #n==0 then
for x = 1, 50 do
grid[x] = {}
path[x] = {}
for y = 1, 50 do
grid[x][y] = false
path[x][y] = false
end
end
n = {{x=1,y=1}}
end
else
is_stepping = false
end
end
end)