Advertisement
FakoTheGreat

Gateway Display Program

Jul 7th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. -- Names for the monitors and gateways.
  2. -- If using modems, check modem for name.
  3. -- If touching computer, use the side.
  4. local monitor = "top"
  5. local leftGateway = ""
  6. local rightGateway = ""
  7.  
  8. -- Names to display above lists.
  9. local leftName = "Left"
  10. local rightName = "Right"
  11.  
  12. -- Variable to set how long to wait
  13. -- before reconnecting to monitor or
  14. -- gateways. Recommended range is 1 to 5.
  15. local waitTime = 1
  16.  
  17. -- Cosmetic variables, adjust to taste.
  18. local useColor = true
  19. local textSize = 1.5
  20. local primaryColor = colors.white
  21. local headerColor = colors.white
  22. local backgroundColor = colors.black
  23. local refreshTimer = 2
  24.  
  25. -- Name alignment. Accepts "left", "right", "center"
  26. local alignment = "center"
  27.  
  28. -- Other options for the gateway lists.
  29. local maxLength = 16
  30. local rightOffset = 18
  31.  
  32. -- Fancy layout options, to add a nice layout to lists.
  33. local fancyLayout = true
  34. local borderColor = colors.yellow
  35. local fHoriz = "-"
  36. local fVert = "|"
  37. local fJoint = "+"
  38.  
  39. -- Used internally. Do not adjust.
  40. local mon = nil
  41. local lGate = nil
  42. local rGate = nil
  43. local lList = nil
  44. local rList = nil
  45.  
  46. -- Waits for peripherals to connect, in case
  47. -- setup is in multiple chunks.
  48. local function getConnected()
  49.     while mon == nil do
  50.       mon = peripheral.wrap(monitor)
  51.       if mon == nil then
  52.         print("Waiting for monitor.")
  53.         os.sleep(waitTime)
  54.       end
  55.     end
  56.  
  57.     while lGate == nil do
  58.       lGate = peripheral.wrap(leftGateway)
  59.       if lGate == nil then
  60.         print("Waiting for "..leftName..".")
  61.         os.sleep(waitTime)
  62.       end
  63.     end
  64.  
  65.     while rGate == nil do
  66.       rGate = peripheral.wrap(rightGateway)
  67.       if rGate == nil then
  68.         print("Waiting for "..rightName..".")
  69.         os.sleep(waitTime)
  70.       end
  71.     end
  72. end
  73.  
  74. -- Shortens names, if needed.
  75. local function trimName(oStr)
  76.   local nStr = ""
  77.  
  78.   if string.len(oStr) > maxLength then
  79.     nStr = string.sub(oStr, 1, 14)
  80.   else
  81.     nStr = oStr
  82.   end
  83.  
  84.   return nStr
  85. end
  86.  
  87. -- Adds spaces to artificially align names.
  88. local function alignText(oStr, align)
  89.   local nStr = ""
  90.   local offset = 0
  91.  
  92.   if string.len(oStr) < maxLength then
  93.     if align == "center" then
  94.       offset = math.floor((maxLength - string.len(oStr)) / 2)
  95.     elseif align == "right" then
  96.       offset = maxLength - string.len(oStr)
  97.     end
  98.   end
  99.  
  100.   for x = 1, offset, 1 do
  101.     nStr = nStr.." "
  102.   end
  103.  
  104.   nStr = nStr..oStr
  105.  
  106.   return nStr
  107. end
  108.  
  109. -- Sets name color for specific people.
  110. local function getColor(name)
  111.   local nColor = primaryColor
  112.  
  113.   if string.sub(name,1,3) == "Sly" then
  114.     nColor = colors.purple
  115.   elseif string.sub(name,1,4) == "Prof" then
  116.     nColor = colors.lightBlue
  117.   elseif string.sub(name,1,3) == "Nut" then
  118.     nColor = colors.purple
  119.   elseif string.sub(name,1,7) == "Khaycat" then
  120.     nColor = colors.pink
  121.   end
  122.  
  123.   return nColor
  124. end
  125.  
  126. -- Prints the given list of names on screen.
  127. local function printNames(nameTable, offset)
  128.   local curName = ""
  129.   local curColor = colors.white
  130.   local pos = 1
  131.  
  132.   while pos < 11 do
  133.     if nameTable[pos] == nil then
  134.       curName = " "
  135.     else
  136.       v = nameTable[pos]
  137.       curName = trimName(v["name"])
  138.     end
  139.  
  140.     mon.setCursorPos(offset, 2 + pos)
  141.    
  142.     if useColor then
  143.       curColor = getColor(curName)
  144.       mon.setTextColor(curColor)
  145.     end
  146.    
  147.     mon.write(alignText(curName, alignment))
  148.  
  149.     pos = pos + 1
  150.   end
  151. end
  152.  
  153. -- Prints out borders to make screen look fancy.
  154. local function fancify()
  155.   local width, height = mon.getSize()
  156.   local mCenter = math.floor(width / 2) + 1
  157.  
  158.   if fancyLayout then
  159.     if mon.isColor() then
  160.       mon.setTextColor(borderColor)
  161.     end
  162.     mon.setCursorPos(mCenter, 1)
  163.     mon.write(fVert)
  164.     for y = 3, height, 1 do
  165.       mon.setCursorPos(mCenter, y)
  166.       mon.write(fVert)
  167.     end
  168.     for x = 1, width, 1 do
  169.       mon.setCursorPos(x, 2)
  170.       if x == mCenter then
  171.         mon.write(fJoint)
  172.       else
  173.         mon.write(fHoriz)
  174.       end
  175.     end
  176.   end
  177. end
  178.  
  179. -- Main display function.
  180. local function displayLists()
  181.   mon.clear()
  182.  
  183.   if mon.isColor() and useColor then
  184.     mon.setTextColor(headerColor)
  185.   end
  186.   mon.setCursorPos(1,1)
  187.   mon.write(alignText(leftName, "center"))
  188.   mon.setCursorPos(rightOffset,1)
  189.   mon.write(alignText(rightName, "center"))
  190.  
  191.   if fancyLayout then
  192.     fancify()
  193.   end
  194.  
  195.   printNames(lList, 1)
  196.   printNames(rList, rightOffset)
  197. end
  198.  
  199. print("Gateway display system online, connecting to peripherals.")
  200. getConnected()
  201.  
  202. mon.setBackgroundColor(backgroundColor)
  203. mon.clear()
  204. mon.setTextScale(textSize)
  205.  
  206. print("Peripherals connected. Starting primary loop.")
  207. while true do
  208.   lList = lGate.getAllStacks()
  209.   rList = rGate.getAllStacks()
  210.  
  211.   displayLists()
  212.  
  213.   os.sleep(refreshTimer)
  214. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement