solidity

recursive mining featuring white/blacklists and worldeater

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