Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("beacon") -- pastebin get hjGie0QG beacon
- -- ----------------------------------------------
- -- Utilities
- -- ----------------------------------------------
- -- 第2引数以降には、関数や関数のリストを指定する。リスト内に更にリストが入っていても良い。
- -- executeFunction()に渡す際には「toFlatList()」で入れ子の無いリストにすること。
- function repeatList(count, ...)
- local rslt = { }
- for i = 1, count do
- table.insert(rslt, {...})
- end
- return rslt
- end
- function toFlatList(...)
- local rslt = { }
- local list = {...}
- for i = 1, #list do
- if "table" == type(list[i]) then
- for j = 1, #list[i] do
- if "table" == type(list[i][j]) then
- local tmp = toFlatList(list[i][j])
- for k = 1, #tmp do
- table.insert(rslt, tmp[k])
- end
- else
- table.insert(rslt, list[i][j])
- end
- end
- else
- table.insert(rslt, list[i])
- end
- end
- return rslt
- end
- function executeFunction(functionList)
- for i = 1, #functionList do
- functionList[i]()
- end
- end
- function selectItem(nameAndDamage)
- for i = 1, 16 do
- local d = turtle.getItemDetail(i)
- if nil ~= d then
- if d.name == nameAndDamage.name and d.damage == nameAndDamage.damage then
- turtle.select(i)
- return true
- end
- end
- end
- return false
- end
- function getTotalItemCount(name, damage)
- local count = 0
- for i = 1, 16 do
- local dat = turtle.getItemDetail(i)
- if nil ~= dat then
- if dat.name == name and dat.damage == damage then
- count = count + turtle.getItemCount(i)
- end
- end
- end
- return count
- end
- function dropEx(name, damage, keepCount)
- local count = 0
- for i = 1, 16 do
- local dat = turtle.getItemDetail(i)
- if nil ~= dat then
- if dat.name == name and dat.damage == damage then
- local tmp = turtle.getItemCount(i)
- if count + tmp <= keepCount then
- count = count + tmp
- else
- turtle.select(i)
- turtle.dropDown(count + tmp - keepCount)
- end
- end
- end
- end
- end
- function doSelfRefuel(inspectFunc, placeFunc)
- local function findOne(name, metadata)
- for i = 1, 16 do
- if 1 == turtle.getItemCount(i) then
- local detail = turtle.getItemDetail(i)
- if nil ~= detail and detail.name == name and detail.damage == metadata then
- return i
- end
- end
- end
- return -1
- end
- local selected = turtle.getSelectedSlot()
- local rslt, inspect = inspectFunc()
- if true == rslt and inspect.name == "minecraft:flowing_lava" and inspect.metadata == 0 then
- local bucketSlot = findOne("minecraft:bucket", 0) -- とりあえず空バケツ1個の場合だけ実装する
- if -1 ~= bucketSlot then
- turtle.select(bucketSlot)
- placeFunc() -- 溶岩を汲む
- local lavaBucketSlot = findOne("minecraft:lava_bucket", 0)
- if -1 ~= lavaBucketSlot then
- turtle.select(lavaBucketSlot)
- turtle.refuel()
- end
- end
- end
- turtle.select(selected)
- end
- function selfRefuelUp()
- return doSelfRefuel(turtle.inspectUp, turtle.placeUp)
- end
- function selfRefuel()
- return doSelfRefuel(turtle.inspect, turtle.place)
- end
- function selfRefuelDown()
- return doSelfRefuel(turtle.inspectDown, turtle.placeDown)
- end
- function vacuumAll()
- local dA, dB
- for i = 1, 15 do
- if 0 < turtle.getItemCount(i) and 0 < turtle.getItemSpace(i) then
- dA = turtle.getItemDetail(i)
- for j = i + 1, 16 do
- if 0 < turtle.getItemCount(j) and 0 < turtle.getItemSpace(j) then
- dB = turtle.getItemDetail(j)
- if dA.name == dB.name and dA.damage == dB.damage then
- turtle.select(j)
- turtle.transferTo(i)
- if 0 == turtle.getItemSpace(i) then
- break
- end
- end
- end
- end
- end
- end
- end
- -- ----------------------------------------------
- -- Config
- -- ----------------------------------------------
- local INSPECT__USELESS_BLOCKS = {
- { name = "minecraft:cobblestone", metadata = 0, keepCount = 64 },
- { name = "minecraft:dirt" , metadata = 0, keepCount = 0 },
- { name = "minecraft:gravel" , metadata = 0, keepCount = 0 },
- { name = "minecraft:stone" , metadata = 0, keepCount = 0 }
- }
- local MODEM_DIRECTION = "right"
- -- ----------------------------------------------
- -- Application
- -- ----------------------------------------------
- local ITEM_DETAIL__TORCH = { name = "minecraft:torch" , damage = 0 }
- local ITEM_DETAIL__COBBLE_STONE = { name = "minecraft:cobblestone", damage = 0 }
- local ITEM_DETAIL__CHEST = { name = "minecraft:chest" , damage = 0 }
- local downCount = 0
- local E0 = function()
- local sel = turtle.getSelectedSlot()
- if 1 == sel then
- selfRefuelUp()
- turtle.digUp()
- else
- turtle.select(1)
- selfRefuelUp()
- turtle.digUp()
- turtle.select(sel)
- end
- end
- local E1 = function()
- local sel = turtle.getSelectedSlot()
- if 1 == sel then
- selfRefuel()
- turtle.dig()
- else
- turtle.select(1)
- selfRefuel()
- turtle.dig()
- turtle.select(sel)
- end
- end
- local E2 = function()
- local sel = turtle.getSelectedSlot()
- if 1 == sel then
- selfRefuelDown()
- turtle.digDown()
- else
- turtle.select(1)
- selfRefuelDown()
- turtle.digDown()
- turtle.select(sel)
- end
- end
- local F = function()
- while false == turtle.forward() do
- turtle.dig()
- if 0 < downCount then
- turtle.attack()
- end
- end
- return true
- end
- local U = function()
- while false == turtle.up() do
- turtle.digUp()
- if 0 < downCount then
- turtle.attackUp()
- end
- end
- return true
- end
- local D = function()
- while false == turtle.down() do
- turtle.digDown()
- if 0 < downCount then
- turtle.attackDown()
- end
- end
- return true
- end
- local L = turtle.turnLeft
- local R = turtle.turnRight
- local P0 = function() selfRefuelUp() turtle.placeUp() end
- local P1 = function() selfRefuel() turtle.place() end
- local P2 = function() selfRefuelDown() turtle.placeDown() end
- local SelCobble = function() local r = selectItem(ITEM_DETAIL__COBBLE_STONE) return r end
- local SelTorch = function() local r = selectItem(ITEM_DETAIL__TORCH) return r end
- local SelectChest = function() local r = selectItem(ITEM_DETAIL__CHEST) return r end
- local function isUseful(inspectFunction)
- local flg, data = inspectFunction()
- if false == flg then
- return false
- end
- for i = 1, #INSPECT__USELESS_BLOCKS do
- local useless = INSPECT__USELESS_BLOCKS[i]
- local totalCnt = getTotalItemCount(useless.name, useless.metadata)
- if data.name == useless.name and data.metadata == useless.metadata then
- if useless.keepCount < getTotalItemCount(useless.name, useless.metadata) then
- return true
- else
- return false
- end
- end
- end
- return true
- end
- local dropUselessBlocks = function()
- for i = 1, #INSPECT__USELESS_BLOCKS do
- local useless = INSPECT__USELESS_BLOCKS[i]
- local totalCnt = getTotalItemCount(useless.name, useless.metadata)
- if useless.keepCount < getTotalItemCount(useless.name, useless.metadata) then
- dropEx(useless.name, useless.metadata, useless.keepCount)
- end
- end
- end
- local ResetDownCount = function()
- downCount = 0
- end
- local MineDown = function()
- vacuumAll()
- while true do
- if false == turtle.digDown() and true == turtle.detectDown() then
- return true
- end
- D()
- downCount = downCount + 1
- if 2 == downCount then
- SelCobble()
- P0()
- end
- if true == isUseful(turtle.inspect) then
- E1()
- end
- if 1 == downCount then
- SelCobble()
- E1()
- P1()
- end
- end
- end
- local MineUp = function()
- dropUselessBlocks()
- vacuumAll()
- for i = downCount, 1, -1 do
- if true == isUseful(turtle.inspect) then
- turtle.dig()
- end
- if 1 == downCount then
- SelCobble()
- E1()
- P1()
- end
- E0()
- U()
- downCount = downCount - 1
- if 0 == downCount then
- SelCobble()
- P2()
- end
- end
- return true
- end
- -- 奥行き(前):8、幅(右):6、高さ(上):4の空間を作成する。
- -- 漏水や溶岩を防ぐため、掘削対象の空間と天井を張り直す。
- -- 天井の、前:3、右:1の位置に丸石、前:4、右:1の位置に松明を設置する。
- local MAKE_ROOM_LINE = {
- repeatList(4,
- SelCobble, P2,
- repeatList(3, SelCobble, P0, E0, U),
- SelCobble, E0, P0,
- SelCobble, P1, E1, F,
- SelCobble, E0, P0,
- repeatList(3, SelCobble, P2, E2, D),
- SelCobble, P2, SelCobble, P1, E1, F
- ),
- L, L, E1, F, L, L
- }
- local MAKE_ROOM = {
- repeatList(3,
- MAKE_ROOM_LINE, SelCobble, P1, R, SelCobble, P1, E1, F, R,
- MAKE_ROOM_LINE, L, SelCobble, P1, E1, F, L,
- dropUselessBlocks
- )
- }
- local MAKE_LIGHT_AND_BACK_HOME = {
- L, E1, F, L, L, SelCobble, P1, L,
- repeatList(3, E0, U),
- repeatList(3, E1, F), L,
- repeatList(2, E1, F), SelCobble, E1, P1, L, L, E1, F, L, L,
- SelTorch, E1, P1, L, E1, F, R,
- repeatList(4, E1, F), L,
- repeatList(2, E1, F), L, L,
- repeatList(3, E2, D)
- }
- local MINING_FL = {
- ResetDownCount, MineDown, L, MineUp
- }
- local MINING = {
- repeatList(2,
- R,
- MINING_FL, R, E1, F, E1, F, L, E1, F, L,
- MINING_FL, E1, F, L, E1, F,
- MINING_FL, R, E1, F, E1, F, L, E1, F, L,
- MINING_FL, L, L, E1, F, E1, F, L,
- MINING_FL, E1, F, R, E1, F, E1, F, L, L,
- MINING_FL, E1, F, L, E1, F,
- MINING_FL, E1, F, R, E1, F, E1, F, L, L,
- MINING_FL, E1, F, E1, F,
- dropUselessBlocks,
- SelectSlot1
- )
- }
- local main = function()
- local initialFuelLevel = turtle.getFuelLevel()
- executeFunction(
- toFlatList(
- MAKE_ROOM,
- MAKE_LIGHT_AND_BACK_HOME,
- MINING
- )
- )
- vacuumAll()
- print("completed.")
- local fuel = initialFuelLevel - turtle.getFuelLevel()
- print("consumed " .. tostring(fuel) .. " fuel.")
- end
- beacon.TRANSMITTER_MODEM_DIRECTION = MODEM_DIRECTION
- parallel.waitForAll(main, beacon.transmitter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement