161 lines
3.6 KiB
Lua
161 lines
3.6 KiB
Lua
local M = {}
|
|
|
|
local logger = require("libs.logger")
|
|
|
|
-- Optional external status setter (set via M.setStatusCallback)
|
|
local statusCallback = nil
|
|
|
|
function M.setStatusCallback(fn)
|
|
statusCallback = fn
|
|
end
|
|
|
|
-- Minimum fuel required to proceed with one movement
|
|
local MIN_FUEL = 1 -- you can raise this if you want a safety margin
|
|
|
|
-- 🔋 Attempt to refuel from inventory
|
|
local function refuelFromInventory(minRequired)
|
|
minRequired = minRequired or MIN_FUEL
|
|
local gained = 0
|
|
|
|
for slot = 1, 16 do
|
|
turtle.select(slot)
|
|
if turtle.getItemCount() > 0 and turtle.refuel(0) then
|
|
local before = turtle.getFuelLevel()
|
|
if turtle.refuel(1) then
|
|
local after = turtle.getFuelLevel()
|
|
local added = after - before
|
|
gained = gained + added
|
|
logger.log(string.format("Refueled +%d from slot %d", added, slot))
|
|
if after >= minRequired then break end
|
|
else
|
|
logger.log(string.format("Refuel failed from slot %d", slot))
|
|
end
|
|
end
|
|
end
|
|
|
|
return gained
|
|
end
|
|
|
|
-- Ensure fuel before doing anything
|
|
local function refuelIfNeeded()
|
|
local fuel = turtle.getFuelLevel()
|
|
if fuel == "unlimited" then return true end
|
|
|
|
if fuel < MIN_FUEL then
|
|
logger.log("Low fuel, attempting to refuel...")
|
|
local added = refuelFromInventory(MIN_FUEL)
|
|
fuel = turtle.getFuelLevel()
|
|
end
|
|
|
|
while fuel < MIN_FUEL do
|
|
if statusCallback then statusCallback("out_of_fuel") end
|
|
logger.log("No fuel. Waiting for fuel...")
|
|
sleep(5)
|
|
|
|
-- Try to refuel again
|
|
local added = refuelFromInventory(MIN_FUEL)
|
|
fuel = turtle.getFuelLevel()
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
-- Retry a movement or action with fuel awareness
|
|
local function try(actionFn, times, name)
|
|
times = times or 5
|
|
for i = 1, times do
|
|
if not refuelIfNeeded() then
|
|
sleep(2)
|
|
elseif actionFn() then
|
|
if name then logger.log("Action succeeded: " .. name) end
|
|
return true
|
|
end
|
|
if name then logger.log("Retrying: " .. name) end
|
|
sleep(0.5)
|
|
end
|
|
if name then logger.log("Gave up after retries: " .. name) 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
|