Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- --------
- -- lumberjack
- -- --------
- local function showUsage()
- print("lumberjack")
- print("slot1 : sapling (naegi) x 64")
- print("slot2 : wood x 1")
- end
- local SLOT_1_SAPLING = 1
- local SLOT_2_WOOD = 2
- local VERTICAL_LAYER_CNT = 4 -- even only?
- -- ---------------
- -- Move/Dig Functions
- -- ---------------
- function surelyUp()
- while not turtle.up() do turtle.digUp() end
- end
- function surelyDown()
- while not turtle.down() do turtle.digDown() end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyDigUp()
- while turtle.digUp() do
- os.sleep(0.4)
- end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyDig()
- while turtle.dig() do end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyFwd()
- for i=1,4 do
- local status, err = turtle.forward()
- if status then
- return true -- success!
- elseif err=="Out of fuel" then
- return status, err
- end
- surelyDig() -- face to a normal block or a sand(gravel) hill
- if turtle.detect() and not turtle.dig() then
- return false, "bedrock!"
- end
- if turtle.forward() then return true end -- success!
- if turtle.attack() then
- -- face to monster-mob
- while turtle.attack() do end
- else
- -- face to sand-blocks which is dropping long time
- os.sleep(5) -- probably, adjustment is required
- end
- end
- return turtle.forward()
- end
- -- --------
- --
- -- --------
- local f = function() while not turtle.forward() do end end
- local b = function() while not turtle.back() do end end
- local l = turtle.turnLeft
- local r = turtle.turnRight
- local u = function() while not turtle.up() do end end
- local d = function() while not turtle.down() do end end
- local F = surelyFwd
- local U = surelyUp
- local D = surelyDown
- local e0 = surelyDigUp
- local e1 = surelyDig
- local e2 = turtle.digDown -- excavate
- local p2 = turtle.placeDown
- local sel = turtle.select
- local sSapling = function() turtle.select(SLOT_1_SAPLING) end
- local sWood = function() turtle.select(SLOT_2_WOOD) end
- function vacuum(slotNo)
- local result = false
- turtle.select(slotNo)
- for i = 1, 16 do
- if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
- turtle.select(i)
- turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
- result = true
- if 0 == turtle.getItemSpace(slotNo) then
- break
- end
- turtle.select(slotNo)
- end
- end
- turtle.select(slotNo)
- return result
- end
- local function makeVacuumFunc(slotNo)
- return function()
- if 1 == turtle.getItemCount(slotNo) then
- vacuum(slotNo)
- end
- end
- end
- local vSapling = makeVacuumFunc(SLOT_1_SAPLING)
- local vWood = makeVacuumFunc(SLOT_2_WOOD)
- local function getTotal(slotNo)
- local cnt = turtle.getItemCount(slotNo)
- turtle.select(slotNo)
- for i=1,16 do
- if i ~= slotNo and turtle.compareTo(i) then
- cnt = cnt + turtle.getItemCount(i)
- end
- end
- return cnt
- end
- local function dropDownExceptK(slotNo, dropCnt, keepCnt)
- local function slotAction(i, mustKeep, mustDrop, slotNo)
- local cnt = turtle.getItemCount(i)
- if mustKeep < cnt then
- local dropAble = cnt - mustKeep
- local nowDrop = math.min(dropAble, mustDrop)
- mustKeep = 0
- mustDrop = mustDrop - nowDrop
- turtle.select(i)
- turtle.dropDown(nowDrop)
- turtle.select(slotNo)
- else
- mustKeep = mustKeep - cnt
- end
- end
- turtle.select(slotNo)
- local mustKeep = keepCnt
- local mustDrop = dropCnt
- for i=1,16 do
- if i ~= slotNo and turtle.compareTo(i) then
- slotAction(i, mustKeep, mustDrop, slotNo)
- end
- end
- vacuum(slotNo)
- slotAction(slotNo, mustKeep, mustDrop, slotNo)
- end
- local function dropDownExcept1(slotNo)
- turtle.select(slotNo)
- turtle.dropDown(turtle.getItemCount()-1)
- for i=1,16 do
- if i ~= slotNo and turtle.compareTo(i) then
- turtle.select(i)
- turtle.dropDown()
- turtle.select(slotNo)
- end
- end
- vacuum(slotNo)
- end
- local function dropDownHalf(slotNo)
- turtle.select(slotNo)
- local c = turtle.getItemCount(slotNo)
- turtle.dropDown((c-1)/2)
- for i=1,16 do
- if i~=slotNo and turtle.compareTo(i) then
- turtle.select(i)
- c = turtle.getItemCount(i)
- turtle.dropDown(c/2)
- turtle.select(slotNo)
- end
- end
- end
- local function tableInsertAll(tbl, list)
- for _, v in ipairs(list) do
- table.insert(tbl, v)
- end
- end
- local TAG_ANOTHER_WHEN_LAST = "anotherWhenLast"
- local TAG_SWITCH_ODD_EVEN = "switchOddEven"
- local function toFlatList(list)
- local rslt = { }
- if 3 == #list and "string" == type(list[1])
- and TAG_ANOTHER_WHEN_LAST == list[1] then
- table.insert(rslt, list)
- elseif 3 == #list and "string" == type(list[1])
- and TAG_SWITCH_ODD_EVEN == list[1] then
- table.insert(rslt, list)
- else
- for i, v in ipairs(list) do
- if "function" == type(v) then
- table.insert(rslt, v)
- elseif "table" == type(v) then
- tableInsertAll(rslt, toFlatList(v))
- end
- end
- end
- return rslt
- end
- local function _breakWhenLast_()
- end
- local function _anotherWhenLast_(ordinaryList, anotherList)
- return { TAG_ANOTHER_WHEN_LAST, ordinaryList, anotherList }
- end
- local function _switchOddEven_(firstList, secondList)
- return { TAG_SWITCH_ODD_EVEN, firstList, secondList }
- end
- local breakLast = _breakWhenLast_
- local anotherLast = _anotherWhenLast_
- local swOddEven = _switchOddEven_
- local function rep(num, ...)
- local function tableInsertAllForRep(rslt, list, i, num)
- for j, v in ipairs(list) do
- if _breakWhenLast_ == v then
- if i == num then
- break
- end
- elseif "table" == type(v) and TAG_ANOTHER_WHEN_LAST == v[1] then
- if i ~= num then
- tableInsertAllForRep(rslt, toFlatList(v[2]), i, num)
- else
- tableInsertAllForRep(rslt, toFlatList(v[3]), i, num)
- end
- elseif "table" == type(v) and TAG_SWITCH_ODD_EVEN == v[1] then
- if i % 2 == 1 then
- tableInsertAllForRep(rslt, toFlatList(v[2]), i, num)
- else
- tableInsertAllForRep(rslt, toFlatList(v[3]), i, num)
- end
- else
- table.insert(rslt, v)
- end
- end
- end
- local list = toFlatList({...})
- local rslt = {}
- for i = 1, num do
- tableInsertAllForRep(rslt, list, i, num)
- end
- return rslt
- end
- local function exec(...)
- for i, func in ipairs(toFlatList({...})) do
- func()
- end
- end
- -- --------
- -- main
- -- --------
- while true do
- local beforeFLv = turtle.getFuelLevel()
- local rotDig = { rep(4,e1,l) }
- local returnHeight = (VERTICAL_LAYER_CNT - 1) * 12
- exec(U,U,r,F,F,l)
- exec(
- rep(VERTICAL_LAYER_CNT,
- rep(3,
- rep(3,
- rep(8,U),F,F,
- rep(3,D),rep(5,D,rotDig),D,e2,sSapling,p2,U,
- anotherLast({rep(6,F)},{rep(2,F)})
- ),
- breakLast,swOddEven({r,rep(8,F),r},{l,rep(8,F),l})
- ),
- anotherLast({rep(12,U),r,r,},{r,F,F,l,D,D,rep(returnHeight,D),l,l})
- )
- )
- local afterFLv = turtle.getFuelLevel()
- print(string.format("fuelLevel %d -> %d", beforeFLv, afterFLv))
- break
- end
- -- omake(sample?)
- --exec(f,f,b,b)
- --exec(rep(4,f,f,r))
- --[[
- exec(
- rep(3,
- rep(3,
- rep(3,f),rep(3,b),breakLast,r,f,l
- ),
- l,f,f,r,breakLast,u
- ),
- d,d
- )
- --]]
- --[[
- exec(
- rep(4,
- rep(3,f),breakLast,swOddEven({r,f,r},{l,f,l})
- )
- )
- --]]
- --[[
- exec(
- rep(4,
- rep(3,f),swOddEven({r,f,r},{breakLast,l,f,l})
- )
- )
- --]]
- --[[
- exec(
- rep(3,
- rep(3,
- rep(3,f),rep(3,b),anotherLast({r,f,l},{l,f,f,r})
- ),
- anotherLast({u},{d,d})
- )
- )
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement