Advertisement
hevohevo

ComputerCraft Tutorial: digFor digExcept

Jan 25th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. -- ########################################
  2. -- digFor / digExcept
  3. -- versoin 0.1
  4. -- (c) 2015 hevohevo, License: MIT
  5. -- twitter: @hevohevo, http://hevohevo.hatenablog.com/
  6.  
  7. -- required ComputerCraft1.65
  8.  
  9. -- ブロック名リスト、メタデータも指定できるよ
  10. local block_names = {
  11.   {"dirt",0}, "stone", {"wool",1}
  12. }
  13.  
  14. -- 上記、ブロック名リストにマッチするかどうかを判定するサブ関数
  15. function _matchNames(names, block_name, block_meta)
  16.   for i,p in ipairs(names) do
  17.     if type(p)=="table" then
  18.       if string.match(block_name, p[1]) and block_meta == p[2] then
  19.         return true
  20.       end
  21.     else
  22.       if string.match(block_name, p) then
  23.         return true
  24.       end
  25.     end
  26.   end
  27.  
  28.   return false
  29. end
  30.  
  31. -- 各種digFor/digExcept関数を作るための元関数
  32. function _digFor(tbl, inspect_func, dig_func, except_flag)
  33.   if type(tbl) ~= "table" then return dig_func() end
  34.  
  35.   local status, value = inspect_func()
  36.   print(value["name"])
  37.   if status then
  38.     -- ブロックが存在するとき
  39.     if _matchNames(tbl, value["name"], value["metadata"]) then
  40.       if except_flag then -- 除外フラグtrueならマッチしたときに何もしない
  41.         return false, "Find an exception block"
  42.       else
  43.         return dig_func()
  44.       end
  45.     else
  46.       if except_flag then -- 除外フラグfalseならマッチしたときに採掘
  47.         return dig_func()
  48.       else
  49.         return false, "No block to match"
  50.       end
  51.     end
  52.    
  53.   else
  54.     -- ブロックが存在しないとき
  55.     return dig_func()
  56.   end
  57.  
  58. end
  59.  
  60. -- 各方向の関数を定義
  61. -- digFor/digExcept, digUpFor/digUpExcept, digDownFor/digDownExcept
  62.  
  63. function digFor(tbl)
  64.   return _digFor(tbl, turtle.inspect, turtle.dig, false)
  65. end
  66.  
  67. function digExcept(tbl)
  68.   return _digFor(tbl, turtle.inspect, turtle.dig, true)
  69. end
  70.  
  71. function digUpFor(tbl)
  72.   return _digFor(tbl, turtle.inspectUp, turtle.digUp, false)
  73. end
  74.  
  75. function digUpExcept(tbl)
  76.   return _digFor(tbl, turtle.inspectUp, turtle.digUp, true)
  77. end
  78.  
  79. function digDownFor(tbl)
  80.   return _digFor(tbl, turtle.inspectDown, turtle.digDown, false)
  81. end
  82.  
  83. function digDownExcept(tbl)
  84.   return _digFor(tbl, turtle.inspectDown, turtle.digDown, true)
  85. end
  86.  
  87.  
  88. -- main部分
  89. while true do
  90.   print(digUpExcept(block_names))
  91.   os.sleep(5)
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement