duck

duck

Oct 10th, 2009
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1.  
  2. if levelNum>0 then
  3. mazeWidth = 3+levelNum
  4. mazeHeight = 3+levelNum
  5. else
  6. mazeWidth = 2
  7. mazeHeight = 1
  8. end if
  9.  
  10.  
  11.  
  12. -- build maze cell array
  13. maze = []
  14. cellNum = 0
  15.  
  16. repeat with y = 1 to mazeHeight
  17. mazeLine = []
  18. repeat with x = 1 to mazeWidth
  19. cellNum = cellNum + 1
  20. cellBoundaries = [#n:0,#s:0,#e:0,#w:0]
  21.  
  22. mazeLine.add([#num:cellNum,#x:x,#y:y,#links:[],#neighbours:[],#boundaries:cellBoundaries])
  23.  
  24. end repeat
  25. maze.add(mazeLine)
  26. end repeat
  27. totalCells = cellNum
  28.  
  29.  
  30. -- define Neighbours for each cell
  31. repeat with y = 1 to mazeHeight
  32. repeat with x = 1 to mazeWidth
  33. cell = maze[y][x]
  34.  
  35. if x>1 then cell.neighbours.add(maze[y][x-1])
  36. if y>1 then cell.neighbours.add(maze[y-1][x])
  37. if x<mazeWidth then cell.neighbours.add(maze[y][x+1])
  38. if y<mazeHeight then cell.neighbours.add(maze[y+1][x])
  39.  
  40. end repeat
  41. end repeat
  42.  
  43.  
  44.  
  45. cellStack = []
  46.  
  47. finished = false
  48. visitedCells = 0
  49.  
  50. currentCell = maze[random(mazeHeight)][random(mazeWidth)]
  51.  
  52. repeat while (not finished)
  53.  
  54. --find all Neighbours of CurrentCell with all walls intact
  55. untouchedNeighbours = []
  56. repeat with neighbour in currentCell.neighbours
  57. if neighbour.links.count = 0 then
  58. untouchedNeighbours.add(neighbour)
  59. end if
  60. end repeat
  61.  
  62. --if one or more found
  63. if (untouchedNeighbours.count>0) then
  64. --choose one at random
  65. neighbour = untouchedNeighbours[random(untouchedNeighbours.count)]
  66.  
  67. --knock down the wall between it and CurrentCell
  68. neighbour.links.add(currentCell)
  69. currentCell.links.add(neighbour)
  70.  
  71. --push CurrentCell location on the CellStack
  72. cellStack.add(currentCell)
  73.  
  74. --the neigbour is now the current cell
  75. currentCell = neighbour
  76.  
  77. -- add 1 to VisitedCells
  78. visitedCells = visitedCells + 1
  79.  
  80. oput("[generate maze] "&visitedCells&"/"&totalCells)
  81.  
  82.  
  83. else
  84.  
  85. --pop the most recent cell entry off the CellStack
  86. --make it CurrentCell
  87.  
  88. if cellStack.count > 0 then
  89. currentCell = cellstack[cellStack.count]
  90. cellstack.deleteAt(cellStack.count)
  91. else
  92. finished = true
  93. end if
  94.  
  95.  
  96. end if
  97.  
  98. end repeat
  99.  
  100.  
  101.  
  102. -- extra links:
  103.  
  104. numExtraLinks = integer(mazeHeight*mazeWidth*(0.4-(levelnum/30.0)))
  105. repeat with extraLinkNum = 1 to numExtraLinks
  106.  
  107. currentCell = maze[random(mazeHeight)][random(mazeWidth)]
  108.  
  109. --find all Neighbours of CurrentCell which do not yet link to current cell
  110. unlinkedNeighbours = []
  111.  
  112. repeat with neighbour in currentCell.neighbours
  113. alreadyLinked = false
  114. repeat with neighbourLink in neighbour.links
  115. if neighbourLink.num = currentCell.num then
  116. alreadyLinked = true
  117. end if
  118. end repeat
  119. if not alreadyLinked then
  120. unlinkedNeighbours.add(neighbour)
  121. end if
  122. end repeat
  123.  
  124.  
  125.  
  126.  
  127. --if one or more found
  128. if (unlinkedNeighbours.count>0) then
  129.  
  130. --choose one at random
  131. pickNum = random(unlinkedNeighbours.count)
  132. neighbour = unlinkedNeighbours[pickNum]
  133.  
  134. --knock down the wall between it and CurrentCell
  135. neighbour.links.add(currentCell)
  136. currentCell.links.add(neighbour)
  137.  
  138. else
  139.  
  140. -- all neighbour cells link to this cell
  141. extraLinkNum = extraLinkNum - 1
  142.  
  143. end if
  144.  
  145. end repeat
  146.  
Advertisement
Add Comment
Please, Sign In to add comment