nupanick

Nupanick's digTools v1.0 for computercraft

Dec 29th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. --digTools v1.0
  2. --by nupanick
  3.  
  4. digWait = .5 --for digging through gravel or sand
  5.  
  6. --digRectRoom
  7. --a function to help a computercraft turtle clear out rectangular rooms, something I seem to be doing way too much of.
  8. --the x, y, and z coordinates are relative, with the turtle's starting location at the origin, +x being right, +y being up, and +z being forward.
  9. --the method used is a recursive fill (like with the good old paint bucket) because there may be some blocks the turtle is unable to clear.
  10. --todo: automatic refueling
  11. --todo: remove redundant spinning, long return trip?
  12. --x's, y's, and z's: two points defining the corners of the box to be dug out.
  13. --fillslot: the slot used to fill in empty spaces at the edges of the box (to prevent monsters from wandering in from exposed caves).
  14. --fuelslot: the slot where the turtle will look for spare fuel if it runs low.
  15. function digRectRoom(x1, x2, y1, y2, z1, z2, fuelslot, fillslot, safeblocks)
  16.    
  17.  fill = fillslot
  18.  fuel = fuelslot
  19.  whitelist = safeblocks
  20.  
  21.     --blocks is a 3D array storing the status of the blocks to be dug.
  22.     --0 is the default, 1 means we've dug there successfully, -1 means that cell is undiggable for some reason.
  23.     blocks = {}
  24.     for x = x1, x2 do
  25.         blocks[x] = {}
  26.         for y = y1, y2 do
  27.             blocks[x][y] = {}
  28.             for z = z1, z2 do
  29.                 blocks[x][y][z] = 0
  30.             end
  31.         end
  32.     end --array initialization
  33.  
  34.     x = 0 --coordinates relative to the turtle. Note that the turtle is facing the +z direction.
  35.     y = 0
  36.     z = 0
  37.    
  38.     recursiveDig()
  39.  
  40. end --digRectRoom
  41.  
  42. function recursiveDig()
  43.     --recursively dig on all six adjacent blocks
  44.  
  45.     -- y+
  46.     if blocks[x][y+1] == nil then
  47.         turtle.select(fill)
  48.         turtle.placeUp()
  49.     elseif blocks[x][y+1][z] == 0 then
  50.         success = dig(1)
  51.         if success then
  52.             blocks[x][y+1][z] = 1
  53.             turtle.up()
  54.             y = y + 1
  55.  
  56.             recursiveDig()
  57.  
  58.             dig(-1)
  59.             turtle.down()
  60.             y = y - 1
  61.         else
  62.             blocks[x][y+1][z] = -1
  63.         end
  64.     end
  65.  
  66.     -- y-
  67.     if blocks[x][y-1] == nil then
  68.         turtle.select(fill)
  69.         turtle.placeDown()
  70.     elseif blocks[x][y-1][z] == 0 then
  71.         success = dig(-1)
  72.         if success then
  73.             blocks[x][y-1][z] = 1
  74.             turtle.down()
  75.             y = y - 1
  76.  
  77.             recursiveDig()
  78.  
  79.             dig(1)
  80.             turtle.up()
  81.             y = y + 1
  82.         else
  83.             blocks[x][y-1][z] = -1
  84.         end
  85.     end
  86.  
  87.     -- x+
  88.     if blocks[x+1] == nil then
  89.         turtle.turnRight()
  90.         turtle.select(fill)
  91.         turtle.place()
  92.         turtle.turnLeft()
  93.     elseif blocks[x+1][y][z] == 0 then
  94.         turtle.turnRight()
  95.         success = dig(0)
  96.         if success then
  97.             blocks[x+1][y][z] = 1
  98.             turtle.forward()
  99.             x = x + 1
  100.             turtle.turnLeft()
  101.  
  102.             recursiveDig()
  103.  
  104.             turtle.turnLeft()
  105.             dig(0)
  106.             turtle.forward()
  107.             x = x - 1
  108.             turtle.turnRight()
  109.         else
  110.             blocks[x+1][y][z] = -1
  111.             turtle.turnLeft()
  112.         end
  113.     end
  114.  
  115.     -- x-
  116.     if blocks[x-1] == nil then
  117.         turtle.turnLeft()
  118.         turtle.select(fill)
  119.         turtle.place()
  120.         turtle.turnRight()
  121.     elseif blocks[x-1][y][z] == 0 then
  122.         turtle.turnLeft()
  123.         success = dig(0)
  124.         if success then
  125.             blocks[x-1][y][z] = 1
  126.             turtle.forward()
  127.             x = x - 1
  128.             turtle.turnRight()
  129.  
  130.             recursiveDig()
  131.  
  132.             turtle.turnRight()
  133.             dig(0)
  134.             turtle.forward()
  135.             x = x + 1
  136.             turtle.turnLeft()
  137.         else
  138.             blocks[x-1][y][z] = -1
  139.             turtle.turnRight()
  140.         end
  141.     end
  142.  
  143.     -- z+
  144.     if blocks[x][y][z+1] == nil then
  145.         turtle.select(fill)
  146.         turtle.place()
  147.     elseif blocks[x][y][z+1] == 0 then
  148.         success = dig(0)
  149.         if success then
  150.             blocks[x][y][z+1] = 1
  151.             turtle.forward()
  152.             z = z + 1
  153.  
  154.             recursiveDig()
  155.  
  156.             turtle.turnLeft()
  157.             turtle.turnLeft()
  158.             dig(0)
  159.             turtle.forward()
  160.             turtle.turnRight()
  161.             turtle.turnRight()
  162.             z = z - 1
  163.         else
  164.             blocks[x][y][z+1] = -1
  165.         end
  166.     end
  167.  
  168.     -- z-
  169.     if blocks[x][y][z-1] == nil then
  170.         turtle.select(fill)
  171.         turtle.turnLeft()
  172.         turtle.turnLeft()
  173.         turtle.place()
  174.         turtle.turnRight()
  175.         turtle.turnRight()
  176.     elseif blocks[x][y][z-1] == 0 then
  177.         turtle.turnLeft()
  178.         turtle.turnLeft()
  179.         success = dig(0)
  180.         if success then
  181.             blocks[x][y][z-1] = 1
  182.             turtle.forward()
  183.             z = z - 1
  184.             turtle.turnRight()
  185.             turtle.turnRight()
  186.  
  187.             recursiveDig()
  188.  
  189.             dig(0)
  190.             turtle.forward()
  191.             z = z + 1
  192.         else
  193.             blocks[x][y][z-1] = -1
  194.             turtle.turnRight()
  195.             turtle.turnRight()
  196.         end
  197.     end
  198. end
  199.  
  200.  
  201. --helper method: dig until clear or unable to dig
  202. --dir: 0 = forward, 1 = up, -1 = down.
  203. --returns: true if the space is now empty, -1 if it is blocked.
  204. function dig(dir)
  205.     digFunc = turtle.dig
  206.     detectFunc = turtle.detect
  207.     compareFunc = turtle.compare
  208.     if dir == 1 then
  209.         digFunc = turtle.digUp
  210.         detectFunc = turtle.detectUp
  211.         compareFunc = turtle.compareUp
  212.     elseif dir == -1 then
  213.         digFunc = turtle.digDown
  214.         detectFunc = turtle.detectDown
  215.         compareFunc = turtle.compareDown
  216.     end
  217.  
  218.   if not detectFunc() then return true end
  219.  
  220.   whitelisted = false
  221.     for i,v in pairs(whitelist) do
  222.         turtle.select(v)
  223.         if compareFunc() then whitelisted = true end
  224.     end
  225.     if not whitelisted then return false end
  226.  
  227.     digging = true
  228.   blocked = detectFunc()
  229.   while digging and blocked do
  230.     digging = digFunc()
  231.    sleep(digWait)
  232.     blocked = detectFunc()
  233.   end
  234.  
  235.   return not blocked
  236. end
Advertisement
Add Comment
Please, Sign In to add comment