Advertisement
Saereth

Untitled

Apr 19th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.00 KB | None | 0 0
  1. -- locals
  2. local logs = {}
  3. local openTime = 5
  4. local textSize = 1
  5. local minimumNameLength = 4
  6. local buttonPadding = 1 --Min 1, Space before/after
  7. local centerPadding = 0 --Min 0, Space before/after
  8.  
  9. local buttonColor = colors.lightGray
  10. local buttonActiveColor = colors.orange
  11. local pageBack = "Prev Page"
  12. local pageFore = "Next Page"
  13. local resetButton = "Refresh"
  14. -- Hub Portals
  15. -- local portals = {
  16. --   { name="Spawn"; address="Sileston Protaset Erpulf"},
  17. --   { name="The End"; address="Erpclaus Ommaculf Lanal"},
  18. --   { name="Rapture"; address="Sileston Altatis Claoth"},
  19. --   { name="Reign"; address="Sileston Setusar Erporm"},
  20. --   { name="DS7"; address="Sileston Alsetbrei Lanulf" }
  21. -- }
  22.  
  23. local portals = {
  24.  { name="Hub"; address="Ormonest Lanrushcla Omgon" },
  25.  { name="Random"; address="Random" }
  26. }
  27.  
  28. --internal
  29. local monitor, touch
  30. local gate
  31. local monitorLocation
  32. local monX = 0
  33. local monY = 0
  34. local monMidL = 0
  35. local monMidR = 0
  36. local maxNameLength = 0
  37. local problem = false
  38. local names = {}
  39. local curPage = 1
  40. local lastPage = 1
  41. local namesPerPage = 1
  42. local pages = {}
  43. local gateType = 'Abstract Bus Adapter'
  44.  
  45.  
  46. function writeLog(text)
  47.   print(text)
  48. end
  49.  
  50. function init()
  51.   if not fs.exists('apis') then
  52.     fs.makeDir('apis')
  53.   end
  54.   if not fs.exists('apis/touchpoint') then
  55.     writeLog("You don't have the touchpoint API. Beaming it down.")
  56.     shell.run('pastebin get pFHeia96 apis/touchpoint')
  57.   end
  58.   os.loadAPI('apis/touchpoint')
  59.  
  60.  
  61.   if not fs.exists('apis/json') then
  62.     writeLog("You don't have the JSON API. Grabboring it.")
  63.     shell.run("pastebin get 4nRg9CHU apis/json")
  64.   end
  65.   os.loadAPI('apis/json')
  66.  
  67.   monitorLocation = findSideFor('monitor')
  68.   monitor = peripheral.wrap(monitorLocation)
  69.   gate    = peripheral.find(gateType)
  70.  
  71.   setupScreen()
  72. end
  73.  
  74. function handleMonitorResize()
  75.   writeLog("Screen resized. Setting up screen!")
  76.   setupScreen()
  77.   refresh()
  78. end
  79.  
  80.  
  81. function determineLength(myString)
  82.   local theLength = 0
  83.   if string.len(myString) > maxNameLength then
  84.     theLength = maxNameLength + buttonPadding * 2
  85.   else
  86.     theLength = string.len(myString) + buttonPadding * 2
  87.   end
  88.  
  89.   return theLength
  90. end
  91.  
  92.  
  93. function setupScreen()
  94.   monitor.clear()
  95.   monitor.setTextScale(textSize)
  96.   monX, monY = monitor.getSize()
  97.  
  98.   writeLog("Screen Dimensions: (x: "..monX..", y: "..monY..")")
  99.  
  100.   if (monY < 5) or (monX < (minimumNameLength * 2) + 4) then
  101.     monitor.setBackgroundColor(colors.red)
  102.     monitor.setTextColor(colors.black)
  103.     centerText("!!!", 3)
  104.     centerText("ERROR", 4)
  105.     centerText("VIEW COMPUTER", 5)
  106.     writeLog("Screen too small with current resolution, please adjust.")
  107.     problem = true
  108.   else
  109.     if math.floor(monX/2) == math.ceil(monX/2) then --Even
  110.       monMidR = monX/2 + 1
  111.       monMidL = monMidR - 1
  112.       maxNameLength = (monX - 4)/2 - (centerPadding + buttonPadding) * 2
  113.     else --Odd
  114.       monMidR = math.floor(monX/2) + 1
  115.       monMidL = monMidR
  116.       maxNameLength = (monX - 3)/2 - (centerPadding + buttonPadding) * 2
  117.     end
  118.     namesPerPage = math.ceil((monY - 4) / 2) * 2
  119.   end
  120. end
  121.  
  122. function rebuildPages(p1)
  123.   pages[curPage]:toggleButton(resetButton)
  124.   refresh()
  125. end
  126.  
  127. function doAction(buttonName)
  128.   local pressedButtonDesc
  129.  
  130.   writeLog("doAction("..buttonName..")")
  131.  
  132.   for k,v in ipairs(portals) do
  133.     if v["name"] == buttonName then
  134.       pressedButtonDesc = v
  135.       break
  136.     end
  137.   end
  138.   dialGate(pressedButtonDesc["address"])
  139.   pages[curPage]:flash(buttonName, openTime)
  140. end
  141.  
  142. function prevPage()
  143.   lastPage = curPage
  144.  
  145.   pages[lastPage]:flash(pageBack, 0.125)
  146.  
  147.   curPage = curPage - 1
  148.   if curPage < 1 then
  149.     curPage = #pages
  150.   end
  151.   pages[curPage]:draw()
  152.  
  153. end
  154.  
  155. function nextPage()
  156.   lastPage = curPage
  157.  
  158.   pages[lastPage]:flash(pageFore, 0.125)
  159.  
  160.   curPage = curPage + 1
  161.   if curPage > #pages then
  162.     curPage = 1
  163.   end
  164.   pages[curPage]:draw()
  165. end
  166.  
  167. function displayError(error)
  168.   monitor.clear()
  169.   monitor.setBackgroundColor(colors.red)
  170.   monitor.setTextColor(colors.black)
  171.   centerText("!!!", 3)
  172.   centerText(error, 4)
  173.   writeLog("Error: "..error)
  174. end
  175.  
  176. function buildPage(pN)
  177.   pages[pN] = touchpoint.new(monitorLocation)
  178.   pages[pN]:add(pageBack, prevPage, 1, 1, determineLength(pageBack), 1, buttonColor, buttonActiveColor)
  179.   pages[pN]:add(pageFore, nextPage, monX - determineLength(pageFore) + 1, 1, monX, 1, buttonColor, buttonActiveColor)
  180.  
  181.   if #portals > 0 then
  182.     local curIndex = (pN - 1) * namesPerPage + 1
  183.     for y = 3, monY - 2, 2 do
  184.       if curIndex < #portals + 1 then
  185.         pages[pN]:add(portals[curIndex]["name"], doAction, 2, y, monMidL - 1 - centerPadding, y, buttonColor, buttonActiveColor)
  186.       else
  187.         break
  188.       end
  189.       curIndex = curIndex + 1
  190.       if curIndex < #portals + 1 then
  191.         pages[pN]:add(portals[curIndex]["name"], doAction, monMidR + 1 + centerPadding, y, monX - 1, y, buttonColor, buttonActiveColor)
  192.       else
  193.         break
  194.       end
  195.       curIndex = curIndex + 1
  196.     end
  197.   end
  198.  
  199.   pages[pN]:add(resetButton, rebuildPages, 1, monY, determineLength(resetButton), monY, buttonColor, buttonActiveColor)
  200.  
  201. end
  202.  
  203. function refresh()
  204.   writeLog("Refreshing stargate list.")
  205.   pages = {}
  206.   local myPage = 1
  207.  
  208.   writeLog("# stargates: "..#portals)
  209.   if #portals> 0 then
  210.     writeLog('Names/Page: '..namesPerPage)
  211.     while (myPage - 1) * namesPerPage < #portals do
  212.       writeLog('Populating Page'..myPage..'.')
  213.       buildPage(myPage)
  214.       myPage = myPage + 1
  215.      
  216.     end
  217.   else
  218.     buildPage(1)
  219.   end
  220.  
  221.   if curPage > #pages then
  222.     curPage = #pages
  223.   end
  224.  
  225.   writeLog('Current Page:'..curPage)
  226.   pages[curPage]:draw()
  227. end
  228.  
  229. function dialGate(address)
  230.   if address == "Random" then
  231.    commands.say("Welcome to the Breakin Blocks Server!")
  232.    commands.say("Now Initializing Transport...")
  233.   commands.spreadplayers("1364 4039 50 500 false @p[r=10]")
  234.   else
  235.   writeLog('----')
  236.   writeLog("Dialing "..address)  
  237.   result = gate.sendPacket("FFFF", "action: dial", "timeout: "..openTime, "address: "..address)
  238.   writeLog("Gate told to dial. Result: "..result)
  239.   return result
  240.   end
  241. end
  242.  
  243. function closeGate()
  244.   writeLog("Force closing the gate.")
  245.   result = gate.sendPacket("FFFF", "action: disconnect")
  246.   writeLog("Gate told to close. Result: "..result)
  247. end
  248.  
  249.  
  250. function centerText(text, row)
  251.   local size = string.len(text)
  252.   offset = math.floor((monX - size) / 2) + 1
  253.   monitor.setCursorPos(offset, row)
  254.   monitor.write(text)
  255. end
  256.  
  257. function findSideFor(typeToFind)
  258.   local sides = peripheral.getNames()
  259.   for i, side in ipairs(sides) do
  260.     local type = peripheral.getType(side)
  261.     if type == typeToFind then
  262.       return(side)
  263.     end
  264.   end
  265. end
  266.  
  267.  
  268. init()
  269.  
  270. if not problem then
  271.   refresh()
  272.   while true do
  273.     event, p1 = pages[curPage]:handleEvents(os.pullEvent())
  274.     if event == "button_click" then
  275.       pages[curPage].buttonList[p1].func(p1)
  276.     elseif event == "monitor_resize" then
  277.       handleMonitorResize()
  278.     end
  279.   end
  280. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement