sync on
set display mode 800,600,32
randomize timer()
 
type cell
   visited as integer
   xpos as integer
   ypos as integer
   xconnect as integer
   yconnect as integer
   room as integer
endtype
 
mapwidth=30
mapheight = 30
dens as float =.90
map_density=(mapwidth*mapheight)*dens
rooms=rnd(10)+10
 
dim maze(mapwidth,mapheight) as cell
 
startx=rnd(mapwidth):starty=rnd(mapheight)
 
maze(startx,starty).visited=1
maze(startx,starty).xpos=startx
maze(startx,starty).ypos=starty
maze(startx,starty).xconnect=startx
maze(startx,starty).yconnect=starty
 
repeat
blocked=0
if startx-1<=0
   inc blocked
else
   if maze(startx-1,starty).visited=1 then inc blocked
endif
 
if starty-1<=0
   inc blocked
else
   if maze(startx,starty-1).visited=1 then inc blocked
endif
 
if startx+1>=mapwidth
   inc blocked
else
   if maze(startx+1,starty).visited=1 then inc blocked
endif
 
if starty+1>=mapheight
   inc blocked
else
   if maze(startx,starty+1).visited=1 then inc blocked
endif
 
if blocked<4
 
   repeat
   open=1
   direction=rnd(4)+1
      select direction
         case 1:
         if starty-1>=0
            if maze(startx,starty-1).visited=0
               newx=startx:newy=starty-1
               maze(newx,newy).visited=1
               maze(newx,newy).xpos=newx
               maze(newx,newy).ypos=newy
               maze(newx,newy).xconnect=startx
               maze(newx,newy).yconnect=starty
               open=0
               startx=newx:starty=newy
            endif
         endif
         endcase
         case 2:
         if starty+1<=mapheight
            if maze(startx,starty+1).visited=0
               newx=startx:newy=starty+1
               maze(newx,newy).visited=1
               maze(newx,newy).xpos=newx
               maze(newx,newy).ypos=newy
               maze(newx,newy).xconnect=startx
               maze(newx,newy).yconnect=starty
               open=0
               startx=newx:starty=newy
            endif
         endif
         endcase
         case 3:
         if startx-1>=0
            if maze(startx-1,starty).visited=0
               newx=startx-1:newy=starty
               maze(newx,newy).visited=1
               maze(newx,newy).xpos=newx
               maze(newx,newy).ypos=newy
               maze(newx,newy).xconnect=startx
               maze(newx,newy).yconnect=starty
               open=0
               startx=newx:starty=newy
            endif
         endif
         endcase
 
         case 4:
         if startx+1<=mapwidth
            if maze(startx+1,starty).visited=0
               newx=startx+1
               newy=starty
               maze(newx,newy).visited=1
               maze(newx,newy).xpos=newx
               maze(newx,newy).ypos=newy
               maze(newx,newy).xconnect=startx
               maze(newx,newy).yconnect=starty
               open=0
               startx=newx:starty=newy
            endif
         endif
         endcase
      endselect
   until open=0
else
repeat
  xx=rnd(mapwidth)
  yy=rnd(mapheight)
until maze(xx,yy).visited=1
startx=xx:starty=yy
 
endif
density=0
 
for a=0 to mapwidth
for b=0 to mapheight
   if maze(a,b).visited=1 then inc density
   line maze(a,b).xpos*10,maze(a,b).ypos*10,maze(a,b).xconnect*10,maze(a,b).yconnect*10
next b
next a
if density>=map_density  then map_complete=1
sync
until map_complete=1
 
for a = 1 to rooms
   repeat
      repeat
      xx=rnd(mapwidth):yy=rnd(mapheight)
      until xx>=1 and xx<=mapwidth-1 and yy>=1 and yy<=mapheight-1
   until maze(xx,yy).visited=1 and maze(xx,yy).room=0 and maze(xx-1,yy).room=0 and maze(xx+1,yy).room=0 and maze(xx,yy+1).room=0 and maze(xx,yy-1).room=0
   maze(xx,yy).room=1
next a
 
 
do
cls
for a=0 to mapwidth
for b=0 to mapheight
   ink rgb(255,255,255),0
   line maze(a,b).xpos*10,maze(a,b).ypos*10,maze(a,b).xconnect*10,maze(a,b).yconnect*10
 
next b
next a
 
for a = 0 to mapwidth:for b = 0 to mapheight
if maze(a,b).room=1
      ink rgb(255,0,0),0
      box a*10-5,b*10-5,a*10+5,b*10+5
   endif
next b:next a
text 400,400,"DONE"
sync
loop