turtle/libs/turtleutils.lua
2025-06-27 14:42:52 +01:00

94 lines
1.6 KiB
Lua

local M = {}
-- Helper: retry turtle action up to N times
local function try(actionFn, times)
times = times or 5
for i = 1, times do
if actionFn() then return true end
sleep(0.5)
end
return false
end
-- Basic movement
function M.forward()
return try(turtle.forward)
end
function M.back()
return try(turtle.back)
end
function M.up()
return try(turtle.up)
end
function M.down()
return try(turtle.down)
end
-- Digging with movement
function M.digForward()
while turtle.detect() do
turtle.dig()
sleep(0.3)
end
end
function M.digUp()
while turtle.detectUp() do
turtle.digUp()
sleep(0.3)
end
end
function M.digDown()
while turtle.detectDown() do
turtle.digDown()
sleep(0.3)
end
end
-- Move forward, clearing blocks if necessary
function M.moveUntilClearForward()
while not turtle.forward() do
if turtle.detect() then
turtle.dig()
else
sleep(0.5)
end
end
end
-- Inspection
function M.inspectForward()
local success, data = turtle.inspect()
if success then return data else return nil end
end
function M.inspectUp()
local success, data = turtle.inspectUp()
if success then return data else return nil end
end
function M.inspectDown()
local success, data = turtle.inspectDown()
if success then return data else return nil end
end
-- Turning
function M.turnLeft()
turtle.turnLeft()
end
function M.turnRight()
turtle.turnRight()
end
function M.turnAround()
turtle.turnLeft()
turtle.turnLeft()
end
return M