From 4bdbc006f880534b09c8abd42096ba64a4cafd28 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Mon, 16 Feb 2026 02:41:33 -0500 Subject: [PATCH] feat: Implement wireless home position sync and set functionality for turtles --- turtle.lua | 84 ++++++++++++++++++++++++++++----------------------- webbridge.lua | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 38 deletions(-) diff --git a/turtle.lua b/turtle.lua index 0facbba..8214de3 100644 --- a/turtle.lua +++ b/turtle.lua @@ -65,30 +65,40 @@ local function updatePosition() return false end --- Sync home position with server +-- Sync home position with server (via wireless broadcast) local function syncHomeWithServer() - if not http then - print("HTTP not available, can't sync with server") - return false - end + -- Request home position from server via webbridge + print("Requesting home position from server...") + modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, { + type = "request_home", + turtleID = os.getComputerID() + }) - 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() + -- Wait for response (with timeout) + local timeout = os.startTimer(3) + while true do + local event, side, channel, replyChannel, message = os.pullEvent() - if data and data.homePosition then - state.homePosition = data.homePosition - print("📍 Synced home from server:", textutils.serialize(data.homePosition)) - return true + if event == "timer" and side == timeout then + print("⚠️ Server sync timeout") + return false + elseif event == "modem_message" and channel == CHANNEL_RECEIVE then + if type(message) == "table" and message.type == "home_position" and message.turtleID == os.getComputerID() then + os.cancelTimer(timeout) + if message.homePosition then + state.homePosition = message.homePosition + print("📍 Synced home from server:", textutils.serialize(message.homePosition)) + return true + else + print("No home position on server") + return false + end + end end end - return false end --- Set home position (server-authoritative) +-- Set home position (server-authoritative via wireless) local function setHome() print("Setting home position...") if not updatePosition() then @@ -96,33 +106,31 @@ local function setHome() return false end - -- 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"}) + -- Send to server via wireless + modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, { + type = "set_home", + turtleID = os.getComputerID(), + position = state.position + }) + + -- Wait for confirmation + local timeout = os.startTimer(3) + while true do + local event, side, channel, replyChannel, message = os.pullEvent() - if response then - local data = textutils.unserializeJSON(response.readAll()) - response.close() - - if data and data.success then - state.homePosition = data.homePosition + if event == "timer" and side == timeout then + print("⚠️ Server response timeout, using local home") + state.homePosition = state.position + return true + elseif event == "modem_message" and channel == CHANNEL_RECEIVE then + if type(message) == "table" and message.type == "home_set_confirm" and message.turtleID == os.getComputerID() then + os.cancelTimer(timeout) + state.homePosition = message.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 diff --git a/webbridge.lua b/webbridge.lua index a0e6149..b704c5a 100644 --- a/webbridge.lua +++ b/webbridge.lua @@ -402,6 +402,75 @@ while true do stats.errors = stats.errors + 1 addLog(" -> Server error", colors.red) end + + elseif message.type == "request_home" then + -- Turtle requesting its home position from server + local turtleID = message.turtleID + addLog("Turtle #" .. turtleID .. " requesting home", colors.cyan) + + local success, result = pcall(function() + local response = http.get(SERVER_URL .. "/api/turtle/" .. turtleID .. "/home") + if response then + local content = response.readAll() + response.close() + local data = textutils.unserializeJSON(content) + return data + end + return nil + end) + + if success and result then + modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, { + type = "home_position", + turtleID = turtleID, + homePosition = result.homePosition + }) + addLog(" -> Sent home position", colors.lime) + else + modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, { + type = "home_position", + turtleID = turtleID, + homePosition = nil + }) + addLog(" -> No home on server", colors.yellow) + end + + elseif message.type == "set_home" then + -- Turtle setting its home position + local turtleID = message.turtleID + local position = message.position + addLog("Turtle #" .. turtleID .. " setting home", colors.cyan) + + local success, result = pcall(function() + local response = http.post( + SERVER_URL .. "/api/turtle/" .. turtleID .. "/home", + textutils.serializeJSON({position = position}), + {["Content-Type"] = "application/json"} + ) + if response then + local content = response.readAll() + response.close() + local data = textutils.unserializeJSON(content) + return data + end + return nil + end) + + if success and result and result.success then + modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, { + type = "home_set_confirm", + turtleID = turtleID, + homePosition = result.homePosition + }) + addLog(" -> Home saved to server", colors.lime) + else + modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, { + type = "home_set_confirm", + turtleID = turtleID, + homePosition = position + }) + addLog(" -> Server error, local only", colors.red) + end end end end