Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if levelNum>0 then
- mazeWidth = 3+levelNum
- mazeHeight = 3+levelNum
- else
- mazeWidth = 2
- mazeHeight = 1
- end if
- -- build maze cell array
- maze = []
- cellNum = 0
- repeat with y = 1 to mazeHeight
- mazeLine = []
- repeat with x = 1 to mazeWidth
- cellNum = cellNum + 1
- cellBoundaries = [#n:0,#s:0,#e:0,#w:0]
- mazeLine.add([#num:cellNum,#x:x,#y:y,#links:[],#neighbours:[],#boundaries:cellBoundaries])
- end repeat
- maze.add(mazeLine)
- end repeat
- totalCells = cellNum
- -- define Neighbours for each cell
- repeat with y = 1 to mazeHeight
- repeat with x = 1 to mazeWidth
- cell = maze[y][x]
- if x>1 then cell.neighbours.add(maze[y][x-1])
- if y>1 then cell.neighbours.add(maze[y-1][x])
- if x<mazeWidth then cell.neighbours.add(maze[y][x+1])
- if y<mazeHeight then cell.neighbours.add(maze[y+1][x])
- end repeat
- end repeat
- cellStack = []
- finished = false
- visitedCells = 0
- currentCell = maze[random(mazeHeight)][random(mazeWidth)]
- repeat while (not finished)
- --find all Neighbours of CurrentCell with all walls intact
- untouchedNeighbours = []
- repeat with neighbour in currentCell.neighbours
- if neighbour.links.count = 0 then
- untouchedNeighbours.add(neighbour)
- end if
- end repeat
- --if one or more found
- if (untouchedNeighbours.count>0) then
- --choose one at random
- neighbour = untouchedNeighbours[random(untouchedNeighbours.count)]
- --knock down the wall between it and CurrentCell
- neighbour.links.add(currentCell)
- currentCell.links.add(neighbour)
- --push CurrentCell location on the CellStack
- cellStack.add(currentCell)
- --the neigbour is now the current cell
- currentCell = neighbour
- -- add 1 to VisitedCells
- visitedCells = visitedCells + 1
- oput("[generate maze] "&visitedCells&"/"&totalCells)
- else
- --pop the most recent cell entry off the CellStack
- --make it CurrentCell
- if cellStack.count > 0 then
- currentCell = cellstack[cellStack.count]
- cellstack.deleteAt(cellStack.count)
- else
- finished = true
- end if
- end if
- end repeat
- -- extra links:
- numExtraLinks = integer(mazeHeight*mazeWidth*(0.4-(levelnum/30.0)))
- repeat with extraLinkNum = 1 to numExtraLinks
- currentCell = maze[random(mazeHeight)][random(mazeWidth)]
- --find all Neighbours of CurrentCell which do not yet link to current cell
- unlinkedNeighbours = []
- repeat with neighbour in currentCell.neighbours
- alreadyLinked = false
- repeat with neighbourLink in neighbour.links
- if neighbourLink.num = currentCell.num then
- alreadyLinked = true
- end if
- end repeat
- if not alreadyLinked then
- unlinkedNeighbours.add(neighbour)
- end if
- end repeat
- --if one or more found
- if (unlinkedNeighbours.count>0) then
- --choose one at random
- pickNum = random(unlinkedNeighbours.count)
- neighbour = unlinkedNeighbours[pickNum]
- --knock down the wall between it and CurrentCell
- neighbour.links.add(currentCell)
- currentCell.links.add(neighbour)
- else
- -- all neighbour cells link to this cell
- extraLinkNum = extraLinkNum - 1
- end if
- end repeat
Advertisement
Add Comment
Please, Sign In to add comment