rules54

WarHawk

Jul 18th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 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 default = os.getComputerID()
  14. local channels = {}
  15. local spawners = {}
  16. local sides = {"front", "back", "left", "right", "top", "bottom"}
  17. local buttonConfigure = function()
  18.   buttonMathematics = true
  19. end
  20.  
  21. if bDebug then debug = print else debug = function() end end
  22.  
  23. for i=1, #sides do
  24.   if peripheral.getType(sides[i]) == "modem" then
  25.     modem = peripheral.wrap(sides[i])
  26.   elseif peripheral.getType(sides[i]) == "monitor" then
  27.     monitor = peripheral.wrap(sides[i])
  28.   elseif peripheral.getType(sides[i]) == "drive" then
  29.     drive = 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. --check files for channel lists | copy to computer
  36. if drive ~= nil and disk.isPresent(drive) then
  37.   file = "disk/mobSpawnerChannels"
  38. end
  39.  
  40. local f = io.open(file, "r")
  41. local contents = f:read("*a")
  42. local channels = textutils.unserialize(contents)
  43.  
  44. if type(channels) ~= "table" then
  45.   channels = {}
  46. else
  47.   channels = textutils.unserialize(contents)
  48. end
  49.  
  50. if channels.main == nil then
  51.   channels.main = os.getComputerID()
  52. end
  53.  
  54. f = io.open(file, "w")
  55. f:write(textutils.serialize(channels))
  56. f:close()
  57.  
  58. f = io.open("mobSpawnerChannelsCopy", "w")
  59. f:write(textutils.serialize(channels))
  60. f:close()
  61.  
  62. --Open channels to receive messages
  63. for k, v in pairs(channels) do
  64.   modem.open(tonumber(v))
  65.   modem.transmit(tonumber(v),tonumber(v),"ping")
  66. end
  67.  
  68. monitor.clear()
  69.  
  70. --start loop
  71. while true do
  72.  
  73.   --Reset some variables
  74.  
  75.   wireless = false
  76.   touch = false
  77.  
  78.   --Wait for an event(button or wireless)
  79.   result = { os.pullEvent() } debug("pullEvent")
  80.   if result[1] == "modem_message" then
  81.     senderChannel = result[3]
  82.     replyChannel = result[4]
  83.     message = result[5]
  84.     wireless = true
  85.   elseif result[1] == "monitor_touch" then
  86.     xTouch = result[3]
  87.     yTouch = result[4]
  88.     touch = true
  89.   end
  90.  
  91.   if wireless then
  92.     --If the event was a message,
  93.     --react to message
  94.     if string.find(message, "Spawner") ~= nil then
  95.       local subtract = string.gsub(message, "Spawner ", '')
  96.       spawners[subtract] = {
  97.         channel = tonumber(replyChannel);
  98.         status = false;
  99.         link = false;
  100.         name = tostring(subtract);
  101.         toggle = function ()
  102.           button.toggleButton(spawners[subtract].name)
  103.           spawners[subtract].status = not spawners[subtract].status
  104.           end
  105.       }
  106.     end
  107.    
  108.     --Send a message back to unlinked computers
  109.     for k,v in pairs(spawners) do
  110.       sleep(.1)
  111.       if not v.link then
  112.         modem.transmit(tonumber(v.channel), default, "loop")
  113.         spawners[k].link = true
  114.         spawnerCount = spawnerCount + 1
  115.       end
  116.     end
  117.     debug("Spawner Count: ".. spawnerCount)
  118.   end
  119.  
  120.   if touch then
  121.     --Touch, I remember, 'touch.'
  122.     --if event is a touch, check the touch
  123.     button.checkxy(xTouch, yTouch)
  124.   end
  125.  
  126.   for k,v in pairs(spawners) do
  127.     --determine longest spawner name
  128.     if string.len(v.name) > greatestLength then
  129.       greatestLength = string.len(v.name)
  130.     end
  131.   end
  132.  
  133.   --monitor math
  134.   local xMon, yMon = monitor.getSize()
  135.   local useableMonX = xMon - 2 --border around all printed items
  136.   local useableMonY = yMon - 3 --border around all printed items
  137.   local buttonSize = greatestLength + 2 --button 'padding'
  138.  
  139.   if wireless then
  140.   --create functions to draw button pages
  141.   button.clearTable()
  142.   monitor.clear()
  143.   local buttonCurrent = 1
  144.   local xmin = 7 local ymin = 4 local xmax = 18 local ymax = 6 --first button's coords 7 between x 3 between y
  145.   button.clearTable() -- reset the button table
  146.   for k,v in pairs(spawners) do
  147.     sleep(.1)
  148.     if buttonCurrent == 1 then
  149.       button.setTable(v.name,v.toggle,8,18,4,6,v.status)
  150.     elseif buttonCurrent == 3 then
  151.       button.setTable(v.name,v.toggle,8,18,14,16,v.status)
  152.     elseif buttonCurrent == 2 then
  153.       button.setTable(v.name,v.toggle,32,42,4,6,v.status)
  154.     elseif buttonCurrent == 4 then
  155.       button.setTable(v.name,v.toggle,32,42,14,16,v.status)
  156.     end
  157.     buttonCurrent = buttonCurrent + 1
  158.   end
  159.  
  160.   --print to the monitor
  161.   button.heading("MrPawlyHawk Spawning & Co.")
  162.   end
  163.   for k,v in pairs(spawners) do
  164.   modem.transmit(v.channel, default, tostring(v.status))
  165.   sleep(.1)
  166.   end
  167.   button.screen()
  168. end
Advertisement
Add Comment
Please, Sign In to add comment