Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Determine variables, tables, functions, APIs
- local bDebug = true
- os.loadAPI("button")
- local modem = nil
- local monitor = nil
- local drive = nil
- local result = nil
- local buttonMathematics = false
- local file = "mobSpawnerChannelsCopy"
- local spawnerCount = 0
- local greatestLength = 1
- local default = os.getComputerID()
- local channels = {}
- local spawners = {}
- local sides = {"front", "back", "left", "right", "top", "bottom"}
- local buttonConfigure = function()
- buttonMathematics = true
- end
- if bDebug then debug = print else debug = function() end end
- for i=1, #sides do
- if peripheral.getType(sides[i]) == "modem" then
- modem = peripheral.wrap(sides[i])
- elseif peripheral.getType(sides[i]) == "monitor" then
- monitor = peripheral.wrap(sides[i])
- elseif peripheral.getType(sides[i]) == "drive" then
- drive = sides[i]
- end
- end
- if modem == nil or monitor == nil then
- print("Modem/Monitor not found, please attach peripheral.")
- end
- --check files for channel lists | copy to computer
- if drive ~= nil and disk.isPresent(drive) then
- file = "disk/mobSpawnerChannels"
- end
- local f = io.open(file, "r")
- local contents = f:read("*a")
- local channels = textutils.unserialize(contents)
- if type(channels) ~= "table" then
- channels = {}
- else
- channels = textutils.unserialize(contents)
- end
- if channels.main == nil then
- channels.main = os.getComputerID()
- end
- f = io.open(file, "w")
- f:write(textutils.serialize(channels))
- f:close()
- f = io.open("mobSpawnerChannelsCopy", "w")
- f:write(textutils.serialize(channels))
- f:close()
- --Open channels to receive messages
- for k, v in pairs(channels) do
- modem.open(tonumber(v))
- modem.transmit(tonumber(v),tonumber(v),"ping")
- end
- monitor.clear()
- --start loop
- while true do
- --Reset some variables
- wireless = false
- touch = false
- --Wait for an event(button or wireless)
- result = { os.pullEvent() } debug("pullEvent")
- if result[1] == "modem_message" then
- senderChannel = result[3]
- replyChannel = result[4]
- message = result[5]
- wireless = true
- elseif result[1] == "monitor_touch" then
- xTouch = result[3]
- yTouch = result[4]
- touch = true
- end
- if wireless then
- --If the event was a message,
- --react to message
- if string.find(message, "Spawner") ~= nil then
- local subtract = string.gsub(message, "Spawner ", '')
- spawners[subtract] = {
- channel = tonumber(replyChannel);
- status = false;
- link = false;
- name = tostring(subtract);
- toggle = function ()
- button.toggleButton(spawners[subtract].name)
- spawners[subtract].status = not spawners[subtract].status
- end
- }
- end
- --Send a message back to unlinked computers
- for k,v in pairs(spawners) do
- sleep(.1)
- if not v.link then
- modem.transmit(tonumber(v.channel), default, "loop")
- spawners[k].link = true
- spawnerCount = spawnerCount + 1
- end
- end
- debug("Spawner Count: ".. spawnerCount)
- end
- if touch then
- --Touch, I remember, 'touch.'
- --if event is a touch, check the touch
- button.checkxy(xTouch, yTouch)
- end
- for k,v in pairs(spawners) do
- --determine longest spawner name
- if string.len(v.name) > greatestLength then
- greatestLength = string.len(v.name)
- end
- end
- --monitor math
- local xMon, yMon = monitor.getSize()
- local useableMonX = xMon - 2 --border around all printed items
- local useableMonY = yMon - 3 --border around all printed items
- local buttonSize = greatestLength + 2 --button 'padding'
- if wireless then
- --create functions to draw button pages
- button.clearTable()
- monitor.clear()
- local buttonCurrent = 1
- local xmin = 7 local ymin = 4 local xmax = 18 local ymax = 6 --first button's coords 7 between x 3 between y
- button.clearTable() -- reset the button table
- for k,v in pairs(spawners) do
- sleep(.1)
- if buttonCurrent == 1 then
- button.setTable(v.name,v.toggle,8,18,4,6,v.status)
- elseif buttonCurrent == 3 then
- button.setTable(v.name,v.toggle,8,18,14,16,v.status)
- elseif buttonCurrent == 2 then
- button.setTable(v.name,v.toggle,32,42,4,6,v.status)
- elseif buttonCurrent == 4 then
- button.setTable(v.name,v.toggle,32,42,14,16,v.status)
- end
- buttonCurrent = buttonCurrent + 1
- end
- --print to the monitor
- button.heading("MrPawlyHawk Spawning & Co.")
- end
- for k,v in pairs(spawners) do
- modem.transmit(v.channel, default, tostring(v.status))
- sleep(.1)
- end
- button.screen()
- end
Advertisement
Add Comment
Please, Sign In to add comment