MazeGenerator too slow

Blitz3D Forums/Blitz3D Beginners Area/MazeGenerator too slow

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Super!! Now add a imagebuffer to draw the maze on, save the image buffer as a bmp file, load the bmp file as a terrain, texture it, scale it, light it and then you have a fast dungeon for a game----(if you add in characters, monsters, treasures etc.... Nice Work!
Please note that you will have to use your own images for the textures. The code shows my bmp,png,jpg directories and file names!

Dim horizwalls(0, 0)
Dim vertwalls(0, 0)
Dim walkable(0, 0)

Graphics 1024,768,0,2
SetBuffer BackBuffer()
Global mazebuf=CreateImage(512,512) 

MazeWidth% = Input("How wide do you want the maze to be? ")
MazeHeight% = Input("How high do you want the maze to be? ")
Cls

StartTime% = MilliSecs()
GenerateMaze(MazeWidth%, MazeHeight%)
StopTime% = MilliSecs()

DrawMaze(MazeWidth%, MazeHeight%, StopTime% - StartTime%)
;--------------------------------------------------------------
Cls

Graphics3D 1024,768,16
SetBuffer BackBuffer()
light=CreateLight()
PositionEntity light,100,100,100

camera=CreateCamera()
PositionEntity camera,100,4,120
CameraRange camera,.1,800 

mazio=LoadTerrain("bmp/mazo1.bmp")
maziotex=LoadTexture("jpg/woodpanel.jpg")
ScaleTexture maziotex,4,4
TerrainDetail mazio,3000,True 

EntityTexture mazio,maziotex
ScaleTexture maziotex,4,4
ScaleEntity mazio,1,6,1
sky = CreateSphere(36)
	FlipMesh sky
	ScaleEntity sky,512,300,512
	PositionEntity sky,256,0,256
	sky_tex=LoadTexture("png/cloud256.png")
	ScaleTexture sky_tex,1,1
	EntityTexture sky,sky_tex
    EntityOrder sky,1

While Not KeyDown(1)
If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0 
If KeyDown( 203 )=True Then TurnEntity camera,0,1,0 
If KeyDown( 208 )=True Then MoveEntity camera,0,0,-.5
If KeyDown( 200 )=True Then MoveEntity camera,0,0,.5

UpdateWorld
RenderWorld
Flip

Wend

;0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-

Function GenerateMaze(width%, height%)
	; This function generates the entire maze

	; Redim arrays to hold all required data
    Dim walkable(width%, height%)
	Dim horizwalls(width%, height%)
	Dim vertwalls(width%, height%)

	; Declare some local variables
    Local wentleft = False
    Local wentright = False
    Local wentup = False
    Local wenddown = False
    Local roomcount = False

	; Randomize Rnd-function
	SeedRnd MilliSecs()

	; Fill entire arrays with "True"
    For x = 1 To width%
        For y = 1 To height%
            horizwalls(x, y) = True
            vertwalls(x, y) = True
        Next
    Next

	; Start generating the maze at a random point in the grid (between 1 and 20 in both directions)
    xx = Int(Rnd(1, width%))
    yy = Int(Rnd(1, height%))

	; Set current tile as "walkable"
    walkable(xx, yy) = True

	; Set some other vars
    wentleft = False
    wentright = False
    wentup = False
    wentdown = False

	; Count rooms (number of tiles already created)
    roomcount = 1

    Repeat
		; Choose any direction to go next
        Direction = Int(Rnd(1, 4))

        If wentup And wentdown And wentright And wentleft Then
			Repeat
                xx = Int(Rnd(1, width))
                yy = Int(Rnd(1, height))
            Until walkable(xx, yy) = True
        End If

		; Store current xx and yy positions
        oxx = xx
        oyy = yy

        Select Direction
            Case 1 ; up
                yy = yy - 1

                If yy < 1 Then yy = 1

                If walkable(xx, yy) = False Then
                    roomcount = roomcount + 1
                    walkable(xx, yy) = True
                    horizwalls(xx, yy) = False
                    wentup = True
                Else
                    yy = oyy
                End If

            Case 2 ; left
                xx = xx - 1

                If xx < 1 Then xx = 1

                If walkable(xx, yy) = False Then
                    roomcount = roomcount + 1
                    walkable(xx, yy) = True
                    vertwalls(xx, yy) = False
                    wentleft = True
                Else
                    xx = oxx
                End If

            Case 3 ; down
                yy = yy + 1

                If yy > height Then yy = height

                If walkable(xx, yy) = False Then
                    roomcount = roomcount + 1
                    walkable(xx, yy) = True
                    horizwalls(oxx, oyy) = False
                    wentdown = True
                Else
                    yy = oyy
                End If

            Case 4 ; right
                xx = xx + 1

                If xx > width Then xx = width

                If walkable(xx, yy) = False Then
                    roomcount = roomcount + 1
                    walkable(xx, yy) = True
                    vertwalls(oxx, oyy) = False
                    wentright = True
                Else
                    xx = oxx
                End If
        End Select
		
	Until roomcount = width * height
End Function


Function DrawMaze(width%, height%, TimeNeeded%)
	Cls
    SetBuffer ImageBuffer(mazebuf) 

	Color 255, 255, 255
	

	Line 50, 50, 50, 50 + (height% * 10)
	Line 50, 50, 50 + (width% * 10), 50

	For y = 1 To height%
		For x = 1 To width%
			If horizwalls(x, y) Then
				Line (x * 10) + 40, (y * 10) + 50, (x * 10) + 50, (y * 10) + 50
			EndIf
		Next
	Next

	For y = 1 To height%
		For x = 1 To width%
			If vertwalls(x, y) Then
				Line (x * 10) + 50, (y * 10) + 40, (x * 10) + 50, (y * 10) + 50
			EndIf
		Next
	Next

	Flip
	SaveBuffer(ImageBuffer(mazebuf),"bmp/mazo1.bmp") 
;  press any key to exit this function	 
WaitKey()

End Function