Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- THIS SCRIPT IS THE PROPERTY OF Bane83
- -- You are hereby granted permission to use this script as you see fit, however you are not granted permission to
- -- redistribute, modify, or claim this software as your own.
- function table.copy(t)
- local t2 = {}
- for k,v in pairs(t) do
- t2[k] = v
- end
- return t2
- end
- turtle.original = table.copy(turtle)
- turtle.maxRetries = 10
- turtle.directions = { up = "move_up", down = "move_down", back = "move_back", forward = "move_forward", left = "move_left", right = "move_right" }
- turtle.sturtle = {
- _moveSide = function (dir, n, reorientAfterEachMove, reorientWhenDone, afterEachMove, retryOnFail, ...)
- dir = dir or turtle.directions.left
- n = n or 1
- retryOnFail = retryOnFail or (retryOnFail == nil)
- reorientAfterEachMove = reorientAfterEachMove or false
- reorientWhenDone = reorientWhenDone or (reorientWhenDone == nil)
- if reorientWhenDone and reorientAfterEachMove then
- reorientWhenDone = false
- end
- if (dir == turtle.directions.left) then
- turtle.turnLeft()
- elseif (dir == turtle.directions.right) then
- turtle.turnRight()
- end
- local result = {}
- local _result = false
- for i = 1, n do
- _result = turtle.sturtle._retryMoveDelegate(turtle.original.forward, retryOnFail)
- if (_result) then
- if (reorientAfterEachMove) then
- if (dir == turtle.directions.left) then
- turtle.turnRight()
- elseif (dir == turtle.directions.right) then
- turtle.turnLeft()
- end
- end
- if (afterEachMove ~= nil) then
- afterEachMove(i)
- end
- if (reorientAfterEachMove) then
- if (dir == turtle.directions.left) then
- turtle.turnLeft()
- elseif (dir == turtle.directions.right) then
- turtle.turnRight()
- end
- end
- end
- table.insert(result, _result)
- end
- if (reorientWhenDone) then
- if (dir == turtle.directions.left) then
- turtle.turnRight()
- elseif (dir == turtle.directions.right) then
- turtle.turnLeft()
- end
- end
- return result
- end,
- _dig = function (dir, n, afterEachDig, whenDone, retryOnFail, ...)
- dir = dir or turtle.directions.forward
- n = n or 1
- retryOnFail = retryOnFail or (retryOnFail == nil)
- local func = turtle.sturtle._getDigDelegates(dir)
- local result = {}
- local _result = false
- for i = 1, n do
- while (func.detect()) do
- _result = turtle.sturtle._retryDigDelegate(func.dig, retryOnFail)
- sleep(0.50) -- Wait for falling entities
- end
- if (_result and afterEachDig ~= nil) then
- afterEachDig(i)
- end
- if (n > 1) then
- turtle.sturtle._move(dir, 1)
- end
- table.insert(result, _result)
- end
- if (whenDone ~= nil) then
- whenDone(n)
- end
- return result
- end,
- _move = function (dir, n, afterEachMove, retryOnFail, ...)
- dir = dir or turtle.directions.forward
- n = n or 1
- retryOnFail = retryOnFail or (retryOnFail == nil)
- local func = turtle.sturtle._getDelegate(dir)
- local result = {}
- local _result = false
- for i = 1, n do
- _result = turtle.sturtle._retryMoveDelegate(func, retryOnFail)
- if (_result and afterEachMove ~= nil) then
- afterEachMove(i)
- end
- table.insert(result, _result)
- end
- return result
- end,
- _retryMoveDelegate = function (func, retryOnFail)
- local attempts = 0
- local message = ""
- local _result = func()
- while (_result ~= true and retryOnFail) do
- attempts = attempts + 1
- if (turtle.getFuelLevel() == 0) then
- message = os.getComputerLabel() .. " is out of fuel!" -- TODO: Attempt auto-fueling
- break
- end
- if (attempts == math.floor(turtle.maxRetries / 3)) then
- sleep(0.5)
- elseif (attempts == turtle.maxRetries) then
- if (message == "") then
- message = os.getComputerLabel() .. " is stuck and in need of assistance!"
- end
- turtle.sturtle._needsAssistance(message)
- attempts = 0 -- Starting a new attempt loop
- end
- _result = func()
- end
- return _result
- end,
- _retryDigDelegate = function (func, retryOnFail)
- local attempts = 0
- local message = ""
- local _result = func()
- while (_result ~= true and retryOnFail) do
- attempts = attempts + 1
- if (turtle.getFuelLevel() == 0) then
- message = os.getComputerLabel() .. " is out of fuel!" -- TODO: Attempt auto-fueling
- break
- end
- if (attempts == math.floor(turtle.maxRetries / 3)) then
- sleep(0.5)
- elseif (attempts == turtle.maxRetries) then
- if (message == "") then
- message = os.getComputerLabel() .. " can't dig and is in need of assistance!"
- end
- turtle.sturtle._needsAssistance(message)
- attempts = 0 -- Starting a new attempt loop
- end
- _result = func()
- end
- return _result
- end,
- _needsAssistance = function (message)
- print(message)
- local event, char
- char = "n"
- while string.lower(char) ~= "y" do
- print("Am I ready to go? (Y/N)")
- event, char = os.pullEvent("char")
- end
- end,
- _getDelegate = function (dir)
- local delegates = {
- move_up = turtle.original.up,
- move_down = turtle.original.down,
- move_back = turtle.original.back,
- move_forward = turtle.original.forward
- }
- return delegates[dir]
- end,
- _getDigDelegates = function (dir)
- local delegates = {
- move_up = { dig = turtle.original.digUp, detect = turtle.original.detectUp },
- move_down = { dig = turtle.original.digDown, detect = turtle.original.detectDown },
- move_forward = { dig = turtle.original.dig, detect = turtle.original.detect }
- }
- return delegates[dir]
- end
- }
- turtle.up = setmetatable({up = turtle.up}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.up, ...) end})
- turtle.down = setmetatable({down = turtle.down}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.down, ...) end})
- turtle.back = setmetatable({back = turtle.back}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.back, ...) end})
- turtle.forward = setmetatable({forward = turtle.forward}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.forward, ...) end})
- turtle.left = setmetatable({left = turtle.left}, {__call = function(self, ...) return turtle.sturtle._moveSide(turtle.directions.left, ...) end})
- turtle.right = setmetatable({right = turtle.right}, {__call = function(self, ...) return turtle.sturtle._moveSide(turtle.directions.right, ...) end})
- turtle.dig = setmetatable({dig = turtle.dig}, {__call = function(self, ...) return turtle.sturtle._dig(turtle.directions.forward, ...) end})
- turtle.digUp = setmetatable({digUp = turtle.digUp}, {__call = function(self, ...) return turtle.sturtle._dig(turtle.directions.up, ...) end})
- turtle.digDown = setmetatable({digDown = turtle.digDown}, {__call = function(self, ...) return turtle.sturtle._dig(turtle.directions.down, ...) end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement