==============================================================
   =  Title  : Super basic maze-like room generator
   =  Author : Latch
   =  Date   : 04/25/2008
   =  Update :
   =  Version: .02
   ==============================================================
   Comments    This is a maze generator that uses a one walled
               mesh, creates multiple copies of it based on a
               grid (width x depth), rotates rooms randomly at
               90 degrees increments, and places them.  It creates
               a mazey structure with very little code.
   ==============================================================
remend
 
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
   autocam off
   set display mode 800,600,32
   sync on
   sync rate 60
   randomize timer()
   hide mouse
 
rem =============================================================
rem = MAIN
rem =============================================================
_main:
   gosub _init
   position camera object size x(1)/2,0,object size z(1)/2
   do
      gosub _move_camera
 
      sync
   loop
 
   end
 
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
   _init:
      ink rgb(255,255,255),0
      grey=11
      grey2=12
      gosub _grey
      gosub _grey2
      cls 0
      input "Maze Width ? :";wd
      input "Maze Depth ? :";dp
      Input "Wall x Size# ? :";sizex#
      input "Wall y Hieght# ? :";sizey#
      Print "GENERATING MAZE"
      wait 100
      sync
      one_wall(1,sizex#,sizey#)
      random_rooms(wd,dp,1)
 
      texture object 1,grey2
      set object collision to polygons 1
      set object collision on 1
 
      make object box 2,(sizex#*(wd+1)),sizey#,(sizex#*(dp+1))
      position object 2,(object size x(2)/2)-sizex#,0,(object size z(2)/2)-sizex#
      set object 2,1,1,0
      texture object 2,grey
      set object collision off 2
      offx#=object size x(2)/2
      offz#=object size z(2)/2
 
      make object cube 3,10
      set object collision to boxes 3
      set object collision on 3
      hide object 3
 
      speed#=sizex#/50
 
   return
`----------------------------------------------------------------
   _move_camera:
      oldcamx#=newx#
      oldcamz#=newz#
      newx#=newxvalue(camera position x(),camera angle y(),(upkey()-downkey())*speed#)
      newz#=newzvalue(camera position z(),camera angle y(),(upkey()-downkey())*speed#)
      position camera newx#,0,newz#
 
      rem check collision with maze walls
      bang=object collision(3,0)
      if bang > 0
         if upkey() then drtn#=-1*speed#
         if downkey() then drtn#=speed#
         newx#=newxvalue(camera position x(),camera angle y(),drtn#)
         newz#=newzvalue(camera position z(),camera angle y(),drtn#)
         position camera newx#,0,newz#
      endif
 
      rem check camera leaving box
      x1#=object position x(2)-offx#
      x2#=object position x(2)+offx#
      z1#=object position z(2)-offz#
      z2#=object position z(2)+offz#
      if newx# <= x1# or newx# >= x2#
         if upkey() then drtn#=-1*speed#
         if downkey() then drtn#=speed#
         newx#=newxvalue(camera position x(),camera angle y(),drtn#)
         `newz#=newzvalue(camera position z(),camera angle y(),drtn#)
         position camera newx#,0,newz#
      endif
      if newz# <= z1# or newz# >= z2#
         if upkey() then drtn#=-1*speed#
         if downkey() then drtn#=speed#
         `newx#=newxvalue(camera position x(),camera angle y(),drtn#)
         newz#=newzvalue(camera position z(),camera angle y(),drtn#)
         position camera newx#,0,newz#
      endif
 
      yrotate camera wrapvalue(camera angle y()+((rightkey()-leftkey())*2))
      position object 3,camera position x(),camera position y(),camera position z()
 
      text 0,20,str$(bang)
   return
`----------------------------------------------------------------
   _grey:
      cls 0
      dot 0,1
      get image grey,0,0,2,2
      sync
   return
`----------------------------------------------------------------
   _grey2:
      cls 0
      dot 0,0
      get image grey2,0,0,1,2
      sync
   return
rem =============================================================
rem = FUNCTIONS
rem =============================================================
   function one_wall(obj,sizex#,sizey#)
      make object plain obj,sizex#,sizey#
      offset limb obj,0,0,0,sizex#/2.0
      make mesh from object obj,obj
      delete object obj
      make object obj,obj,0
   endfunction
`----------------------------------------------------------------
   function random_rooms(wd,dp,obj)
      size#=object size x(obj)
      make mesh from object obj,obj
      lmb=0
      for x=0 to wd-1
         for z=0 to dp-1
            inc lmb
            add limb obj,lmb,obj
            offset limb obj,lmb,x*size#,0,z*size#
            rotate limb obj,lmb,0,rnd(3)*90,0
         next z
      next x
      make mesh from object obj,obj
      delete object obj
      make object obj,obj,0
      set object obj,1,1,0
   endfunction