Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("------------")
- print("digDown v1.0")
- print("------------")
- -- Constants
- local params = {...}
- local enderChestItem = "kibe:entangled_chest"
- -- Variables
- local posX = 0 -- rot 0 = forward
- local posY = 0 -- rot 1 = right
- local posZ = 0 -- rot 2 = back
- local rot = 0 -- rot 3 = left
- local lengthX
- local lengthY
- local lengthZ
- local hasEnderChest = false
- local isNewSession = true
- -- Prototypes
- local ParseParams
- local ReadLengthParams
- local CheckFuel
- local ResumeSession
- local SaveSessionStatic
- local SaveSession
- local LoadSessionStatic
- local LoadSession
- local RemoveSession
- local CheckEnderChest
- local CheckForFullInventory
- local StoreItems
- local Forward
- local Up
- local Down
- local ForceForward
- local ForceUp
- local ForceDown
- local Right
- local Left
- local Dig
- local DigUp
- local DigDown
- local Face
- local MoveX
- local MoveY
- local MoveZ
- local GoDig
- local GetLevel
- local GetTargetX
- local GetTargetY
- local GetTargetZ
- local DigZ
- -- Parse params
- ParseParams = function()
- if (#params == 1 and params[1] == "resume") then
- local hasValidLastSession = ResumeSession()
- isNewSession = false
- return hasValidLastSession
- elseif (#params == 3) then
- local hasValidParams = ReadLengthParams()
- if (not hasValidParams) then
- return false
- end
- local hasEnoughtFuel = CheckFuel()
- if (not hasEnoughtFuel) then
- return false
- end
- isNewSession = true
- return true
- else
- print("Usage:")
- print(" digDown [lengthX] [lengthY] [lenghtZ]")
- print(" The turle will dig down to the left from the turtle's position")
- print(" digDown resume")
- print(" Resumes the last saved digDown session")
- return false
- end
- end
- ReadLengthParams = function()
- -- Validade Params
- if (tonumber(params[1]) < 1) then
- term.write("Param [" .. params[1] .. "] is invalid")
- return false
- elseif (tonumber(params[2]) < 1) then
- term.write("Param [" .. params[2] .. "] is invalid")
- return false
- elseif (tonumber(params[3]) < 1) then
- term.write("Param [" .. params[3] .. "] is invalid")
- return false
- end
- lengthX = tonumber(params[1])
- lengthY = tonumber(params[2])
- lengthZ = tonumber(params[3])
- return true
- end
- CheckFuel = function()
- local fuelRequired = lengthX * lengthZ * math.ceil(lengthY / 3)
- fuelRequired = fuelRequired + lengthX + lengthY + lengthZ
- if (lengthY > 1) then
- fuelRequired = fuelRequired + math.floor(lengthY / 3) * 3
- end
- if (turtle.getFuelLevel() < fuelRequired) then
- term.write("Fuel required: " .. fuelRequired .. ", turtle fuel: " .. turtle.getFuelLevel())
- return false
- end
- return true
- end
- ResumeSession = function()
- local loadedSessionStatic = LoadSessionStatic()
- if (not loadedSessionStatic) then
- return false
- end
- local loadedSession = LoadSession()
- if (not loadedSession) then
- return false
- end
- return true
- end
- -- File
- SaveSessionStatic = function()
- if (not fs.exists(".digDown/")) then
- fs.makeDir(".digDown/")
- end
- local file = fs.open(".digDown/lastSessionStatic.txt", "w")
- file.write(tostring(lengthX) .. "\n")
- file.write(tostring(lengthY) .. "\n")
- file.write(tostring(lengthZ) .. "\n")
- file.close()
- end
- SaveSession = function()
- if (not fs.exists(".digDown/")) then
- fs.makeDir(".digDown/")
- end
- local file = fs.open(".digDown/lastSession.txt", "w")
- file.write(tostring(posX) .. "\n")
- file.write(tostring(posY) .. "\n")
- file.write(tostring(posZ) .. "\n")
- file.write(tostring(rot) .. "\n")
- file.close()
- end
- LoadSessionStatic = function()
- if (not fs.exists(".digDown/lastSessionStatic.txt")) then
- return false
- end
- local file = fs.open(".digDown/lastSessionStatic.txt", "r")
- lengthX = tonumber(file.readLine())
- lengthY = tonumber(file.readLine())
- lengthZ = tonumber(file.readLine())
- file.close()
- return true
- end
- LoadSession = function()
- if (not fs.exists(".digDown/lastSession.txt")) then
- return false
- end
- local file = fs.open(".digDown/lastSession.txt", "r")
- posX = tonumber(file.readLine())
- posY = tonumber(file.readLine())
- posZ = tonumber(file.readLine())
- rot = tonumber(file.readLine())
- file.close()
- return true
- end
- RemoveSession = function()
- if (fs.exists(".digDown/lastSessionStatic.txt")) then
- fs.delete(".digDown/lastSessionStatic.txt")
- end
- if (fs.exists(".digDown/lastSession.txt")) then
- fs.delete(".digDown/lastSession.txt")
- end
- end
- -- Chest
- CheckEnderChest = function()
- turtle.select(1)
- local info = turtle.getItemDetail()
- if (info ~= nil and info.name == enderChestItem) then
- hasEnderChest = true
- end
- end
- StoreItems = function(side)
- if (hasEnderChest) then
- turtle.select(1)
- if (side == 1) then
- turtle.place()
- for i = 2, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- turtle.dig()
- elseif (side == 2) then
- turtle.placeUp()
- for i = 2, 16 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(1)
- turtle.digUp()
- elseif (side == 3) then
- turtle.placeDown()
- for i = 2, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(1)
- turtle.digDown()
- end
- end
- end
- CheckForFullInventory = function(side)
- if (turtle.getItemCount(16) ~= 0) then
- CheckEnderChest()
- StoreItems(side)
- end
- end
- -- Moviment
- Forward = function()
- if (turtle.forward()) then
- if (rot == 0) then
- posZ = posZ + 1
- elseif (rot == 1) then
- posX = posX - 1
- elseif (rot == 2) then
- posZ = posZ - 1
- elseif (rot == 3) then
- posX = posX + 1
- end
- SaveSession()
- return true
- end
- return false
- end
- Up = function()
- if (turtle.up()) then
- posY = posY + 1
- SaveSession()
- return true
- end
- return false
- end
- Down = function()
- if (turtle.down()) then
- posY = posY - 1
- SaveSession()
- return true
- end
- return false
- end
- ForceForward = function()
- while Forward() == false do
- end
- end
- ForceUp = function()
- while Up() == false do
- end
- end
- ForceDown = function()
- while Down() == false do
- end
- end
- Right = function()
- turtle.turnRight()
- rot = (rot + 1) % 4
- SaveSession()
- end
- Left = function()
- turtle.turnLeft()
- rot = (rot - 1) % 4
- SaveSession()
- end
- Dig = function()
- while (turtle.detect()) do
- turtle.dig()
- end
- CheckForFullInventory(1)
- end
- DigUp = function()
- while (turtle.detectUp()) do
- turtle.digUp()
- end
- CheckForFullInventory(2)
- end
- DigDown = function()
- while (turtle.detectDown()) do
- turtle.digDown()
- end
- CheckForFullInventory(3)
- end
- Face = function(direction)
- if (rot == direction) then
- return
- end
- local map = {function()
- Right()
- end, function()
- Right()
- Right()
- end, function()
- Left()
- end};
- local mapIndex = (4 + direction - rot) % 4
- map[mapIndex]()
- end
- MoveX = function(x, preWork, postWork)
- local face
- if (posX == x) then
- return
- elseif (posX > x) then
- face = 1
- else
- face = 3
- end
- while (posX ~= x) do
- if (preWork) then
- preWork()
- end
- Face(face)
- Dig()
- ForceForward()
- if (postWork) then
- postWork()
- end
- end
- end
- MoveY = function(y, preWork, postWork)
- local dig
- local move
- if (posY == y) then
- return
- elseif (posY > y) then
- dig = DigDown
- move = ForceDown
- else
- dig = DigUp
- move = ForceUp
- end
- while (posY ~= y) do
- if (preWork) then
- preWork()
- end
- dig()
- move()
- if (postWork) then
- postWork()
- end
- end
- end
- MoveZ = function(z, preWork, postWork)
- local face
- if (posZ == z) then
- return
- elseif (posZ < z) then
- face = 0
- else
- face = 2
- end
- while (posZ ~= z) do
- if (preWork) then
- preWork()
- end
- Face(face)
- Dig()
- ForceForward()
- if (postWork) then
- postWork()
- end
- end
- end
- -- Functions
- GoDig = function()
- if (posY + 1 <= 0) then
- DigUp()
- end
- if (posY - 1 > -lengthY) then
- DigDown()
- end
- if (rot == 0) then
- if (posZ + 1 <= lengthZ) then
- Dig()
- end
- elseif (rot == 2) then
- if (posZ - 1 > 0) then
- Dig()
- end
- end
- end
- GetLevel = function()
- return math.floor(math.abs(posY) / 3)
- end
- GetTargetX = function()
- local level = GetLevel()
- local levelMod = level % 2
- if (levelMod == 0) then
- return lengthX - 1
- else
- return 0
- end
- end
- GetTargetY = function(level)
- local mod = lengthY % 3
- if (mod == 1) then
- return level * -3
- else
- return (level * -3) - 1
- end
- end
- GetTargetZ = function()
- local level = GetLevel()
- local levelMod = level % 2
- local posMod = posX % 2
- if (levelMod == posMod) then
- return lengthZ
- else
- return 1
- end
- end
- DigZ = function()
- local targetZ = GetTargetZ()
- MoveZ(targetZ, GoDig, GoDig)
- end
- -- Dig Loop
- -- ::dig::
- ParseParams()
- print("Now digging...")
- print(" " .. lengthX * lengthY * lengthZ .. " blocks")
- local levels = math.ceil(lengthY / 3)
- if (isNewSession) then
- SaveSessionStatic()
- SaveSession()
- Dig()
- ForceForward()
- if (lengthY > 1) then
- DigDown()
- ForceDown()
- end
- end
- local level = GetLevel()
- repeat
- -- Navigate X
- local targetX = GetTargetX()
- MoveX(targetX, DigZ, DigZ)
- level = level + 1
- if (level < levels) then
- local targetY = GetTargetY(level)
- MoveY(targetY)
- end
- until level == levels
- CheckEnderChest()
- if (hasEnderChest) then
- Face((rot + 2) % 4)
- StoreItems(1)
- end
- MoveY(0)
- MoveX(0)
- MoveZ(0)
- RemoveSession()
Add Comment
Please, Sign In to add comment