rules54

Control

Jul 14th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. -- Determine variables, tables, functions, APIs
  2. os.loadAPI("button")
  3. local modem = nil
  4. local monitor = nil
  5. local result = nil
  6. local greatestLength = 1
  7. local spawners = {}
  8. local sides = {"front", "back", "left", "right", "top", "bottom"}
  9. local default = 1
  10. local id = tonumber(os.getComputerID())
  11. local label = os.getComputerLabel()
  12. local function event(timeout)
  13.   local result = {}
  14.   local eventFunction  = function()
  15.     result = { os.pullEvent() }
  16.     if result[1] == "monitor_touch" or result[1] == "modem_message" then
  17.       returnValue = true
  18.     end
  19.   end
  20.   parallel.waitForAny(function() sleep(timeout) end, eventFunction)
  21.   if returnValue then return result end
  22. end
  23. --Determine which sides the peripherals are located
  24. for i=1, #sides do
  25.   if peripheral.getType(sides[i]) == "modem" then
  26.     modem = peripheral.wrap(sides[i])
  27.   elseif peripheral.getType(sides[i]) == "monitor" then
  28.     monitor = peripheral.wrap(sides[i])
  29.   end
  30. end
  31. if modem == nil or monitor == nil then
  32.   print("Modem/Monitor not found, please attach peripheral.")
  33. end
  34.  
  35. --Open a channelto receive messages
  36. modem.open(default) modem.transmit(default, default, "ping")
  37.  
  38. while true do
  39.  
  40.   --Reset some variables
  41.   wireless = false
  42.   touch = false
  43.  
  44.   --Wait for an event(button or wireless)
  45.   result = { os.pullEvent() }
  46.   if result[1] == "modem_message" then
  47.     senderChannel = result[3]
  48.     replyChannel = result[4]
  49.     message = result[5]
  50.     wireless = true
  51.   elseif result[1] == "monitor_touch" then
  52.     xTouch = result[3]
  53.     yTouch = result[4]
  54.     touch = true
  55.   end
  56.  
  57.   if wireless then
  58.     --If the event was a message,
  59.     --react to message
  60.     if string.find(message, "Spawner") ~= nil then
  61.       local subtract = string.gsub(message, "Spawner ", '')
  62.       spawners[subtract] = { channel = tonumber(replyChannel), status = false, link = false, name = tostring(subtract) }
  63.     end
  64.  
  65.     --Send a message back to available computers
  66.     for k,v in pairs(spawners) do
  67.       if not v.link then
  68.         modem.transmit(tonumber(v.channel), default, "loop")
  69.         spawners[v.link] = true
  70.       end
  71.     end
  72.   end
  73.  
  74.   if touch then
  75.     --Touch, I remember, 'touch.'
  76.     button.checkxy(xTouch, yTouch)
  77.   end
  78.  
  79.   --monitor stuff
  80.   for k,v in pairs(spawners) do
  81.     if string.len(v.name) > greatestLength then
  82.       greatestLength = string.len(v.name)
  83.     end
  84.   end
  85.   xMon, yMon = monitor.getSize()
  86.   possibleButtonRows = (xMon - 4)/(greastestLength + 4)
  87.   possibleButtonColumns = (yMon - 3)/3
  88.  
  89.   button.heading("Spawner Control System")
  90.  
  91.  
  92. end
Advertisement
Add Comment
Please, Sign In to add comment