rules54

Untitled

Jul 15th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 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 spawnerCount = 0
  7. local greatestLength = 1
  8. local spawners = {}
  9. local sides = {"front", "back", "left", "right", "top", "bottom"}
  10. local default = 1
  11. local id = tonumber(os.getComputerID())
  12. local label = os.getComputerLabel()
  13. local function toggle(variable)
  14. spawners[variable].status = not spawners[variable].status
  15. end
  16. local function event(timeout)
  17. local result = {}
  18. local eventFunction = function()
  19. result = { os.pullEvent() }
  20. if result[1] == "monitor_touch" or result[1] == "modem_message" then
  21. returnValue = true
  22. end
  23. end
  24. parallel.waitForAny(function() sleep(timeout) end, eventFunction)
  25. if returnValue then return result end
  26. end
  27. --Determine which sides the peripherals are located
  28. for i=1, #sides do
  29. if peripheral.getType(sides[i]) == "modem" then
  30. modem = peripheral.wrap(sides[i])
  31. elseif peripheral.getType(sides[i]) == "monitor" then
  32. monitor = peripheral.wrap(sides[i])
  33. end
  34. end
  35. if modem == nil or monitor == nil then
  36. print("Modem/Monitor not found, please attach peripheral.")
  37. end
  38.  
  39. --Open a channelto receive messages
  40. modem.open(default) modem.transmit(default, default, "ping")
  41.  
  42. while true do
  43.  
  44. --Reset some variables
  45. wireless = false
  46. touch = false
  47.  
  48. --Wait for an event(button or wireless)
  49. result = { os.pullEvent() }
  50. if result[1] == "modem_message" then
  51. senderChannel = result[3]
  52. replyChannel = result[4]
  53. message = result[5]
  54. wireless = true
  55. elseif result[1] == "monitor_touch" then
  56. xTouch = result[3]
  57. yTouch = result[4]
  58. touch = true
  59. end
  60.  
  61. if wireless then
  62. --If the event was a message,
  63. --react to message
  64. if string.find(message, "Spawner") ~= nil then
  65. local subtract = string.gsub(message, "Spawner ", '')
  66. spawners[subtract] = { channel = tonumber(replyChannel), status = false, link = false, name = tostring(subtract), toggle = function () button.toggleButton(spawners.subtract.name) spawners.subtract.status = not spawners.subtract.status end }
  67. end
  68.  
  69. --Send a message back to available computers
  70. for k,v in pairs(spawners) do
  71. if not v.link then
  72. modem.transmit(tonumber(v.channel), default, "loop")
  73. spawners[k].link = true
  74. spawnerCount = spawnerCount + 1
  75. end
  76. end
  77. end
  78.  
  79. if touch then
  80. --Touch, I remember, 'touch.'
  81. button.checkxy(xTouch, yTouch)
  82. end
  83.  
  84. --monitor stuff
  85. button.heading("Spawner Control System")
  86.  
  87. for k,v in pairs(spawners) do
  88. --determine longest spawner name
  89. if string.len(v.name) > greatestLength then
  90. greatestLength = string.len(v.name)
  91. end
  92. end
  93. --monitor math
  94. local xMon, yMon = monitor.getSize()
  95. local position = {}
  96. local buttonSize = greatestLength + 4
  97. local possibleButtonRows = math.floor((xMon - 4)/buttonSize) local possibleButtonColumns = math.floor((yMon - 3)/3)
  98. local possibleButtons = possibleButtonRows*possibleButtonColumns
  99. if spawnerCount > possibleButtons then
  100. print("Need more monitors")
  101. end
  102.  
  103.  
  104. --create button out of spawner table
  105. local buttonCurrent = 1
  106.  
  107. for k,v in pairs(spawners) do
  108. local xmin, ymin, xmax, ymax = 0
  109. local rowCurrent = math.floor(buttonCurrent/possibleButtonColumns)
  110. local columnCurrent = math.floor(buttonCurrent/possibleButtonRows)
  111. if buttonCurrent == 1 then
  112. xmin = 2 xmax = xmin+buttonSize
  113. ymin = 3 ymax = ymin+3
  114. else
  115. xmin = columnCurrent-2 xmax = xmin + buttonSize
  116. ymin = (rowCurrent*3)+2 ymax = ymin + 3
  117. end
  118. button.setTable(v.name, v.toggle, xmin, xmax, ymin, ymax)
  119. buttonCurrent = buttonCurrent + 1
  120. end
  121.  
  122. --print to the monitor
  123. button.screen()
  124. for k,v in pairs(spawners) do
  125. modem.transmit(v.channel, default, tostring(v.status))
  126. end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment