JPiolho

[Minecraft][CC] Hill Eater

Mar 1st, 2012
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. -- Hill Eater
  2. -- Version 1.1
  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 curX = 0
  8. local curY = 0
  9. local curZ = 0
  10. local currentFacing = 1
  11.  
  12. local visited = {}
  13. local stack = {}
  14.  
  15.  
  16. local function AddVisited(x,y,z)
  17.     table.insert(visited,{x,y,z});
  18. end
  19.  
  20. local function AddStack(x,y,z)
  21.     table.insert(stack,{x,y,z})
  22. end
  23.  
  24. local function PopStack()
  25.     local val = stack[#stack]
  26.     table.remove(stack,#stack);
  27.  
  28.     return val[1],val[2],val[3]
  29. end
  30.  
  31. local function IsVisited(x,y,z)
  32.  
  33.     if z < 0 then return true end
  34.  
  35.     local v
  36.  
  37.     for _,v in pairs(visited) do
  38.         if v[1] == x and v[2] == y and v[3] == z then
  39.             return true
  40.         end
  41.     end
  42.  
  43.     return false
  44. end
  45.  
  46. local function FaceDirection(dir)
  47.     local i
  48.  
  49.     if dir == 1 and currentFacing == 4 then
  50.         turtle.turnRight()
  51.         currentFacing = 1
  52.     end
  53.  
  54.     if dir == 4 and currentFacing == 1 then
  55.         turtle.turnLeft()
  56.         currentFacing = 4
  57.     end
  58.  
  59.     if dir > currentFacing then
  60.         while currentFacing < dir do
  61.             turtle.turnRight()
  62.             currentFacing = currentFacing + 1
  63.         end
  64.     end
  65.  
  66.     if dir < currentFacing then
  67.         while currentFacing > dir do
  68.             turtle.turnLeft()
  69.             currentFacing = currentFacing - 1
  70.         end
  71.     end
  72.  
  73. end
  74.  
  75. local hasMoved = false
  76. local function MoveToCell(x,y,z,mustdig)
  77.  hasMoved = false
  78.  
  79.  if curX == x and curY == y and curZ == z then
  80.   hasMoved = true
  81.   return
  82.  end
  83.  
  84.     if x > curX then
  85.         FaceDirection(2)
  86.     elseif x < curX then
  87.         FaceDirection(4)
  88.     elseif y > curY then
  89.         FaceDirection(1)
  90.     elseif y < curY then
  91.         FaceDirection(3)
  92.     end
  93.  
  94.     if z < curZ then
  95.         if turtle.digDown() or mustdig == false then
  96.          while turtle.down() == false do
  97.           sleep(0.5)
  98.           turtle.digDown()
  99.          end
  100.         else
  101.          if IsVisited(x,y,z) == false then AddVisited(x,y,z) end
  102.          return
  103.         end
  104.  
  105.     elseif z > curZ then
  106.         if turtle.digUp() or mustdig == false then
  107.          while turtle.up() == false do
  108.           sleep(0.5)
  109.           turtle.digUp()
  110.          end
  111.         else
  112.          if IsVisited(x,y,z) == false then AddVisited(x,y,z) end
  113.          return
  114.         end
  115.     else
  116.         if turtle.dig() or mustdig == false then
  117.          while turtle.forward() == false do
  118.           sleep(0.5)
  119.           turtle.dig()
  120.          end
  121.         else
  122.          if IsVisited(x,y,z) == false then AddVisited(x,y,z) end
  123.          return
  124.         end
  125.     end
  126.  
  127.     curX = x
  128.     curY = y
  129.     curZ = z
  130.  
  131.     hasMoved = true
  132. end
  133.  
  134.  
  135.  
  136. math.randomseed(os.time())
  137.  
  138. AddVisited(curX,curY,curZ)
  139. AddStack(curX,curY,curZ)
  140.  
  141. repeat
  142.     local valids = {}
  143.  
  144.  
  145.     if curZ > 0 then
  146.      if IsVisited(curX,curY,curZ-1) == false then
  147.       if turtle.detectDown() then table.insert(valids,{curX,curY,curZ-1}) else AddVisited(curX,curY,curZ-1) end
  148.      end
  149.     end
  150.  
  151.     if IsVisited(curX,curY,curZ+1) == false then
  152.      if turtle.detectUp() then table.insert(valids,{curX,curY,curZ+1}) else AddVisited(curX,curY,curZ+1) end
  153.     end
  154.  
  155.     if IsVisited(curX,curY+1,curZ) == false then
  156.      FaceDirection(1)
  157.      if turtle.detect() then table.insert(valids,{curX,curY+1,curZ}) else AddVisited(curX,curY+1,curZ) end
  158.     end
  159.  
  160.     if IsVisited(curX+1,curY,curZ) == false then
  161.      FaceDirection(2)
  162.      if turtle.detect() then table.insert(valids,{curX+1,curY,curZ}) else AddVisited(curX+1,curY,curZ) end
  163.     end
  164.  
  165.     if IsVisited(curX,curY-1,curZ) == false then
  166.      FaceDirection(3)
  167.      if turtle.detect() then table.insert(valids,{curX,curY-1,curZ}) else AddVisited(curX,curY-1,curZ) end
  168.     end
  169.  
  170.     if IsVisited(curX-1,curY,curZ) == false then
  171.      FaceDirection(4)
  172.      if turtle.detect() then table.insert(valids,{curX-1,curY,curZ}) else AddVisited(curX-1,curY,curZ) end
  173.     end
  174.  
  175.  
  176.     if #valids > 0 then
  177. AddStack(curX,curY,curZ)
  178.  
  179.         local targetX
  180.         local targetY
  181.         local targetZ
  182.  
  183.         local i = math.random(1,#valids)
  184.  
  185.         targetX = valids[i][1]
  186.         targetY = valids[i][2]
  187.         targetZ = valids[i][3]
  188.  
  189.         AddVisited(targetX,targetY,targetZ)
  190.  
  191.         MoveToCell(targetX,targetY,targetZ,true)
  192.  
  193.         if hasMoved then
  194.  
  195.         end
  196.     else
  197.         local targetX, targetY,targetZ
  198.         targetX, targetY,targetZ = PopStack()
  199.  
  200.  
  201.  
  202.         repeat
  203.          MoveToCell(targetX,targetY,targetZ,false)
  204.         until hasMoved == true
  205.  
  206.  
  207.     end
  208.  
  209.  
  210. until #stack == 0
Advertisement
Add Comment
Please, Sign In to add comment