Advertisement
Machuga14

ComputerCraft FTB Infinity Skyblock Server

Apr 30th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. -- This applicaiton serves as a server for my turtle bots as I make more...
  2. -- I honestly don't intend for other people to use it, I'm just putting it here for myself...
  3. -- But I don't mind others using it, so feel free to take a gander, reuse, reinvent, plagiarize, whatever...
  4. -- The main program for my turtle bots is here:
  5. -- http://pastebin.com/n5cxXaRb
  6.  
  7. -- Notes:
  8. --       Turtle 1 will be on Port 100
  9. --       Turtle 2 will be on Port 102
  10. --       Turtle 3 will be on Port 103
  11.  
  12. serverPort = 25565
  13.  
  14. -- Unknown transmission will be printed cycling incrementally between these lines.
  15. unknownSourceTop = 30
  16. unknownSourceBottom = 40
  17. unknownSourceLine = unknownSourceTop -- Keep track of the current line.
  18.  
  19. -- Normal Text Color
  20. defaultTextColor = colors.white
  21. -- Normal Screen Color
  22. defaultBackColor = colors.black
  23.  
  24. -- Keeps track of whether we have already received at least one unknown message.
  25. -- Used to prevent from accidentally printing an orange
  26. -- square where it doesn't belong.
  27. hasReceivedAtLeastOneUnknownMessage = false
  28.  
  29. -- Prints a message from an unkown source
  30. function PrintMessageFromUnknownSource(message, port, monitor)
  31.   -- Mark the current line.
  32.   monitor.setCursorPos(1, unknownSourceLine)
  33.   monitor.clearLine()
  34.   monitor.blit(" ","d","d")
  35.   -- Write the text from the unknown source
  36.   monitor.setCursorPos(2, unknownSourceLine)
  37.   monitor.write(unknownSourceLine.." - ["..port.."] - "..message)
  38.  
  39.   if hasReceivedAtLeastOneUnknownMessage then
  40.     -- Clear the previous line's special mark
  41.     if unknownSourceLine == unknownSourceTop then
  42.       monitor.setCursorPos(1, unknownSourceBottom)
  43.       monitor.blit(" ", "1", "1") -- draw an orange box (space)
  44.     else
  45.       monitor.setCursorPos(1, unknownSourceLine - 1)
  46.       monitor.blit(" ", "1", "1") -- draw an orange box (space)
  47.     end
  48.   else
  49.     hasReceivedAtLeastOneUnknownMessage = true
  50.   end
  51.  
  52.  
  53.   -- Increment and wrap unknownSourceLine
  54.   unknownSourceLine = unknownSourceLine + 1
  55.  
  56.   if unknownSourceLine > unknownSourceBottom then
  57.     unknownSourceLine = unknownSourceTop
  58.   end
  59. end
  60.  
  61. -- Prints a turtle message in it's corresponding location
  62. function printTurtleMessage(message, distance, port, monitor)
  63.   successful = false -- Used to keep track of we found a corresponding turtlebot.
  64.  
  65.   if port == 100 then
  66.     successful = true
  67.     monitor.setCursorPos(1, 5)
  68.     monitor.clearLine()
  69.     monitor.write("Turtle 1: "..message)
  70.   elseif port == 102 then
  71.     successful = true
  72.     monitor.setCursorPos(1, 6)
  73.     monitor.clearLine()
  74.     monitor.write("Turtle 2: "..message)
  75.   elseif port == 103 then
  76.     successful = true
  77.     monitor.setCursorPos(1, 7)
  78.     monitor.clearLine()
  79.     monitor.write("Turtle 3: "..message)
  80.   elseif port == 104 then
  81.     successful = true
  82.     monitor.setCursorPos(1, 8)
  83.     monitor.clearLine()
  84.     monitor.write("Turtle 4: "..message)
  85.   elseif port == 105 then
  86.     successful = true
  87.     monitor.setCursorPos(1, 9)
  88.     monitor.clearLine()
  89.     monitor.write("Turtle 5: "..message)
  90.   elseif port == 106 then
  91.     successful = true
  92.     monitor.setCursorPos(1, 10)
  93.     monitor.clearLine()
  94.     monitor.write("Turtle 6: "..message)
  95.   elseif port == 107 then
  96.     successful = true
  97.     monitor.setCursorPos(1, 11)
  98.     monitor.clearLine()
  99.     monitor.write("Turtle 7: "..message)
  100.   elseif port == 108 then
  101.     successful = true
  102.     monitor.setCursorPos(1, 12)
  103.     monitor.clearLine()
  104.     monitor.write("Turtle 8: "..message)
  105.   elseif port == 109 then
  106.     successful = true
  107.     monitor.setCursorPos(1, 13)
  108.     monitor.clearLine()
  109.     monitor.write("Turtle 9: "..message)
  110.   elseif port == 110 then
  111.     successful = true
  112.     monitor.setCursorPos(1, 14)
  113.     monitor.clearLine()
  114.     monitor.write("Turtle 10: "..message)
  115.   end
  116.  
  117.   if not successful then
  118.     PrintMessageFromUnknownSource(message, port, monitor)
  119.   end
  120. end
  121.  
  122. -- Get a reference to the Modem.
  123. modem = peripheral.wrap("back") -- Put the location of the modem here.
  124.  
  125. -- Open a channel to listen to on the Modem
  126. modem.open(serverPort)
  127.  
  128. if modem.isOpen(serverPort)
  129. then
  130.   write("The modem is open.\r\n")
  131. else
  132.   write("The modem is closed.\r\n")
  133. end
  134.  
  135. -- Get a reference to the Monitor
  136. monitor = peripheral.wrap("top")
  137. monitor.clear()
  138.  
  139. monitor.setCursorPos(1, 1)
  140. monitor.setTextColor(defaultTextColor)
  141. monitor.write("This computer is listening for incoming transmissions at address: "..serverPort)
  142.  
  143. while true do
  144.   write("Listening for event\r\n")
  145.  
  146.   local event, modemSide, senderChannel,
  147.     replyChannel, message, senderDistance = os.pullEvent("modem_message")
  148.  
  149.   printTurtleMessage(message, senderDistance,
  150.     replyChannel, monitor)
  151.  
  152.   write("Retrieved an event\r\n")
  153. end
  154.  
  155. write("Press any key to exit\r\n")
  156. os.pullEvent("key")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement