Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This applicaiton serves as a server for my turtle bots as I make more...
- -- I honestly don't intend for other people to use it, I'm just putting it here for myself...
- -- But I don't mind others using it, so feel free to take a gander, reuse, reinvent, plagiarize, whatever...
- -- The main program for my turtle bots is here:
- -- http://pastebin.com/n5cxXaRb
- -- Notes:
- -- Turtle 1 will be on Port 100
- -- Turtle 2 will be on Port 102
- -- Turtle 3 will be on Port 103
- serverPort = 25565
- -- Unknown transmission will be printed cycling incrementally between these lines.
- unknownSourceTop = 30
- unknownSourceBottom = 40
- unknownSourceLine = unknownSourceTop -- Keep track of the current line.
- -- Normal Text Color
- defaultTextColor = colors.white
- -- Normal Screen Color
- defaultBackColor = colors.black
- -- Keeps track of whether we have already received at least one unknown message.
- -- Used to prevent from accidentally printing an orange
- -- square where it doesn't belong.
- hasReceivedAtLeastOneUnknownMessage = false
- -- Prints a message from an unkown source
- function PrintMessageFromUnknownSource(message, port, monitor)
- -- Mark the current line.
- monitor.setCursorPos(1, unknownSourceLine)
- monitor.clearLine()
- monitor.blit(" ","d","d")
- -- Write the text from the unknown source
- monitor.setCursorPos(2, unknownSourceLine)
- monitor.write(unknownSourceLine.." - ["..port.."] - "..message)
- if hasReceivedAtLeastOneUnknownMessage then
- -- Clear the previous line's special mark
- if unknownSourceLine == unknownSourceTop then
- monitor.setCursorPos(1, unknownSourceBottom)
- monitor.blit(" ", "1", "1") -- draw an orange box (space)
- else
- monitor.setCursorPos(1, unknownSourceLine - 1)
- monitor.blit(" ", "1", "1") -- draw an orange box (space)
- end
- else
- hasReceivedAtLeastOneUnknownMessage = true
- end
- -- Increment and wrap unknownSourceLine
- unknownSourceLine = unknownSourceLine + 1
- if unknownSourceLine > unknownSourceBottom then
- unknownSourceLine = unknownSourceTop
- end
- end
- -- Prints a turtle message in it's corresponding location
- function printTurtleMessage(message, distance, port, monitor)
- successful = false -- Used to keep track of we found a corresponding turtlebot.
- if port == 100 then
- successful = true
- monitor.setCursorPos(1, 5)
- monitor.clearLine()
- monitor.write("Turtle 1: "..message)
- elseif port == 102 then
- successful = true
- monitor.setCursorPos(1, 6)
- monitor.clearLine()
- monitor.write("Turtle 2: "..message)
- elseif port == 103 then
- successful = true
- monitor.setCursorPos(1, 7)
- monitor.clearLine()
- monitor.write("Turtle 3: "..message)
- elseif port == 104 then
- successful = true
- monitor.setCursorPos(1, 8)
- monitor.clearLine()
- monitor.write("Turtle 4: "..message)
- elseif port == 105 then
- successful = true
- monitor.setCursorPos(1, 9)
- monitor.clearLine()
- monitor.write("Turtle 5: "..message)
- elseif port == 106 then
- successful = true
- monitor.setCursorPos(1, 10)
- monitor.clearLine()
- monitor.write("Turtle 6: "..message)
- elseif port == 107 then
- successful = true
- monitor.setCursorPos(1, 11)
- monitor.clearLine()
- monitor.write("Turtle 7: "..message)
- elseif port == 108 then
- successful = true
- monitor.setCursorPos(1, 12)
- monitor.clearLine()
- monitor.write("Turtle 8: "..message)
- elseif port == 109 then
- successful = true
- monitor.setCursorPos(1, 13)
- monitor.clearLine()
- monitor.write("Turtle 9: "..message)
- elseif port == 110 then
- successful = true
- monitor.setCursorPos(1, 14)
- monitor.clearLine()
- monitor.write("Turtle 10: "..message)
- end
- if not successful then
- PrintMessageFromUnknownSource(message, port, monitor)
- end
- end
- -- Get a reference to the Modem.
- modem = peripheral.wrap("back") -- Put the location of the modem here.
- -- Open a channel to listen to on the Modem
- modem.open(serverPort)
- if modem.isOpen(serverPort)
- then
- write("The modem is open.\r\n")
- else
- write("The modem is closed.\r\n")
- end
- -- Get a reference to the Monitor
- monitor = peripheral.wrap("top")
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(defaultTextColor)
- monitor.write("This computer is listening for incoming transmissions at address: "..serverPort)
- while true do
- write("Listening for event\r\n")
- local event, modemSide, senderChannel,
- replyChannel, message, senderDistance = os.pullEvent("modem_message")
- printTurtleMessage(message, senderDistance,
- replyChannel, monitor)
- write("Retrieved an event\r\n")
- end
- write("Press any key to exit\r\n")
- os.pullEvent("key")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement