Advertisement
Houshalter

Remove all blocks ComputerCraft Script

Dec 18th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. blocks = {
  2.     'redwood'
  3. }
  4. x = 0
  5. y = 0
  6. z = 0
  7. xv = 1
  8. yv = 0
  9. todo = {{0,0,0}}
  10. nodes = {['0,0,0']=1}
  11. explored = {}
  12. function digForward()
  13.     while not turtle.forward() do
  14.         turtle.dig()
  15.     end
  16.     x = x+xv
  17.     y = y +yv
  18. end
  19.  
  20. function digDown()
  21.     while not turtle.down() do
  22.         turtle.digDown()
  23.     end
  24.     z = z - 1
  25. end
  26.  
  27. function digUp()
  28.     while not turtle.up() do
  29.         turtle.digUp()
  30.     end
  31.     z = z + 1
  32. end
  33.  
  34. function turnRight()
  35.     turtle.turnRight()
  36.     if xv == 1 then
  37.         xv, yv = 0, 1
  38.     elseif xv == -1 then
  39.         xv, yv = 0, -1
  40.     elseif yv == 1 then
  41.         yv, xv = 0, -1
  42.     elseif yv == -1 then
  43.         yv, xv = 0, 1
  44.     end
  45. end
  46.  
  47. function check(name)
  48.     local a = false
  49.     for k, pat in pairs(blocks) do
  50.         a = a or name:find(pat)
  51.     end
  52.     return a
  53. end
  54.  
  55. function newBlock(a, block, x,y,z)
  56.     if a and check(block.name) then
  57.         local newPoint = string.format('%i,%i,%i',x,y,z)
  58.         if nodes[newPoint] then
  59.             print('n: '..nodes[newPoint])
  60.             todo[nodes[newPoint]] = nil
  61.         end
  62.         table.insert(todo, {x,y,z})
  63.         print(#todo)
  64.         nodes[newPoint] = #todo
  65.     end
  66. end
  67.  
  68. function newNode()
  69.     local point = string.format('%i,%i,%i',x,y,z)
  70.     if nodes[point] then
  71.         todo[nodes[point]] = nil
  72.     end
  73.     explored[point] = true
  74.     for i = 1, 4 do
  75.         turnRight()
  76.         local a, block = turtle.inspect()
  77.         newBlock(a, block, x+xv,y+yv,z)
  78.     end
  79.     local a, block = turtle.inspectUp()
  80.     newBlock(a, block, x,y,z+1)
  81.     local a, block = turtle.inspectDown()
  82.     newBlock(a, block, x,y,z-1)
  83.     --goto first node on todo list
  84. end
  85.  
  86. function sign(n)
  87.     return (n > 0) and 1 or  -1
  88. end
  89.  
  90. function goto(gx,gy,gz)
  91.     --print(string.format('cords: %i, %i, %i', x, y, z))
  92.     --print(string.format('goals: %i, %i, %i', gx, gy, gz))
  93.     --print(string.format('velocity: %i, %i', xv, yv))
  94.     --print('Todo: '..tostring(#todo))
  95.     if gx ~= x then
  96.         while sign(gx-x) ~= xv do
  97.             turnRight()
  98.         end
  99.         while x ~= gx do
  100.             digForward()
  101.             --newNode()
  102.         end
  103.     elseif gy ~= y then
  104.         while sign(gy-y) ~= yv do
  105.             turnRight()
  106.         end
  107.         --while y ~= gy do
  108.             digForward()
  109.             --newNode()
  110.         --end
  111.     elseif z ~= gz then
  112.         if gz > z then
  113.             digUp()
  114.             --newNode()
  115.         else
  116.             digDown()
  117.         end
  118.     else return end
  119.     newNode()
  120.     goto(gx,gy,gz)
  121. end
  122.  
  123. newNode()
  124. while #todo > 0 do
  125.     if not todo[#todo] then table.remove(todo, #todo) print "table error" end
  126.     goto(unpack(todo[#todo]))
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement