This commit is contained in:
Laurie Fisher 2025-06-27 15:21:08 +01:00
parent f1f29471e1
commit e0c5b67eea
Signed by: PinkEyedOrphan
GPG Key ID: 7F827B68147AEE76

View File

@ -3,7 +3,6 @@ local M = {}
local logger = require("libs.logger")
-- Optional external status setter (set via M.setStatusCallback)
-- Status callback
local statusCallback = nil
function M.setStatusCallback(fn) statusCallback = fn end
@ -12,7 +11,6 @@ local manualPos = { x = 0, y = 0, z = 0 }
local directions = { "north", "east", "south", "west" }
local facing = 1
-- GPS attempt
local function tryGPS()
local x, y, z = gps.locate(2) -- 2 second timeout
@ -58,11 +56,10 @@ function M.getPosition()
end
end
-- Minimum fuel required to proceed with one movement
local MIN_FUEL = 1 -- you can raise this if you want a safety margin
local MIN_FUEL = 1
-- 🔋 Attempt to refuel from inventory
-- Attempt to refuel from inventory
local function refuelFromInventory(minRequired)
minRequired = minRequired or MIN_FUEL
local gained = 0
@ -99,10 +96,15 @@ local function refuelIfNeeded()
while fuel < MIN_FUEL do
if statusCallback then statusCallback("out_of_fuel") end
logger.log("No fuel. Waiting for fuel...")
local x, y, z, gpsActive = M.getPosition()
local posText = gpsActive and
string.format("GPS (%d,%d,%d)", x, y, z) or
string.format("Manual (%d,%d,%d)", x, y, z)
logger.log("No fuel. Waiting for fuel... Location: " .. posText)
sleep(5)
-- Try to refuel again
local added = refuelFromInventory(MIN_FUEL)
fuel = turtle.getFuelLevel()
end
@ -127,49 +129,41 @@ local function try(actionFn, times, name)
return false
end
-- Movement wrappers that update manual position
-- Movement wrappers that update manual position and use try()
function M.forward()
if turtle.forward() then
updatePositionOnMove("forward")
return true
else
return false
end
local function action() return turtle.forward() end
local result = try(action, 5, "forward")
if result then updatePositionOnMove("forward") end
return result
end
function M.back()
if turtle.back() then
updatePositionOnMove("back")
return true
else
return false
end
local function action() return turtle.back() end
local result = try(action, 5, "back")
if result then updatePositionOnMove("back") end
return result
end
function M.up()
if turtle.up() then
updatePositionOnMove("up")
return true
else
return false
end
local function action() return turtle.up() end
local result = try(action, 5, "up")
if result then updatePositionOnMove("up") end
return result
end
function M.down()
if turtle.down() then
updatePositionOnMove("down")
return true
else
return false
end
local function action() return turtle.down() end
local result = try(action, 5, "down")
if result then updatePositionOnMove("down") end
return result
end
-- Digging with movement
-- Digging functions (not wrapped in try, but you can add if you want)
function M.digForward()
while turtle.detect() do
turtle.dig()
sleep(0.3)
logger.log("Dug block in front")
end
end
@ -177,6 +171,7 @@ function M.digUp()
while turtle.detectUp() do
turtle.digUp()
sleep(0.3)
logger.log("Dug block above")
end
end
@ -184,18 +179,21 @@ function M.digDown()
while turtle.detectDown() do
turtle.digDown()
sleep(0.3)
logger.log("Dug block below")
end
end
-- Move forward, clearing blocks if necessary
-- Move forward clearing blocks as needed
function M.moveUntilClearForward()
while not turtle.forward() do
logger.log("Attempting to move forward (auto-clear)")
while not M.forward() do
if turtle.detect() then
turtle.dig()
M.digForward()
else
sleep(0.5)
end
end
logger.log("Moved forward")
end
-- Inspection
@ -214,15 +212,17 @@ function M.inspectDown()
if success then return data else return nil end
end
-- Turning functions that update facing direction
-- Turning functions that update facing
function M.turnLeft()
turtle.turnLeft()
updateFacingLeft()
logger.log("Turned left")
end
function M.turnRight()
turtle.turnRight()
updateFacingRight()
logger.log("Turned right")
end
function M.turnAround()
@ -230,6 +230,7 @@ function M.turnAround()
turtle.turnLeft()
updateFacingLeft()
updateFacingLeft()
logger.log("Turned around")
end
return M