Advertisement
kssr3951

lumberjack

Apr 18th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.73 KB | None | 0 0
  1. -- --------
  2. -- lumberjack
  3. -- --------
  4. local function showUsage()
  5.   print("lumberjack")
  6.   print("slot1 : sapling (naegi) x 64")
  7.   print("slot2 : wood            x  1")
  8. end
  9. local SLOT_1_SAPLING = 1
  10. local SLOT_2_WOOD    = 2
  11. local VERTICAL_LAYER_CNT = 4 -- even only?
  12. -- ---------------
  13. -- Move/Dig Functions
  14. -- ---------------
  15. function surelyUp()
  16.   while not turtle.up() do turtle.digUp() end
  17. end
  18. function surelyDown()
  19.   while not turtle.down() do turtle.digDown() end
  20. end
  21. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  22. function surelyDigUp()
  23.   while turtle.digUp() do
  24.     os.sleep(0.4)
  25.   end
  26. end
  27. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  28. function surelyDig()
  29.   while turtle.dig() do end
  30. end
  31. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  32. function surelyFwd()
  33.   for i=1,4 do
  34.     local status, err = turtle.forward()
  35.     if status then
  36.       return true  -- success!
  37.     elseif err=="Out of fuel" then
  38.       return status, err
  39.     end
  40.  
  41.     surelyDig() -- face to a normal block or a sand(gravel) hill
  42.  
  43.     if turtle.detect() and not turtle.dig() then
  44.       return false, "bedrock!"
  45.     end
  46.  
  47.     if turtle.forward() then return true end -- success!
  48.  
  49.     if turtle.attack() then
  50.       -- face to monster-mob
  51.       while turtle.attack() do end
  52.     else
  53.       -- face to sand-blocks which is dropping long time
  54.       os.sleep(5) -- probably, adjustment is required
  55.     end
  56.   end
  57.   return turtle.forward()
  58. end
  59. -- --------
  60. --
  61. -- --------
  62. local f = function() while not turtle.forward() do end end
  63. local b = function() while not turtle.back() do end end
  64. local l = turtle.turnLeft
  65. local r = turtle.turnRight
  66. local u = function() while not turtle.up() do end end
  67. local d = function() while not turtle.down() do end end
  68. local F = surelyFwd
  69. local U = surelyUp
  70. local D = surelyDown
  71. local e0 = surelyDigUp
  72. local e1 = surelyDig
  73. local e2 = turtle.digDown -- excavate
  74. local p2 = turtle.placeDown
  75. local sel = turtle.select
  76. local sSapling = function() turtle.select(SLOT_1_SAPLING) end
  77. local sWood    = function() turtle.select(SLOT_2_WOOD) end
  78. function vacuum(slotNo)
  79.   local result = false
  80.   turtle.select(slotNo)
  81.   for i = 1, 16 do
  82.     if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
  83.       turtle.select(i)
  84.       turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
  85.       result = true
  86.       if 0 == turtle.getItemSpace(slotNo) then
  87.         break
  88.       end
  89.       turtle.select(slotNo)
  90.     end
  91.   end
  92.   turtle.select(slotNo)
  93.   return result
  94. end
  95. local function makeVacuumFunc(slotNo)
  96.   return function()
  97.       if 1 == turtle.getItemCount(slotNo) then
  98.         vacuum(slotNo)
  99.       end
  100.     end
  101. end
  102. local vSapling = makeVacuumFunc(SLOT_1_SAPLING)
  103. local vWood    = makeVacuumFunc(SLOT_2_WOOD)
  104. local function getTotal(slotNo)
  105.   local cnt = turtle.getItemCount(slotNo)
  106.   turtle.select(slotNo)
  107.   for i=1,16 do
  108.     if i ~= slotNo and turtle.compareTo(i) then
  109.       cnt = cnt + turtle.getItemCount(i)
  110.     end
  111.   end
  112.   return cnt
  113. end
  114. local function dropDownExceptK(slotNo, dropCnt, keepCnt)
  115.   local function slotAction(i, mustKeep, mustDrop, slotNo)
  116.     local cnt = turtle.getItemCount(i)
  117.     if mustKeep < cnt then
  118.       local dropAble = cnt - mustKeep
  119.       local nowDrop = math.min(dropAble, mustDrop)
  120.       mustKeep = 0
  121.       mustDrop = mustDrop - nowDrop
  122.       turtle.select(i)
  123.       turtle.dropDown(nowDrop)
  124.       turtle.select(slotNo)
  125.     else
  126.       mustKeep = mustKeep - cnt
  127.     end
  128.   end
  129.   turtle.select(slotNo)
  130.   local mustKeep = keepCnt
  131.   local mustDrop = dropCnt
  132.   for i=1,16 do
  133.     if i ~= slotNo and turtle.compareTo(i) then
  134.       slotAction(i, mustKeep, mustDrop, slotNo)
  135.     end
  136.   end
  137.   vacuum(slotNo)
  138.   slotAction(slotNo, mustKeep, mustDrop, slotNo)
  139. end
  140. local function dropDownExcept1(slotNo)
  141.   turtle.select(slotNo)
  142.   turtle.dropDown(turtle.getItemCount()-1)
  143.   for i=1,16 do
  144.     if i ~= slotNo and turtle.compareTo(i) then
  145.       turtle.select(i)
  146.       turtle.dropDown()
  147.       turtle.select(slotNo)
  148.     end
  149.   end
  150.   vacuum(slotNo)
  151. end
  152. local function dropDownHalf(slotNo)
  153.   turtle.select(slotNo)
  154.   local c = turtle.getItemCount(slotNo)
  155.   turtle.dropDown((c-1)/2)
  156.   for i=1,16 do
  157.     if i~=slotNo and turtle.compareTo(i) then
  158.       turtle.select(i)
  159.       c = turtle.getItemCount(i)
  160.       turtle.dropDown(c/2)
  161.       turtle.select(slotNo)
  162.     end
  163.   end
  164. end
  165. local function tableInsertAll(tbl, list)
  166.   for _, v in ipairs(list) do
  167.     table.insert(tbl, v)
  168.   end
  169. end
  170. local TAG_ANOTHER_WHEN_LAST = "anotherWhenLast"
  171. local TAG_SWITCH_ODD_EVEN = "switchOddEven"
  172. local function toFlatList(list)
  173.   local rslt = { }
  174.   if 3 == #list and "string" == type(list[1])
  175.     and TAG_ANOTHER_WHEN_LAST == list[1] then
  176.     table.insert(rslt, list)
  177.   elseif 3 == #list and "string" == type(list[1])
  178.     and TAG_SWITCH_ODD_EVEN == list[1] then
  179.     table.insert(rslt, list)
  180.   else
  181.     for i, v in ipairs(list) do
  182.       if "function" == type(v) then
  183.         table.insert(rslt, v)
  184.       elseif "table" == type(v) then
  185.         tableInsertAll(rslt, toFlatList(v))
  186.       end
  187.     end
  188.   end
  189.   return rslt
  190. end
  191. local function _breakWhenLast_()
  192. end
  193. local function _anotherWhenLast_(ordinaryList, anotherList)
  194.   return { TAG_ANOTHER_WHEN_LAST, ordinaryList, anotherList }
  195. end
  196. local function _switchOddEven_(firstList, secondList)
  197.   return { TAG_SWITCH_ODD_EVEN, firstList, secondList }
  198. end
  199. local breakLast = _breakWhenLast_
  200. local anotherLast = _anotherWhenLast_
  201. local swOddEven = _switchOddEven_
  202. local function rep(num, ...)
  203.   local function tableInsertAllForRep(rslt, list, i, num)
  204.     for j, v in ipairs(list) do
  205.       if _breakWhenLast_ == v then
  206.         if i == num then
  207.           break
  208.         end
  209.       elseif "table" == type(v) and TAG_ANOTHER_WHEN_LAST == v[1] then
  210.         if i ~= num then
  211.           tableInsertAllForRep(rslt, toFlatList(v[2]), i, num)
  212.         else
  213.           tableInsertAllForRep(rslt, toFlatList(v[3]), i, num)
  214.         end
  215.       elseif "table" == type(v) and TAG_SWITCH_ODD_EVEN == v[1] then
  216.         if i % 2 == 1 then
  217.           tableInsertAllForRep(rslt, toFlatList(v[2]), i, num)
  218.         else
  219.           tableInsertAllForRep(rslt, toFlatList(v[3]), i, num)
  220.         end
  221.       else
  222.         table.insert(rslt, v)
  223.       end
  224.     end
  225.   end
  226.   local list = toFlatList({...})
  227.   local rslt = {}
  228.   for i = 1, num do
  229.     tableInsertAllForRep(rslt, list, i, num)
  230.   end
  231.   return rslt
  232. end
  233. local function exec(...)
  234.   for i, func in ipairs(toFlatList({...})) do
  235.     func()
  236.   end
  237. end
  238. -- --------
  239. -- main
  240. -- --------
  241. while true do
  242.   local beforeFLv = turtle.getFuelLevel()
  243.   local rotDig = { rep(4,e1,l) }
  244.   local returnHeight = (VERTICAL_LAYER_CNT - 1) * 12
  245.   exec(U,U,r,F,F,l)
  246.   exec(
  247.     rep(VERTICAL_LAYER_CNT,
  248.       rep(3,
  249.         rep(3,
  250.           rep(8,U),F,F,
  251.           rep(3,D),rep(5,D,rotDig),D,e2,sSapling,p2,U,
  252.           anotherLast({rep(6,F)},{rep(2,F)})
  253.         ),
  254.         breakLast,swOddEven({r,rep(8,F),r},{l,rep(8,F),l})
  255.       ),
  256.       anotherLast({rep(12,U),r,r,},{r,F,F,l,D,D,rep(returnHeight,D),l,l})
  257.     )
  258.   )
  259.   local afterFLv = turtle.getFuelLevel()
  260.   print(string.format("fuelLevel %d -> %d", beforeFLv, afterFLv))
  261.   break
  262. end
  263. -- omake(sample?)
  264.   --exec(f,f,b,b)
  265.   --exec(rep(4,f,f,r))
  266.   --[[
  267.   exec(
  268.     rep(3,
  269.       rep(3,
  270.         rep(3,f),rep(3,b),breakLast,r,f,l
  271.       ),
  272.       l,f,f,r,breakLast,u
  273.     ),
  274.     d,d
  275.   )
  276.   --]]
  277.   --[[
  278.   exec(
  279.     rep(4,
  280.       rep(3,f),breakLast,swOddEven({r,f,r},{l,f,l})
  281.     )
  282.   )
  283.   --]]
  284.   --[[
  285.   exec(
  286.     rep(4,
  287.       rep(3,f),swOddEven({r,f,r},{breakLast,l,f,l})
  288.     )
  289.   )
  290.   --]]
  291.   --[[
  292.   exec(
  293.     rep(3,
  294.       rep(3,
  295.         rep(3,f),rep(3,b),anotherLast({r,f,l},{l,f,f,r})
  296.       ),
  297.       anotherLast({u},{d,d})
  298.     )
  299.   )
  300.   --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement