Advertisement
Saereth

transporterCode

Jan 27th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- locals
  2. local logs = {}
  3. local openTime = 2
  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.blue
  10. local buttonActiveColor = colors.gray
  11. local pageBack = "Prev Page"
  12. local pageFore = "Next Page"
  13. local resetButton = "Refresh"
  14. -- Hub Portals
  15. local portals = {
  16.  
  17.   { name="Spawn", x="238", y="139", z="-3038"},
  18.   { name="BloodMagic", x="-729", y="66", z="4116"},
  19.   { name="Doomgull", x="-3629", y="65", z="-1070"},
  20.   { name="DLI/Kelec", x="4569", y="90", z="-538"},
  21.   { name="Blue", x="-619", y="64", z="1055"}
  22.  
  23. }
  24.  
  25.  
  26. --internal
  27. local monitor, touch
  28. local transporter
  29. local monitorLocation
  30. local monX = 0
  31. local monY = 0
  32. local monMidL = 0
  33. local monMidR = 0
  34. local maxNameLength = 0
  35. local problem = false
  36. local names = {}
  37. local curPage = 1
  38. local lastPage = 1
  39. local namesPerPage = 1
  40. local pages = {}
  41. local transType = 'mo_transporter'
  42. --transporter = peripheral.find(transType)
  43. transporter = peripheral.wrap("mo_transporter_0")
  44.  
  45. function writeLog(text)
  46.   print(text)
  47. end
  48.  
  49. function init()
  50.   if not fs.exists('apis') then
  51.     fs.makeDir('apis')
  52.   end
  53.   if not fs.exists('apis/touchpoint') then
  54.     writeLog("You don't have the touchpoint API. Beaming it down.")
  55.     shell.run('pastebin get pFHeia96 apis/touchpoint')
  56.   end
  57.   os.loadAPI('apis/touchpoint')
  58.  
  59.  
  60.   if not fs.exists('apis/json') then
  61.     writeLog("You don't have the JSON API. Grabboring it.")
  62.     shell.run("pastebin get 4nRg9CHU apis/json")
  63.   end
  64.   os.loadAPI('apis/json')
  65.  
  66.   monitorLocation = findSideFor('monitor')
  67.   monitor = peripheral.wrap(monitorLocation)
  68.  
  69.  
  70.   setupScreen()
  71. end
  72.  
  73. function handleMonitorResize()
  74.   writeLog("Screen resized. Setting up screen!")
  75.   setupScreen()
  76.   refresh()
  77. end
  78.  
  79.  
  80. function determineLength(myString)
  81.   local theLength = 0
  82.   if string.len(myString) > maxNameLength then
  83.     theLength = maxNameLength + buttonPadding * 2
  84.   else
  85.     theLength = string.len(myString) + buttonPadding * 2
  86.   end
  87.  
  88.   return theLength
  89. end
  90.  
  91.  
  92. function setupScreen()
  93.   monitor.clear()
  94.   monitor.setTextScale(textSize)
  95.   monX, monY = monitor.getSize()
  96.   monitor.setTextColor(colors.orange)
  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 = 0;
  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.   --writeLog("array variable = "..pressedButtonDesc["x"])
  139.   startTransport(pressedButtonDesc["x"],pressedButtonDesc["y"],pressedButtonDesc["z"])
  140.   pages[curPage]:flash(buttonName, openTime)
  141. end
  142.  
  143. function prevPage()
  144.   lastPage = curPage
  145.  
  146.   pages[lastPage]:flash(pageBack, 0.125)
  147.  
  148.   curPage = curPage - 1
  149.   if curPage < 1 then
  150.     curPage = #pages
  151.   end
  152.   pages[curPage]:draw()
  153.  
  154. end
  155.  
  156. function nextPage()
  157.   lastPage = curPage
  158.  
  159.   pages[lastPage]:flash(pageFore, 0.125)
  160.  
  161.   curPage = curPage + 1
  162.   if curPage > #pages then
  163.     curPage = 1
  164.   end
  165.   pages[curPage]:draw()
  166. end
  167.  
  168. function displayError(error)
  169.   monitor.clear()
  170.   monitor.setBackgroundColor(colors.red)
  171.   monitor.setTextColor(colors.black)
  172.   centerText("!!!", 3)
  173.   centerText(error, 4)
  174.   writeLog("Error: "..error)
  175. end
  176.  
  177. function buildPage(pN)
  178.   pages[pN] = touchpoint.new(monitorLocation)
  179.   pages[pN]:add(pageBack, prevPage, 1, 1, determineLength(pageBack), 1, buttonColor, buttonActiveColor)
  180.   pages[pN]:add(pageFore, nextPage, monX - determineLength(pageFore) + 1, 1, monX, 1, buttonColor, buttonActiveColor)
  181.  
  182.   if #portals > 0 then
  183.     local curIndex = (pN - 1) * namesPerPage + 1
  184.     for y = 3, monY - 2, 2 do
  185.       if curIndex < #portals + 1 then
  186.         pages[pN]:add(portals[curIndex]["name"], doAction, 2, y, monMidL - 1 - centerPadding, y, buttonColor, buttonActiveColor)
  187.       else
  188.         break
  189.       end
  190.       curIndex = curIndex + 1
  191.       if curIndex < #portals + 1 then
  192.         pages[pN]:add(portals[curIndex]["name"], doAction, monMidR + 1 + centerPadding, y, monX - 1, y, buttonColor, buttonActiveColor)
  193.       else
  194.         break
  195.       end
  196.       curIndex = curIndex + 1
  197.     end
  198.   end
  199.  
  200.   pages[pN]:add(resetButton, rebuildPages, 1, monY, determineLength(resetButton), monY, buttonColor, buttonActiveColor)
  201.  
  202. end
  203.  
  204. function refresh()
  205.   writeLog("Refreshing Destination list.")
  206.   pages = {}
  207.   local myPage = 1
  208.  
  209.   writeLog("# Detinations: "..#portals)
  210.   if #portals> 0 then
  211.     writeLog('Names/Page: '..namesPerPage)
  212.     while (myPage - 1) * namesPerPage < #portals do
  213.       writeLog('Populating Page'..myPage..'.')
  214.       buildPage(myPage)
  215.       myPage = myPage + 1
  216.      
  217.     end
  218.   else
  219.     buildPage(1)
  220.   end
  221.  
  222.   if curPage > #pages then
  223.     curPage = #pages
  224.   end
  225.  
  226.   writeLog('Current Page:'..curPage)
  227.   pages[curPage]:draw()
  228. end
  229.  
  230. function startTransport(Tx,Ty,Tz)
  231.   writeLog('----')
  232.   writeLog("Setting Transporter Address X:"..Tx.." Y:"..Ty.." Z:"..Tz)  
  233.   transporter.setX(0,tonumber(Tx))
  234.   transporter.setY(0,tonumber(Ty))
  235.   transporter.setZ(0,tonumber(Tz))
  236. end
  237.  
  238.  
  239. function centerText(text, row)
  240.   local size = string.len(text)
  241.   offset = math.floor((monX - size) / 2) + 1
  242.   monitor.setCursorPos(offset, row)
  243.   monitor.write(text)
  244. end
  245.  
  246. function findSideFor(typeToFind)
  247.   local sides = peripheral.getNames()
  248.   for i, side in ipairs(sides) do
  249.     local type = peripheral.getType(side)
  250.     if type == typeToFind then
  251.       return(side)
  252.     end
  253.   end
  254. end
  255.  
  256.  
  257. init()
  258.  
  259. if not problem then
  260.   refresh()
  261.   while true do
  262.     event, p1 = pages[curPage]:handleEvents(os.pullEvent())
  263.     if event == "button_click" then
  264.       pages[curPage].buttonList[p1].func(p1)
  265.     elseif event == "monitor_resize" then
  266.       handleMonitorResize()
  267.     end
  268.   end
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement