115 lines
2.2 KiB
Lua
115 lines
2.2 KiB
Lua
local M = {}
|
|
|
|
local logger = require("libs.logger")
|
|
|
|
-- Minimum fuel required to proceed with one movement
|
|
local MIN_FUEL = 1 -- you can raise this if you want a safety margin
|
|
|
|
-- Ensure the turtle has fuel
|
|
local function checkFuel()
|
|
local fuel = turtle.getFuelLevel()
|
|
if fuel == "unlimited" then return true end -- creative mode
|
|
if fuel == nil or fuel < MIN_FUEL then
|
|
logger.log("Not enough fuel! Current level: " .. tostring(fuel))
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
-- Helper: retry an action with fuel check
|
|
local function try(actionFn, times)
|
|
times = times or 5
|
|
for i = 1, times do
|
|
if not checkFuel() then
|
|
logger.log("Waiting for fuel...")
|
|
sleep(2)
|
|
elseif 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
|