SHARE
TWEET

Quantum Link Terminal

MoonlightOwl Sep 19th, 2014 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Quantum Link Terminal by Totoro --
  2. -- Version 2.  All right reserved! --
  3.  
  4. local serial = require('serialization')
  5. local fs = require('filesystem')
  6. local event = require('event')
  7. local term = require('term')
  8. local com = require('component')
  9. local gpu = com.gpu
  10. local modem = com.modem
  11. local cb = com.commandblock
  12. local ae = com.appeng_blocks_controller
  13.  
  14. --  Constants  --
  15. TIME_RECHARGE = 30
  16. secret_port = 27
  17.  
  18. --    Colors   --
  19. backcolor = 0x000000
  20. forecolor = 0xFFFFFF
  21. infocolor = 0x0066FF
  22. errorcolor = 0xFF0000
  23. truecolor = 0x00FF00
  24. inactivecolor = 0xCCCCCC
  25. goldcolor = 0xCCCC00
  26. --     ****     --
  27.  
  28. --     Codes    --
  29. pingcode = 0x031337
  30. refreshcode = 0xC0FFEE
  31. --     ****     --
  32.  
  33. -- vars
  34. blocked = false
  35. cooldown = 0
  36. current = nil
  37. selector_page = 0
  38. max_page = 0
  39. frame = 0     -- 0: 'go'-frame  1: selector
  40. refresh = true
  41.  
  42. -- money, money, money
  43. money = 264
  44. cash = ae.countOfItemType(money)
  45.  
  46. -- init graphics
  47. gosize = {width = 16, height = 3}
  48. selectorsize = {width = 24, height = 6}
  49.  
  50. oldw, oldh = gpu.getResolution()
  51. gpu.setResolution(gosize.width, gosize.height)
  52. term.clear()
  53. gpu.setForeground(forecolor)
  54. gpu.setBackground(backcolor)
  55.  
  56. -- teleports
  57. teleports = {}
  58.  
  59. function loadTeleports()
  60.   if fs.exists("telelist.txt") then
  61.     local file = io.open("telelist.txt", "r")
  62.     local data = file:read("*a")
  63.     teleports = serial.unserialize(data)
  64.     file:close()
  65.     if teleports == nil then teleports = {} end
  66.   end
  67. end
  68. function saveTeleports()
  69.   local file = io.open("telelist.txt", "w")
  70.   local data = serial.serialize(teleports)
  71.   file:write(data)
  72.   file:close()
  73. end
  74. function addTeleport(name, price, x, y, z)
  75.   table.insert(teleports, {name = name, price = price, x = x, y = y, z = z, blocked = false})
  76.   max_page = math.floor(#teleports/5)
  77. end
  78. function blockTeleport(tele, state)
  79.   tele.blocked = state
  80. end
  81. function teleport(tele, player)
  82.   cb.setCommand('/tp '..player..' '..tele.x..' '..tele.y..' '..tele.z)
  83.   cb.executeCommand()
  84.   cb.setCommand('')
  85.   cooldown = TIME_RECHARGE
  86.   selector_page = 0
  87.   refresh = true
  88. end
  89.  
  90.  
  91. -- calculation
  92. function update()
  93.   if cooldown > 0 then
  94.     cooldown = cooldown-1
  95.     refresh = true
  96.   else
  97.     if current ~= nil then
  98.       if current.price > 0 then
  99.         offer = ae.countOfItemType(money) - cash
  100.         if offer >= current.price then
  101.           cash = cash + offer
  102.           teleport(current, '@p')
  103.         end
  104.       end
  105.     end
  106.   end
  107. end
  108.  
  109.  
  110. -- screens
  111. go_half = gosize.width/2
  112. go_offset = (gosize.width-12)/2+1
  113.  
  114. function goframe()
  115.   term.clear()
  116.  
  117.   if cooldown == 0 then
  118.     if current ~= nil then
  119.       term.setCursor(go_half-#current.name/2+1, 1)
  120.       if current.price > 0 then gpu.setForeground(goldcolor) end
  121.       term.write(current.name)
  122.       if current.blocked then
  123.         gpu.setForeground(errorcolor)
  124.         term.setCursor(1, 2)
  125.         term.write(' on maintenance ')
  126.         gpu.setForeground(forecolor)
  127.       elseif current.price > 0 then
  128.         term.setCursor(go_half-#('$'..current.price)/2+1, 2)
  129.         term.write('$'..current.price)
  130.         gpu.setForeground(forecolor)
  131.       end
  132.     else
  133.       term.setCursor(go_half-5, 1)
  134.       gpu.setForeground(errorcolor)
  135.       term.write("- no links -")
  136.       gpu.setForeground(forecolor)
  137.     end
  138.     term.setCursor(go_offset,3)
  139.     gpu.setBackground(infocolor)
  140.     term.write('[ Teleport ]')
  141.     gpu.setBackground(backcolor)
  142.   else
  143.     term.setCursor(go_half-2,1)
  144.     term.write('[    ]')
  145.     term.setCursor(go_half,1)
  146.     term.write(cooldown)
  147.     term.setCursor(go_offset,3)
  148.     gpu.setBackground(errorcolor)
  149.     term.write('[ Cooldown ]')
  150.     gpu.setBackground(backcolor)
  151.   end
  152. end
  153.  
  154.  
  155. selector_half = selectorsize.width/2
  156. function selectorframe()
  157.   term.clear()
  158.  
  159.   line = selector_page * 5
  160.   for i=line+1, line+5 do
  161.     if i>#teleports then break end
  162.     term.setCursor(selector_half-#teleports[i].name/2+1, i-line)
  163.     if teleports[i].price > 0 then gpu.setForeground(goldcolor) end
  164.     term.write(teleports[i].name)
  165.     if teleports[i].price > 0 then gpu.setForeground(forecolor) end
  166.   end
  167.  
  168.   gpu.setBackground(infocolor)
  169.   term.setCursor(2, selectorsize.height)
  170.   term.write('[ < Prev]')
  171.   term.setCursor(selectorsize.width-9, selectorsize.height)
  172.   term.write('[ Next >]')
  173.   gpu.setBackground(backcolor)
  174.  
  175.   if max_page == 0 then
  176.     term.setCursor(11, selectorsize.height)
  177.     term.write(' __ ')
  178.   else
  179.     term.setCursor(11+(selector_page/max_page*3), selectorsize.height)
  180.     term.write('_')
  181.   end
  182. end
  183.  
  184. function changeframe(num)
  185.   frame = num
  186.   refresh = true
  187. end
  188.  
  189.  
  190.  
  191. -- == Main == --
  192. loadTeleports()
  193. current = teleports[1]
  194. modem.open(secret_port)
  195.  
  196. while true do
  197.   if refresh then
  198.     if frame == 0 then goframe()
  199.     elseif frame == 1 then selectorframe() end
  200.     refresh = false
  201.   end
  202.  
  203.   -- waiting for events
  204.   name, address, a, b, c, username, data = event.pull(1)
  205.  
  206.   if name == 'touch' then
  207.     if frame == 0 then
  208.       if cooldown == 0 then
  209.         if b==3 then                      -- teleport player
  210.           if current ~= nil then
  211.             if current.price == 0 and not current.blocked then
  212.               teleport(current, username)
  213.               current = teleports[math.random(1, #teleports)]
  214.             else
  215.               gpu.setForeground(errorcolor)
  216.               term.setCursor(1,2)
  217.               if current.blocked then term.write("    Blocked!    ")
  218.               else term.write("   Please pay!  ") end
  219.               gpu.setForeground(forecolor)
  220.               os.sleep(0.5)
  221.               refresh = true
  222.             end
  223.           end
  224.         elseif b == 1 then
  225.           gpu.setResolution(selectorsize.width, selectorsize.height)
  226.           changeframe(1)
  227.         end
  228.       end
  229.     elseif frame == 1 then
  230.       if b == 6 then
  231.         if a <= 10 then
  232.           if selector_page > 0 then
  233.             selector_page = selector_page - 1
  234.             refresh = true
  235.           end
  236.         elseif a > 14 then
  237.           if selector_page < max_page then
  238.             selector_page = selector_page + 1
  239.             refresh = true
  240.           end
  241.         end
  242.       else
  243.         num = b+selector_page*5
  244.         if num <= #teleports then current = teleports[num] end
  245.         gpu.setResolution(gosize.width, gosize.height)
  246.         changeframe(0)
  247.       end
  248.     end
  249.   elseif name == 'key_down' then break
  250.   elseif name == 'modem_message' then
  251.     if username == refreshcode then
  252.       local t = serial.unserialize(data)
  253.       if t ~= nil then
  254.         teleports = t
  255.         current = teleports[math.random(1, #teleports)]
  256.         refresh = true
  257.       end
  258.     elseif username == pingcode then
  259.       modem.send(a, secret_port, pingcode)
  260.     end
  261.   end
  262.  
  263.   update()
  264. end
  265.  
  266. -- end
  267. saveTeleports()
  268. term.clear()
  269. modem.close(secret_port)
  270. gpu.setForeground(0xFFFFFF)
  271. gpu.setBackground(0x000000)
  272. gpu.setResolution(oldw, oldh)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top