Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- for Minecraft (ComputerCraft mod)
- -- config & start
- local chestsSlot = 16
- local delay = 0
- local distance = 0
- local side = 'r'
- local useChests = true
- local height = 1
- local goDown = false
- local autoStart = false
- local function setStart(args) -- [dist] <side> <chests> <auto start> <height)>
- if #args == 0 then
- return false
- end
- if tonumber(args[1]) == nil then
- return false
- else
- distance = args[1]
- end
- for i = 2, #args do
- a = args[i]
- h = tonumber(a)
- if h ~= nil then
- if h < 0 then
- goDown = true
- end
- height = math.abs(h)
- elseif a == 'l' or a == 'r' then
- side = a
- elseif a == 'c' then
- useChests = false
- elseif a == 's' then
- autoStart = true
- elseif a == 'u' then
- goDown = true
- end
- end
- return true
- end
- local function showWarn()
- print("Error")
- print("Digs a box with horizontal edge lenght [d]")
- print("Usage: " .. shell.getRunningProgram() .. " [d] <l/r> <c> <s> <n>")
- print("d - hor. edge lenght")
- print("l/r - left/right")
- print("c - disable inventory management")
- print("s - silent start")
- print("n - number = height/3")
- print("u - go down [goes up by default]")
- end
- local function writeStat(done, total)
- term.clear()
- term.setCursorPos(1,1)
- done = math.ceil(done/total * 100)
- print("Progress "..done.."%")
- end
- -- core
- local function clearInv()
- if not useChests then
- return
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return
- end
- end
- local backupSlot = turtle.getSelectedSlot()
- turtle.select(chestsSlot)
- turtle.placeDown()
- for i = 1, 16 do
- if i ~= chestsSlot then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- turtle.select(backupSlot)
- end
- local function safeDigUp(s)
- while turtle.detectUp() do
- turtle.digUp()
- if not (s == 0 or s == nil) then
- sleep(s)
- end
- end
- end
- local function step(s)
- while turtle.detect() do
- turtle.dig()
- if not (s == 0 or s == nil) then
- sleep(s)
- end
- end
- safeDigUp(s)
- if turtle.detectDown() then
- turtle.digDown()
- end
- clearInv()
- while not turtle.forward() do
- sleep(1)
- end
- cntr = cntr + 1
- return true
- end
- local function line(l)
- for i = 1, l do
- step(delay)
- writeStat(cntr, totalDist)
- end
- end
- local function tTurn(inverted)
- inv = inverted or false
- if side == 'l' or side == 'L' then
- if not inv then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- if side == 'r' or side == 'R' then
- if not inv then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- end
- local function room()
- distLeft = distance - 1
- while distLeft >= 1 do
- line(distLeft)
- tTurn()
- line(distLeft)
- tTurn()
- line(distLeft)
- tTurn()
- distLeft = distLeft - 1
- line(distLeft)
- tTurn()
- if distLeft >= 1 then
- line(1)
- end
- distLeft = distLeft - 1
- end
- turtle.digDown()
- safeDigUp(delay)
- end
- local function home()
- if cntr == 1 then
- return
- end
- for i = 1, math.floor( (distance-1)/2) do
- turtle.back()
- end
- tTurn()
- for i = 1, math.floor( distance/2 ) do
- turtle.back()
- end
- tTurn(true)
- if goDown then
- turtle.down()
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- else
- turtle.up()
- safeDigUp(delay)
- turtle.up()
- safeDigUp(delay)
- turtle.up()
- end
- end
- local function returnToStart()
- for i = 1, math.floor( (distance-1)/2) do
- turtle.back()
- end
- tTurn()
- for i = 1, math.floor( distance/2 ) do
- turtle.back()
- end
- tTurn(true)
- for i = 1, (height-1)*3 do
- if goDown then
- turtle.up()
- else
- turtle.down()
- end
- end
- end
- -- main
- args = { ... }
- cntr = 1
- if not setStart(args) then
- showWarn()
- return
- end
- if useChests then
- if turtle.getItemCount(chestsSlot) == 0 then
- print("No chests found. Some loot may be lost. Proceed anyway? (y)")
- q = read()
- if not (q == "y" or q == "Y") then
- return
- end
- end
- end
- totalDist = distance * distance * height + ((height-1) * 3) +
- (math.floor( (distance-1)/2) + math.floor( distance/2 )) * (height-1)
- print("Fuel: "..turtle.getFuelLevel().." / "..totalDist)
- if turtle.getFuelLevel() < totalDist then
- print("\n\nNot enought fuel.\nProgram will be aborted")
- return nil
- else
- print(" ...OK")
- end
- if not useChests then
- print("Inventory management disabled")
- end
- if not autoStart then
- print("\nPress <Enter> to start".."\n".."Or hold <Ctrl + T> to abort")
- read()
- end
- for i = 1, height do
- home()
- room()
- end
- returnToStart()
- term.clear()
- term.setCursorPos(1, 1)
- print("Done!".."\n".."Total steps: "..tostring(cntr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement