feat: Refactor home position sync and set functions for non-blocking operations
This commit is contained in:
73
turtle.lua
73
turtle.lua
@@ -74,28 +74,9 @@ local function syncHomeWithServer()
|
|||||||
turtleID = os.getComputerID()
|
turtleID = os.getComputerID()
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Wait for response (with timeout)
|
-- Don't block - just request and continue
|
||||||
local timeout = os.startTimer(3)
|
-- The response will be handled in the message processing loop
|
||||||
while true do
|
return true
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Set home position (server-authoritative via wireless)
|
-- Set home position (server-authoritative via wireless)
|
||||||
@@ -106,31 +87,19 @@ local function setHome()
|
|||||||
return false
|
return false
|
||||||
end
|
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, {
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
type = "set_home",
|
type = "set_home",
|
||||||
turtleID = os.getComputerID(),
|
turtleID = os.getComputerID(),
|
||||||
position = state.position
|
position = state.position
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Wait for confirmation
|
print("✅ Home set at:", textutils.serialize(state.homePosition))
|
||||||
local timeout = os.startTimer(3)
|
print(" (Syncing with server in background)")
|
||||||
while true do
|
return true
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Movement tracking
|
-- Movement tracking
|
||||||
@@ -741,7 +710,27 @@ parallel.waitForAny(
|
|||||||
if event == "modem_message" then
|
if event == "modem_message" then
|
||||||
print("Modem message on channel " .. channel)
|
print("Modem message on channel " .. channel)
|
||||||
if channel == CHANNEL_RECEIVE then
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user