Advertisement
klindley

Door

Oct 29th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local serial = require("serialization")
  3. local event = require("event")
  4. local sides = require("sides")
  5. local comp = require("component")
  6. local imglib = require("imglib")
  7.  
  8. local modem = comp.modem
  9. local rs = comp.redstone
  10. local doorSrvr = "3ab63315-a0bd-41da-831c-af889ff9415a"
  11. function unknownEvent() end
  12. local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
  13.  
  14. local gpus = {}
  15. local gpuIds = {}
  16. local myImage = "/Images/lcars-split-2"
  17. local doors = {}
  18. local debugTerm = ""
  19. local debugText = {"Door Services V1.2015.10.29","DEBUG"}
  20.  
  21. local imgs = {}
  22.  
  23. local C_DOOR_PORT = 11
  24.  
  25. function handleEvent(eventID, ...)
  26. if (eventID) then
  27. myEventHandlers[eventID](...)
  28. end
  29. end
  30.  
  31. function toggleDebug(screen)
  32. debugTerm = screen
  33. end
  34.  
  35. function openDoor(w)
  36. rs.setOutput(doors[w].side, 15)
  37. event.timer(5, function() rs.setOutput(doors[w].side, 0) end)
  38. end
  39.  
  40. function bindScreens()
  41. local tmpGpus = comp.list('gpu')
  42. for k, v in pairs(tmpGpus) do
  43. gpus[k] = comp.proxy(k)
  44. table.insert(gpuIds, k)
  45. end
  46. local curIndex = 1
  47. local tmpScreens = comp.list('screen')
  48. for k, v in pairs(tmpScreens) do
  49. if curIndex < #gpuIds then
  50. gpus[gpuIds[curIndex]].bind(k)
  51. curIndex = curIndex + 1
  52. end
  53. end
  54. end
  55.  
  56. function showImage()
  57. --loadImage("lcars-split-2", myImage)
  58.  
  59. for k, v in pairs(gpus) do
  60. if v.getScreen() ~= debugTerm then
  61. v.setResolution(70, 35)
  62. imglib.draw(imgs.standard, v, 1, 1)
  63. v.setBackground(0x000000)
  64. v.setForeground(0xFFFFFF)
  65. if doors[v.getScreen()] ~= nil then
  66. writeStatusText(k, doors[v.getScreen()].name, 20, 5)
  67. writeStatusText(k, doors[v.getScreen()].rmNum, 20, 3)
  68. end
  69. else
  70. v.setResolution(v.maxResolution())
  71. v.setBackground(0x000000)
  72. v.setForeground(0xFFFFFF)
  73. v.fill(1, 1, 160, 50, " ")
  74. for i = 1, #debugText do
  75. v.set(1, i, debugText[i])
  76. end
  77. end
  78. end
  79. end
  80.  
  81. function debugMessage(msg)
  82. if #debugText > 40 then
  83. debugText = {"DEBUG: "}
  84. end
  85. table.insert(debugText, msg)
  86. end
  87.  
  88. function findScreen(s)
  89. for k, v in pairs(gpus) do
  90. if v.getScreen() == s then return k end
  91. end
  92. end
  93.  
  94. function writeStatusText(_g, t, x, y)
  95. gpus[_g].set(x, y, t)
  96. end
  97.  
  98. function newDoor(m)
  99. debugMessage("doorSrvr: "..doorSrvr)
  100. debugMessage("newDoor(" .. m .. ")")
  101. local _t = {}
  102. _t.screen = m
  103. _t.op = "addDoor"
  104. debugMessage("Message sent: "..serial.serialize(modem.send(doorSrvr, 11, serial.serialize(_t))))
  105. end
  106.  
  107. function discover()
  108. local _t = {}
  109. _t.op = "discovery"
  110. modem.broadcast(11, serial.serialize(_t))
  111. end
  112.  
  113. function myEventHandlers.modem_message(rcvr, sndr, prt, dstnc, msg)
  114. local _m = serial.unserialize(msg)
  115.  
  116. if prt == 11 and sndr ~= rcvr and rcvr == modem.address then
  117. if _m.op == "discoveryresp" then
  118. debugMessage("Discovery received.")
  119. debugMessage("Server set to: "..sndr)
  120. doorSrvr = sndr
  121. elseif _m.op == "addDoor" then
  122. debugMessage("addDoor received.")
  123. doors[_m.screen] = _m
  124. end
  125. end
  126. end
  127.  
  128. function myEventHandlers.touch(s, x, y, b, p)
  129. if x == 1 and y == 1 then
  130. print (s)
  131. toggleDebug(s)
  132. else
  133. if doors[s] ~= nil then
  134. openDoor(s)
  135. else
  136. newDoor(s)
  137. end
  138. end
  139. end
  140.  
  141. function start()
  142. imgs.standard = imglib.load("/Images/lcars-split-2")
  143.  
  144. bindScreens()
  145. showImage()
  146. discover()
  147. bRunning = true
  148. modem.open(C_DOOR_PORT)
  149. modem.setStrength(1280)
  150.  
  151. while bRunning do
  152. showImage()
  153. handleEvent(event.pull(1))
  154. end
  155. end
  156. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement