Maxstripe

stargatetest

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