Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --SmartTurtleV2 By Jeremiah Lowe
- --(c) Jeremiah Lowe 2018
- --A simple turtle library for allowing
- --the easy creation of turtle scripts
- local fuelSlot = 1
- local fuelBufferSize = 10
- local autoRefuel = false
- local direction = 0
- local invHomeName = nil
- local invFullCallback = nil
- local invTempHome = "_inv_tmp_hme"
- local homes = {}
- -- Fwd (0)
- --Lft (3) Rgt (1)
- -- Bwd (2)
- local function normalize(val)
- while val > 3 do val = val - 4 end
- while val < 0 do val = val + 4 end
- return val
- end
- local function _dirChg(by) direction = normalize(direction + by) end
- local function _movChg(by)
- for k, h in pairs(homes) do
- if direction == 0 then
- homes[k].movementY = h.movementY + by
- elseif direction == 1 then
- homes[k].movementX = h.movementX + by
- elseif direction == 2 then
- homes[k].movementY = h.movementY - by
- elseif direction == 3 then
- homes[k].movementX = h.movementX - by
- end
- end
- end
- local function _dptChg(by)
- for k, h in pairs(homes) do
- homes[k].depthChange = h.depthChange + by
- end
- end
- local function _autoRefuel(amt)
- if not autoRefuel then return end
- tamt = amt + fuelBufferSize
- if tamt > turtle.getFuelLimit() then
- tamt = turtle.getFuelLevel()
- end
- slot = turtle.getSelectedSlot()
- turtle.select(fuelSlot)
- if turtle.getFuelLevel() < tamt then print("Refuelling") end
- while turtle.getFuelLevel() < tamt do
- turtle.refuel(1)
- end
- turtle.select(slot)
- end
- local function _rotateHome(h)
- if not pointTo(h.direction) then return false end
- return true
- end
- local function _fwdAbsVal(val)
- amt = val
- if val < 0 then amt = -val end
- return forward(amt)
- end
- local function _moveHome(h)
- --X axis
- if h.movementX ~= 0 then
- d = 3
- if h.movementX < 0 then d = 1 end
- if not pointTo(d) then return false end
- if not _fwdAbsVal(h.movementX) then return false end
- end
- if h.movementY ~= 0 then
- d = 2
- if h.movementY < 0 then d = 0 end
- if not pointTo(d) then return false end
- if not _fwdAbsVal(h.movementY) then return false end
- end
- return true
- end
- local function _depthHome(h)
- depthDir = 1
- reposAmt = -h.depthChange
- if h.depthChange > 0 then
- depthDir = -1
- reposAmt = h.depthChange
- end
- for i=1, reposAmt do
- if depthDir > 0 then
- if not turtle.up() then return false end
- else
- if not turtle.down() then return false end
- end
- _dptChg(depthDir)
- end
- return true
- end
- local function _goHome(h)
- if h == nil then error("Home object expected, got nil!") end
- if not _depthHome(h) then return false end
- if not _moveHome(h) then return false end
- if not _rotateHome(h) then return false end
- return true
- end
- function turnLeft(amt)
- if amt == nil then amt = 1 end
- for i=1,amt do
- if turtle.turnLeft() then _dirChg(-1)
- else return false end
- end
- return true
- end
- function turnRight(amt)
- if amt == nil then amt = 1 end
- for i=1,amt do
- if turtle.turnRight() then _dirChg(1)
- else return false end
- end
- return true
- end
- function forward(amt, digF)
- if amt == nil then amt = 1 end
- if digF == nil then digF = false end
- _autoRefuel(amt)
- for i=1,amt do
- if turtle.forward() then
- _movChg(1)
- else
- if not digF then
- return false
- elseif dig() and turtle.forward() then _movChg(1)
- else return false end
- end
- end
- return true
- end
- function back(amt) return backward(amt) end
- function backward(amt)
- if amt == nil then amt = 1 end
- _autoRefuel(amt)
- for i=1,amt do
- if turtle.back() then _movChg(-1)
- else return false end
- end
- return true
- end
- function up(amt, dig)
- if amt == nil then amt = 1 end
- if dig == nil then dig = false end
- _autoRefuel(amt)
- for i=1,amt do
- if turtle.up() then
- _dptChg(1)
- else
- if not dig then
- return false
- elseif digUp() and turtle.up() then _dptChg(1)
- else return false end
- end
- end
- return true
- end
- function down(amt, dig)
- if amt == nil then amt = 1 end
- if dig == nil then dig = false end
- _autoRefuel(amt)
- for i=1,amt do
- if turtle.down() then
- _dptChg(-1)
- else
- if not dig then
- return false
- elseif digDown() and turtle.down() then _dptChg(-1)
- else return false end
- end
- end
- return true
- end
- local function _checkInv()
- if invHomeName ~= nil and isInventoryFull() then
- createHome(invTempHome)
- home(invHomeName)
- if invFullCallback ~= nil then
- invFullCallback()
- end
- home(invTempHome)
- end
- end
- function digUp()
- _checkInv()
- return turtle.digUp()
- end
- function digDown()
- _checkInv()
- return turtle.digDown()
- end
- function dig()
- _checkInv()
- return turtle.dig()
- end
- function getDepth() return depthChange end
- function getMovementX() return movementX end
- function getMovementY() return movementY end
- function getDirection() return direction end
- function pointTo(toDir)
- left = true
- d = direction - toDir
- d2 = 4 - d
- if d > d2 then left = false end
- if left then
- while direction ~= toDir do
- if not turnLeft() then return false end
- end
- else
- while direction ~= toDir do
- if not turnRight() then return false end
- end
- end
- return true
- end
- function goHome(homeName)
- if homeName == nil then error("No home name provided!") end
- h = homes[homeName]
- if h == nil then error("No home with name " .. homeName .. " exists!") end
- return _goHome(h)
- end
- function home(homeName) return goHome(homeName) end
- local function _homeObj()
- local h = {}
- h.movementX = 0
- h.movementY = 0
- h.depthChange = 0
- h.direction = direction
- return h
- end
- function createHome(homeName)
- if homeName == nil then error("No home name provided!") end
- homes[homeName] = _homeObj()
- end
- function getFreeSlots()
- free = 0
- for i=1,16 do
- if turtle.getItemCount(i) == 0 then free = free + 1 end
- end
- return free
- end
- function turnAround(amt)
- if amt == nil then amt = 1 end
- turnRight(2 * amt)
- end
- function isInventoryFull()
- for i=1,16 do
- local a = turtle.getItemCount(i)
- if a == 0 then return false end
- end
- return true
- end
- function setInventoryFullHome(homeName) invHomeName = homeName end
- function getInventoryFullHome() return invHomeName end
- function setInventoryFullCallback(cback) invFullCallback = cback end
- function getInventoryFullCallback() return invFullCallback end
- function enableAutoRefuel() autoRefuel = true end
- function disableAutoRefuel() autoRefuel = false end
- function setFuelSlot(slot)
- if slot < 0 then return false end
- if slot > 16 then return false end
- fuelSlot = slot
- return true
- end
- function getFuelSlot() return fuelSlot end
- function getFuelBufferSize() return fuelBufferSize end
- function setFuelBufferSize(size)
- if size < 0 then return false end
- if size > turtle.getFuelLimit() then return false end
- fuelBufferSize = size
- return true
- end
Add Comment
Please, Sign In to add comment