Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- --------
- -- charcoalMaker
- -- --------
- local function showUsage()
- print("charcoalMaker")
- print("slot1 : sapling (naegi)")
- print("slot2 : wood")
- end
- local SLOT_1_SAPLING = 1
- local SLOT_2_WOOD = 2
- local REQUIRED_FUEL_LEVEL = 1600
- -- ---------------
- -- 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 -- [e]xcavate
- local e1 = surelyDig
- local e2 = turtle.digDown
- local p2 = turtle.placeDown
- --local sel = turtle.select
- local t0 = turtle.dropUp -- [t]hrow
- local t1 = turtle.drop
- local t2 = turtle.drowDown
- local k0 = turtle.suckUp -- suc[k]
- local k1 = turtle.suck
- local k2 = turtle.suckDown
- local sSapling = function() turtle.select(SLOT_1_SAPLING) end
- local sWood = function() turtle.select(SLOT_2_WOOD) end
- function vacuum(slotNo, fullOption)
- local keepCnt
- if nil == fullOption then
- keepCnt = 1
- elseif true == fullOption then
- keepCnt = 0
- end
- local result = false
- turtle.select(slotNo)
- for i = 1, 16 do
- if slotNo ~= i and turtle.compareTo(i) and keepCnt < 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
- -- exec()やrep()で使えるように関数を戻す関数にしてみた。
- local function sel(slotNo)
- return function()
- turtle.select(slotNo)
- 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 dropAll(dropFunc)
- local dropFn = turtle.drop
- if nil ~= dropFunc then
- dropFn = dropFunc
- end
- for i = 1, 16 do
- if 1 <= turtle.getItemCount(i) then
- turtle.select(i)
- dropFn()
- end
- end
- end
- local function dropEx(slot, count, dropFunc)
- local sameItems = { slot }
- turtle.select(slot)
- for i = 1, 16 do
- if slot ~= i and 0 < turtle.getItemCount(i) and turtle.compareTo(i) then
- table.insert(sameItems, i)
- end
- end
- local required = count
- for _, i in ipairs(sameItems) do
- local cnt = math.min(turtle.getItemCount(i), required)
- turtle.select(i)
- dropFunc(cnt)
- required = required - cnt
- if 0 == required then
- break
- end
- end
- if 0 == turtle.getItemCount(slot) then
- for _, i in ipairs(sameItems) do
- if slot ~= i and 0 < turtle.getItemCount(i) then
- turtle.select(i)
- turtle.transferTo(slot)
- break
- end
- end
- end
- end
- local function suckEx(count, suckFunc, stackLimit)
- local stackLim = 64
- if nil ~= stackLimit then
- stackLim = stackLimit
- end
- local required = count
- local firstSlot = nil
- local rslt
- while true do
- local before = { }
- for i = 1, 16 do
- before[i] = turtle.getItemCount(i)
- end
- rslt = suckFunc(math.min(required, stackLim))
- for i = 1, 16 do
- local cnt = turtle.getItemCount(i)
- if cnt ~= before[i] then
- if nil == firstSlot then
- firstSlot = i
- end
- required = required - (cnt - before[i])
- end
- end
- if 0 == required or false == rslt then
- break
- end
- end
- return firstSlot
- end
- local function countAll(slotNo)
- turtle.select(slotNo)
- local cnt = turtle.getItemCount(slotNo)
- if 0 == cnt then
- return 0
- else
- for i = 1, 16 do
- if i ~= slotNo and turtle.compareTo(i) then
- cnt = cnt + turtle.getItemCount(i)
- end
- end
- return cnt
- 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
- -- --------
- -- utility
- -- --------
- local function countAny()
- local cnt = 0
- for i = 1, 16 do
- cnt = cnt + turtle.getItemCount(i)
- end
- return cnt
- end
- local function countEx(slot)
- local cnt = turtle.getItemCount(slot)
- if 0 == cnt then
- return 0
- end
- turtle.select(slot)
- for i = 1, 16 do
- if slot ~= i and 0 < turtle.getItemCount(i) and turtle.compareTo(i) then
- cnt = cnt + turtle.getItemCount(i)
- end
- end
- return cnt
- end
- -- --------
- -- application
- -- --------
- -- 管理対象
- -- 苗木
- -- 原木
- -- 木材
- -- 棒
- -- 松明
- -- 木炭
- -- チェスト
- local function newAcc()
- return { count = 0, chests = { } }
- end
- local account = {}
- account.sapling = newAcc()
- account.wood = newAcc()
- account.woodPlank = newAcc()
- account.stick = newAcc()
- account.torch = newAcc()
- account.charcoal = newAcc()
- account.chest = newAcc()
- -- 原木3個を原木2個で焼くと木炭が3個できる。燃焼のムダもなさそう。木炭1個で原木8個焼けるので、以降は原木を燃料として使わない。
- --
- -- 木炭のストックが無いときの原木を使った木炭作成は、プログラムの初回は必ず行うようにすればいい。
- -- ①かまど1号を用いて、原木3個を原木2個で焼く(原木2個ロスト)
- -- ②かまど1~3号に、出来上がった木炭を1個ずつセット、原木8こずつを焼く(木炭3個ロスト)
- local function ddXw(dropCnt)
- local cnt = turtle.getItemCount(SLOT_2_WOOD)
- if 0 == cnt then
- return
- elseif cnt <= dropCnt then
- vacuum(SLOT_2_WOOD)
- end
- turtle.dropDown(dropCnt)
- end
- local function dd8w()
- ddXw(8)
- end
- local function findVacantSlot()
- for i = 1, 16 do
- if 0 == turtle.getItemCount(i) then
- return i
- end
- end
- return nil
- end
- local function suckUp1()
- while not turtle.suckUp(1) do end
- end
- local function dropUp1()
- turtle.dropUp(1)
- end
- local function dropExceptSlot(slot)
- for i = 1, 16 do
- if i ~= slot then
- if 1 <= turtle.getItemCount(i) then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- end
- local function fillTightly()
- local j = 16
- for i = 1, 15 do
- for j = j, i + 1, -1 do
- local space = turtle.getItemSpace(i)
- local cnt = turtle.getItemCount(j)
- if 0 == space then
- break
- end
- if 0 < cnt then
- if j ~= turtle.getSelectedSlot() then
- turtle.select(j)
- end
- turtle.transferTo(i, math.min(space, cnt))
- if 0 == turtle.getItemSpace(i) then
- break
- end
- end
- end
- end
- end
- local function firstCharcoalMake()
- -- 初回のみ実行する木炭作成処理。(原木で木炭を3個作成し、それを元手にかまど1から8で木炭を作成する。)
- -- 原木3個を原木2個で焼く
- vacuum(SLOT_2_WOOD)
- exec(r,f,l,f,f,l,f,r)
- turtle.select(SLOT_2_WOOD)
- turtle.dropUp(2)
- exec(f,u,u,b)
- turtle.dropDown(3)
- -- 原木8個ずつをかまど1から8へ投入
- exec(
- rep(4,dd8w,f,f,dd8w,breakLast,swOddEven({r,f,f,r},{l,f,f,l}))
- )
- exec(r,rep(5,f),d,d,f,r)
- -- 原木8x8個をドロップしているので、空きスロットは必ずある
- vacuum(SLOT_2_WOOD)
- local charcoalSlot = findVacantSlot()
- turtle.select(charcoalSlot)
- -- かまど1で原木を使って焼いた1個めの木炭(1P1)を取り出し、かまど2に燃料として投入。(1P1回収)(1P2開始)(2A開始)
- exec(suckUp1,f,f,dropUp1)
- -- かまど1で原木を使って焼いた2個めの木炭(1P2)を取り出し、かまど3に燃料として投入。(1P2回収)(1P3開始)(3B開始)
- exec(b,b,suckUp1,r,f,f,dropUp1)
- -- かまど2で焼きあがった木炭(2A)を取り出し、かまど1に燃料として投入。(2A回収)(1C開始?)
- exec(l,f,f,l,f,f,r,suckUp1,b,b,dropUp1)
- -- かまど1で原木を使って焼いた3個めの木炭(1P3)を取り出し、かまど4に燃料として投入。(1P3回収)(4D開始)
- exec(suckUp1,f,f,r,f,f,dropUp1)
- -- かまど3で焼きあがった木炭(3B)を取り出し、かまど5に燃料として投入。(3B回収)(5E開始)
- exec(l,b,b,suckUp1,r,f,f,dropUp1)
- -- かまど1で焼きあがった木炭(1C)を取り出し、かまど6に燃料として投入。(1C回収)(6F開始)
- exec(rep(4,b),suckUp1,rep(4,f),l,f,f,dropUp1)
- -- かまど4で焼きあがった木炭(4D)を取り出し、かまど8に燃料として投入。(4D回収)(8G開始)
- exec(r,b,b,suckUp1,rep(4,f),dropUp1)
- -- かまど5で焼きあがった木炭(5E)を取り出し、かまど7に燃料として投入。(5E回収)(7H開始)
- exec(b,b,l,b,b,r,suckUp1,f,f,dropUp1)
- -- 素材投入用の初期位置に戻る
- exec(b,u,u,rep(5,b),l)
- vacuum(SLOT_2_WOOD)
- end
- local function firstCharcoalMake2()
- local woodCnt = countAll(SLOT_2_WOOD)
- local charcoalCnt = math.floor((woodCnt / 2) / 8) * 8
- local charcoalCntForTorch = math.floor((woodCnt / 4) / 8) * 8
- local woodCntForTorch = charcoalCntForTorch / 8
- local woodCntForChest = math.floor((woodCnt / 8) / 2) * 2
- local woodCntReserve = woodCnt - charcoalCnt - charcoalCntForTorch
- - woodCntForTorch - woodCntForChest
- --[[
- print(string.format("total = %d / charCnt = %d / charForTorch = %d",
- woodCnt, charcoalCnt, charcoalCntForTorch))
- print(string.format("woodForTorch = %d / woodForChest = %d / reserve = %d",
- woodCntForTorch, woodCntForChest, woodCntReserve))
- --]]
- -- 木炭の原料を投入する
- local furnaceNo = 1
- local charcoalAllCnt = charcoalCnt + charcoalCntForTorch
- local baseCnt = math.floor(charcoalAllCnt / 64)
- local exNo = (charcoalAllCnt / 8) % 8
- --print(string.format("baseCnt = %d / exNo = %d", baseCnt, exNo))
- -- 投入できる限りの量の原木を投入
- local exCnt
- for i = 1, 8 do
- if i <= exNo then
- exCnt = 1
- else
- exCnt = 0
- end
- ddXw(math.min(8*7, (baseCnt + exCnt) * 8))
- if i == 1 or i == 3 or i == 5 or i == 7 then
- exec(f,f)
- elseif i == 2 or i == 6 then
- exec(r,f,f,r)
- elseif i == 4 then
- exec(l,f,f,l)
- end
- end
- exec(r,rep(5,f),d,d,l,f,f,r,f,r)
- end
- local function gotoWarehousePosFromInitPos()
- exec(rep(4,u),r)
- end
- local function craftChests()
- -- すべてチェストにしてしまう
- vacuum(SLOT_2_WOOD)
- local woodCnt = countAll(SLOT_2_WOOD)
- dropAll()
- local woodCntForPlank = math.min(woodCnt, 64 * 13)
- -- 原木を木材に加工して木材チェストに保存する位置に移動
- exec(l,f,r,f,r)
- -- 最大13スタックの原木を木材に変える
- for i = 1, 13 do
- turtle.select(1)
- local rslt = turtle.suck()
- if false == rslt then
- break
- end
- exec(l,l)
- for j = 1, 4 do
- local cnt = turtle.getItemCount(1)
- turtle.craft()
- if 16 < cnt then
- dropExceptSlot(1)
- else
- dropAll()
- break
- end
- end
- exec(r,r)
- end
- -- 木材をチェストに加工してチェスト用チェストに保存する位置に移動
- exec(r,f,r,rep(3,f),r,f,r)
- -- 木材チェストの全ての原木からチェストを作ってチェスト用チェストに保存する
- local doughnut = { 1,2,3,5,7,9,10,11 }
- local notFull = false
- while true do
- local maxCnt = 0
- local minCnt = 64
- for i = 1, 8 do
- turtle.select(doughnut[i])
- local rslt = turtle.suck()
- if false == rslt then
- notFull = true
- break
- end
- local cnt = turtle.getItemCount(doughnut[i])
- maxCnt = math.max(maxCnt, cnt)
- minCnt = math.min(minCnt, cnt)
- end
- -- 原木をドーナツ型に配置したつもりだが、数がそろっていない場合はそろえる
- if notFull or minCnt ~= maxCnt then
- local ttl = 0
- for i = 1, 16 do ttl = ttl + turtle.getItemCount(i) end
- if 8 <= ttl then
- local par = math.floor(ttl / 8)
- for _, i in ipairs(doughnut) do
- local iCnt = turtle.getItemCount(i)
- if par < iCnt then
- turtle.select(i)
- for _, j in ipairs(doughnut) do
- if i ~= j then
- local jCnt = turtle.getItemCount(j)
- if jCnt < par then
- turtle.transferTo(j, math.min(iCnt - par, par - jCnt))
- iCnt = turtle.getItemCount(i)
- if par == iCnt then
- break
- end
- end
- end
- end
- end
- end
- for _, i in ipairs(doughnut) do
- local cnt = turtle.getItemCount(i)
- if par < cnt then
- turtle.select(i)
- turtle.drop(cnt - par)
- end
- end
- else
- dropAll()
- break
- end
- end
- turtle.craft()
- exec(l,l)
- dropAll()
- exec(r,r)
- end
- end
- local function gotoInitPosFromWarehousePos()
- exec(l,rep(4,d))
- end
- local function suckUpDropUp()
- local charcoalSlot = findVacantSlot()
- turtle.select(charcoalSlot)
- turtle.suckUp()
- vacuum(charcoalSlot)
- turtle.dropUp()
- end
- local function gotoCharcoalHatInitPosFromTreePlantationInitPos()
- exec(rep(3,e0,u),l,e1,f,r,rep(15,e1,f),l,e2,d,rep(3,e1,f),e2,d)
- end
- local function gotoPlantationFromCharcoalHat()
- exec(u,u,l,l,rep(4,e1,f))
- exec(r,rep(15,e1,f))
- exec(rep(3,e2,d),dropAll,l,l)
- exec(f)
- turtle.select(1)
- turtle.suckDown()
- exec(b,r,f)
- turtle.select(2)
- turtle.suckDown()
- exec(b,l)
- end
- local function lumberjackPart()
- -- 植林場のプログラム(lumberjack)を実行
- shell.run("lumberjack")
- -- 苗木を前方のチェストに退避
- exec(e1,f)
- while true do
- local sapCnt = turtle.getItemCount(SLOT_1_SAPLING)
- if 1 < sapCnt then
- turtle.select(1)
- turtle.dropDown(sapCnt - 1)
- end
- vacuum(SLOT_1_SAPLING, true)
- sapCnt = turtle.getItemCount(SLOT_1_SAPLING)
- if sapCnt == 1 then
- turtle.dropDown()
- break
- elseif sapCnt < 1 then
- break
- end
- end
- exec(l,l,e1,f,l)
- -- 原木1個(サンプル)を右のチェストに退避
- exec(e1,f)
- vacuum(SLOT_2_WOOD,true)
- turtle.select(SLOT_2_WOOD)
- turtle.dropDown(1)
- exec(l,l,e1,f,r)
- -- 植林場の開始位置から炭焼き小屋の開始位置へ移動
- gotoCharcoalHatInitPosFromTreePlantationInitPos()
- end
- local function firstAction()
- -- 余計なアイテムを持っていたら捨てる(移動中に苗木を拾う可能性がある)
- turtle.select(SLOT_2_WOOD)
- for i = 1, 16 do
- if i ~= SLOT_2_WOOD and 0 < turtle.getItemCount(i) and false == turtle.compareTo(i) then
- turtle.select(i)
- turtle.dropDown()
- turtle.select(SLOT_2_WOOD)
- end
- end
- -- 初回限定の木炭作成動作その1
- firstCharcoalMake()
- -- 初回限定の木炭作成動作その2。(その1で作った木炭でさらに木炭を作る)
- firstCharcoalMake2()
- -- 倉庫階のスタート位置(&向き)に移動する
- gotoWarehousePosFromInitPos()
- -- 残りの原木をすべて使ってチェストを作成する
- craftChests()
- -- 倉庫のスタート地点(&向き)に戻る
- exec(r,f,l,rep(4,f),l)
- -- かまど階のスタート位置に戻る
- gotoInitPosFromWarehousePos()
- -- 焼きあがった木炭を取り出し、燃料としてセットする
- exec(r,f,l,f,f,l,f,r)
- exec(rep(4,suckUpDropUp,f,f,suckUpDropUp,breakLast,swOddEven({r,f,f,r},{l,f,f,l})))
- -- かまど階のスタート位置に戻る
- exec(r,rep(5,f),l,f,f,r,f,r)
- end
- local function secondAction()
- -- 原木の個数を取得
- local woodCnt = countAll(SLOT_2_WOOD)
- -- 原木を一旦全部退避する
- exec(b)
- dropAll(turtle.dropDown)
- exec(f)
- -- 焼きあがった木炭を取り出し、燃料としてセットする。回数が進むとここで余りが発生するはず。
- exec(r,f,l,f,f,l,f,r)
- exec(rep(4,suckUpDropUp,f,f,suckUpDropUp,breakLast,swOddEven({r,f,f,r},{l,f,f,l})))
- -- かまど階のスタート位置に戻る
- exec(r,rep(5,f),l,f,f,r,f,r)
- -- 燃料補給
- fillTightly()
- local requiredFuelLv = 3000
- local reqChar = requiredFuelLv / 80 + 1
- local bf = turtle.getFuelLevel()
- turtle.refuel(math.min(reqChar, turtle.getItemCount(1)))
- local af = turtle.getFuelLevel()
- print(string.format("refuel %d -> %d (%d)", bf, af, af - bf))
- -- 木炭の個数を数える
- local charCnt = countAny()
- -- 松明
- local torchRate = 8
- local torchChar = math.floor(math.floor(charCnt / torchRate) / 8) * 8
- local torchWood = math.floor(torchChar / 4) * 2 / 4
- -- チェスト
- local chestRate = 8
- local chestWood = math.floor(math.floor(woodCnt / chestRate) / 2) * 2
- -- 原木(貯蔵数)
- local storeRate = 5
- local storeWood = math.floor(woodCnt / storeRate)
- -- 木炭(製造数)
- local charWood = math.floor((woodCnt - torchWood - chestWood - storeWood) / 8) * 8
- -- 木炭(貯蔵数)
- local storeChar = charCnt - torchChar
- -- 原木(貯蔵数)を再計算
- storeWood = woodCnt - torchWood - chestWood - charWood
- -- 確認
- --[[
- print("charCnt = " .. tostring(charCnt))
- print("woodCnt = " .. tostring(woodCnt))
- print("torchChar = " .. tostring(torchChar))
- print("torchWood = " .. tostring(torchWood))
- print("chestWood = " .. tostring(chestWood))
- print("storeWood = " .. tostring(storeWood))
- print(" charWood = " .. tostring( charWood))
- print("storeChar = " .. tostring(storeChar))
- --]]
- if 0 < torchChar or 0 < storeChar then
- -- ------------------------------------------
- -- 木炭を、松明製造用/貯蔵用のチェストに入れる。
- -- TODO 貯蔵用チェストがいっぱいになっている場合はチェストを増設する
- -- ------------------------------------------
- -- 倉庫階のスタート位置(&向き)に移動する
- gotoWarehousePosFromInitPos()
- -- 松明製造用の木炭用のチェストに移動し、松明分の木炭を保存
- exec(l,f,r,rep(3,f),r)
- fillTightly()
- dropEx(1, torchChar, turtle.drop)
- -- 木炭貯蔵用のチェストに移動し、残りの木炭を保存
- exec(l,f,f,r)
- dropAll()
- -- 倉庫階のスタート位置(&向き)に戻る
- exec(r,rep(5,f),l,f,l)
- -- かまど階のスタート位置に戻る
- gotoInitPosFromWarehousePos()
- end
- -- 原木をすべて取り出す
- exec(b)
- for i = 1, 16 do if false == turtle.suckDown() then break end end
- exec(f)
- fillTightly()
- -- 原木をかまどに投入
- exec(r,f,l,f,f,u,u,l,f,r)
- local function makeFnDropDown(count)
- local FURNACE_CNT = 8
- local UNIT_CNT = 8 -- 木炭1個で効率良く焼ける原木の数
- local required = count
- local baseCnt = math.floor(required / FURNACE_CNT / UNIT_CNT) * UNIT_CNT
- local extra = (required - baseCnt * UNIT_CNT) / UNIT_CNT
- local callCnt = 0
- return function()
- callCnt = callCnt + 1
- local dropCnt = baseCnt
- if callCnt <= extra then
- dropCnt = dropCnt + UNIT_CNT
- end
- fillTightly()
- dropEx(1, dropCnt, turtle.dropDown)
- end
- end
- local ddEx = makeFnDropDown(charWood)
- exec(rep(4,ddEx,f,f,ddEx,breakLast,swOddEven({r,f,f,r},{l,f,f,l})))
- -- かまど階の初期位置に戻る
- exec(r,rep(5,f),d,d,l,f,f,r,f,r)
- -- 倉庫階のスタート位置(&向き)に移動する
- gotoWarehousePosFromInitPos()
- -- 貯蔵分の木材をチェストに保存する
- exec(l,f,r,rep(5,f),l)
- fillTightly()
- dropEx(1, storeWood, turtle.drop)
- -- 倉庫階のスタート位置(&向き)に戻る
- exec(l,rep(4,f),l)
- -- 残りの原木(チェスト用、松明用)を退避
- dropAll()
- -- 松明クラフト用に木材をクラフトする
- local req = torchWood
- while true do
- turtle.select(1)
- turtle.suck(math.min(req, 64))
- local cnt = turtle.getItemCount(1)
- req = req - cnt
- exec(l,l)
- for j = 1, 4 do
- local cnt = turtle.getItemCount(1)
- turtle.craft()
- if 16 < cnt then
- dropExceptSlot(1)
- else
- dropAll()
- break
- end
- end
- if 0 == req then
- break
- end
- exec(r,r)
- end
- -- 松明をクラフトする
- req = torchWood * 4
- local slots = { 1, 5 }
- while true do
- local cnt = 0
- for _, i in ipairs(slots) do
- turtle.select(i)
- turtle.suck(math.min(req / 2, 16))
- cnt = cnt + turtle.getItemCount(i)
- end
- req = req - cnt
- turtle.craft()
- turtle.select(1)
- turtle.transferTo(5)
- exec(r,f,f,r)
- turtle.suck(math.min(cnt * 2, 64))
- exec(l,l)
- for j = 1, 4 do
- local cnt2 = turtle.getItemCount(1)
- turtle.craft()
- if 16 < cnt2 then
- turtle.select(2)
- turtle.drop()
- else
- dropAll()
- break
- end
- end
- if 0 == req then
- break
- end
- exec(l,f,f,r)
- end
- -- 倉庫階のスタート位置(&向き)に移動する
- exec(l,rep(3,f),l,f,l)
- -- craftChests()の変な前提条件に合わせる
- fillTightly()
- turtle.select(1)
- turtle.transferTo(2)
- -- 残りの原木をすべて使ってチェストを作成する
- craftChests()
- -- 倉庫階のスタート位置(&向き)に移動する
- exec(r,f,l,rep(4,f),l)
- -- クラフトした木炭、松明、チェストの数を返す
- local rsltCharCnt = storeChar
- local rsltTorchCnt = torchChar * 4
- local rsltChestCnt = chestWood / 2
- return rsltCharCnt, rsltTorchCnt, rsltChestCnt
- end
- local function deliveryAction(charCnt, torchCnt, chestCnt)
- -- 4つの採掘拠点の補充用チェストに、木炭、松明、チェストを補充する
- local items = { }
- -- 木炭を取得
- exec(l,f,r,rep(5,f),r)
- local firstSlot = suckEx(charCnt, turtle.suck)
- if nil ~= firstSlot then
- items.charcoal = { }
- items.charcoal.slot = firstSlot
- items.charcoal.cnt = countEx(firstSlot)
- end
- --print("[char ]firstSlot = " .. tostring(firstSlot) .. " / cnt = " .. tostring(items.charcoal.cnt))
- -- 松明を取得
- exec(r,f,f,r)
- firstSlot = suckEx(torchCnt, turtle.suck)
- if nil ~= firstSlot then
- items.torch = { }
- items.torch.slot = firstSlot
- items.torch.cnt = countEx(firstSlot)
- end
- --print("[torch]firstSlot = " .. tostring(firstSlot) .. " / cnt = " .. tostring(items.torch.cnt))
- -- チェストを取得
- exec(l,f,r,rep(3,f),l,f,r)
- firstSlot = suckEx(chestCnt, turtle.suck)
- if nil ~= firstSlot then
- items.chest = { }
- items.chest.slot = firstSlot
- items.chest.cnt = countEx(firstSlot)
- end
- --print("[chest]firstSlot = " .. tostring(firstSlot) .. " / cnt = " .. tostring(items.chest.cnt))
- -- 採掘拠点に移動
- exec(l,f,r,f,l,f,d,d)
- exec(f,f,r,f,l,rep(3,f))
- exec(rep(4,d),r,f,l,f,u,f)
- for i = 1, 4 do
- exec(l)
- local sbj = items.charcoal
- if nil ~= sbj then
- turtle.select(sbj.slot)
- dropEx(sbj.slot, sbj.cnt / 4, turtle.drop)
- end
- exec(u)
- sbj = items.torch
- if nil ~= sbj then
- turtle.select(sbj.slot)
- dropEx(sbj.slot, sbj.cnt / 4, turtle.drop)
- end
- exec(u)
- sbj = items.chest
- if nil ~= sbj then
- turtle.select(sbj.slot)
- dropEx(sbj.slot, sbj.cnt / 4, turtle.drop)
- end
- exec(d,d,r)
- exec(f,f,l,b)
- end
- -- 倉庫のチェスト置場まで戻る
- exec(r,r,f,d,f)
- exec(r,f,rep(6,u),f,l,rep(6,f))
- -- 余ったアイテムを戻す(チェスト)
- sbj = items.chest
- if nil ~= sbj then
- if 0 < turtle.getItemCount(sbj.slot) then
- dropEx(sbj.slot, countEx(sbj.slot), turtle.drop)
- end
- end
- -- 余ったアイテムを戻す(松明)
- exec(r,f,l,rep(3,f),r)
- sbj = items.torch
- if nil ~= sbj then
- if 0 < turtle.getItemCount(sbj.slot) then
- dropEx(sbj.slot, countEx(sbj.slot), turtle.drop)
- end
- end
- -- 余ったアイテムを戻す(木炭)
- exec(l,f,r,rep(3,f),l,f,r)
- sbj = items.charcoal
- if nil ~= sbj then
- if 0 < turtle.getItemCount(sbj.slot) then
- dropEx(sbj.slot, countEx(sbj.slot), turtle.drop)
- end
- end
- -- 倉庫階のスタート位置(&向き)に移動する
- exec(r,rep(5,f),l,f,l)
- -- かまど階のスタート位置に戻る
- gotoInitPosFromWarehousePos()
- end
- -- -------------------------------
- -- main
- -- -------------------------------
- for i = 1, 10 do
- -- 現在のFuelLevelを取得
- print("[" .. tostring(i) .."] fuelLv = " .. tostring(turtle.getFuelLevel()))
- if turtle.getFuelLevel() < 5000 then
- error("stop")
- end
- -- 植林場のプログラム(lumberjack)を実行し、炭焼き小屋の初期位置に移動する
- lumberjackPart()
- if i == 1 then
- -- 炭焼き小屋での1回目のアクションを実行し、炭焼き小屋の初期位置に戻る
- firstAction()
- else
- -- 炭焼き小屋での2回目以降のアクションを実行し、倉庫部分の初期位置に戻る
- local charCnt, torchCnt, chestCnt = secondAction()
- -- 少しは倉庫に溜める
- --print(string.format("result chr=%d / torch=%d / chest=%d", charCnt, torchCnt, chestCnt))
- local deliverChar = math.floor(charCnt * 0.9)
- local deliverTorch = math.floor(torchCnt * 0.9)
- local deliverChest = math.floor(chestCnt * 0.9)
- --print(string.format("deliver chr=%d / torch=%d / chest=%d", deliverChar, deliverTorch, deliverChest))
- -- 4つの採掘拠点の補充用チェストに、木炭、松明、チェストを補充し、炭焼き小屋の初期位置に戻る
- deliveryAction(deliverChar, deliverTorch, deliverChest)
- end
- -- 植林場に移動する
- gotoPlantationFromCharcoalHat()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement