rules54

Untitled

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