Advertisement
Guest User

western sg inter

a guest
Oct 30th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. dofile("config")
  2. dofile("compat")
  3. dofile("addresses")
  4.  
  5. function pad(s, n)
  6. return s .. string.rep(" ", n - string.len(s))
  7. end
  8.  
  9. function showMenu()
  10. setCursor(1, 1)
  11. for i, na in pairs(addresses) do
  12. print(string.format("%d %s", i, na[1]))
  13. end
  14. print("")
  15. print("D Disconnect")
  16. print("O Open Iris")
  17. print("C Close Iris")
  18. print("Q Quit")
  19. print("")
  20. write("Option? ")
  21. end
  22.  
  23. function getIrisState()
  24. ok, result = pcall(sg.irisState)
  25. return result
  26. end
  27.  
  28. function showState()
  29. locAddr = sg.localAddress()
  30. remAddr = sg.remoteAddress()
  31. state, chevrons = sg.stargateState()
  32. energy = sg.energyAvailable()
  33. iris = sg.irisState()
  34. showAt(30, 1, "Local: " .. locAddr)
  35. showAt(30, 2, "Remote: " .. remAddr)
  36. showAt(30, 3, "State: " .. state)
  37. showAt(30, 4, "Energy: " .. energy)
  38. showAt(30, 5, "Iris: " .. iris)
  39. showAt(30, 6, "Engaged: " .. chevrons)
  40. end
  41.  
  42. function showAt(x, y, s)
  43. setCursor(x, y)
  44. write(pad(s, 50))
  45. -- write(string.rep(" ", 20))
  46. -- setCursor(x, y)
  47. -- write(s)
  48. end
  49.  
  50. function showMessage(mess)
  51. showAt(1, screen_height, mess)
  52. -- setCursor(1, screen_height)
  53. -- term.clearLine()
  54. -- if mess then
  55. -- write(mess)
  56. -- end
  57. end
  58.  
  59. function showError(mess)
  60. i = string.find(mess, ": ")
  61. if i then
  62. mess = "Error: " .. string.sub(mess, i + 2)
  63. end
  64. showMessage(mess)
  65. end
  66.  
  67. handlers = {}
  68.  
  69. function dial(name, addr)
  70. showMessage(string.format("Dialling %s (%s)", name, addr))
  71. sg.dial(addr)
  72. end
  73.  
  74. handlers[key_event_name] = function(e)
  75. c = key_event_char(e)
  76. if c == "d" then
  77. sg.disconnect()
  78. elseif c == "o" then
  79. sg.openIris()
  80. elseif c == "c" then
  81. sg.closeIris()
  82. elseif c == "q" then
  83. running = false
  84. elseif c >= "1" and c <= "9" then
  85. na = addresses[tonumber(c)]
  86. if na then
  87. dial(na[1], na[2])
  88. end
  89. end
  90. end
  91.  
  92. function handlers.sgChevronEngaged(e)
  93. chevron = e[3]
  94. symbol = e[4]
  95. showMessage(string.format("Chevron %s engaged! (%s)", chevron, symbol))
  96. end
  97.  
  98. function eventLoop()
  99. while running do
  100. showState()
  101. e = {pull_event()}
  102. name = e[1]
  103. f = handlers[name]
  104. if f then
  105. showMessage("")
  106. ok, result = pcall(f, e)
  107. if not ok then
  108. showError(result)
  109. end
  110. end
  111. end
  112. end
  113.  
  114. function main()
  115. term.clear()
  116. showMenu()
  117. eventLoop()
  118. term.clear()
  119. setCursor(1, 1)
  120. end
  121.  
  122. running = true
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement