diff --git a/turtle.lua b/turtle.lua index 68f7b4e..a012ab2 100644 --- a/turtle.lua +++ b/turtle.lua @@ -565,27 +565,33 @@ local commands = { -- Process incoming messages local function processMessage(message) - print("Received message on modem") - print(" Type: " .. type(message)) - - if type(message) == "table" then - print(" Has command: " .. tostring(message.command ~= nil)) - if message.command then - print(" Command: " .. message.command) - end + if type(message) ~= "table" then + return end - if type(message) == "table" and message.command then - print("Processing command: " .. message.command) + -- Check if message is targeted to this turtle + local myID = os.getComputerID() + if message.target and message.target ~= myID then + -- This command is for a different turtle, ignore it + return + end + + print("Received command message") + print(" Target: " .. tostring(message.target)) + print(" My ID: " .. myID) + + if message.command then + print(" Command: " .. message.command) local handler = commands[message.command] if handler then + print(" Executing command...") local success, result = handler(message.param) modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, { status = success and "ok" or "error", message = result or "", - turtleID = os.getComputerID() + turtleID = myID }) broadcastStatus() diff --git a/webbridge.lua b/webbridge.lua index b46270a..b9d9486 100644 --- a/webbridge.lua +++ b/webbridge.lua @@ -301,11 +301,18 @@ while true do stats.commandsSent = stats.commandsSent + 1 addLog("CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow) - modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, { + local commandPacket = { command = cmd.command, param = cmd.param, target = turtleID - }) + } + + modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket) + + -- Debug: show what we're sending + if not hasMonitor then + print(" Sent to channel " .. COMMAND_CHANNEL .. ": target=" .. turtleID) + end end end