yumetodo

digdown_stairs.lua

May 15th, 2021 (edited)
421
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- 指定した方向のブロックをcompare_fnが返すスロットのアイテムと置換する
  2. -- @params direction 方向。0=正面,1=上,2=下,3=右,4=左
  3. -- @params compare_fn 比較関数。inspectの結果(bool, any)が渡される。設置に使用するアイテムのあるスロット番号か設置しない場合nilを返す
  4. function break_if(direction, compare_fn)
  5.     function impl(inspect_fn, dig_fn, place_fn)
  6.         local use_slot = compare_fn(inspect_fn())
  7.         if not (nil ~= use_slot) then
  8.             return true
  9.         end
  10.         local dig_re, dig_message = dig_fn()
  11.         if not dig_re then
  12.             return false, dig_message
  13.         end
  14.         return true
  15.     end
  16.     if (direction == 0) then
  17.         return impl(turtle.inspect, turtle.dig)
  18.     elseif (direction == 1) then
  19.         return impl(turtle.inspectUp, turtle.digUp)
  20.     elseif (direction == 2) then
  21.         return impl(turtle.inspectDown, turtle.digDown)
  22.     elseif (direction == 3) then
  23.         turtle.turnRight()
  24.         local re, message = impl(turtle.inspect, turtle.dig)
  25.         turtle.turnLeft()
  26.         return re, message
  27.     elseif (direction == 4) then
  28.         turtle.turnLeft()
  29.         local re, message = impl(turtle.inspect, turtle.dig)
  30.         turtle.turnRight()
  31.         return re, message
  32.     end
  33.     return false, string.format("unknown direction: %d", direction)
  34. end
  35.  
  36. -- local w, h, len = ...
  37. function replace_cond(i_re, i_v)
  38.     if i_re == nil then
  39.         return nil
  40.     end
  41.     if i_v.name ~= "appliedenergistics2:sky_stone_chest" then
  42.         return 1
  43.     end
  44.     print("not to break", i_v.name)
  45.     return nil
  46. end
  47. local n = ...
  48. print("dig down y axis to ", n)
  49. local move_fns = { turtle.down, turtle.forward }
  50. for i = 0, n * 2 do
  51.     break_if(2, replace_cond)
  52.     break_if(0, replace_cond)
  53.     print(i % 2)
  54.     local move_re = move_fns[i % 2 + 1]()
  55.     if not move_re then
  56.         print("failed to move")
  57.         break
  58.     end
  59.     if (i + 1) % 4 == 0 then
  60.         turtle.turnRight()
  61.         print("turn")
  62.     end
  63. end
RAW Paste Data