Advertisement
DropSquad

Untitled

Nov 21st, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. --
  2. -- @ JimmyChance
  3. --
  4.  
  5. local maze_size = {20, 20, 10} -- square maze
  6. local cell_size = 20
  7.  
  8. cellStack = {}
  9. rService = game:GetService("RunService")
  10. directions = {
  11. ["0, 1"] = 1,
  12. ["0, -1"] = 3,
  13. ["1, 0"] = 2,
  14. ["-1, 0"] = 4,
  15. } -- L,R,D,U
  16. -- W: LDRU
  17. map = {}
  18. count = 0
  19. maze = Instance.new("Model", workspace)
  20. maze.Name = "Maze"
  21.  
  22. function makeBrick(c, h, s)
  23. local walls = c[3]
  24. local ch = Instance.new("Model", maze)
  25. ch.Name = "Cell"
  26. local cellpos = c[2]
  27. local p = Instance.new("Part", ch)
  28. p.Anchored = true
  29. p.Name = "Base"
  30. p.CFrame = CFrame.new(cellpos) * CFrame.new(0, -h/2, 0)
  31. p.Size = Vector3.new(cell_size, 1, cell_size)
  32. p.FormFactor = "Custom"
  33. for i = 1, 4 do
  34. local sb = Instance.new("Part", ch)
  35. sb.Anchored = true
  36. sb.FormFactor = "Custom"
  37. sb.Name = "Support Beam"
  38. sb.Size = Vector3.new(1, h, 1)
  39. local basepos = p.Position
  40. local sX = p.Size.X/2 - 0.5
  41. local sZ = p.Size.Z/2 - 0.5
  42. if i == 1 then
  43. sb.Position = basepos + Vector3.new(sX, h/2, sZ)
  44. elseif i == 2 then
  45. sb.Position = cellpos - Vector3.new(sX, 0, sZ)
  46. elseif i == 3 then
  47. sb.Position = cellpos + Vector3.new(sX, 0, -sZ)
  48. elseif i == 4 then
  49. sb.Position = cellpos - Vector3.new(sX, 0, -sZ)
  50. end
  51. end
  52. for i = 1, 4 do
  53. if walls[i] then
  54. local part = Instance.new("Part", ch)
  55. part.Name = "Wall"
  56. part.Anchored = true
  57. part.FormFactor = "Custom"
  58. local basepos = p.Position
  59. local sX = p.Size.X/2 - 0.5
  60. local sZ = p.Size.Z/2 - 0.5
  61. if i == 1 then
  62. part.Size = Vector3.new(cell_size - 2, h, 1)
  63. part.Position = cellpos + Vector3.new(0, 0, -sZ)
  64. elseif i == 2 then
  65. part.Size = Vector3.new(1, h, cell_size - 2)
  66. part.Position = cellpos + Vector3.new(-sX, 0, 0)
  67. elseif i == 3 then
  68. part.Size = Vector3.new(cell_size - 2, h, 1)
  69. part.Position = cellpos + Vector3.new(0, 0, sZ)
  70. elseif i == 4 then
  71. part.Size = Vector3.new(1, h, cell_size - 2)
  72. part.Position = cellpos + Vector3.new(sX, 0, 0)
  73. end
  74. end
  75. end
  76. end
  77.  
  78. function createPositions(startPos, cellSize, mazeSize)
  79. local pos = startPos
  80. local xSize = mazeSize[1]
  81. local zSize = mazeSize[2]
  82. local height = mazeSize[3]
  83. for x = 1, xSize do
  84. map[x] = {}
  85. for z = 1, zSize do
  86. local posY = (startPos + Vector3.new(0, height/2, 0)).Y
  87. local p = Vector3.new((x - 1) * cellSize + startPos.X, posY, (z - 1) * cellSize + startPos.Z)
  88. map[x][z] = {{x, z}, p, {true, true, true, true}, false}
  89. end
  90. end
  91. return map
  92. end
  93.  
  94. function getNearby(cell, tab)
  95. local v = Vector2.new(cell[1][1], cell[1][2])
  96. local n = {}
  97. for i = 1, #tab do
  98. for h = 1, #tab[i] do
  99. local current = tab[i][h]
  100. local v2 = Vector2.new(current[1][1], current[1][2])
  101. local l = (v - v2).magnitude
  102. if l == 1 and not current[4] then
  103. local dir = (v - v2).unit
  104. local num = directions[tostring(dir)]
  105. n[#n + 1] = {current, num}
  106. end
  107. end
  108. end
  109. return n
  110. end
  111.  
  112. function generateMaze(exitEnabled)
  113. local m = createPositions(Vector3.new(0, 0, 0), cell_size, maze_size)
  114. local xSize = maze_size[1]
  115. local zSize = maze_size[2]
  116. if exitEnabled[1] then
  117. local choice = exitEnabled[2]
  118. if choice == 0 then
  119. choice = math.random(zSize)
  120. end
  121. m[1][choice][3][2] = false
  122. choice = exitEnabled[3]
  123. if choice == 0 then
  124. choice = math.random(zSize)
  125. end
  126. m[xSize][choice][3][4] = false
  127. end
  128.  
  129. cellStack[1] = {m[1][1], nil}
  130. repeat rService.RenderStepped:wait()
  131. local ncells = getNearby(cellStack[#cellStack][1], m)
  132. if #ncells > 0 then
  133. local ccell = ncells[math.random(#ncells)]
  134. local d = ccell[2]
  135. local q = d + 2
  136. if q > 4 then
  137. q = q - 4
  138. end
  139. ccell[1][4] = true
  140. ccell[1][3][q] = false
  141. cellStack[#cellStack + 1] = ccell
  142. local old = cellStack[#cellStack - 1]
  143. old[1][3][d] = false
  144. count = count + 1
  145. else
  146. table.remove(cellStack, #cellStack)
  147. end
  148.  
  149. print(count)
  150.  
  151. until count == xSize * zSize
  152. for x = 1, xSize do
  153. if m[x] then
  154. for z = 1, zSize do
  155. rService.RenderStepped:wait()
  156. makeBrick(m[x][z], maze_size[3], cell_size)
  157. end
  158. end
  159. end
  160. end
  161.  
  162. wait(4)
  163. generateMaze({true, 0, 0})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement