Advertisement
Guest User

ComputerCraft Maze Generator

a guest
Dec 28th, 2012
1,772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. --Creates a square maze with specified side length-
  2. args = {...}
  3. if #args < 1 then
  4.   print("Usage: maze <size>")
  5.   return
  6. end
  7. print("Place torches in slot 1 and fuel in slot 16, then press enter.")
  8. read()
  9. turtle.select(16)
  10. size = tonumber(args[1])
  11. maze = {}
  12. for x = 1,size do
  13.   maze[x] = {}
  14.   for y = 1,size do
  15.     maze[x][y] = false
  16.   end
  17. end
  18. mx = 1 --X-coordinate
  19. my = 1 --Y-coordinate
  20. md = 1 --Direction
  21. maze[1][1] = true
  22. -------------------------
  23. -------------------------
  24. function isOpen(d)
  25.   x = mx + dx(d)
  26.   y = my + dy(d)
  27.   if x < 1 or y < 1 or x > size or y > size then
  28.     return false
  29.   end  
  30.   m = maze[x][y]
  31.   return not m
  32. end  
  33. -------------------------
  34. function dx(d)
  35.   if d == 2 then
  36.     return 1
  37.   elseif d == 4 then
  38.     return -1
  39.   end
  40.   return 0
  41. end
  42. -------------------------
  43. function dy(d)
  44.   if d == 1 then
  45.     return 1
  46.   elseif d == 3 then
  47.     return -1
  48.   end
  49.   return 0
  50. end
  51. -------------------------
  52. function face(d)
  53.   if d > 4 then d = d - 4 end
  54.   if math.abs(md - d) == 1 then
  55.     if md > d then
  56.       turnLeft()
  57.     else
  58.       turnRight()
  59.     end
  60.   elseif math.abs(md - d) == 2 then
  61.     turnRight()
  62.     turnRight()
  63.   elseif math.abs(md - d) == 3 then
  64.     if md > d then
  65.       turnRight()
  66.     else
  67.       turnLeft()
  68.     end
  69.   end
  70.   md = d
  71. end
  72. -------------------------
  73. function turnLeft()
  74.   md = md - 1
  75.   if md == 0 then md = 4 end
  76.   turtle.turnLeft()
  77. end
  78. function turnRight()
  79.   md = md + 1
  80.   if md == 5 then md = 1 end
  81.   turtle.turnRight()
  82. end
  83. -------------------------
  84. torches = 0
  85. function carve(t)
  86.   for i = 1,2 do
  87.     if turtle.getFuelLevel() == 0 then
  88.       if not turtle.refuel(1) then
  89.         shell.run("reboot")
  90.       end
  91.     end
  92.     while not turtle.forward() do
  93.       turtle.dig()
  94.     end
  95.     if t then turtle.digDown() end
  96.   end
  97.   mx = mx + dx(md)
  98.   my = my + dy(md)
  99.   maze[mx][my] = true
  100.   if t then
  101.     torches = torches + 1
  102.     if torches == 5 then
  103.       turtle.select(1)
  104.       torches = 0
  105.       turtle.placeDown()
  106.       turtle.select(16)
  107.     end
  108.   end
  109. end
  110. -------------------------
  111. --Fisher-Yates shuffle---
  112. function shuffle(t)
  113.   local n = #t
  114.   while n >= 2 do
  115.     local k = math.random(n)
  116.     t[n], t[k] = t[k], t[n]
  117.     n = n - 1
  118.   end
  119.   return t
  120. end
  121. -------------------------
  122. function main()
  123.   local last = 0
  124.   local dirs = {1,2,3,4}
  125.   dirs = shuffle(dirs)
  126.   last = md
  127.   for k,v in ipairs(dirs) do
  128.     if isOpen(v) then
  129.       face(v)
  130.       carve(true)
  131.       main()
  132.     end
  133.   end
  134.   face(last + 2)
  135.   carve(false)
  136. end
  137. -------------------------
  138. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement