Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This program will allow the turtle to automine,
- --chunkload himself, put all materials in an ender-chest when
- --he's done, and return back to the destination.
- --A return-to-surface autostart failsafe may be added
- --later.
- --Should I display the debug renderer?
- local useDebugRenderer = true
- --Flags
- isEnderChest = false
- isLoaded = false
- spareCobble = false
- --My own custom method of saving time
- os.loadAPI("move")
- local args = {...}
- --GLOBAL VARIABLES
- local version = "0.0.1"
- local status = "Starting up..."
- --Current Position
- local homeX = 0
- local homeY = 0
- local homeZ = 0
- local homeDir = "north"
- --Loop Resume Position
- local loopX = 0
- local loopY = 0
- local loopZ = 0
- local loopDir = 0
- --ChunkLoaded boudaries
- local ChunkStartX = homeX-16
- local ChunkStartZ = homeZ-16
- local ChunkEndX = homeX+16
- local ChunkEndZ = homeZ+16
- local ceilingY = 0
- --FUNCTIONS
- --MAIN FUNCTIONS
- function init(...)
- --Starts up and sets up the turtle and puts him in position to begin minePattern
- print("This program assumes an enderchest is in Slot 1 and a chunkloader is in slot 2.")
- print("Press any key to continue...")
- read()
- turtle.select(2)
- if turtle.detectUp() then
- move.digUp()
- end
- turtle.placeUp()
- --Establishing where Mr. Turtle needs to return to when done
- homeX, homeY, homeZ, homeDir = move.getCoords()
- ChunkStartX = homeX-16
- ChunkStartZ = homeZ-16
- ChunkEndX = homeX+16
- ChunkEndZ = homeZ+16
- move.fDown(5)
- ceiling = getPos("y")
- --Variables finished, moving into position
- print("Moving into position to begin mining operation...")
- move.goto(ChunkStartX, ceiling-1, ChunkEndZ, "north")
- minePattern()
- move.goto(homeX, homeY, homeZ, homeDir)
- turtle.select(3)
- turtle.placeDown()
- turtle.select(1)
- --Chunkloader above, can't clear inv here, gotta move.
- move.fForward()
- clearInv("all")
- move.turnLeft()
- move.turnLeft()
- move.fForward()
- move.turnLeft()
- move.turnLeft()
- turtle.select(2)
- turtle.digUp()
- turtle.select(1)
- end
- function minePattern()
- --Here is the loop which makes the turtle eat the entire 32x32 grid of the area.
- for i=1,16 do
- print("Mining progress: Cycle "..i.."/16")
- for i=1, 16 do
- --Building the 'ceiling'
- move.digUp()
- turtle.select(3)
- turtle.placeUp()
- turtle.select(1)
- --Going down
- mineDownLoop()
- move.goto(getPos("x"), ceiling-1, getPos("z")-2, "north")
- end
- --Moving over to the next column and facing down
- move.goto(getPos("x")+1, ceiling-1, getPos("z"), "south")
- for i=1, 16 do
- --Building the 'ceiling'
- move.digUp()
- turtle.select(3)
- turtle.placeUp()
- turtle.select(1)
- --Going down
- mineDownLoop()
- move.goto(getPos("x"), ceiling-1, getPos("z")+2, "south")
- end
- --And now to move to third row if it's not the last one
- if move.xPos ~= ChunkEndX then
- move.goto(getPos("x")+1,ceiling-1, getPos("z"), "north")
- else
- print("Alright, I'm done, going home!")
- end
- end
- end
- function hasInv()
- if turtle.getItemCount(16) > 0 then
- sortInv()
- end
- end
- function dungeonCheck(side)
- if turtle.detectDown() and not turtle.digDown() then
- return "This is crap, not a chest."
- else
- if side == "down" then
- hasInv()
- while turtle.suckDown() do
- hasInv()
- end
- elseif side == "forward" then
- hasInv()
- while turtle.suck() do
- hasInv()
- end
- elseif side == "up" then
- hasInv()
- while turtle.suckUp() do
- hasInv()
- end
- else
- print("You are passing a carpy argument into dungeonCheck()")
- end
- end
- end
- function mineDownLoop()
- local bedrock = false
- while not bedrock do
- if not turtle.digDown() and turtle.detectDown() then
- bedrock = true
- else
- hasInv()
- dungeonCheck("down")
- turtle.digDown()
- hasInv()
- dungeonCheck("forward")
- turtle.dig()
- move.fDown()
- end
- end
- end
- --UTILITY FUNCTIONS
- function compressInv()
- for i = 1, 16 do
- --Step through each inv slot
- for k = 16, i, -1 do
- turtle.select(k)
- if turtle.compareTo(i) or turtle.getItemCount(i) == 0 then
- turtle.transferTo(i)
- end
- end
- end
- turtle.select(1)
- end
- function sortInv()
- --Kills cobble with a fiery passion immediately
- if not spareCobble then
- move.turnLeft()
- move.turnLeft()
- countCrapItem1 = 0
- countCrapItem2 = 0
- for i=4, 16 do
- turtle.select(i)
- if turtle.compareTo(2) then
- countCrapItem1 = countCrapItem1 + turtle.getItemCount(i)
- end
- if turtle.compareTo(3) then
- countCrapItem2 = countCrapItem2 + turtle.getItemCount(i)
- end
- end
- if countCrapItem1 > 191 then
- --Kill crap slot1 except one placeholder here if in new version
- for i=4, 16 do
- turtle.select(i)
- if turtle.compareTo(2) then
- turtle.select(i)
- turtle.dropUp()
- end
- end
- end
- if countCrapItem2 > 191 then
- --Kill crap slot1 except one placeholder here if in new version
- for i=4, 16 do
- turtle.select(i)
- if turtle.compareTo(3) then
- turtle.select(i)
- turtle.dropUp()
- end
- end
- end
- move.turnLeft()
- move.turnLeft()
- end
- --Checks all slots for fuel and eats them if he needs it
- for i=2, 16 do
- turtle.select(i)
- while turtle.refuel(1) and turtle.getFuelLevel() < 100000 and turtle.getItemCount(i) > 0 do
- turtle.refuel(1)
- end
- end
- --Here, we are going to compress items onto others if possible
- compressInv()
- --If his inventory 16 still has crap in it, that means it's too damn full. Here, we will now kill all items into a chest.
- clearInv("partial")
- end
- function clearInv(mode)
- if isEnderChest then
- move.fUp()
- turtle.select(1)
- turtle.placeDown()
- if mode == "all" then
- for i=2, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- elseif mode == "partial" then
- for i=4, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- else
- print("Error: Invalid input was passed into clearInv!")
- end
- turtle.select(1)
- turtle.digDown()
- move.fDown()
- else
- --GotoChestHere
- end
- end
- function canMineForward()
- --Checks ChunkLoad boundaries to ensure what he's about to check
- --is even chunkloaded (can't mine in non-loaded chunks)
- local x, y, z, dir = move.getCoords()
- if dir == "north" then
- if z > ChunkStartZ then
- return true
- else
- return false
- end
- elseif dir == "south" then
- if z < ChunkEndZ then
- return true
- else
- return false
- end
- elseif dir == "east" then
- if x > ChunkStartX then
- return true
- else
- return false
- end
- elseif dir == "west" then
- if x < ChunkEndX then
- return true
- else
- return false
- end
- else
- end
- end
- function getPos(coordType)
- --If you only need one coordinate letter of the turtle
- local x, y, z, dir = move.getCoords()
- if coordType == "x" then
- return x
- elseif coordType == "y" then
- return y
- elseif coordType == "z" then
- return z
- elseif coordType == "dir" then
- return dir
- else
- print("Improper parameter passed into getPos!")
- end
- end
- function renderDebug()
- --Will display important variables on-screen if set to true in code
- if useDebugRenderer==true then
- term.clear()
- term.setCursorPos(1,1)
- print("Debug Output Code v"..version)
- print("========================")
- print("X: "..x)
- print("Y: "..y)
- print("Z: "..z)
- print("Direction: "..getDir())
- print("========================")
- print("Status: "..status)
- print("Fuel: "..turtle.getFuelLevel())
- print("========================")
- end
- end
- --END OF FUNCTIONS
- --EXECUTION CODE
- init()
Advertisement
Add Comment
Please, Sign In to add comment