solidity

recursive mining for computercraft turtles

May 12th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --recursive digging
  2. -- usage: recdig <mode> [list]
  3.  
  4. local sides = { "top", "front", "bottom", "left", "right", "back" }
  5.  
  6. local targs = { ... }
  7.  
  8. --local mode = "any"        -- dig any block detected
  9. --local mode = "white"  -- takes a whitelist of blocks in form of inventory slots
  10. --local mode = "black"  -- takes a blacklist of blocks in form of inventory slots
  11. local mode = ""
  12. local list = {}
  13.  
  14. function checkInput()
  15.     local valid = true
  16.     if #targs == 0 then
  17.         valid = false
  18.     end
  19.     if #targs == 1 and targs[1] ~= "any" then
  20.         valid = false
  21.     end
  22.     if #targs > 1 then
  23.         if targs[1] ~= "white" and targs[1] ~= "black" then
  24.             valid = false
  25.         end
  26.         for i = 2,#targs do
  27.             if tonumber(targs[i]) < 1 or tonumber(targs[i]) > 16 then
  28.                 valid = false
  29.             end
  30.         end
  31.     end
  32.     if not valid then
  33.         print("usage: recdig <mode> [list]")
  34.         print("modes: any, white, black")
  35.         print("list: list of slots used for black- or white-listing")
  36.     end
  37.     return valid
  38. end
  39.  
  40. function turnAround()
  41.     turtle.turnLeft()
  42.     turtle.turnLeft()
  43. end
  44.  
  45. function checkSides()
  46.     sleep(0.25)
  47.     for s = 1,#sides do
  48.         print("checking "..sides[s])
  49.         checkSide(sides[s])
  50.     end
  51. end
  52.  
  53. function wantThis(mode, side)       -- returns true or false depending on whether the block we're checking is one we want
  54.     if mode == "any" then
  55.         if side == "up" then
  56.             return turtle.detectUp()
  57.         elseif side == "down" then
  58.             return turtle.detectDown()
  59.         elseif side == "front" then
  60.             return turtle.detect()
  61.         end
  62.     elseif mode == "white" then
  63.         local ret = false
  64.         for l = 1,#list do
  65.             turtle.select(l)
  66.             if side == "up" then
  67.                 if turtle.compareUp() then
  68.                     ret = true
  69.                 end
  70.             elseif side == "down" then
  71.                 if turtle.compareDown() then
  72.                     ret = true
  73.                 end
  74.             elseif side == "front" then
  75.                 if turtle.compare() then
  76.                     ret = true
  77.                 end
  78.             end
  79.         end
  80.         return ret
  81.     elseif mode == "black" then
  82.         local ret = true
  83.         for l = 1,#list do
  84.             turtle.select(l)
  85.             if side == "up" then
  86.                 if turtle.compareUp() or not turtle.detectUp() then
  87.                     ret = false
  88.                 end
  89.             elseif side == "down" then
  90.                 if turtle.compareDown() or not turtle.detectDown() then
  91.                     ret = false
  92.                 end
  93.             elseif side == "front" then
  94.                 if turtle.compare() or not turtle.detect() then
  95.                     ret = false
  96.                 end
  97.             end
  98.         end
  99.         return ret
  100.     end
  101. end
  102.  
  103. function checkSide(side)
  104.     if side == "top" then
  105.         if wantThis(mode, "up") then
  106.             print("block above detected")
  107.             while not turtle.up() do
  108.                 if not turtle.digUp() then
  109.                     turtle.attackUp()
  110.                 end
  111.             end
  112.             checkSides()
  113.             while not turtle.down() do
  114.                 turtle.attackDown()
  115.             end
  116.         end
  117.     elseif side == "front" then
  118.         if wantThis(mode, "front") then
  119.             print("blcok in front detected")
  120.             while not turtle.forward() do
  121.                 if not turtle.dig() then
  122.                     turtle.attack()
  123.                 end
  124.             end
  125.             checkSides()
  126.             turnAround()
  127.             while not turtle.forward() do
  128.                 turtle.attack()
  129.             end
  130.             turnAround()
  131.         end
  132.     elseif side == "bottom" then
  133.         if wantThis(mode, "down") then
  134.             print("block below detected")
  135.             while not turtle.down() do
  136.                 if not turtle.digDown() then
  137.                     turtle.attackDown()
  138.                 end
  139.             end
  140.             checkSides()
  141.             while not turtle.up() do
  142.                 turtle.attackUp()
  143.             end
  144.         end
  145.     elseif side == "left" then
  146.         turtle.turnLeft()
  147.         if wantThis(mode, "front") then
  148.             print("block left detected")
  149.             while not turtle.forward() do
  150.                 if not turtle.dig() then
  151.                     turtle.attack()
  152.                 end
  153.             end
  154.             checkSides()
  155.             turnAround()
  156.             while not turtle.forward() do
  157.                 turtle.attack()
  158.             end
  159.             turnAround()
  160.         end
  161.         turtle.turnRight()
  162.     elseif side == "right" then
  163.         turtle.turnRight()
  164.         if wantThis(mode, "front") then
  165.             print("block right detected")
  166.             while not turtle.forward() do
  167.                 if not turtle.dig() then
  168.                     turtle.attack()
  169.                 end
  170.             end
  171.             checkSides()
  172.             turnAround()
  173.             while not turtle.forward() do
  174.                 turtle.attack()
  175.             end
  176.             turnAround()
  177.         end
  178.         turtle.turnLeft()
  179.     elseif side == "back" then
  180.         turnAround()
  181.         if wantThis(mode, "front") then
  182.             print("block behind detected")
  183.             while not turtle.forward() do
  184.                 if not turtle.dig() then
  185.                     turtle.attack()
  186.                 end
  187.             end
  188.             checkSides()
  189.             turnAround()
  190.             while not turtle.forward() do
  191.                 turtle.attack()
  192.             end
  193.             turnAround()
  194.         end
  195.         turnAround()
  196.     end
  197. end
  198.  
  199. if checkInput() then
  200.     mode = targs[1]
  201.     if #targs > 1 then
  202.         for i = 2,#targs do
  203.             list[i-1] = targs[i]
  204.         end
  205.     end
  206.     if mode == "any" then
  207.         print("Ready to start recursive mining in any-mode. I will mine all blocks I can reach until I run out of blocks or fuel.")
  208.     elseif mode == "black" then
  209.         print("Ready to start recursive mining in "..mode.."-list mode. I will mine all blocks not on the supplied list")
  210.     elseif mode == "white" then
  211.         print("Ready to start recursive mining in "..mode.."-list mode. I will mine all blocks on the supplied list")
  212.     end
  213.     print("ready to start? y/n")
  214.     local yesno = read()
  215.     if yesno ~= "y" then
  216.         exit()
  217.     else
  218.         checkSides()
  219.     end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment