Advertisement
Treyzotic

maze

Mar 15th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. --
  2. -- @ JimmyChance
  3. --
  4. local playerName = "epiclightining"
  5. local maze_size = {10, 10, 10} -- square maze
  6. local cell_size = 10
  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.  
  20. function makeBrick(c, h, s)
  21. local walls = c[3]
  22. local ch = Instance.new("Model", maze)
  23. ch.Name = "Cell"
  24. local cellpos = c[2]
  25. local p = Instance.new("Part", ch)
  26. p.Anchored = true
  27. p.Name = "Base"
  28. p.CFrame = CFrame.new(cellpos) * CFrame.new(0, -h/2, 0)
  29. p.Size = Vector3.new(cell_size, 1, cell_size)
  30. p.FormFactor = "Custom"
  31. for i = 1, 4 do
  32. local sb = Instance.new("Part", ch)
  33. sb.Anchored = true
  34. sb.FormFactor = "Custom"
  35. sb.Name = "Support Beam"
  36. sb.Size = Vector3.new(1, h, 1)
  37. local basepos = p.Position
  38. local sX = p.Size.X/2 - 0.5
  39. local sZ = p.Size.Z/2 - 0.5
  40. if i == 1 then
  41. sb.Position = basepos + Vector3.new(sX, h/2, sZ)
  42. elseif i == 2 then
  43. sb.Position = cellpos - Vector3.new(sX, 0, sZ)
  44. elseif i == 3 then
  45. sb.Position = cellpos + Vector3.new(sX, 0, -sZ)
  46. elseif i == 4 then
  47. sb.Position = cellpos - Vector3.new(sX, 0, -sZ)
  48. end
  49. end
  50. for i = 1, 4 do
  51. if walls[i] then
  52. local part = Instance.new("Part", ch)
  53. part.Name = "Wall"
  54. part.Anchored = true
  55. part.FormFactor = "Custom"
  56. local basepos = p.Position
  57. local sX = p.Size.X/2 - 0.5
  58. local sZ = p.Size.Z/2 - 0.5
  59. if i == 1 then
  60. part.Size = Vector3.new(cell_size - 2, h, 1)
  61. part.Position = cellpos + Vector3.new(0, 0, -sZ)
  62. elseif i == 2 then
  63. part.Size = Vector3.new(1, h, cell_size - 2)
  64. part.Position = cellpos + Vector3.new(-sX, 0, 0)
  65. elseif i == 3 then
  66. part.Size = Vector3.new(cell_size - 2, h, 1)
  67. part.Position = cellpos + Vector3.new(0, 0, sZ)
  68. elseif i == 4 then
  69. part.Size = Vector3.new(1, h, cell_size - 2)
  70. part.Position = cellpos + Vector3.new(sX, 0, 0)
  71. end
  72. end
  73. end
  74. end
  75.  
  76. function createPositions(startPos, cellSize, mazeSize)
  77. local pos = startPos
  78. local xSize = mazeSize[1]
  79. local zSize = mazeSize[2]
  80. local height = mazeSize[3]
  81. for x = 1, xSize do
  82. map[x] = {}
  83. for z = 1, zSize do
  84. local posY = (startPos + Vector3.new(0, height/2, 0)).Y
  85. local p = Vector3.new((x - 1) * cellSize + startPos.X, posY, (z - 1) * cellSize + startPos.Z)
  86. map[x][z] = {{x, z}, p, {true, true, true, true}, false}
  87. end
  88. end
  89. return map
  90. end
  91.  
  92. function getNearby(cell, tab)
  93. local v = Vector2.new(cell[1][1], cell[1][2])
  94. local n = {}
  95. for i = 1, #tab do
  96. for h = 1, #tab[i] do
  97. local current = tab[i][h]
  98. local v2 = Vector2.new(current[1][1], current[1][2])
  99. local l = (v - v2).magnitude
  100. if l == 1 and not current[4] then
  101. local dir = (v - v2).unit
  102. local num = directions[tostring(dir)]
  103. n[#n + 1] = {current, num}
  104. end
  105. end
  106. end
  107. return n
  108. end
  109.  
  110. function generateMaze(exitEnabled)
  111. local m = createPositions(Vector3.new(0, 0, 0), cell_size, maze_size)
  112. local xSize = maze_size[1]
  113. local zSize = maze_size[2]
  114. if exitEnabled[1] then
  115. local choice = exitEnabled[2]
  116. if choice == 0 then
  117. choice = math.random(zSize)
  118. end
  119. m[1][choice][3][2] = false
  120. choice = exitEnabled[3]
  121. if choice == 0 then
  122. choice = math.random(zSize)
  123. end
  124. m[xSize][choice][3][4] = false
  125. end
  126.  
  127. cellStack[1] = {m[1][1], nil}
  128. while count < (xSize * zSize) do
  129. rService.RenderStepped:wait()
  130. local ncells = getNearby(cellStack[#cellStack][1], m)
  131. if #ncells > 0 then
  132. local ccell = ncells[math.random(#ncells)]
  133. local d = ccell[2]
  134. local q = d + 2
  135. if q > 4 then
  136. q = q - 4
  137. end
  138. ccell[1][4] = true
  139. ccell[1][3][q] = false
  140. cellStack[#cellStack + 1] = ccell
  141. local old = cellStack[#cellStack - 1]
  142. old[1][3][d] = false
  143. count = count + 1
  144. else
  145. table.remove(cellStack, #cellStack)
  146. end
  147. end
  148. for x = 1, xSize do
  149. if m[x] then
  150. for z = 1, zSize do
  151. rService.RenderStepped:wait()
  152. makeBrick(m[x][z], maze_size[3], cell_size)
  153. end
  154. end
  155. end
  156. end
  157.  
  158. local x;
  159. local z;
  160. local h;
  161. local c;
  162.  
  163. code = 0
  164. function onChat(p, message)
  165. local user = p
  166. print("Chat event", code)
  167. if message:sub(1, 4):lower() == "gen:" then -- {10, 10, 10, 10}
  168. code = 1
  169. elseif message:lower() ~= "clear" and code == 1 then
  170. x = tonumber(message)
  171. code = 2
  172. elseif message:lower() ~= "clear" and code == 2 then
  173. z = tonumber(message)
  174. code = 3
  175. elseif message:lower() ~= "clear" and code == 3 then
  176. h = tonumber(message)
  177. code = 4
  178. elseif message:lower() ~= "clear" and code == 4 then
  179. c = tonumber(message)
  180. code = 5
  181. elseif message:lower() == "clear" then
  182. code = 0
  183. end
  184. if code == 5 then
  185. if workspace:FindFirstChild("Maze") then
  186. workspace:FindFirstChild("Maze"):Destroy()
  187. end
  188. maze_size = {x, z, h}
  189. cell_size = c
  190. generateMaze({true, 0, 0})
  191. end
  192. end
  193.  
  194. wait(2)
  195. local player = game.Players[playerName]
  196. player.Character.Humanoid.WalkSpeed = 50
  197. player.Chatted:connect(function(msg) onChat(player, msg) end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement