rules54

Control

Jul 18th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. -- Determine variables, tables, functions, APIs
  2. local bDebug = true
  3.  
  4. os.loadAPI("button")
  5. local modem = nil
  6. local monitor = nil
  7. local drive = nil
  8. local result = nil
  9. local buttonMathematics = false
  10. local file = "mobSpawnerChannelsCopy"
  11. local spawnerCount = 0
  12. local greatestLength = 1
  13. local selectPage = 1
  14. local default = os.getComputerID()
  15. local channels = {}
  16. local spawners = {}
  17. local test = {}
  18. local poop = function() print("yes") end
  19. local sides = {"front", "back", "left", "right", "top", "bottom"}
  20. local buttonConfigure = function()
  21.   buttonMathematics = true
  22. end
  23. local function toggle(variable)
  24.   spawners[variable].status = not spawners[variable].status
  25.   button.toggleButton(spawners[variable].name)
  26. end
  27. if bDebug then debug = print else debug = function() end end
  28. -- local function event(timeout)
  29.   -- local result = {}
  30.   -- local eventFunction  = function()
  31.     -- result = { os.pullEvent() }
  32.     -- if result[1] == "monitor_touch" or result[1] == "modem_message" then
  33.       -- returnValue = true
  34.     -- end
  35.   -- end
  36.   -- parallel.waitForAny(function() sleep(timeout) end, eventFunction)
  37.   -- if returnValue then return result end
  38. -- end
  39. --Determine which sides the peripherals are located
  40. for i=1, #sides do
  41.   if peripheral.getType(sides[i]) == "modem" then
  42.     modem = peripheral.wrap(sides[i])
  43.   elseif peripheral.getType(sides[i]) == "monitor" then
  44.     monitor = peripheral.wrap(sides[i])
  45.   elseif peripheral.getType(sides[i]) == "drive" then
  46.     drive = sides[i]
  47.   end
  48. end
  49. if modem == nil or monitor == nil then
  50.   print("Modem/Monitor not found, please attach peripheral.")
  51. end
  52. --check files for channel lists | copy to computer
  53. if drive ~= nil and disk.isPresent(drive) then
  54.   file = "disk/mobSpawnerChannels"
  55. end
  56.  
  57. local f = io.open(file, "r")
  58. local contents = f:read("*a")
  59. local channels = textutils.unserialize(contents)
  60.  
  61. if type(channels) ~= "table" then
  62.   channels = {}
  63. else
  64.   channels = textutils.unserialize(contents)
  65. end
  66.  
  67. if channels.main == nil then
  68.   channels.main = os.getComputerID()
  69. end
  70.  
  71. f = io.open(file, "w")
  72. f:write(textutils.serialize(channels))
  73. f:close()
  74.  
  75. f = io.open("mobSpawnerChannelsCopy", "w")
  76. f:write(textutils.serialize(channels))
  77. f:close()
  78.  
  79. --Open channels to receive messages
  80. for k, v in pairs(channels) do
  81.   modem.open(tonumber(v))
  82.   modem.transmit(tonumber(v),tonumber(v),"ping")
  83. end
  84.  
  85. monitor.clear()
  86.  
  87. --start loop
  88. while true do
  89.  
  90.   --Reset some variables
  91.  
  92.   wireless = false
  93.   touch = false
  94.  
  95.   --Wait for an event(button or wireless)
  96.   result = { os.pullEvent() } debug("pullEvent")
  97.   if result[1] == "modem_message" then
  98.     senderChannel = result[3]
  99.     replyChannel = result[4]
  100.     message = result[5]
  101.     wireless = true
  102.   elseif result[1] == "monitor_touch" then
  103.     xTouch = result[3]
  104.     yTouch = result[4]
  105.     touch = true
  106.   end
  107.  
  108.   if wireless then
  109.     --If the event was a message,
  110.     --react to message
  111.     if string.find(message, "Spawner") ~= nil then
  112.       local subtract = string.gsub(message, "Spawner ", '')
  113.       spawners[subtract] = {
  114.         channel = tonumber(replyChannel);
  115.         status = false;
  116.         link = false;
  117.         name = tostring(subtract);
  118.         toggle = function ()
  119.           button.toggleButton(tostring(spawners[subtract].name))
  120.           spawners[subtract].status = not spawners[subtract].status
  121.           end
  122.       }
  123.     end
  124.    
  125.     --Send a message back to unlinked computers
  126.     for k,v in pairs(spawners) do
  127.       sleep(.1)
  128.       if not v.link then
  129.         modem.transmit(tonumber(v.channel), default, "loop")
  130.         spawners[k].link = true
  131.         spawnerCount = spawnerCount + 1
  132.       end
  133.     end
  134.     debug("Spawner Count: ".. spawnerCount)
  135.   end
  136.  
  137.   if touch then
  138.     --Touch, I remember, 'touch.'
  139.     --if event is a touch, check the touch
  140.     button.checkxy(xTouch, yTouch)
  141.   end
  142.  
  143.   for k,v in pairs(spawners) do
  144.     --determine longest spawner name
  145.     if string.len(v.name) > greatestLength then
  146.       greatestLength = string.len(v.name)
  147.     end
  148.   end
  149.   debug("Determined greatestStringSize")
  150.  
  151.   --monitor math
  152.   local xMon, yMon = monitor.getSize()
  153.   local useableMonX = xMon - 2 --border around all printed items
  154.   local useableMonY = yMon - 3 --border around all printed items
  155.   local buttonSize = greatestLength + 2 --button 'padding'
  156.  
  157.   if buttonMathematics then
  158.  
  159.   local buttonHeight = 3 + 2 --button height, plus padding, plus spacing
  160.   local buttonDistance = (useableMonX-(buttonSize*spawnerCount))/(spawnerCount - 1) --Calculate the distance between each button horizontally | Thanks to Bubba on CC Forums
  161.   local buttonCount = spawnerCount --Rename for simplicity
  162.   local buttonColumns = 1 debug("buttonColumns = 1")
  163.   while buttonDistance < 1 do --Calculate the number of buttons in a row | the number of columns
  164.     buttonColumns = buttonCount - 1 sleep(0) debug("buttonDistance Loop")
  165.     buttonDistance = (useableMonX -(buttonSize*buttonColumns))/(buttonColumns - 1)
  166.   end
  167.   local buttonRows = math.floor((useableMonY / buttonHeight)) --Determine the number of rows that may fit on a page
  168.   -- local buttonHeightDistance = (useableMonY - (buttonHeight/(buttonRows-1))) --Calculate space between buttons vertically
  169.   -- while buttonHeightDistance < 1 do
  170.     -- buttonRows = ((buttonCount / buttonColumns) - 1)
  171.     -- buttonHeightDistance = (useableMonY - (buttonHeight*(buttonRows - 1)/) debug("buttonHeightDistance Loop")
  172.   -- end
  173.   local buttonPages = math.ceil(buttonCount / (buttonRows*buttonColumns)) --Calculate the number of pages required
  174.   debug("Columns: "..buttonColumns) debug("Rows: "..buttonRows)
  175.  
  176.   --create functions to draw button pages
  177.   button.clearTable()
  178.   monitor.clear()
  179.   local buttonCurrent = 1
  180.   local draw = {}
  181.   local xmin = 2 local ymin = 3 local xmax = 0 local ymax = 5 --first button's coords
  182.   button.clearTable() -- reset the button table
  183.   for k,v in pairs(spawners) do
  184.     sleep(.1)
  185.     local rowCurrent = math.ceil(buttonCurrent/buttonColumns) debug("rowCurrent") --determine the current row
  186.     while rowCurrent > buttonRows do rowCurrent = rowCurrent - buttonRows end sleep(0) debug("rowCurrent") --regardless of page
  187.     local columnCurrent = math.ceil(buttonCurrent/buttonRows) debug("columnCurrent")-- determine the current column
  188.     while columnCurrent > buttonColumns do columnCurrent = columnCurrent - buttonColumns end  sleep(0) debug("columnCurrent")-- regardless of page
  189.     local pageCurrent = math.ceil(buttonCurrent/(buttonColumns*buttonRows)) debug("pageCurrent")-- determine the current page
  190.     --coordinate calculate before drawing
  191.     xmax = xmin+buttonSize-1 ymax = ymin+2
  192.     local testString = tostring(v.name)
  193.     if pageCurrent == selectPage then
  194.       button.setTable(testString,v.toggle,xmin,xmax,ymin,ymax,v.status)
  195.     end
  196.     debug(xmin.. " "..xmax.." "..ymin.." "..ymax)
  197.     --coordinate calculate after drawing
  198.     if rowCurrent ~= 1 then
  199.       ymin = (rowCurrent*5)+3
  200.     else ymin = 3 end
  201.     xmin = columnCurrent*buttonDistance
  202.     buttonCurrent = buttonCurrent + 1
  203.   end
  204.   debug("Completed table-coordinate-organization")
  205.   --declare some button functions
  206.   debug("Executed draw table functions.")
  207.   draw.nextPage = function() selectPage = selectPage + 1 end
  208.   draw.prevPage = function() selectPage = selectPage - 1 end
  209.  
  210.   if selectPage < buttonPages then
  211.     button.setTable(">", draw.nextPage, xMon, xMon, yMon, yMon)
  212.   end
  213.   if selectPage ~= 1 then
  214.     button.setTable("<", draw.prevPage, 1, 1, yMon, yMon)
  215.   end
  216.    
  217.   --print to the monitor
  218.   monitor.clear()
  219.   button.heading("Spawner Control System")
  220.   button.screen() debug("This should be seen with buttons on screen")
  221.   else
  222.     button.clearTable()
  223.     button.heading("Check Terminal for Details")
  224.     table.insert(test, 1, function()
  225.       button.setTable("Begin Button Configuration", buttonConfigure, 1, 25, 10, 13) end)
  226.     test[1]()
  227.     button.screen()
  228.   end
  229.   for k,v in pairs(spawners) do
  230.     modem.transmit(v.channel, default, tostring(v.status))
  231.     sleep(.1)
  232.   end
  233.   sleep(1)
  234.   button.screen()
  235. end
Advertisement
Add Comment
Please, Sign In to add comment