test
This commit is contained in:
parent
394af21e87
commit
f1f29471e1
@ -3,11 +3,61 @@ 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)
|
-- Position tracking state for manual mode
|
||||||
statusCallback = fn
|
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
|
||||||
|
if x and y and z then
|
||||||
|
return { x = math.floor(x), y = math.floor(y), z = math.floor(z) }
|
||||||
end
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Manual position update helpers
|
||||||
|
local function updatePositionOnMove(direction)
|
||||||
|
if direction == "forward" then
|
||||||
|
if directions[facing] == "north" then manualPos.z = manualPos.z - 1
|
||||||
|
elseif directions[facing] == "south" then manualPos.z = manualPos.z + 1
|
||||||
|
elseif directions[facing] == "east" then manualPos.x = manualPos.x + 1
|
||||||
|
elseif directions[facing] == "west" then manualPos.x = manualPos.x - 1 end
|
||||||
|
elseif direction == "back" then
|
||||||
|
if directions[facing] == "north" then manualPos.z = manualPos.z + 1
|
||||||
|
elseif directions[facing] == "south" then manualPos.z = manualPos.z - 1
|
||||||
|
elseif directions[facing] == "east" then manualPos.x = manualPos.x - 1
|
||||||
|
elseif directions[facing] == "west" then manualPos.x = manualPos.x + 1 end
|
||||||
|
elseif direction == "up" then manualPos.y = manualPos.y + 1
|
||||||
|
elseif direction == "down" then manualPos.y = manualPos.y - 1 end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function updateFacingLeft()
|
||||||
|
facing = facing - 1
|
||||||
|
if facing < 1 then facing = 4 end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function updateFacingRight()
|
||||||
|
facing = facing + 1
|
||||||
|
if facing > 4 then facing = 1 end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Public function to get current position (GPS or manual)
|
||||||
|
function M.getPosition()
|
||||||
|
local gpsPos = tryGPS()
|
||||||
|
if gpsPos then
|
||||||
|
return gpsPos.x, gpsPos.y, gpsPos.z, true
|
||||||
|
else
|
||||||
|
return manualPos.x, manualPos.y, manualPos.z, false
|
||||||
|
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 -- you can raise this if you want a safety margin
|
||||||
@ -77,22 +127,43 @@ local function try(actionFn, times, name)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Basic movement
|
-- Movement wrappers that update manual position
|
||||||
function M.forward()
|
function M.forward()
|
||||||
return try(turtle.forward)
|
if turtle.forward() then
|
||||||
|
updatePositionOnMove("forward")
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.back()
|
function M.back()
|
||||||
return try(turtle.back)
|
if turtle.back() then
|
||||||
|
updatePositionOnMove("back")
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.up()
|
function M.up()
|
||||||
return try(turtle.up)
|
if turtle.up() then
|
||||||
|
updatePositionOnMove("up")
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.down()
|
function M.down()
|
||||||
return try(turtle.down)
|
if turtle.down() then
|
||||||
|
updatePositionOnMove("down")
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Digging with movement
|
-- Digging with movement
|
||||||
function M.digForward()
|
function M.digForward()
|
||||||
@ -143,18 +214,22 @@ function M.inspectDown()
|
|||||||
if success then return data else return nil end
|
if success then return data else return nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Turning
|
-- Turning functions that update facing direction
|
||||||
function M.turnLeft()
|
function M.turnLeft()
|
||||||
turtle.turnLeft()
|
turtle.turnLeft()
|
||||||
|
updateFacingLeft()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.turnRight()
|
function M.turnRight()
|
||||||
turtle.turnRight()
|
turtle.turnRight()
|
||||||
|
updateFacingRight()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.turnAround()
|
function M.turnAround()
|
||||||
turtle.turnLeft()
|
turtle.turnLeft()
|
||||||
turtle.turnLeft()
|
turtle.turnLeft()
|
||||||
|
updateFacingLeft()
|
||||||
|
updateFacingLeft()
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user