Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Slaves
- -- By Fenthis
- slave_file = "/slave_id.txt"
- slave_channel = 6969
- status_channel = 2069
- -- Set a reasonable name for a slave.
- l = "Slave_" .. os.getComputerID()
- print("Setting label to: " .. l)
- os.setComputerLabel(l)
- -- Assign to group
- args = { ... }
- if #args > 0 then
- group = args[1]
- f = fs.open(slave_file, "w")
- f.writeLine(group)
- f.close()
- else
- if fs.exists(slave_file) then
- f = fs.open(slave_file, "r")
- group = f.readLine()
- f.close()
- else
- print("I don't know what slave group I am in.")
- exit()
- end
- end
- print("My group ID is: \""..group.."\"")
- -- Connect to the model
- local modem = peripheral.wrap("right")
- modem.open(slave_channel)
- -- Send a reply
- local function send_status(message)
- x, y, z = gps.locate()
- if x then
- message.location = vector.new(x,y,z)
- end
- message.sender = os.getComputerLabel()
- message.group = group
- modem.transmit(status_channel, slave_channel,
- textutils.serialize(message))
- end
- -- The message handling
- local function run_command(message)
- cmd = textutils.unserialize(message)
- if not cmd then return end
- if not cmd.group == group then return end
- if cmd.message_type == "slave_command" then
- print("Recieved command: \"" .. cmd.command .. "\" from " .. cmd.sender)
- send_status({["message_type"] = "running command",
- ["command"] = cmd.command})
- a = cmd.args or {}
- shell.run(cmd.command, unpack(a))
- send_status({["message_type"] = "finished command",
- ["command"] = cmd.command})
- end
- end
- -- Announce us
- send_status({["message_type"] = "online"})
- -- Main Loop
- print("Waiting for commands, press Q to quit.")
- while true do
- local event, modemSide, senderChannel,
- replyChannel, message, senderDistance = os.pullEvent()
- if event == "char" then
- if (modemSide == "Q" or modemSide == "q") then
- break
- end
- elseif event == "modem_message" then
- -- print("Message: " .. message)
- run_command(message)
- end
- end
- -- Exit!
- modem.close(slave_channel)
- send_status({["message_type"] = "offline"})
- print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement