feat: Implement wireless home position sync and set functionality for turtles
This commit is contained in:
84
turtle.lua
84
turtle.lua
@@ -65,30 +65,40 @@ local function updatePosition()
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sync home position with server
|
-- Sync home position with server (via wireless broadcast)
|
||||||
local function syncHomeWithServer()
|
local function syncHomeWithServer()
|
||||||
if not http then
|
-- Request home position from server via webbridge
|
||||||
print("HTTP not available, can't sync with server")
|
print("Requesting home position from server...")
|
||||||
return false
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
end
|
type = "request_home",
|
||||||
|
turtleID = os.getComputerID()
|
||||||
|
})
|
||||||
|
|
||||||
local url = "http://webbridge:3001/api/turtle/" .. os.getComputerID() .. "/home"
|
-- Wait for response (with timeout)
|
||||||
local response = http.get(url)
|
local timeout = os.startTimer(3)
|
||||||
|
while true do
|
||||||
if response then
|
local event, side, channel, replyChannel, message = os.pullEvent()
|
||||||
local data = textutils.unserializeJSON(response.readAll())
|
|
||||||
response.close()
|
|
||||||
|
|
||||||
if data and data.homePosition then
|
if event == "timer" and side == timeout then
|
||||||
state.homePosition = data.homePosition
|
print("⚠️ Server sync timeout")
|
||||||
print("📍 Synced home from server:", textutils.serialize(data.homePosition))
|
return false
|
||||||
return true
|
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
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Set home position (server-authoritative)
|
-- Set home position (server-authoritative via wireless)
|
||||||
local function setHome()
|
local function setHome()
|
||||||
print("Setting home position...")
|
print("Setting home position...")
|
||||||
if not updatePosition() then
|
if not updatePosition() then
|
||||||
@@ -96,33 +106,31 @@ local function setHome()
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Send to server
|
-- Send to server via wireless
|
||||||
if http then
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
local url = "http://webbridge:3001/api/turtle/" .. os.getComputerID() .. "/home"
|
type = "set_home",
|
||||||
local response = http.post(url, textutils.serializeJSON({
|
turtleID = os.getComputerID(),
|
||||||
position = state.position
|
position = state.position
|
||||||
}), {["Content-Type"] = "application/json"})
|
})
|
||||||
|
|
||||||
|
-- Wait for confirmation
|
||||||
|
local timeout = os.startTimer(3)
|
||||||
|
while true do
|
||||||
|
local event, side, channel, replyChannel, message = os.pullEvent()
|
||||||
|
|
||||||
if response then
|
if event == "timer" and side == timeout then
|
||||||
local data = textutils.unserializeJSON(response.readAll())
|
print("⚠️ Server response timeout, using local home")
|
||||||
response.close()
|
state.homePosition = state.position
|
||||||
|
return true
|
||||||
if data and data.success then
|
elseif event == "modem_message" and channel == CHANNEL_RECEIVE then
|
||||||
state.homePosition = data.homePosition
|
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))
|
print("✅ Home set at:", textutils.serialize(state.homePosition))
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-- Movement tracking
|
-- Movement tracking
|
||||||
|
|||||||
@@ -402,6 +402,75 @@ while true do
|
|||||||
stats.errors = stats.errors + 1
|
stats.errors = stats.errors + 1
|
||||||
addLog(" -> Server error", colors.red)
|
addLog(" -> Server error", colors.red)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user