test
This commit is contained in:
parent
f1f29471e1
commit
e0c5b67eea
@ -3,7 +3,6 @@ local M = {}
|
|||||||
local logger = require("libs.logger")
|
local logger = require("libs.logger")
|
||||||
|
|
||||||
-- Optional external status setter (set via M.setStatusCallback)
|
-- Optional external status setter (set via M.setStatusCallback)
|
||||||
-- Status callback
|
|
||||||
local statusCallback = nil
|
local statusCallback = nil
|
||||||
function M.setStatusCallback(fn) statusCallback = fn end
|
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 directions = { "north", "east", "south", "west" }
|
||||||
local facing = 1
|
local facing = 1
|
||||||
|
|
||||||
|
|
||||||
-- GPS attempt
|
-- GPS attempt
|
||||||
local function tryGPS()
|
local function tryGPS()
|
||||||
local x, y, z = gps.locate(2) -- 2 second timeout
|
local x, y, z = gps.locate(2) -- 2 second timeout
|
||||||
@ -58,11 +56,10 @@ function M.getPosition()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Minimum fuel required to proceed with one movement
|
-- 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)
|
local function refuelFromInventory(minRequired)
|
||||||
minRequired = minRequired or MIN_FUEL
|
minRequired = minRequired or MIN_FUEL
|
||||||
local gained = 0
|
local gained = 0
|
||||||
@ -99,10 +96,15 @@ local function refuelIfNeeded()
|
|||||||
|
|
||||||
while fuel < MIN_FUEL do
|
while fuel < MIN_FUEL do
|
||||||
if statusCallback then statusCallback("out_of_fuel") end
|
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)
|
sleep(5)
|
||||||
|
|
||||||
-- Try to refuel again
|
|
||||||
local added = refuelFromInventory(MIN_FUEL)
|
local added = refuelFromInventory(MIN_FUEL)
|
||||||
fuel = turtle.getFuelLevel()
|
fuel = turtle.getFuelLevel()
|
||||||
end
|
end
|
||||||
@ -127,49 +129,41 @@ local function try(actionFn, times, name)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Movement wrappers that update manual position
|
-- Movement wrappers that update manual position and use try()
|
||||||
function M.forward()
|
function M.forward()
|
||||||
if turtle.forward() then
|
local function action() return turtle.forward() end
|
||||||
updatePositionOnMove("forward")
|
local result = try(action, 5, "forward")
|
||||||
return true
|
if result then updatePositionOnMove("forward") end
|
||||||
else
|
return result
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.back()
|
function M.back()
|
||||||
if turtle.back() then
|
local function action() return turtle.back() end
|
||||||
updatePositionOnMove("back")
|
local result = try(action, 5, "back")
|
||||||
return true
|
if result then updatePositionOnMove("back") end
|
||||||
else
|
return result
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.up()
|
function M.up()
|
||||||
if turtle.up() then
|
local function action() return turtle.up() end
|
||||||
updatePositionOnMove("up")
|
local result = try(action, 5, "up")
|
||||||
return true
|
if result then updatePositionOnMove("up") end
|
||||||
else
|
return result
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.down()
|
function M.down()
|
||||||
if turtle.down() then
|
local function action() return turtle.down() end
|
||||||
updatePositionOnMove("down")
|
local result = try(action, 5, "down")
|
||||||
return true
|
if result then updatePositionOnMove("down") end
|
||||||
else
|
return result
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Digging functions (not wrapped in try, but you can add if you want)
|
||||||
-- Digging with movement
|
|
||||||
function M.digForward()
|
function M.digForward()
|
||||||
while turtle.detect() do
|
while turtle.detect() do
|
||||||
turtle.dig()
|
turtle.dig()
|
||||||
sleep(0.3)
|
sleep(0.3)
|
||||||
|
logger.log("Dug block in front")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -177,6 +171,7 @@ function M.digUp()
|
|||||||
while turtle.detectUp() do
|
while turtle.detectUp() do
|
||||||
turtle.digUp()
|
turtle.digUp()
|
||||||
sleep(0.3)
|
sleep(0.3)
|
||||||
|
logger.log("Dug block above")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -184,18 +179,21 @@ function M.digDown()
|
|||||||
while turtle.detectDown() do
|
while turtle.detectDown() do
|
||||||
turtle.digDown()
|
turtle.digDown()
|
||||||
sleep(0.3)
|
sleep(0.3)
|
||||||
|
logger.log("Dug block below")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Move forward, clearing blocks if necessary
|
-- Move forward clearing blocks as needed
|
||||||
function M.moveUntilClearForward()
|
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
|
if turtle.detect() then
|
||||||
turtle.dig()
|
M.digForward()
|
||||||
else
|
else
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
logger.log("Moved forward")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Inspection
|
-- Inspection
|
||||||
@ -214,15 +212,17 @@ function M.inspectDown()
|
|||||||
if success then return data else return nil end
|
if success then return data else return nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Turning functions that update facing direction
|
-- Turning functions that update facing
|
||||||
function M.turnLeft()
|
function M.turnLeft()
|
||||||
turtle.turnLeft()
|
turtle.turnLeft()
|
||||||
updateFacingLeft()
|
updateFacingLeft()
|
||||||
|
logger.log("Turned left")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.turnRight()
|
function M.turnRight()
|
||||||
turtle.turnRight()
|
turtle.turnRight()
|
||||||
updateFacingRight()
|
updateFacingRight()
|
||||||
|
logger.log("Turned right")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.turnAround()
|
function M.turnAround()
|
||||||
@ -230,6 +230,7 @@ function M.turnAround()
|
|||||||
turtle.turnLeft()
|
turtle.turnLeft()
|
||||||
updateFacingLeft()
|
updateFacingLeft()
|
||||||
updateFacingLeft()
|
updateFacingLeft()
|
||||||
|
logger.log("Turned around")
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
Loading…
Reference in New Issue
Block a user