Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This program branch mines...
- -- It will also place torches. Make sure
- -- there's torches in the 1st slot.
- -- It will also deposit its items into
- -- a chest below where it started when
- -- it's inventory is full.
- --
- --================ ARGUMENT FLAGS ================================
- --> -t <true/false>|<0/1>
- -- If true, turtle will place torches from slot 1.
- -- Default is true.
- --> -l <int:[1, inf)>
- -- Specifies the length in blocks of each branch.
- -- Default is 12. Past that, mobs can spawn.
- --> -b <int:[1, inf)>
- -- Specififes the number of segments, or branches*2, to mine.
- -- Default is 21. Past that, it runs out of torches.
- --> -s <int:[0, inf)>
- -- Specifies the number of blocks inbetween each branch.
- -- Default is 3. The optimal space for diamond mining.
- --> -d <true/false>|<0/1>
- -- If true, turtle will deposit it's items into the chest when it's full.
- -- Default is true.
- --================================================================
- -- Author : Matthew (FaceInCake) Eppel
- -- Date : Sept 14, 2019
- -- Version : 2.0
- local args = {...}
- -- These are all the changable values
- local branches = 21
- local length = 12
- local space = 3
- local placeTorches = true
- local dropItems = true
- function checkInteger(s)
- local n = tonumber(s)
- if n == nil then
- print("[ERROR]: '"..s.."' is not a number")
- return nil
- end
- n = math.floor(n)
- if n < 0 then
- print("[ERROR]: '"..s.."' is not an integer")
- return nil
- end
- return n
- end
- -- Parse all the arguments
- function parseArguments()
- for i=1, #args do
- local s = tostring(args[i])
- if s:sub(1,1) == '-' then
- i = i + 1
- if i > #args then
- break
- end
- local c = s:sub(2,2)
- if c == 'b' then
- local n = checkInteger(args[i])
- if n == nil then
- return 1
- end
- branches = n
- elseif c == 'l' then
- local n = checkInteger(args[i])
- if n == nil then
- return 1
- end
- length = n
- elseif c == 's' then
- local n = checkInteger(args[i])
- if n == nil then
- return 1
- end
- space = n
- elseif c == 't' then
- local s = tostring(args[i])
- if s == "false"
- or s == "0"
- or s == "off" then
- placeTorches = false
- elseif s ~= "true"
- and s ~= "1"
- and s ~= "on" then
- print("[ERROR]: '"..s.."' is not a valid boolean")
- return 1
- end
- elseif c == 'd' then
- local s = tostring(args[i])
- if s == "false"
- or s == "0"
- or s == "off" then
- dropItems = false
- elseif s ~= "true"
- and s ~= "1"
- and s ~= "on" then
- print("[ERROR]: '"..s.."' is not a valid boolean")
- return 1
- end
- end
- end
- end
- return 0
- end
- -- Digs forward, but checks if a new block fell into place, and digs it
- function delete()
- while turtle.detect() do
- turtle.dig()
- os.sleep(0.1)
- end
- end
- -- Goes forward one space until it is successful
- function forward()
- while not turtle.forward() do
- os.sleep(0.2)
- end
- end
- -- Goes backwards until it reaches `dist` blocks away
- function back(dist)
- local count = 0
- while count < dist do
- if turtle.back() then
- count = count + 1
- else
- os.sleep(0.2)
- end
- end
- end
- -- Returns false if there is at least 1 space of inventory, true otherwise
- function inventoryFull()
- for i=16, 1, -1 do
- if turtle.getItemCount(i)==0 then
- return false
- end
- end
- return true
- end
- -- Sideways offset from chest.
- -- Positive is to the right.
- local xOffset = 0
- -- Forwards offset from chest
- local yOffset = 0
- -- Direction. 0=forward, 1=right, etc.
- local face = 0
- -- Turns left and increments 'face'
- function turnLeft()
- turtle.turnLeft()
- face = face - 1
- if face < 0 then
- face = 3
- end
- end
- -- Turns right and increments 'face'
- function turnRight()
- turtle.turnRight()
- face = face + 1
- if face > 3 then
- face = 0
- end
- end
- -- Does a 180deg turn and increments 'face'
- function oneEighty()
- if face >= 2 then
- face = face - 2
- turtle.turnLeft()
- turtle.turnLeft()
- else
- face = face + 2
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- -- Increments xOffset/yOffset by n, depending on the current 'face'
- function increment(n)
- if face == 0 then
- yOffset = yOffset + n
- elseif face == 1 then
- xOffset = xOffset + n
- elseif face == 2 then
- yOffset = yOffset - n
- elseif face == 3 then
- xOffset = xOffset - n
- else
- return false
- end
- return true
- end
- -- This function returns to where it was originally placed
- function recall()
- if xOffset > 0 then
- -- face needs to be 3
- if face==0 then
- turnLeft()
- elseif face==1 then
- oneEighty()
- elseif face==2 then
- turnRight()
- end
- elseif xOffset < 0 then
- -- Face needs to be 1
- if face==0 then
- turnRight()
- elseif face==2 then
- turnLeft()
- elseif face==3 then
- oneEighty()
- end
- end
- -- Move to middle
- for i=1, math.abs(xOffset) do
- forward()
- end
- -- Face needs to be 2
- if face==0 then
- oneEighty()
- elseif face==1 then
- turnRight()
- elseif face==3 then
- turnLeft()
- end
- -- Move to chest
- for i=1, yOffset do
- forward()
- end
- -- Move down
- while not turtle.down() do
- os.sleep(0.1)
- end
- -- Face back again
- oneEighty()
- end
- -- This long function takes control of the turtle, it
- -- will return to where it was placed when this program
- -- was run and drop all of it's items below itself, then
- -- return back to where it was and continue mining.
- function deposit()
- -- Store value so they can be reset
- local _xOffset = xOffset
- local _yOffset = yOffset
- local _face = face
- -- Returns to the chest
- recall()
- -- Drop all items
- for i=16, 2, -1 do
- turtle.select(i)
- turtle.dropDown(64)
- end
- -- Reselect the torches
- turtle.select(1)
- -- If we're not carrying torches, drop'em
- if not placeTorches then
- turtle.dropDown(64)
- end
- -- Move up
- while not turtle.up() do
- os.sleep(0.1)
- end
- -- Move back to the branch it was at
- for i=1, _yOffset do
- forward()
- end
- -- Turn to face '_face'
- for i=1, _face do
- turnRight()
- end
- -- Go back to original spot in branch
- for i=1, math.abs(_xOffset) do
- forward()
- end
- -- Reset our values just in case
- xOffset = _xOffset
- yOffset = _yOffset
- face = _face
- end
- function branch()
- for i=1, length do
- delete()
- if dropItems then
- if inventoryFull() then
- deposit()
- end
- end
- forward()
- increment(1)
- turtle.digDown()
- if dropItems then
- if inventoryFull() then
- deposit()
- end
- end
- end
- back(1)
- if placeTorches then
- turtle.place()
- end
- back(length-1)
- increment(-length)
- end
- function main()
- if parseArguments()~=0 then
- return -1
- end
- while turtle.detectUp() do
- turtle.digUp()
- os.sleep(0.2)
- end
- while not turtle.up() do
- os.sleep(0.1)
- end
- for i=1, branches do
- for j=0, space do
- delete()
- if dropItems then
- if inventoryFull() then
- deposit()
- end
- end
- forward()
- increment(1)
- turtle.digDown()
- if dropItems then
- if inventoryFull() then
- deposit()
- end
- end
- end
- if placeTorches then
- turtle.placeDown()
- end
- turnLeft()
- branch()
- oneEighty()
- branch()
- turnLeft()
- end
- recall()
- return 0
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment