Advertisement
Guest User

autorun.lua

a guest
Apr 30th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.32 KB | None | 0 0
  1. --
  2. --  Interactive stargate control program
  3. --  Shows stargate state and allows dialling
  4. --  addresses selected from a list
  5. --  with automated Iris control
  6. --  by DarknessShadow
  7. --
  8.  
  9. dofile("config.lua")
  10. dofile("compat.lua")
  11. dofile("addresses.lua")
  12.  
  13. debug = false
  14.  
  15. gpu.setBackground(0x333333)
  16.  
  17. function pad(s, n)
  18.   return s .. string.rep(" ", n - string.len(s))
  19. end
  20.  
  21. function showMenu()
  22.   setCursor(1, 1)
  23.   print("Address Page " .. seite + 1)
  24.   for i, na in pairs(addresses) do
  25.     if i >= 1 + seite * 9 and i <= 9 + seite * 9 then
  26.       print(i - seite * 9 .. " " .. na[1])
  27.       if sg.energyToDial(na[2]) == nil then
  28.         gpu.setForeground(0xFF0000)
  29.         print("   <Error>")
  30.         gpu.setForeground(0xFFFFFF)
  31.       else
  32.         print("   ".. string.format("%.1f", (sg.energyToDial(na[2])*energymultiplicator)/1000).." k")
  33.       end
  34.     end
  35.     maxseiten = i / 9
  36.   end
  37.   iris = sg.irisState()
  38. end
  39.  
  40. function getIrisState()
  41.   ok, result = pcall(sg.irisState)
  42.   return result
  43. end
  44.  
  45. function iriscontroller()
  46.   if state == "Dialing" then
  47.     messageshow = true
  48.   end
  49.   if direction == "Incoming" and incode == IDC and control == "Off" then
  50.     IDCyes = true
  51.   end
  52.   if direction == "Incoming" and incode == IDC and iriscontrol == "on" and control == "On" then
  53.     if iris == "Offline" then
  54.       sg.sendMessage("IDC Accepted Iris: Offline")
  55.     else
  56.       sg.openIris()
  57.       os.sleep(2)
  58.       sg.sendMessage("IDC Accepted Iris: Open")
  59.     end
  60.     iriscontrol = "off"
  61.     IDCyes = true
  62.   elseif direction == "Incoming" and send == true then
  63.     sg.sendMessage("Iris Control: "..control.." Iris: "..iris)
  64.     send = false
  65.   end
  66.   if wormhole == "in" and state == "Dialling" and iriscontrol == "on" and control == "On" then
  67.     if iris == "Offline" then else
  68.       sg.closeIris()
  69.     end
  70.     k = "close"
  71.   end
  72.   if iris == "Closing" and control == "On" then k = "open" end
  73.   if state == "Idle" and k == "close" and control == "On" then
  74.     outcode = nil
  75.     if iris == "Offline" then else
  76.       sg.openIris()
  77.     end
  78.     iriscontrol = "on"
  79.     wormhole = "in"
  80.     codeaccepted = "-"
  81.     activationtime = 0
  82.     entercode = false
  83.     showidc = ""
  84.     AddNewAddress = false
  85.   end
  86.   if state == "Idle" and control == "On" then
  87.     iriscontrol = "on"
  88.   end
  89.   if state == "Closing" then
  90.     send = true
  91.     incode = "-"
  92.     showMessage("")
  93.     IDCyes = false
  94.   end
  95.   if state == "Idle" then
  96.     incode = "-"
  97.   end
  98.   if state == "Closing" and control == "On" then
  99.     k = "close"
  100.   end
  101.   if state == "Connected" and direction == "Outgoing" and send == true then
  102.     if outcode == "-" or outcode == nil then else
  103.       sg.sendMessage(outcode)
  104.       send = false
  105.     end
  106.   end
  107.   if codeaccepted == "-" or codeaccepted == nil then
  108.   elseif messageshow == true then
  109.     gpu.setForeground(0xFF0000)
  110.     showMessage("Message received: "..codeaccepted)
  111.     gpu.setForeground(0xFFFFFF)
  112.     if codeaccepted == "Request: Disconnect Stargate" then
  113.       os.sleep(1)
  114.       sg.disconnect()
  115.       os.sleep(1)
  116.     end
  117.     messageshow = false
  118.     incode = "-"
  119.     codeaccepted = "-"
  120.   end
  121.   if state == "Idle" then
  122.     activationtime = 0
  123.     entercode = false
  124.     remoteName = ""
  125.   end
  126. end
  127.  
  128. function neueZeile(b)
  129.   zeile = zeile + b
  130. end
  131.  
  132. function newAddress(a)
  133.   if AddNewAddress == true then
  134.     f = io.open ("addresses.lua", "a")
  135.     f:seek ("end", -1)
  136.     f:write('  {"' .. remAddr .. '", "' .. remAddr .. '", ""},\n}')
  137.     f:close ()
  138.     AddNewAddress = false
  139.     dofile("addresses.lua")
  140.     showMenu()
  141.   end
  142. end
  143.  
  144. function destinationName()
  145.   if remoteName == "" and state == "Dialling" and wormhole == "in" then
  146.     for j, na in pairs(addresses) do
  147.       if remAddr == na[2] then
  148.         if na[1] == na[2] then
  149.           remoteName = "Unknown"
  150.         else
  151.           remoteName = na[1]
  152.           break
  153.         end
  154.       end
  155.     end
  156.     if remoteName == "" then
  157.       newAddress(remAddr)
  158.     end
  159.   end
  160. end
  161.  
  162. function getAddress(A)
  163.   if A == "" or A == nil then
  164.     return ""
  165.   else
  166.     return string.sub(A, 1, 4) .. "-" .. string.sub(A, 5, 7) .. "-" .. string.sub(A, 8, 9)
  167.   end
  168. end
  169.  
  170. function showState()
  171.   gpu.setBackground(0x333333)
  172.   locAddr = getAddress(sg.localAddress())
  173.   remAddr = getAddress(sg.remoteAddress())
  174.   destinationName()
  175.   state, chevrons, direction = sg.stargateState()
  176.   iris = sg.irisState()
  177.   iriscontroller()
  178.   energy = sg.energyAvailable()*energymultiplicator
  179.   zeile = 1
  180.   showAt(40, zeile,  "Local Address:    " .. locAddr)
  181.   neueZeile(1)
  182.   showAt(40, zeile,  "Destination Addr: " .. remAddr)
  183.   neueZeile(1)
  184.   showAt(40, zeile,  "Destination Name: " .. remoteName)
  185.   neueZeile(1)
  186.   showAt(40, zeile,  "State:            " .. state)
  187.   neueZeile(1)
  188.   showenergy()
  189.   neueZeile(1)
  190.   showAt(40, zeile,  "Iris:             " .. iris)
  191.   neueZeile(1)
  192.   if iris == "Offline" then
  193.   else
  194.     showAt(40, zeile,  "Iris Control:     " .. control)
  195.     neueZeile(1)
  196.   end
  197.   if IDCyes == true then
  198.     showAt(40, zeile, "IDC:              Accepted")
  199.     neueZeile(1)
  200.   else
  201.     showAt(40, zeile, "IDC:              " .. incode)
  202.     neueZeile(1)
  203.   end
  204.   showAt(40, zeile,  "Engaged:          " .. chevrons)
  205.   neueZeile(1)
  206.   showAt(40, zeile,  "Direction:        " .. direction)
  207.   neueZeile(1)
  208.   activetime()
  209.   neueZeile(1)
  210.   autoclose()
  211.   neueZeile(1)
  212.   if debug == true then
  213.     showAt(40, zeile, "Version:          1.3.8")
  214.     neueZeile(1)
  215.   end
  216.   showControls()
  217. end
  218.  
  219. function showControls()
  220.   neueZeile(3)
  221.   showAt(40, zeile, "Controls")
  222.   neueZeile(1)
  223.   showAt(40, zeile, "D Disconnect")
  224.   neueZeile(1)
  225.   if iris == "Offline" then
  226.     control = "Off"
  227.   else
  228.     showAt(40, zeile, "O Open Iris")
  229.     neueZeile(1)
  230.     showAt(40, zeile, "C Close Iris")
  231.     neueZeile(1)
  232.     showAt(40, zeile, "I Iris Control On/Off")
  233.     neueZeile(1)
  234.   end
  235.   showAt(40, zeile, "E Enter IDC")
  236.   neueZeile(1)
  237.   if maxseiten > seite + 1 then
  238.     showAt(40, zeile, "→ Next Page")
  239.     neueZeile(1)
  240.   end
  241.   if seite >= 1 then
  242.     showAt(40, zeile, "← Previous Page")
  243.     neueZeile(1)
  244.   end
  245.   if debug == true then
  246.     showAt(40, zeile, "Q Quit")
  247.   end
  248. end
  249.  
  250. function autoclose()
  251.   if autoclosetime == false then
  252.     showAt(40, zeile, "Autoclose:        Off")
  253.   else
  254.     showAt(40, zeile, "Autoclose:        " .. autoclosetime .. "s")
  255.     if (activationtime - os.time()) / sectime > autoclosetime and state == "Connected" then
  256.       sg.disconnect()
  257.     end
  258.   end
  259. end
  260.  
  261. function showenergy()
  262.   if energy < 10000000 then
  263.     showAt(40, zeile, "Energy "..energytype..":        " .. string.format("%.1f", energy/1000) .. " k")
  264.   else
  265.     showAt(40, zeile, "Energy "..energytype..":        " .. string.format("%.1f", energy/1000000) .. " M")
  266.   end
  267. end
  268.  
  269. function activetime()
  270.   if state == "Connected" then
  271.     if activationtime == 0 then
  272.       activationtime = os.time()
  273.     end
  274.     time = (activationtime - os.time())/sectime
  275.     if time > 0 then
  276.       showAt(40, zeile, "Time:             " .. string.format("%.1f", time) .. "s")
  277.     end
  278.   else
  279.     showAt(40, zeile, "Time:")
  280.   end
  281. end
  282.  
  283. function showAt(x, y, s, h)
  284.   setCursor(x, y)
  285.   if h == nil then
  286.     write(pad(s, 50))
  287.   else
  288.     write(pad(s, 1))
  289.   end
  290. end
  291.  
  292. function showMessage(mess)
  293.   showAt(1, screen_height, mess)
  294. end
  295.  
  296. function showError(mess)
  297.   i = string.find(mess, ": ")
  298.   if i then
  299.     mess = "Error: " .. string.sub(mess, i + 2)
  300.   end
  301.   showMessage(mess)
  302. end
  303.  
  304. handlers = {}
  305.  
  306. function dial(name, addr)
  307.   showMessage(string.format("Dialling %s (%s)", name, addr))
  308.   remoteName = name
  309.   check(sg.dial(addr))
  310. end
  311.  
  312. handlers[key_event_name] = function(e)
  313.   c = key_event_char(e)
  314.   if e[3] == 13 then
  315.     entercode = false
  316.     sg.sendMessage(enteridc)
  317.   elseif entercode == true then
  318.     enteridc = enteridc .. c
  319.     showidc = showidc .. "*"
  320.     showMessage("Enter IDC: " .. showidc)
  321.   elseif c == "e" then
  322.     if state == "Connected" and direction == "Outgoing" then
  323.       enteridc = ""
  324.       showidc = ""
  325.       entercode = true
  326.       showMessage("Enter IDC:")
  327.     else
  328.       showMessage("Stargate not Connected")
  329.     end
  330.   elseif c == "d" then
  331.     if state == "Connected" and direction == "Incoming" then
  332.         sg.sendMessage("Request: Disconnect Stargate")
  333.     else
  334.         sg.disconnect()
  335.     end
  336.   elseif c == "o" then
  337.     if iris == "Offline" then else
  338.       sg.openIris()
  339.       if wormhole == "in" then
  340.         if iris == "Offline" then
  341.         else
  342.           os.sleep(2)
  343.           sg.sendMessage("Manual Override: Iris Open")
  344.         end
  345.       end
  346.       if state == "Idle" then
  347.         iriscontrol = "on"
  348.       else iriscontrol = "off"
  349.       end
  350.     end
  351.   elseif c == "c" then
  352.     if iris == "Offline" then else
  353.       sg.closeIris()
  354.       iriscontrol = "off"
  355.       if wormhole == "in" then
  356.         sg.sendMessage("Manual Override: Iris Closed")
  357.       end
  358.     end
  359.   elseif c == "q" then
  360.     running = false
  361.   elseif c >= "1" and c <= "9" then
  362.     c = c + seite * 9
  363.     na = addresses[tonumber(c)]
  364.     iriscontrol = "off"
  365.     wormhole = "out"
  366.     if na then
  367.       dial(na[1], na[2])
  368.       if na[3] == "-" then
  369.         else outcode = na[3]
  370.       end
  371.     end
  372.   elseif c == "i" then
  373.     if iris == "Offline" then else
  374.       send = true
  375.       if control == "On" then
  376.         control = "Off"
  377.       else
  378.         control = "On"
  379.       end
  380.     end
  381.   elseif e[3] == 0 and e[4] == 203 then
  382.     if seite == 0 then
  383.     else
  384.       seite = seite - 1
  385.       term.clear()
  386.       showState()
  387.       showMenu()
  388.     end
  389.   elseif e[3] == 0 and e[4] == 205 then
  390.     if seite + 1 < maxseiten then
  391.       seite = seite + 1
  392.       term.clear()
  393.       showState()
  394.       showMenu()
  395.     end
  396.   end
  397. end
  398.  
  399. function handlers.sgChevronEngaged(e)
  400.   chevron = e[3]
  401.   symbol = e[4]
  402.   showMessage(string.format("Chevron %s engaged! (%s)", chevron, symbol))
  403. end
  404.  
  405. function eventLoop()
  406.   while running do
  407.     showState()
  408.     e = {pull_event()}
  409.     if e[1] == nil then
  410.     else
  411.       name = e[1]
  412.       f = handlers[name]
  413.       if f then
  414.         showMessage("")
  415.         ok, result = pcall(f, e)
  416.         if not ok then
  417.           showError(result)
  418.         end
  419.       end
  420.       if string.sub(e[1],1,3) == "sgM" and direction == "Incoming" and wormhole == "in" then
  421.         if e[3] == "" then else
  422.           incode = e[3]
  423.           messageshow = true
  424.         end
  425.       end
  426.       if string.sub(e[1],1,3) == "sgM" and direction == "Outgoing" then
  427.         codeaccepted = e[3]
  428.         messageshow = true
  429.       end
  430.     end
  431.   end
  432. end
  433.  
  434. function main()
  435.   term.clear()
  436.   showMenu()
  437.   eventLoop()
  438.   gpu.setBackground(0x00000)
  439.   term.clear()
  440.   setCursor(1, 1)
  441. end
  442.  
  443. if sg.stargateState() == "Idle" and sg.irisState() == "Closed" then
  444.   sg.openIris()
  445. end
  446.  
  447. messageshow = true
  448.  
  449. running = true
  450. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement