feat: Implement home position syncing with server and add distance checks for safe operations

This commit is contained in:
MayaTheShy
2026-02-16 02:38:25 -05:00
parent e87ee43822
commit bd758e4c7b

View File

@@ -24,6 +24,8 @@ local config = {
minFuelToExplore = 1000, minFuelToExplore = 1000,
statusUpdateInterval = 5, -- seconds statusUpdateInterval = 5, -- seconds
maxStuckAttempts = 3, maxStuckAttempts = 3,
maxDistanceFromHome = 200, -- blocks - stay within reasonable range
pauseIfNoPlayersNearby = false, -- set true if chunk loading is concern
valuableBlocks = { valuableBlocks = {
["minecraft:coal_ore"] = 1, ["minecraft:coal_ore"] = 1,
["minecraft:iron_ore"] = 2, ["minecraft:iron_ore"] = 2,
@@ -63,19 +65,64 @@ local function updatePosition()
return false return false
end end
-- Sync home position with server
local function syncHomeWithServer()
if not http then
print("HTTP not available, can't sync with server")
return false
end
local url = "http://webbridge:3001/api/turtle/" .. os.getComputerID() .. "/home"
local response = http.get(url)
if response then
local data = textutils.unserializeJSON(response.readAll())
response.close()
if data and data.homePosition then
state.homePosition = data.homePosition
print("📍 Synced home from server:", textutils.serialize(data.homePosition))
return true
end
end
return false
end
-- Set home position (server-authoritative)
local function setHome() local function setHome()
print("Setting home position...") print("Setting home position...")
if updatePosition() then if not updatePosition() then
state.homePosition = { print("Failed to get GPS position for home")
x = state.position.x, return false
y = state.position.y,
z = state.position.z
}
print("Home set at: " .. state.homePosition.x .. ", " .. state.homePosition.y .. ", " .. state.homePosition.z)
return true
end end
print("Failed to get GPS position for home")
return false -- Send to server
if http then
local url = "http://webbridge:3001/api/turtle/" .. os.getComputerID() .. "/home"
local response = http.post(url, textutils.serializeJSON({
position = state.position
}), {["Content-Type"] = "application/json"})
if response then
local data = textutils.unserializeJSON(response.readAll())
response.close()
if data and data.success then
state.homePosition = data.homePosition
print("✅ Home set at:", textutils.serialize(state.homePosition))
return true
end
end
end
-- Fallback to local storage if server unavailable
state.homePosition = {
x = state.position.x,
y = state.position.y,
z = state.position.z
}
print("⚠️ Home set locally (server unavailable)")
return true
end end
-- Movement tracking -- Movement tracking
@@ -236,6 +283,26 @@ local function getDistance(pos1, pos2)
return math.abs(pos1.x - pos2.x) + math.abs(pos1.y - pos2.y) + math.abs(pos1.z - pos2.z) return math.abs(pos1.x - pos2.x) + math.abs(pos1.y - pos2.y) + math.abs(pos1.z - pos2.z)
end end
-- Check if turtle is too far from home
local function isTooFarFromHome()
if not state.position or not state.homePosition then
return false
end
local distance = getDistance(state.position, state.homePosition)
return distance > config.maxDistanceFromHome
end
-- Check if it's safe to continue operations (for chunk loading concerns)
local function shouldPauseOperations()
if not config.pauseIfNoPlayersNearby then
return false -- Feature disabled
end
-- You can add player detection logic here if using commands
-- For now, just check distance from home as a safety measure
return isTooFarFromHome()
end
-- Try to dig and move forward -- Try to dig and move forward
local function forceForward() local function forceForward()
for i = 1, 5 do for i = 1, 5 do
@@ -526,6 +593,12 @@ local commands = {
explore = function() explore = function()
print("Explore command received") print("Explore command received")
if not state.homePosition then
return false, "Home not set! Use setHome first"
end
if isTooFarFromHome() then
return false, "Already at max distance from home"
end
state.mode = "exploring" state.mode = "exploring"
state.stuckCounter = 0 state.stuckCounter = 0
broadcastStatus() broadcastStatus()
@@ -534,6 +607,12 @@ local commands = {
mine = function() mine = function()
print("Mine command received") print("Mine command received")
if not state.homePosition then
return false, "Home not set! Use setHome first"
end
if isTooFarFromHome() then
return false, "Already at max distance from home"
end
state.mode = "exploring" -- Use exploring mode for mining state.mode = "exploring" -- Use exploring mode for mining
state.stuckCounter = 0 state.stuckCounter = 0
broadcastStatus() broadcastStatus()
@@ -621,6 +700,14 @@ else
print("Position tracking disabled for now.") print("Position tracking disabled for now.")
end end
-- Sync state with server
print("Syncing with server...")
if syncHomeWithServer() then
print("✅ Server sync complete")
else
print("⚠️ Server sync failed, using local state")
end
updateFuel() updateFuel()
print("Ready! Waiting for commands...") print("Ready! Waiting for commands...")
@@ -658,8 +745,18 @@ parallel.waitForAny(
if state.mode == "exploring" then if state.mode == "exploring" then
updateFuel() updateFuel()
-- Safety check: too far from home
if isTooFarFromHome() then
print("Too far from home! Returning...")
state.mode = "returning"
broadcastStatus()
-- Check if we need to pause (chunk loading concern)
elseif shouldPauseOperations() then
print("Pausing operations (safety limit)")
state.mode = "idle"
broadcastStatus()
-- Check if we should stop -- Check if we should stop
if needsRefuel() or inventoryFull() then elseif needsRefuel() or inventoryFull() then
print("Need to return home") print("Need to return home")
state.mode = "returning" state.mode = "returning"
broadcastStatus() broadcastStatus()