feat: Refactor home position sync and set functions for non-blocking operations

This commit is contained in:
MayaTheShy
2026-02-16 03:07:20 -05:00
parent 4bdbc006f8
commit 1435a7ac55

View File

@@ -74,28 +74,9 @@ local function syncHomeWithServer()
turtleID = os.getComputerID()
})
-- Wait for response (with timeout)
local timeout = os.startTimer(3)
while true do
local event, side, channel, replyChannel, message = os.pullEvent()
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
-- Don't block - just request and continue
-- The response will be handled in the message processing loop
return true
end
-- Set home position (server-authoritative via wireless)
@@ -106,31 +87,19 @@ local function setHome()
return false
end
-- Send to server via wireless
-- Set locally immediately
state.homePosition = state.position
-- Send to server via wireless (fire and forget)
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 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
print("✅ Home set at:", textutils.serialize(state.homePosition))
print(" (Syncing with server in background)")
return true
end
-- Movement tracking
@@ -741,7 +710,27 @@ parallel.waitForAny(
if event == "modem_message" then
print("Modem message on channel " .. channel)
if channel == CHANNEL_RECEIVE then
processMessage(message)
-- Handle home position sync responses
if type(message) == "table" then
if message.type == "home_position" and message.turtleID == os.getComputerID() then
if message.homePosition then
state.homePosition = message.homePosition
print("📍 Synced home from server:", textutils.serialize(message.homePosition))
else
print("No home position on server")
end
elseif message.type == "home_set_confirm" and message.turtleID == os.getComputerID() then
if message.homePosition then
state.homePosition = message.homePosition
print("✅ Server confirmed home:", textutils.serialize(message.homePosition))
end
else
-- Regular command processing
processMessage(message)
end
else
processMessage(message)
end
end
end
end