feat: Implement home position syncing with server and add distance checks for safe operations
This commit is contained in:
119
turtle.lua
119
turtle.lua
@@ -24,6 +24,8 @@ local config = {
|
||||
minFuelToExplore = 1000,
|
||||
statusUpdateInterval = 5, -- seconds
|
||||
maxStuckAttempts = 3,
|
||||
maxDistanceFromHome = 200, -- blocks - stay within reasonable range
|
||||
pauseIfNoPlayersNearby = false, -- set true if chunk loading is concern
|
||||
valuableBlocks = {
|
||||
["minecraft:coal_ore"] = 1,
|
||||
["minecraft:iron_ore"] = 2,
|
||||
@@ -63,19 +65,64 @@ local function updatePosition()
|
||||
return false
|
||||
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()
|
||||
print("Setting home position...")
|
||||
if updatePosition() then
|
||||
state.homePosition = {
|
||||
x = state.position.x,
|
||||
y = state.position.y,
|
||||
z = state.position.z
|
||||
}
|
||||
print("Home set at: " .. state.homePosition.x .. ", " .. state.homePosition.y .. ", " .. state.homePosition.z)
|
||||
return true
|
||||
if not updatePosition() then
|
||||
print("Failed to get GPS position for home")
|
||||
return false
|
||||
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
|
||||
|
||||
-- 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)
|
||||
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
|
||||
local function forceForward()
|
||||
for i = 1, 5 do
|
||||
@@ -526,6 +593,12 @@ local commands = {
|
||||
|
||||
explore = function()
|
||||
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.stuckCounter = 0
|
||||
broadcastStatus()
|
||||
@@ -534,6 +607,12 @@ local commands = {
|
||||
|
||||
mine = function()
|
||||
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.stuckCounter = 0
|
||||
broadcastStatus()
|
||||
@@ -621,6 +700,14 @@ else
|
||||
print("Position tracking disabled for now.")
|
||||
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()
|
||||
|
||||
print("Ready! Waiting for commands...")
|
||||
@@ -658,8 +745,18 @@ parallel.waitForAny(
|
||||
if state.mode == "exploring" then
|
||||
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
|
||||
if needsRefuel() or inventoryFull() then
|
||||
elseif needsRefuel() or inventoryFull() then
|
||||
print("Need to return home")
|
||||
state.mode = "returning"
|
||||
broadcastStatus()
|
||||
|
||||
Reference in New Issue
Block a user