rules54

wtf

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