JPiolho

[Minecraft][CC] Turtle Maze Generator

Feb 29th, 2012
3,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. -- Turtle Maze Generator
  2. -- Version 1.0
  3. -- By JPiolho
  4. -- Feel free to copy and edit as long you credit the original author
  5. -- More Info @ http://bit.ly/zoxn6V
  6.  
  7. local isDebug = false
  8.  
  9. local mazeWidth = 6
  10. local mazeHeight = 6
  11.  
  12. local curX = 1
  13. local curY = 1
  14. local currentFacing = 1
  15.  
  16. local visited = {}
  17. local stack = {}
  18.  
  19. local function FaceDirection(dir)
  20.     local i
  21.  
  22.     if dir == 1 and currentFacing == 4 then
  23.         turtle.turnRight()
  24.         currentFacing = 1
  25.     end
  26.  
  27.     if dir == 4 and currentFacing == 1 then
  28.         turtle.turnLeft()
  29.         currentFacing = 4
  30.     end
  31.  
  32.     if dir > currentFacing then
  33.         while currentFacing < dir do
  34.             turtle.turnRight()
  35.             currentFacing = currentFacing + 1
  36.         end
  37.     end
  38.  
  39.     if dir < currentFacing then
  40.         while currentFacing > dir do
  41.             turtle.turnLeft()
  42.             currentFacing = currentFacing - 1
  43.         end
  44.     end
  45.  
  46. end
  47.  
  48. local function MoveToCell(x,y)
  49.     if isDebug == true then
  50.         print("Moving [" .. curX .. "," .. curY .. "] to [" .. x .. "," .. y .. "]")
  51.         sleep(2)
  52.     end
  53.  
  54.     if x > curX then
  55.         FaceDirection(2)
  56.     elseif x < curX then
  57.         FaceDirection(4)
  58.     elseif y > curY then
  59.         FaceDirection(1)
  60.     elseif y < curY then
  61.         FaceDirection(3)
  62.     end
  63.  
  64.     turtle.dig()
  65.     --turtle.digUp()
  66.     --turtle.digDown()
  67.     turtle.forward()
  68.     turtle.dig()
  69.     --turtle.digUp()
  70.     --turtle.digDown()
  71.     turtle.forward()
  72.  
  73.  
  74.  
  75.     curX = x
  76.     curY = y
  77. end
  78.  
  79.  
  80. local function IsVisited(x,y)
  81.  
  82.     if x <= 0 or y <= 0 or x > mazeWidth or y > mazeHeight then return true end
  83.  
  84.     local v
  85.  
  86.     for _,v in pairs(visited) do
  87.         if v[1] == x and v[2] == y then
  88.             return true
  89.         end
  90.     end
  91.  
  92.     return false
  93. end
  94.  
  95.  
  96. local function AddVisited(x,y)
  97.     table.insert(visited,{x,y});
  98. end
  99.  
  100. local function AddStack(x,y)
  101.     table.insert(stack,{x,y})
  102. end
  103.  
  104. local function PopStack()
  105.     local val = stack[#stack]
  106.     table.remove(stack,#stack);
  107.  
  108.     return val[1],val[2]
  109. end
  110.  
  111. math.randomseed(os.time())
  112.  
  113. AddVisited(curX,curY)
  114.  
  115.  
  116. local lastPercent
  117.  
  118. repeat
  119.     local possibleVisits = {}
  120.  
  121.     if IsVisited(curX-1,curY) == false then
  122.         table.insert(possibleVisits,{curX-1,curY})
  123.     end
  124.     if IsVisited(curX+1,curY) == false then
  125.         table.insert(possibleVisits,{curX+1,curY})
  126.     end
  127.     if IsVisited(curX,curY+1) == false then
  128.         table.insert(possibleVisits,{curX,curY+1})
  129.     end
  130.     if IsVisited(curX,curY-1) == false then
  131.         table.insert(possibleVisits,{curX,curY-1})
  132.     end
  133.  
  134.  
  135.  
  136.     if #possibleVisits > 0 then
  137.  
  138.         AddStack(curX,curY)
  139.  
  140.         local targetX
  141.         local targetY
  142.  
  143.         local i = math.random(1,#possibleVisits)
  144.  
  145.         targetX = possibleVisits[i][1]
  146.         targetY = possibleVisits[i][2]
  147.  
  148.         -- Remove the wall (lol)
  149.  
  150.  
  151.         AddVisited(targetX,targetY)
  152.  
  153.         MoveToCell(targetX,targetY)
  154.  
  155.     else
  156.         local targetX, targetY
  157.         targetX, targetY = PopStack()
  158.  
  159.         MoveToCell(targetX,targetY)
  160.     end
  161.  
  162.     local percent = math.floor((#visited / (mazeWidth * mazeHeight))*100)
  163.  
  164.     if percent ~= lastPercent then
  165.         term.clear()
  166.         term.setCursorPos(1,1)
  167.  
  168.         print("Progress: " .. percent .. "%")
  169.         lastPercent = percent
  170.     end
  171.  
  172. until #stack == 0
Advertisement
Add Comment
Please, Sign In to add comment