Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- --------
- -- chunkDig6
- -- --------
- local function showUsage()
- print("chunkDig6")
- --print("slot1 : torch x 64")
- --print("slot2 : wood x 1")
- end
- local INITIAL_HEIGHT = 58
- local BOTTOM_HEIGHT = 54 --5
- local WIDTH = 10
- local DEPTH = 10
- local SLOT_TORCH = 1
- local SLOT_CHEST = 2
- local SLOT_COBBLESTONE = 3
- local SLOT_STONE = 4
- local SLOT_GRAVEL = 5
- local SLOT_DART = 6
- local SLOTS_FREE = { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
- local SLOTS_USELESS_SAMPLE = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART, SLOT_COBBLESTONE }
- local SLOTS_KEEP_32 = { SLOT_COBBLESTONE }
- local SLOTS_KEEP_1 = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
- -- ---------------
- -- turtle position and direction
- -- ---------------
- local turtlePos = { x = 0, y = 0, z = 0, dir = 0, mineMove = {} }
- local dzWhenFwd = {[0]=1, [1]=0, [2]=-1, [3]= 0}
- local dxWhenFwd = {[0]=0, [1]=1, [2]= 0, [3]=-1}
- local moveLog = nil
- -- ---------------
- -- Move/Dig Functions
- -- ---------------
- function surelyUp()
- while not turtle.up() do turtle.digUp() end
- turtlePos.y = turtlePos.y + 1
- if nil ~= moveLog then
- table.insert(moveLog, "u")
- end
- end
- function surelyDown()
- while not turtle.down() do turtle.digDown() end
- turtlePos.y = turtlePos.y - 1
- if nil ~= moveLog then
- table.insert(moveLog, "d")
- 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
- turtlePos.x = turtlePos.x + dxWhenFwd[turtlePos.dir]
- turtlePos.z = turtlePos.z + dzWhenFwd[turtlePos.dir]
- if nil ~= moveLog then
- table.insert(moveLog, "f")
- end
- 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
- turtlePos.x = turtlePos.x + dxWhenFwd[turtlePos.dir]
- turtlePos.z = turtlePos.z + dzWhenFwd[turtlePos.dir]
- if nil ~= moveLog then
- table.insert(moveLog, "f")
- end
- 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 = surelyFwd -- function() while not turtle.forward() do end end
- --local b = function() while not turtle.back() do end end
- local l = function()
- turtle.turnLeft()
- turtlePos.dir = (turtlePos.dir + 4 - 1) % 4
- if nil ~= moveLog then
- table.insert(moveLog, "l")
- end
- end
- local r = function()
- turtle.turnRight()
- turtlePos.dir = (turtlePos.dir + 4 + 1) % 4
- if nil ~= moveLog then
- table.insert(moveLog, "r")
- end
- end
- local u = surelyUp
- local d = surelyDown
- 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 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 dropDownUselessBlocks()
- local cnt
- for _, v in ipairs(SLOTS_KEEP_1) do
- cnt = turtle.getItemCount(v)
- if 1 < cnt then
- turtle.select(v)
- turtle.dropDown(cnt - 1)
- end
- end
- for _, v in ipairs(SLOTS_KEEP_32) do
- cnt = turtle.getItemCount(v)
- if 32 < cnt then
- turtle.select(v)
- turtle.dropDown(cnt - 32)
- end
- end
- for _, v in ipairs(SLOTS_FREE) do
- cnt = turtle.getItemCount(v)
- if 0 < cnt then
- turtle.select(v)
- for _, w in ipairs(SLOTS_USELESS_SAMPLE) do
- if turtle.compareTo(w) then
- turtle.dropDown()
- break
- end
- end
- end
- end
- turtle.select(1)
- end
- -- ---------------
- -- refuelPrompt
- -- ---------------
- function refuelPrompt(moveFunc)
- local firstTime = true;
- while true do
- local rslt, msg;
- if nil ~= moveFunc then
- rslt, msg = moveFunc();
- else
- if firstTime then
- firstTime = false;
- rslt = false;
- msg = "Out of fuel";
- else
- return true;
- end
- end
- if not rslt and msg == "Out of fuel" then
- print("Out of fuel. please select [r]efuel or [q]uit.");
- print("(r/q)");
- local ch = read();
- if "q" == ch then
- print("Are you sure you want to stop the script?");
- print("(y/n)");
- ch = read();
- if "y" == ch then
- error("out of fuel.");
- end
- elseif "r" == ch then
- local selSlot = 1;
- while true do
- print("Please type current selected slot number.");
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- selSlot = num;
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- while true do
- print("please set fuel item and type slot number.");
- print("When you finish refueling, Please type q.");
- print("FuelLevel = " .. turtle.getFuelLevel());
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- turtle.select(num);
- local fRslt, fMsg = turtle.refuel();
- if not fRslt then
- print(fMsg);
- end
- elseif "q" == ch then
- turtle.select(selSlot);
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- end
- else
- return rslt, msg;
- 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
- -- --------
- local throughFlag = false
- local isTopHeight = true
- local function switchTopBottom()
- isTopHeight = not isTopHeight
- end
- local function returnToTopHeight()
- if not isTopHeight then
- exec(rep(3,e0,u))
- isTopHeight = true
- end
- end
- local function e1WorthOnly()
- local worth = true
- for i, v in ipairs(SLOTS_USELESS_SAMPLE) do
- turtle.select(v)
- if turtle.compare() then
- worth = false
- break
- end
- end
- turtle.select(1)
- if worth then
- e1()
- end
- end
- local function checkInventory()
- for i = 16,1,-1 do
- if 0 == turtle.getItemCount(i) then
- return true
- end
- end
- return false
- end
- local function returnAction()
- turtle.turnLeft()
- turtle.turnLeft()
- moveLog = nil
- local len = #turtlePos.mineMove
- for i = len, 1, -1 do
- local ch = turtlePos.mineMove[i]
- if "f" == ch then
- surelyFwd()
- elseif "u" == ch then
- surelyDown()
- elseif "d" == ch then
- surelyUp()
- elseif "l" == ch then
- turtle.turnRight()
- elseif "r" == ch then
- turtle.turnLeft()
- end
- end
- if turtle.getFuelLevel() < 1000 then
- refuelPrompt()
- end
- print("inventory is full.")
- print("take items and hit enter key.")
- io.read()
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, len do
- local ch = turtlePos.mineMove[i]
- if "f" == ch then
- surelyFwd()
- elseif "u" == ch then
- surelyUp()
- elseif "d" == ch then
- surelyDown()
- elseif "l" == ch then
- turtle.turnLeft()
- elseif "r" == ch then
- turtle.turnRight()
- end
- end
- moveLog = turtlePos.mineMove
- end
- local function checkReturn()
- if turtle.getFuelLevel() < 1000 then
- returnAction()
- end
- if false == checkInventory() then
- dropDownUselessBlocks()
- if false == checkInventory() then
- returnAction()
- end
- end
- end
- local function keepAndPlace(slotNo, placeFunc)
- if 1 == turtle.getItemCount(slotNo) then
- vacuum(slotNo)
- end
- if 2 <= turtle.getItemCount(slotNo) then
- turtle.select(slotNo)
- placeFunc()
- end
- end
- local function makeUpperRoom(torchFlg)
- exec(u,u,u)
- surelyDigUp()
- keepAndPlace(SLOT_COBBLESTONE, turtle.placeUp)
- surelyDig()
- keepAndPlace(SLOT_COBBLESTONE, turtle.place)
- surelyDown()
- if torchFlg then
- keepAndPlace(SLOT_TORCH, turtle.placeUp)
- end
- surelyDig()
- surelyDown()
- surelyDig()
- surelyDown()
- surelyDig()
- end
- local function fallPreventionDown()
- if turtlePos.y == -1 then
- keepAndPlace(SLOT_COBBLESTONE, turtle.place)
- elseif turtlePos.y == -2 then
- keepAndPlace(SLOT_COBBLESTONE, turtle.placeUp)
- end
- end
- local function fallPreventionUp()
- if turtlePos.y == -1 then
- keepAndPlace(SLOT_COBBLESTONE, turtle.place)
- elseif turtlePos.y == 0 then
- keepAndPlace(SLOT_COBBLESTONE, turtle.placeDown)
- end
- end
- local function miningAction()
- level = INITIAL_HEIGHT - BOTTOM_HEIGHT
- if throughFlag then
- throughFlag = not throughFlag
- else
- throughFlag = not throughFlag
- if isTopHeight then
- makeUpperRoom(true)
- turtlePos.mineMove = {}
- moveLog = turtlePos.mineMove
- exec(rep(level,checkReturn,fallPreventionDown,e2,d,checkReturn,e1WorthOnly),switchTopBottom)
- else
- exec(rep(level,checkReturn,e0,checkReturn,
- e1WorthOnly,fallPreventionUp,u,fallPreventionUp),switchTopBottom)
- makeUpperRoom(false)
- end
- end
- end
- while true do
- exec(
- rep(WIDTH,
- rep(DEPTH,
- breakLast,miningAction,checkReturn,e1,f
- ),
- anotherLast(
- {
- swOddEven({r},{l}),miningAction,checkReturn,e1,f,swOddEven({r},{l})
- },
- {
- returnToTopHeight,
- swOddEven(
- {l,rep(WIDTH-1,f),l,rep(DEPTH-1,f),l,l},
- {r,rep(WIDTH-1,f),r})
- }
- )
- )
- )
- break
- end
- --[[
- local SLOT_TORCH = 1
- local SLOT_CHEST = 2
- local SLOT_COBBLESTONE = 3
- local SLOT_STONE = 4
- local SLOT_GRAVEL = 5
- local SLOT_DART = 6
- local SLOTS_FREE = { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
- local SLOTS_USELESS_SAMPLE = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART, SLOT_COBBLESTONE }
- local SLOTS_KEEP_ALL = { SLOT_TORCH, SLOT_CHEST }
- local SLOTS_KEEP_32 = { SLOT_COBBLESTONE }
- local SLOTS_KEEP_1 = { SLOT_STONE, SLOT_GRAVEL, SLOT_DART }
- local INITIAL_HEIGHT = 58
- local BOTTOM_HEIGHT = 5
- local LOW_FUEL_LEVEL = 300
- local SIZE_LEFT = 80
- local SIZE_FORWARD = 80
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement