Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Determine variables, tables, functions, APIs
- local bDebug = false
- 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 selectPage = 1
- local default = os.getComputerID()
- local channels = {}
- local spawners = {}
- local test = {}
- local poop = function() print("yes") end
- local sides = {"front", "back", "left", "right", "top", "bottom"}
- local buttonConfigure = function()
- buttonMathematics = true
- end
- local function toggle(variable)
- spawners[variable].status = not spawners[variable].status
- end
- if bDebug then debug = print else debug = function() end end
- -- local function event(timeout)
- -- local result = {}
- -- local eventFunction = function()
- -- result = { os.pullEvent() }
- -- if result[1] == "monitor_touch" or result[1] == "modem_message" then
- -- returnValue = true
- -- end
- -- end
- -- parallel.waitForAny(function() sleep(timeout) end, eventFunction)
- -- if returnValue then return result end
- -- end
- --Determine which sides the peripherals are located
- 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(tostring(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
- debug("Determined greatestStringSize")
- --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 buttonMathematics then
- local buttonHeight = 3 + 2 --button height, plus padding, plus spacing
- local buttonDistance = (useableMonX-(buttonSize*spawnerCount))/(spawnerCount - 1) --Calculate the distance between each button horizontally | Thanks to Bubba on CC Forums
- local buttonCount = spawnerCount --Rename for simplicity
- local buttonColumns = 1 debug("buttonColumns = 1")
- while buttonDistance < 1 do --Calculate the number of buttons in a row | the number of columns
- buttonColumns = buttonCount - 1 sleep(0) debug("buttonDistance Loop")
- buttonDistance = (useableMonX -(buttonSize*buttonColumns))/(buttonColumns - 1)
- end
- local buttonRows = math.floor((useableMonY / buttonHeight)) --Determine the number of rows that may fit on a page
- -- local buttonHeightDistance = (useableMonY - (buttonHeight/(buttonRows-1))) --Calculate space between buttons vertically
- -- while buttonHeightDistance < 1 do
- -- buttonRows = ((buttonCount / buttonColumns) - 1)
- -- buttonHeightDistance = (useableMonY - (buttonHeight*(buttonRows - 1)/) debug("buttonHeightDistance Loop")
- -- end
- local buttonPages = math.ceil(buttonCount / (buttonRows*buttonColumns)) --Calculate the number of pages required
- debug("Columns: "..buttonColumns) debug("Rows: "..buttonRows)
- --create functions to draw button pages
- local buttonCurrent = 1
- local draw = {}
- local xmin = 2 local ymin = 3 local xmax = 0 local ymax = 5 --first button's coords
- button.clearTable() -- reset the button table
- for k,v in pairs(spawners) do
- sleep(.1)
- local rowCurrent = math.ceil(buttonCurrent/buttonColumns) debug("rowCurrent") --determine the current row
- while rowCurrent > buttonRows do rowCurrent = rowCurrent - buttonRows end sleep(0) debug("rowCurrent") --regardless of page
- local columnCurrent = math.ceil(buttonCurrent/buttonRows) debug("columnCurrent")-- determine the current column
- while columnCurrent > buttonColumns do columnCurrent = columnCurrent - buttonColumns end sleep(0) debug("columnCurrent")-- regardless of page
- local pageCurrent = math.ceil(buttonCurrent/(buttonColumns*buttonRows)) debug("pageCurrent")-- determine the current page
- --coordinate calculate before drawing
- xmax = xmin+buttonSize-1 ymax = ymin+2
- local testString = tostring(v.name)
- table.insert(draw, tonumber(buttonCurrent), function() button.setTable(testString,v.toggle,xmin,xmax,ymin,ymax) end)
- debug(xmin.. " "..xmax.." "..ymin.." "..ymax)
- --coordinate calculate after drawing
- if rowCurrent ~= 1 then
- ymin = (rowCurrent*5)+3
- else ymin = 3 end
- xmin = columnCurrent*buttonDistance
- buttonCurrent = buttonCurrent + 1
- end
- debug("Completed table-coordinate-organization")
- --declare some button functions
- for i=1, #draw do
- sleep(.1)
- --if (i <= (selectPage*buttonRows*buttonColumns)) and (i > ((selectPage+1)*buttonRows*buttonColumns)) then
- draw[i]()
- button.screen()
- print("draw["..i.."] = ".. tostring(draw[i]))
- --end
- end
- debug("Executed draw table functions.")
- draw.nextPage = function() selectPage = selectPage + 1 end
- draw.prevPage = function() selectPage = selectPage - 1 end
- if selectPage < buttonPages then
- button.setTable(">", draw.nextPage, xMon, xMon, yMon, yMon)
- end
- if selectPage ~= 1 then
- button.setTable("<", draw.prevPage, 1, 1, yMon, yMon)
- end
- --print to the monitor
- monitor.clear()
- button.heading("Spawner Control System")
- button.screen() debug("This should be seen with buttons on screen")
- else
- button.clearTable()
- button.heading("Check Terminal for Details")
- table.insert(test, 1, function()
- button.setTable("Begin Button Configuration", buttonConfigure, 1, 25, 10, 13) end)
- test[1]()
- button.screen()
- end
- for k,v in pairs(spawners) do
- modem.transmit(v.channel, default, tostring(v.status))
- sleep(.1)
- end
- sleep(1)
- button.screen()
- end
Advertisement
Add Comment
Please, Sign In to add comment