Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fs = require("filesystem")
- local serial = require("serialization")
- local event = require("event")
- local sides = require("sides")
- local comp = require("component")
- local imglib = require("imglib")
- local modem = comp.modem
- local rs = comp.redstone
- local doorSrvr = "3ab63315-a0bd-41da-831c-af889ff9415a"
- function unknownEvent() end
- local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
- local gpus = {}
- local gpuIds = {}
- local myImage = "/Images/lcars-split-2"
- local doors = {}
- local debugTerm = ""
- local debugText = {"Door Services V1.2015.10.29","DEBUG"}
- local imgs = {}
- local C_DOOR_PORT = 11
- function handleEvent(eventID, ...)
- if (eventID) then
- myEventHandlers[eventID](...)
- end
- end
- function toggleDebug(screen)
- debugTerm = screen
- end
- function openDoor(w)
- rs.setOutput(doors[w].side, 15)
- event.timer(5, function() rs.setOutput(doors[w].side, 0) end)
- end
- function bindScreens()
- local tmpGpus = comp.list('gpu')
- for k, v in pairs(tmpGpus) do
- gpus[k] = comp.proxy(k)
- table.insert(gpuIds, k)
- end
- local curIndex = 1
- local tmpScreens = comp.list('screen')
- for k, v in pairs(tmpScreens) do
- if curIndex < #gpuIds then
- gpus[gpuIds[curIndex]].bind(k)
- curIndex = curIndex + 1
- end
- end
- end
- function showImage()
- --loadImage("lcars-split-2", myImage)
- for k, v in pairs(gpus) do
- if v.getScreen() ~= debugTerm then
- v.setResolution(70, 35)
- imglib.draw(imgs.standard, v, 1, 1)
- v.setBackground(0x000000)
- v.setForeground(0xFFFFFF)
- if doors[v.getScreen()] ~= nil then
- writeStatusText(k, doors[v.getScreen()].name, 20, 5)
- writeStatusText(k, doors[v.getScreen()].rmNum, 20, 3)
- end
- else
- v.setResolution(v.maxResolution())
- v.setBackground(0x000000)
- v.setForeground(0xFFFFFF)
- v.fill(1, 1, 160, 50, " ")
- for i = 1, #debugText do
- v.set(1, i, debugText[i])
- end
- end
- end
- end
- function debugMessage(msg)
- if #debugText > 40 then
- debugText = {"DEBUG: "}
- end
- table.insert(debugText, msg)
- end
- function findScreen(s)
- for k, v in pairs(gpus) do
- if v.getScreen() == s then return k end
- end
- end
- function writeStatusText(_g, t, x, y)
- gpus[_g].set(x, y, t)
- end
- function newDoor(m)
- debugMessage("doorSrvr: "..doorSrvr)
- debugMessage("newDoor(" .. m .. ")")
- local _t = {}
- _t.screen = m
- _t.op = "addDoor"
- debugMessage("Message sent: "..serial.serialize(modem.send(doorSrvr, 11, serial.serialize(_t))))
- end
- function discover()
- local _t = {}
- _t.op = "discovery"
- modem.broadcast(11, serial.serialize(_t))
- end
- function myEventHandlers.modem_message(rcvr, sndr, prt, dstnc, msg)
- local _m = serial.unserialize(msg)
- if prt == 11 and sndr ~= rcvr and rcvr == modem.address then
- if _m.op == "discoveryresp" then
- debugMessage("Discovery received.")
- debugMessage("Server set to: "..sndr)
- doorSrvr = sndr
- elseif _m.op == "addDoor" then
- debugMessage("addDoor received.")
- doors[_m.screen] = _m
- end
- end
- end
- function myEventHandlers.touch(s, x, y, b, p)
- if x == 1 and y == 1 then
- print (s)
- toggleDebug(s)
- else
- if doors[s] ~= nil then
- openDoor(s)
- else
- newDoor(s)
- end
- end
- end
- function start()
- imgs.standard = imglib.load("/Images/lcars-split-2")
- bindScreens()
- showImage()
- discover()
- bRunning = true
- modem.open(C_DOOR_PORT)
- modem.setStrength(1280)
- while bRunning do
- showImage()
- handleEvent(event.pull(1))
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement