rules54

Untitled

Jul 15th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 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. monitor.clear()
  42. while true do
  43.  
  44. --Reset some variables
  45.  
  46. wireless = false
  47. touch = false
  48.  
  49. --Wait for an event(button or wireless)
  50. result = { os.pullEvent() }
  51. if result[1] == "modem_message" then
  52. senderChannel = result[3]
  53. replyChannel = result[4]
  54. message = result[5]
  55. wireless = true
  56. elseif result[1] == "monitor_touch" then
  57. xTouch = result[3]
  58. yTouch = result[4]
  59. touch = true
  60. end
  61.  
  62. if wireless then
  63. --If the event was a message,
  64. --react to message
  65. if string.find(message, "Spawner") ~= nil then
  66. local subtract = string.gsub(message, "Spawner ", '')
  67. spawners[subtract] = {
  68. channel = tonumber(replyChannel);
  69. status = false;
  70. link = false;
  71. name = tostring(subtract);
  72. toggle = function ()
  73. button.toggleButton(spawners.subtract.name)
  74. spawners.subtract.status = not spawners.subtract.status
  75. end
  76. }
  77. end
  78.  
  79. --Send a message back to unlinked computers
  80. for k,v in pairs(spawners) do
  81. if not v.link then
  82. modem.transmit(tonumber(v.channel), default, "loop")
  83. spawners[k].link = true
  84. spawnerCount = spawnerCount + 1
  85. end
  86. end
  87. end
  88.  
  89. if touch then
  90. --Touch, I remember, 'touch.'
  91. --if event is a touch, check the touch
  92. button.checkxy(xTouch, yTouch)
  93. end
  94.  
  95. --monitor stuff
  96. button.heading("Spawner Control System")
  97.  
  98. for k,v in pairs(spawners) do
  99. --determine longest spawner name
  100. if string.len(v.name) > greatestLength then
  101. greatestLength = string.len(v.name)
  102. end
  103. end
  104. --monitor math
  105. local xMon, yMon = monitor.getSize()
  106. local position = {}
  107. local buttonSize = greatestLength + 4
  108. local possibleButtonRows = math.floor((xMon - 4)/buttonSize) local possibleButtonColumns = math.floor((yMon - 3)/3)
  109. local possibleButtons = possibleButtonRows*possibleButtonColumns
  110. if spawnerCount > possibleButtons then
  111. print("Need more monitors")
  112. end
  113.  
  114.  
  115. --create button out of spawner table
  116. local buttonCurrent = 1
  117.  
  118. for k,v in pairs(spawners) do
  119. local xmin, ymin, xmax, ymax = 0
  120. local rowCurrent = math.floor(buttonCurrent/possibleButtonColumns)
  121. local columnCurrent = math.floor(buttonCurrent/possibleButtonRows)
  122. if buttonCurrent == 1 then
  123. xmin = 2 xmax = xmin+buttonSize
  124. ymin = 3 ymax = ymin+3
  125. else
  126. xmin = columnCurrent-2 xmax = xmin + buttonSize
  127. ymin = (rowCurrent*3)+2 ymax = ymin + 3
  128. end
  129. button.setTable(v.name, v.toggle, xmin, xmax, ymin, ymax)
  130. buttonCurrent = buttonCurrent + 1
  131. end
  132.  
  133. --print to the monitor
  134. button.screen()
  135. for k,v in pairs(spawners) do
  136. modem.transmit(v.channel, default, tostring(v.status))
  137. end
  138. end
Advertisement
Add Comment
Please, Sign In to add comment