feat: Implement wireless home position sync and set functionality for turtles

This commit is contained in:
MayaTheShy
2026-02-16 02:41:33 -05:00
parent 9048d197a8
commit 4bdbc006f8
2 changed files with 115 additions and 38 deletions

View File

@@ -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