rules54

Control_Pre_Monitor

Jul 14th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | None | 0 0
  1. -- Determine variables, tables, etc.
  2. local modem = nil
  3. local monitor = nil
  4. local sides = {"front", "back", "left", "right", "top", "bottom"}
  5. local default = 1
  6. local id = tonumber(os.getComputerID())
  7. local label = os.getComputerLabel()
  8. local channels = {}
  9. local function receive(timeout)
  10.   local result = {}
  11.   local function rnEvent()
  12.     result[1] = os.pullEvent()
  13.     if result[1] == "monitor_touch" or result[1] == "modem_message" then
  14.       returnValue = true
  15.     end
  16.   end
  17.   parallel.waitForAny(function() sleep(timeout) end, rnEvent)
  18.   if returnValue then return result end
  19. end
  20.  
  21. --Determine which sides the peripherals are located
  22. for i=1, #sides do
  23.   if peripheral.getType(sides[i]) == "modem" then
  24.     modem = peripheral.wrap(sides[i])
  25.   elseif periperal.getType(sides[i]) == "monitor" then
  26.     monitor = peripheral.wrap(sides[i])
  27.   end
  28. end
  29. if modem == nil or monitor == nil then
  30.   print("Modem/Monitor not found, please attach peripheral.")
  31. end
  32.  
  33. --Open a channelto receive messages
  34. modem.open(default) modem.transmit(default, default, "ping")
  35.  
  36. while true do
  37.  
  38.   --Wait for a message
  39.   local event, side, sC, replyChannel, message, sD = receive(5)
  40.  
  41.   --If the message begins with "Spawner" then add
  42.   --the sending computer plus the mob type to a list
  43.   if string.find(message, "Spawner") ~= nil then
  44.     local subtract = string.gsub(message, "Spawner ", '')
  45.     table.insert(channels, tonumber(replyChannel), subtract)
  46.     print("Added ".. subtract.. " to list.")
  47.   end
  48.  
  49.   --Send a message back to available computers
  50.   for k,v in pairs(channels) do
  51.     modem.transmit(tonumber(k), default, "loop")
  52.   end
  53.   sleep(5)
  54. end
Advertisement
Add Comment
Please, Sign In to add comment