Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local monitor = term.current()
- local sizeX,sizeY = monitor.getSize()
- -- Logging
- local lastMsg = ""
- local lastMsgCount = 0
- local function logMsg(msg)
- monitor.setCursorPos(1,13)
- monitor.clearLine()
- if lastMsg == msg then
- lastMsgCount = lastMsgCount + 1
- msg = msg .. " " .. lastMsgCount
- else
- lastMsg = msg
- lastMsgCount = 0
- monitor.scroll(1)
- end
- monitor.setCursorPos(1,12)
- monitor.clearLine()
- monitor.write(msg)
- monitor.setCursorPos(1,13)
- end
- local function msg(msg)
- monitor.setCursorPos(1,13)
- monitor.clearLine()
- monitor.write(msg)
- end
- -- Turtle Inventory
- local function getItemList()
- local itemList = {}
- for slot=1, 16 do
- itemList[slot] = turtle.getItemDetail(slot)
- end
- return itemList
- end
- local function findEmptySlot(itemList, size)
- for slot=1, size do
- if not itemList[slot] then
- return true, slot
- end
- end
- return false, nil
- end
- local function isStairs(itemName)
- return string.match(itemName, "_stairs$") ~= nil
- end
- local function isTorch(itemName)
- return string.match(itemName, "minecraft:torch") ~= nil
- end
- local function selectStairSlot()
- for slot = 1, 16 do
- local itemDetail = turtle.getItemDetail(slot)
- if itemDetail ~= nil and isStairs(itemDetail.name) then
- turtle.select(slot)
- return true
- end
- end
- return false
- end
- local function selectTorchSlot()
- for slot = 1, 16 do
- local itemDetail = turtle.getItemDetail(slot)
- if itemDetail ~= nil and isTorch(itemDetail.name) then
- turtle.select(slot)
- return true
- end
- end
- return false
- end
- local function refuel()
- for slot = 1, 16 do
- local itemDetail = turtle.getItemDetail(slot)
- turtle.select(slot)
- if itemDetail ~= nil and turtle.refuel(1) then
- return true
- end
- end
- return false
- end
- -- Setup
- if #tArgs == 0 then
- logMsg("To start digging a stair type:")
- logMsg("ccstairs <1>")
- logMsg("<1>: the number of floors to dig.")
- return 0
- end
- local floorsToMine = tArgs[1] + 0
- local function startup()
- local startupSuccess = true
- local items = getItemList()
- local freeSlotCount = 0
- local stairsCount = 0
- local torchesCount = 0
- for slot = 1, 16 do
- if not items[slot] then
- freeSlotCount = freeSlotCount + 1
- elseif isStairs(items[slot].name) then
- stairsCount = stairsCount + items[slot].count
- elseif isTorch(items[slot].name) then
- torchesCount = torchesCount + items[slot].count
- end
- end
- local slotsNeeded = 0
- if floorsToMine <= 2 then
- slotsNeeded = floorsToMine * 4
- else
- slotsNeeded = 8 + ((floorsToMine - 2) * 5)
- end
- slotsNeeded = math.ceil(slotsNeeded / 64)
- local fuelNeeded = 0
- if floorsToMine == 1 then
- fuelNeeded = 1
- elseif floorsToMine == 2 then
- fuelNeeded = 3
- else
- fuelNeeded = 3 + ((floorsToMine - 2) * 4)
- end
- logMsg("There is " .. freeSlotCount .. " free slots.")
- logMsg("There is " .. stairsCount .. " stairs.")
- logMsg("There is " .. torchesCount .. " torches.")
- logMsg("There is " .. turtle.getFuelLevel() .. " fuel left.")
- if freeSlotCount < slotsNeeded then
- startupSuccess = false
- logMsg("Not enough free slots ! (".. freeSlotCount .. "/" .. slotsNeeded .. ")")
- end
- if stairsCount < floorsToMine then
- startupSuccess = false
- logMsg("Not enough stairs ! (".. stairsCount .. "/" .. floorsToMine .. ")")
- end
- if floorsToMine > 2 and torchesCount < floorsToMine / 2 then
- startupSuccess = false
- logMsg("Not enough torches ! (".. torchesCount .. "/" .. (floorsToMine / 2) .. ")")
- end
- while turtle.getFuelLevel() < fuelNeeded and refuel() do
- end
- if turtle.getFuelLevel() < fuelNeeded then
- startupSuccess = false
- logMsg("Not enough fuel ! (".. fuelLevel .. "/" .. fuelNeeded .. ")")
- end
- return startupSuccess
- end
- if not startup() then
- return 0
- end
- -- Digging
- local function move(direction)
- local movement = nil
- if direction == "forward" then
- movement = turtle.forward
- elseif direction == "up" then
- movement = turtle.up
- elseif direction == "down" then
- movement = turtle.down
- else
- error("Error: Bad direction")
- end
- while not movement() do
- -- fuel check
- if turtle.getFuelLevel() < 1 then
- refuel()
- else
- msg("Can't move " .. direction)
- end
- end
- end
- local function dig(direction)
- local detect = nil
- local dig = nil
- local inspect = nil
- if direction == "forward" then
- detect = turtle.detect
- dig = turtle.dig
- inspect = turtle.inspect
- elseif direction == "up" then
- detect = turtle.detectUp
- dig = turtle.digUp
- inspect = turtle.inspectUp
- elseif direction == "down" then
- detect = turtle.detectDown
- dig = turtle.digDown
- inspect = turtle.inspectDown
- else
- error("Error: Bad direction")
- end
- while detect() do
- if not dig() then
- local block = inspect()
- if block == nil then
- block = {name = "unknown"}
- end
- msg("Can't dig " .. direction .. " " .. block.name)
- end
- end
- end
- local function placeStair()
- if selectStairSlot() then
- while not turtle.turnRight() do
- end
- while not turtle.turnRight() do
- end
- while not turtle.placeDown() do
- end
- while not turtle.turnRight() do
- end
- while not turtle.turnRight() do
- end
- return true
- end
- return false
- end
- local function digFirstLevel()
- dig("up")
- dig("forward")
- move("forward")
- dig("up")
- dig("down")
- return placeStair()
- end
- local function digSecondLevel()
- dig("forward")
- move("forward")
- dig("up")
- dig("down")
- move("down")
- dig("down")
- return placeStair()
- end
- local function digRemainingLevels(floorNumber)
- move("up")
- dig("forward")
- move("forward")
- dig("up")
- if floorNumber % 2 == 0 then
- if selectTorchSlot() then
- turtle.placeUp()
- end
- end
- dig("down")
- move("down")
- dig("down")
- move("down")
- dig("down")
- return placeStair()
- end
- for i = 1, floorsToMine do
- if i == 1 then
- digFirstLevel()
- elseif i == 2 then
- digSecondLevel()
- else
- digRemainingLevels(i)
- end
- end
- monitor.scroll(1)
- monitor.setCursorPos(1,13)
Add Comment
Please, Sign In to add comment