Guest User

startup

a guest
Sep 24th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.93 KB | None | 0 0
  1. mon = peripheral.find("monitor")
  2. sg = peripheral.find("stargate")
  3. chat = peripheral.find("chat_box")
  4. mon.setBackgroundColor(colors.black)
  5. mon.clear()
  6. maxEng = 50000
  7. dialling = {}
  8.  
  9.  
  10.  
  11.  
  12. function drawChevrons() --draws cheyvrons on the screen
  13.   x,y = mon.getSize()
  14.   chevX1 = x/3
  15.   chevX2 = x/3*2+1
  16.   chevY1 = y/3-2
  17.   chevY2 = y/3*2 +2
  18.   mon.setBackgroundColor(colors.black)
  19.   for yc = chevY1-2, chevY2-2 do
  20.     for xc = chevX1-2, chevX2-2 do
  21.       mon.setCursorPos(xc, yc)
  22.       mon.write(" ")
  23.     end
  24.   end
  25.   mon.setBackgroundColor(colors.lightGray)
  26.   for i = chevX1+2, chevX2-2 do
  27.     mon.setCursorPos(i,chevY1)
  28.     mon.write(" ")
  29.   end
  30.   for i = chevX1+2, chevX2-2 do
  31.     mon.setCursorPos(i,chevY2)
  32.     mon.write(" ")
  33.   end
  34.   for i = chevY1+2, chevY2-2 do
  35.     mon.setCursorPos(chevX1,i)
  36.     mon.write(" ")
  37.   end
  38.   for i = chevY1+2, chevY2-2 do
  39.     mon.setCursorPos(chevX2, i)
  40.     mon.write(" ")
  41.   end
  42.   chev1pos = {chevX1, chevY2 }
  43.   mon.setBackgroundColor(colors.gray)
  44.   mon.setTextColor(colors.black)
  45.   mon.setCursorPos(math.floor(chev1pos[1]), math.floor(chev1pos[2])-1)
  46.   mon.write(" > ")
  47.   chev2pos = {chevX1, chevY1 + ((chevY2 - chevY1) / 2) }
  48.   mon.setCursorPos(math.floor(chev2pos[1]-1), math.floor(chev2pos[2]))
  49.   mon.write(" > ")
  50.   chev3pos = {chevX1, chevY1 }
  51.   mon.setCursorPos(math.floor(chev3pos[1]), math.floor(chev3pos[2]+1))
  52.   mon.write(" > ")
  53.   chev4pos = {chevX1 + ((chevX2 - chevX1) / 2), chevY1 }
  54.   mon.setCursorPos(math.floor(chev4pos[1]-1), math.floor(chev4pos[2]))
  55.   mon.write(" V ")
  56.   chev5pos = {chevX2, chevY1 }
  57.   mon.setCursorPos(math.floor(chev5pos[1]-2), math.floor(chev5pos[2])+1)
  58.   mon.write(" < ")
  59.   chev6pos = {chevX2, chevY1 + ((chevY2 - chevY1) / 2) }
  60.   mon.setCursorPos(math.floor(chev6pos[1]-1), math.floor(chev6pos[2]))
  61.   mon.write(" < ")
  62.   chev7pos = {chevX2, chevY2 }
  63.   mon.setCursorPos(math.floor(chev7pos[1]-2), math.floor(chev7pos[2]-1))
  64.   mon.write(" < ")
  65.   chev8pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
  66.   mon.setCursorPos(math.floor(chev8pos[1]-1), math.floor(chev8pos[2]))
  67.   mon.write("   ")
  68. --  chev9pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
  69. --  mon.setCursorPos(math.floor(chev8pos[1]-1), chevY1 + ((chevY2 - chevY1) / 2))
  70. --  mon.write(" 9 ")
  71.   mon.setBackgroundColor(colors.black)
  72.   mon.setCursorPos(x/2 - 4, y/2 - 1)
  73.   mon.write("           ")
  74.   mon.setCursorPos(x/2-1, y/2+4)
  75.   mon.write("     ")
  76. end
  77.  
  78. function drawChev( chevInfo )
  79.   mon.setBackgroundColor(colors.gray)
  80.   x,y = mon.getSize()
  81.   chevX1 = x/3
  82.   chevX2 = x/3*2+1
  83.   chevY1 = y/3-2
  84.   chevY2 = y/3*2 +2
  85.   if chevInfo[1] == 1 then
  86.     chev1pos = {chevX1, chevY2 }
  87.     mon.setBackgroundColor(colors.gray)
  88.     mon.setCursorPos(math.floor(chev1pos[1]), math.floor(chev1pos[2])-1)
  89.     mon.write(" "..chevInfo[2].." ")
  90.   elseif chevInfo[1] == 2 then
  91.     chev2pos = {chevX1, chevY1 + ((chevY2 - chevY1) / 2) }
  92.     mon.setCursorPos(math.floor(chev2pos[1]-1), math.floor(chev2pos[2]))
  93.     mon.write(" "..chevInfo[2].." ")
  94.   elseif chevInfo[1] == 3 then
  95.     chev3pos = {chevX1, chevY1 }
  96.     mon.setCursorPos(math.floor(chev3pos[1]), math.floor(chev3pos[2]+1))
  97.     mon.write(" "..chevInfo[2].." ")
  98.   elseif chevInfo[1] == 4 then
  99.     chev4pos = {chevX1 + ((chevX2 - chevX1) / 2), chevY1 }
  100.     mon.setCursorPos(math.floor(chev4pos[1]-1), math.floor(chev4pos[2]))
  101.     mon.write(" "..chevInfo[2].." ")
  102.   elseif chevInfo[1] == 5 then
  103.     chev5pos = {chevX2, chevY1 }
  104.     mon.setCursorPos(math.floor(chev5pos[1]-2), math.floor(chev5pos[2])+1)
  105.     mon.write(" "..chevInfo[2].." ")
  106.   elseif chevInfo[1] == 6 then
  107.     chev6pos = {chevX2, chevY1 + ((chevY2 - chevY1) / 2) }
  108.     mon.setCursorPos(math.floor(chev6pos[1]-1), math.floor(chev6pos[2]))
  109.     mon.write(" "..chevInfo[2].." ")
  110.   elseif chevInfo[1] == 7 then
  111.     chev7pos = {chevX2, chevY2 }
  112.     mon.setCursorPos(math.floor(chev7pos[1]-2), math.floor(chev7pos[2]-1))
  113.     mon.write(" "..chevInfo[2].." ")
  114.   elseif chevInfo[1] == 8 then
  115.     chevsave8 = chevInfo[2]
  116.   elseif chevInfo[1] == 9 then
  117.     chev8pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
  118.     mon.setCursorPos(math.floor(chev8pos[1]-1), math.floor(chev8pos[2]))
  119.     mon.write(""..chevsave8.." "..chevInfo[2])
  120.   mon.setBackgroundColor(colors.black)
  121. end
  122.  
  123. end
  124.  
  125. function drawSgStatus(status) -- draws stargate status
  126.   if status ~= "Idle" then
  127.   term.setCursorPos(1,2)
  128.   write(status) --needed for sting length because sting.len() won't work with stargateStatus()
  129.   xc, yc = term.getCursorPos()
  130.   term.clear()
  131.   term.setCursorPos(1,2)
  132.   write("> ")
  133.   if xc%2 == 1 then
  134.     xc = xc+1
  135.     even = true
  136.   else
  137.     even = false
  138.   end
  139.   mon.setBackgroundColor(colors.black)
  140.   if status == "Connected" then
  141.     mon.setTextColor(colors.lightBlue)
  142.   elseif status == "Dialling" then
  143.     mon.setTextColor(colors.orange)
  144.   else
  145.     mon.setTextColor(colors.green)
  146.   end
  147.   x,y = mon.getSize()
  148.   mon.setCursorPos((x/2+1) - 6, y/2+2)
  149.   mon.write("          ")
  150.   mon.setCursorPos((x/2+1) - (xc/2-1), y/2+2)
  151.   mon.write(status)
  152.   if even == true then
  153.     mon.write(".")
  154.   end
  155.   end
  156. end
  157.  
  158.  
  159. function drawLocalAddress() -- draws the address stargate being controlled
  160.   x,y = mon.getSize()
  161.   mon.setBackgroundColor(colors.black)
  162.   mon.setTextColor(colors.lightGray)
  163.   mon.setCursorPos(x/2-7, 1)
  164.   mon.write("Stargate Address:")
  165.  
  166. end
  167.  
  168. function drawDial() -- draws the button to access the dialing menu
  169.   x,y = mon.getSize()
  170.   state, int = sg.stargateState()
  171.   for yc = y-3, y-1 do
  172.     for xc = x/2-15, x/2 do
  173.       if state == "Idle" then
  174.         mon.setBackgroundColor(colors.lightGray)
  175.       else
  176.         mon.setBackgroundColor(colors.gray)
  177.       end
  178.       mon.setCursorPos(xc,yc)
  179.       mon.write(" ")
  180.     end
  181.   end
  182.   mon.setCursorPos(x/2-7, y-2)
  183.   mon.setTextColor(colors.black)
  184.   mon.write("List")
  185. end
  186.  
  187. function drawTerm() -- draws the button to terminate the stargate connection to another gate
  188.   x,y = mon.getSize()
  189.   state, int = sg.stargateState()
  190.   for yc = y-3, y-1 do
  191.     for xc = x/2+2, x/2+15 do
  192.       if state == "Connected" or state == "Connecting" or state == "Dialling" then
  193.         mon.setBackgroundColor(colors.lightGray)
  194.       else
  195.         mon.setBackgroundColor(colors.gray)
  196.       end
  197.       mon.setCursorPos(xc,yc)
  198.       mon.write(" ")
  199.     end
  200.   end
  201.   mon.setCursorPos(x/2+3, y-2)
  202.   mon.setTextColor(colors.black)
  203.   mon.write("Eteindre")
  204. end
  205.  
  206.  
  207. function drawHome() -- draws the home screen
  208.   mon.setBackgroundColor(colors.black)
  209.   x,y = mon.getSize()
  210.   mon.clear()
  211.   mon.setCursorPos(1,y)
  212.   mon.setTextColor(colors.gray)
  213.   mon.setBackgroundColor(colors.black)
  214.    
  215.   drawChevrons()
  216.   status, int = sg.stargateState()
  217.   drawSgStatus(tostring(status))
  218.  
  219.  
  220.   drawLocalAddress()
  221.  
  222.   drawDial()
  223.   mon.setCursorBlink(false)
  224.   drawTerm()
  225. end
  226.  
  227. function drawBookmarksPage()
  228.   mon.setBackgroundColor(colors.black)
  229.   mon.clear()
  230.   mon.setTextColor(colors.black)
  231.   x,y = mon.getSize()
  232.   for yc = 1,y-3 do
  233.     if yc%2 == 1 then
  234.       mon.setBackgroundColor(colors.lightBlue)
  235.     else
  236.       mon.setBackgroundColor(colors.lightGray)
  237.     end
  238.     for xc = 1,x do
  239.       mon.setCursorPos(xc, yc)
  240.       mon.write(" ")
  241.     end
  242.   end
  243.   for i= 1,y do
  244.     if i%2 == 1 then
  245.       mon.setBackgroundColor(colors.lightBlue)
  246.     else
  247.       mon.setBackgroundColor(colors.lightGray)
  248.     end
  249.     if fs.exists(tostring(i)) then
  250.       file = fs.open(tostring(i),"r")
  251.       bookmark = textutils.unserialize(file.readAll())
  252.       file.close()
  253.       mon.setCursorPos(1,i)
  254.       for k,v in pairs(bookmark) do
  255.         if k == "name" then
  256.           mon.write(v)
  257.           mon.setCursorPos(x/2, i)
  258.           mon.write(bookmark.address)
  259.           mon.setCursorPos(x,i)
  260.         end
  261.       end
  262.     end
  263.   end
  264.   mon.setCursorPos(x/2, y-1)
  265.   mon.setBackgroundColor(colors.black)
  266.   mon.setTextColor(colors.white)
  267.   mon.write("BACK")
  268. end
  269.  
  270.  
  271. function inputPage(type)
  272.   mon.clear()
  273.   term.redirect(mon)
  274.   term.setBackgroundColor(colors.lightGray)
  275.   term.clear()
  276.   x,y = term.getSize()
  277.   term.setCursorPos(x/2-8, y/2-2)
  278.   print("Set an address name")
  279.   term.setCursorPos(x/2 - 4, y/2)
  280.   print("         ")
  281.   term.setCursorPos(x/2 - 4, y/2)
  282.   nameInput = read()
  283.   addressInput = "nil"
  284.   term.setBackgroundColor(colors.lightGray)
  285.   term.clear()
  286.   term.setCursorPos(x/2-9, y/2-4)
  287.   print("Enter Stargate address")
  288.   if type == "secEntry" then
  289.     term.setCursorPos(x/2-10, y/2-2)
  290.     print("DO NOT ENTER ANY HYPHONS")
  291.   end
  292.   term.setBackgroundColor(colors.black)
  293.   term.setCursorPos(x/2 - 5, y/2)
  294.   print("           ")
  295.   term.setCursorPos(x/2 - 5, y/2)
  296.   addressInput = string.upper(read())
  297.   newGate ={name = nameInput, address = addressInput}
  298.   term.redirect(term.native())
  299.   return newGate
  300. end
  301.  
  302. function drawRemoteAddress(name)
  303.   mon.setBackgroundColor(colors.black)
  304.   x,y = mon.getSize()
  305.   mon.setCursorPos((x/2+1) - string.len(name)/2, y/2-2)
  306.   mon.write(name)
  307. end
  308.  
  309.  
  310.  
  311.  
  312. if fs.exists("currentSec") then -- checks to see if there's list of gates stored for security reasons
  313.   file = fs.open("currentSec", "r")
  314.   currentSec = file.readAll()
  315.   file.close()
  316. else
  317.   currentSec = "NONE"
  318. end
  319. mon.setTextScale(1)
  320. drawHome()
  321. while true do
  322.   event, param1, param2, param3 = os.pullEvent()
  323.   if event == "monitor_touch" then
  324.     x,y = mon.getSize()
  325.     if param2 > x/2-15 and param2 <= x/2 and param3 >= y-3 and param3 <= y-1 then -- Click has opened dial menu
  326.       status, int = sg.stargateState()
  327.         if status == "Idle" then
  328.             while true do
  329.                 drawBookmarksPage()
  330.                 event, param1, param2, param3 = os.pullEvent()
  331.                 if event == "monitor_touch" then
  332.                     if param3 >= y-2 then -- user clicked back
  333.                         drawHome()
  334.                         break
  335.                        
  336.                        
  337.                     else -- user has clicked on a bookmark
  338.                         if fs.exists(tostring(param3)) then
  339.                           file = fs.open(tostring(param3), "r")
  340.                           gateData = textutils.unserialize(file.readAll()) -- GATE DATA VARIABLE!!!
  341.                           file.close()
  342.                           drawHome()
  343.                           for k,v in pairs(gateData) do
  344.                             if k == "address" then
  345.                               ok, result = pcall(sg.dial, v)
  346.                               if ok then
  347.                                 status, int = sg.stargateState()
  348.                                 drawSgStatus(status)                               
  349.                               else
  350.                                 drawSgStatus("Error")      
  351.                               end
  352.                             end
  353.                           end
  354.                            
  355.                             for k,v in ipairs(gateData) do
  356.                                 if k == "name"  then
  357.                                     name = v
  358.                                     mon.setTextColor(colors.orange)
  359.                                     drawRemoteAddress(name)                                                    
  360.                                     print(k,v)
  361.                                 end  
  362.                            
  363.                             sleep(.5)
  364.                           end
  365.                          
  366.                          
  367.                         break
  368.                         end
  369.                     end
  370.                 else
  371.                   drawHome()
  372.                   break
  373.                 end
  374.             end
  375.         end
  376.        
  377.      
  378.    
  379.     elseif param2 > x/2+2 and param2 <= x/2+15 and param3 >= y-3 and param3 <= y-1 then -- user clicked TERM
  380.       ok, result = pcall(sg.disconnect)
  381.       drawChevrons()
  382.     end
  383.    
  384.   elseif event == "sgDialIn" then
  385.     mon.setTextColor(colors.orange)
  386.    
  387.     if fs.exists("currentSec") then
  388.       file = fs.open("currentSec", "r")
  389.       currentSec = file.readAll()
  390.       file.close()
  391.     end
  392.     if fs.exists("secList") then
  393.       file = fs.open("secList", "r")
  394.       secList = textutils.unserialize(file.readAll())
  395.       for k,v in pairs(secList) do
  396.         address = v.address
  397.         if string.sub(v.address,1,7) == param2 or v.address == param2 then
  398.          
  399.           secGate = true
  400.         end
  401.       end
  402.     end
  403.    
  404.    
  405.    
  406.   elseif event == "sgStargateStateChange" or "sgChevronEngaged" then
  407.     drawDial()
  408.    
  409.     drawTerm()
  410.     status, int = sg.stargateState()
  411.     drawSgStatus(tostring(status))
  412.     if status == "idle" then
  413.       isConnected = false
  414.     else
  415.       isConnected = true
  416.     end
  417.     if event == "sgChevronEngaged" then
  418.       mon.setTextColor(colors.orange)
  419.       drawChev({param2, param3})
  420.       if param2 == 1 then
  421.         dialling = {}
  422.       end
  423.       table.insert(dialling, param2, param3)
  424.       --drawRemoteAddress()
  425.       chat.say("Attention, ne reste pas en face de la porte quand elle s'ouvre ou tu risque de mourrir")
  426.     elseif param2 == "Idle" then  
  427.     drawChevrons()
  428.     elseif param2 == "Connected" then
  429.    
  430.       mon.setTextColor(colors.lightBlue)
  431.       drawRemoteAddress()
  432.       for k,v in pairs(dialling) do
  433.         drawChev({k,v})
  434.       end
  435.       sg.sendMessage(sg.irisState())
  436.     end
  437.   end
  438.   end
Advertisement
Add Comment
Please, Sign In to add comment