Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not turtle then
- error( "Cannot load turtle API on computer" )
- end
- --enhanced turn functions with co-ordinate tracking
- function turn(face, pos)
- face = sUtil.faceForm(face)
- pos = sUtil.posForm(pos)
- if face == 'right' then
- turtle.turnRight()
- pos[4] = (pos[4] + 1) % 4
- elseif face == 'back' then
- turtle.turnRight()
- turtle.turnRight()
- pos[4] = (pos[4] + 2) % 4
- elseif face == 'left' then
- turtle.turnLeft()
- pos[4] = (pos[4] + 3) % 4
- end
- return pos
- end
- function turnTo(f, pos)
- f = sUtil.nForm(f)
- f = f % 4
- pos = sUtil.posForm(pos)
- local d = (f - pos[4]) % 4
- if d == 1 then pos = turn('right', pos)
- elseif d == 2 then pos = turn('back', pos)
- elseif d == 3 then pos = turn('left', pos)
- end
- return pos
- end
- --basic movement functions with co-ordinate tracking
- function f(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- if turtle.forward() then
- if pos[4]==0 then pos[3] = pos[3]+1
- elseif pos[4]==1 then pos[1] = pos[1]-1
- elseif pos[4]==2 then pos[3] = pos[3]-1
- elseif pos[4]==3 then pos[1] = pos[1]+1
- end
- end
- end
- return pos
- end
- function b(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- if turtle.back() then
- if pos[4]==0 then pos[3] = pos[3]-1
- elseif pos[4]==1 then pos[1] = pos[1]+1
- elseif pos[4]==2 then pos[3] = pos[3]+1
- elseif pos[4]==3 then pos[1] = pos[1]-1
- end
- end
- end
- return pos
- end
- function u(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- if turtle.up() then
- pos[2] = pos[2]+1
- end
- end
- return pos
- end
- function d(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- if turtle.down() then
- pos[2] = pos[2]-1
- end
- end
- return pos
- end
- function l(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- pos = turn('left', pos)
- pos = f(n, pos)
- pos = turn('right', pos)
- return pos
- end
- function r(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- pos = turn('right', pos)
- pos = f(n, pos)
- pos = turn('left', pos)
- return pos
- end
- --destructive movement functions with co-ordinate tracking
- function mine(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- end
- if pos[4]==0 then pos[3] = pos[3]+1
- elseif pos[4]==1 then pos[1] = pos[1]-1
- elseif pos[4]==2 then pos[3] = pos[3]-1
- elseif pos[4]==3 then pos[1] = pos[1]+1
- end
- end
- return(pos)
- end
- function mineUp(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- while not turtle.up() do
- turtle.digUp()
- turtle.attackUp()
- end
- pos[2] = pos[2] + 1
- end
- return(pos)
- end
- function mineDown(n, pos)
- n = sUtil.nForm(n, 1)
- pos = sUtil.posForm(pos)
- if turtle.getFuelLevel() == 0 then
- error('Turtle is out of fuel')
- end
- for i=1,n do
- turtle.digDown()
- while not turtle.down() do
- turtle.attackDown()
- end
- pos[2] = pos[2] - 1
- end
- return(pos)
- end
- --Composite movement functions
- function moveTo(target, pos)
- target = sUtil.posForm(target)
- pos = sUtil.posForm(pos)
- --x-axis movement
- if pos[1] > target[1] then
- pos = turnTo(1, pos)
- local d = pos[1] - target[1]
- pos = f(d, pos)
- elseif pos[1] < target[1] then
- pos = turnTo(3, pos)
- local d = target[1] - pos[1]
- pos = f(d, pos)
- end
- --z-axis movement
- if pos[3] > target[3] then
- pos = turnTo(2, pos)
- local d = pos[3] - target[3]
- pos = f(d, pos)
- elseif pos[3] < target[3] then
- pos = turnTo(0, pos)
- local d = target[3] - pos[3]
- pos = f(d, pos)
- end
- --vertical movement
- if pos[2] ~= target[2] then
- local d = target[2] - pos[2]
- if d > 0 then
- pos = u(d, pos)
- else
- pos = d(-d, pos)
- end
- end
- pos = turnTo(target[4], pos)
- return pos
- end
- function pathTo(target, pos)
- print('This function is just a placeholder, sorry.')
- return pos
- end
- function mineTo(target, pos)
- target = sUtil.posForm(target)
- pos = sUtil.posForm(pos)
- --x-axis movement
- if pos[1] > target[1] then
- pos = turnTo(1, pos)
- local d = pos[1] - target[1]
- pos = mine(d, pos)
- elseif pos[1] < target[1] then
- pos = turnTo(3, pos)
- local d = target[1] - pos[1]
- pos = mine(d, pos)
- end
- --z-axis movement
- if pos[3] > target[3] then
- pos = turnTo(2, pos)
- local d = pos[3] - target[3]
- pos = mine(d, pos)
- elseif pos[3] < target[3] then
- pos = turnTo(0, pos)
- local d = target[3] - pos[3]
- pos = mine(d, pos)
- end
- --vertical movement
- if pos[2] ~= target[2] then
- local d = target[2] - pos[2]
- if d > 0 then
- pos = mineUp(d, pos)
- else
- pos = mineDown(-d, pos)
- end
- end
- pos = turnTo(target[4], pos)
- return pos
- end
- --Non-motion reliable dig function
- function dig(face)
- face = sUtil.faceForm(face, 'front')
- turn(face)
- if face == 'up' then
- while turtle.detectUp() do
- turtle.digUp()
- turtle.attackUp()
- sleep(0.1)
- end
- elseif face == 'down' and turtle.detectDown() then
- turtle.digDown()
- else
- while turtle.detect() do
- turtle.dig()
- turtle.attack()
- sleep(0.1)
- end
- end
- face = sUtil.faceInv(face)
- turn(face)
- end
- --Reliable place functions
- function place(face)
- face = sUtil.faceForm(face)
- turn(face)
- if face == 'down' then
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- turtle.digDown()
- turtle.attackDown()
- end
- end
- elseif face == 'up' then
- if not turtle.compareUp() then
- while not turtle.placeUp() do
- turtle.digUp()
- turtle.attackUp()
- end
- end
- else
- if not turtle.compare() then
- while not turtle.place() do
- turtle.dig()
- turtle.attack()
- end
- end
- end
- face = sUtil.faceInv(face)
- turn(face, {0, 0, 0, 0})
- end
- --Extended detect function, works as compare if slot specified
- function detect(face, s)
- face = sUtil.faceForm(face, 'front')
- s = sUtil.slotForm(s, true)
- if s ~= nil then
- if select(s) == 0 then
- error('Detect cannot compare to an empty slot')
- end
- end
- q = false
- turn(face)
- if s == nil then
- if face == 'up' then
- q = turtle.detectUp()
- elseif face == 'down' then
- q = turtle.detectDown()
- else q = turtle.detect()
- end
- else
- if face == 'up' then
- q = turtle.compareUp()
- elseif face == 'down' then
- q = turtle.compareDown()
- else q = turtle.compare()
- end
- end
- turn(sUtil.faceInv(face))
- return q
- end
- --Enhanced refuel function
- function refuel(s, level)
- s = sUtil.slotForm(s, true)
- level = sUtil.nForm(level, 1)
- local current = turtle.getFuelLevel()
- if current >= level then
- print('Turtle has enough fuel')
- return true
- else print('Refuelling')
- end
- if s ~= nil then turtle.select(s) end
- while current < level do
- if not(turtle.refuel(1)) then
- print('Run out of fuel.')
- return false
- end
- current = turtle.getFuelLevel()
- end
- return true
- end
- --Extended select function
- function select(s)
- s = sUtil.slotForm(s, false)
- turtle.select(s)
- return turtle.getItemCount(s)
- end
- --Find all slots containing the current item in slot s
- function findSlots(s)
- s = sUtil.slotForm(s, true)
- if s ~= nil then turtle.select(s) end
- local slots = {}
- local j=1
- for i = 1,16 do
- if turtle.compareTo(i) then
- slots[j] = i
- j = j+1
- end
- end
- return slots
- end
- --Count the contents of multiple slots
- function sumCount(slots)
- local n = 0
- for i, v in ipairs(slots) do
- n = n + turtle.getItemCount(sUtil.slotForm(v))
- end
- return n
- end
- --Deposit all but one of an item (option for all). Returns number deposited.
- function smartDeposit(s, face, keep)
- s = sUtil.slotForm(s)
- face = sUtil.faceForm(face)
- if keep ~= false then keep = true end
- local a = findSlots(s)
- local n = 0
- turn(face)
- for i, v in ipairs(a) do
- turtle.select(v)
- local c = turtle.getItemCount(v)
- if v == s and keep then c = c-1 end
- if face == 'up' then turtle.dropUp(c)
- elseif face == 'down' then turtle.dropDown(c)
- else turtle.drop(c) end
- n = n + c
- end
- turn(face, true)
- return n
- end
- --Extended transpose function to allow turtle to prioritise a slot
- function ioTranspose(n, face, s)
- n = n or 1
- face = face or 'front'
- if s~=nil then
- s = s % 16
- if s==0 then
- s = 16
- end
- turtle.select(s)
- end
- sUtil.transpose(n, face)
- end
Advertisement
Add Comment
Please, Sign In to add comment