Advertisement
hevohevo

digFor / digExcept

Jan 24th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. -- required ComputerCraft1.65
  2.  
  3. local block_names = {
  4.   {"dirt",0}, "stone", {"wool",1}
  5. }
  6.  
  7. function matchNames(names, block_name, block_meta)
  8.   for i,p in ipairs(names) do
  9.     if type(p)=="table" then
  10.       if string.match(block_name, p[1]) and block_meta == p[2] then
  11.         return true
  12.       end
  13.     else
  14.       if string.match(block_name, p) then
  15.         return true
  16.       end
  17.     end
  18.   end
  19.  
  20.   return false
  21. end
  22.  
  23. function digExcept(tbl)
  24.   if not tbl then return turtle.dig() end
  25.  
  26.   local status, value = turtle.inspect()
  27.   if status then
  28.     -- ブロックが存在するとき
  29.     if matchNames(tbl, value["name"], value["metadata"]) then
  30.       -- マッチしたとき
  31.       return false, "Find the exception block"
  32.     else
  33.       -- マッチしないとき
  34.       return turtle.dig()
  35.     end
  36.    
  37.   else
  38.     -- ブロックが存在しないとき
  39.     return turtle.dig()
  40.   end
  41. end
  42.  
  43. function digFor(tbl)
  44.   if not tbl then return turtle.dig() end
  45.  
  46.   local status, value = turtle.inspect()
  47.   if status then
  48.     -- ブロックが存在するとき
  49.     if matchNames(tbl, value["name"], value["metadata"]) then
  50.       -- マッチしたとき
  51.       return turtle.dig()
  52.     else
  53.       -- マッチしないとき
  54.       return false, "No block to match"
  55.     end
  56.    
  57.   else
  58.     -- ブロックが存在しないとき
  59.     return turtle.dig()
  60.   end
  61.  
  62. end
  63.  
  64. while true do
  65.   -- print(digFor(block_names))
  66.   print(digExcept(block_names))
  67.   os.sleep(5)
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement