Advertisement
AlwayzPatty

Untitled

Nov 13th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. local sg = peripheral.find("stargate")
  2.  
  3. local event, p1, p2, p3 = nil
  4. local screenSelection = 1
  5. local screen = nil
  6. local prevX, prevY = nil
  7. local state, engaged, direction = sg.stargateState()
  8. local favAddresses = {}
  9.  
  10. function resetScreen()
  11. term.setBackgroundColor(colors.gray)
  12. term.setTextColor(colors.black)
  13. term.clear()
  14. term.setCursorPos(1, 1)
  15. end
  16.  
  17. function PrintInline(text, x, y)
  18. term.setCursorPos(prevX + x, prevY + y)
  19. term.write(text)
  20. prevX = prevX + x
  21. prevY = prevY + y
  22. end
  23.  
  24. function Print(text, x, y)
  25. term.setCursorPos(x, y)
  26. term.clearLine()
  27. term.write(text)
  28. prevX = x
  29. prevY = y
  30. end
  31.  
  32. function drawScreen(screenSettings)
  33. resetScreen()
  34.  
  35. term.setBackgroundColor(colors.white)
  36. for i = 1, 3, 1 do
  37. term.setCursorPos(1, i)
  38. term.clearLine()
  39. end
  40. Print(screenSettings["title"], getCenterX(screenSettings["title"]), 2)
  41. term.setBackgroundColor(colors.gray)
  42.  
  43. --For each option of a screen
  44. local line = getCenterY() - math.floor(tablelength(screenSettings["options"]) / 2)
  45. local curSel = 1
  46. for k, v in ipairs(screenSettings["options"]) do
  47. local label = v["option"]
  48. if curSel == screenSelection then
  49. term.setTextColor(colors.white)
  50. Print("[ ", getCenterX("[ " .. label .. " ]"), line)
  51. term.setTextColor(colors.black)
  52. PrintInline(label, string.len("[ "), 0, true)
  53. term.setTextColor(colors.white)
  54. PrintInline(" ]", string.len(label), 0, true)
  55. term.setTextColor(colors.black)
  56. else
  57. Print(label, getCenterX(label), line)
  58. end
  59. line = line + 1
  60. curSel = curSel + 1
  61. end
  62. end
  63.  
  64. function getCenterX(text)
  65. local x,_ = term.getSize()
  66. return math.floor((x / 2) - (string.len(text) / 2))
  67. end
  68.  
  69. function getCenterY()
  70. local _,y = term.getSize()
  71. return math.floor(y / 2)
  72. end
  73.  
  74. function tablelength(T)
  75. local count = 0
  76. for _ in pairs(T) do count = count + 1 end
  77. return count
  78. end
  79.  
  80. function tableAt(T, index)
  81. local i = 0
  82. for _, v in pairs(T) do
  83. if i == index then
  84. return v
  85. end
  86. i = i + 1
  87. end
  88. end
  89.  
  90. function startup()
  91. screenSelection = 1
  92. screen = {
  93. title = "Stargate Control Program",
  94. options = {
  95. {
  96. option = "Dial Favorite",
  97. func = dialFavoriteScreen
  98. },
  99. {
  100. option = "Dial Manually",
  101. func = dialManually
  102. },
  103. {
  104. option = "Exit",
  105. func = exitProgram
  106. }
  107. }
  108. }
  109.  
  110. if state ~= "Idle" then
  111. table.insert(screen["options"], 3,
  112. {
  113. option = "Close Gate",
  114. func = function()
  115. sg.disconnect()
  116. startup()
  117. end
  118. })
  119. end
  120. end
  121.  
  122. function dialFavoriteScreen()
  123. screenSelection = 1
  124. screen = {
  125. title = "Dial Favorites",
  126. options = {
  127. {
  128. option = "Add Favorite",
  129. func = function()
  130. if not fs.exists("favorites") then
  131. fs.open("favorites", "rw").close()
  132. end
  133. f = fs.open("favorites","ra")
  134.  
  135. local _, y = term.getSize()
  136.  
  137. Print("Name:", 1, y - 1)
  138. term.setCursorPos(1, y)
  139. term.clearLine()
  140. local name = read()
  141.  
  142. Print("Address:", 1, y - 1)
  143. term.setCursorPos(1, y)
  144. term.clearLine()
  145. local address = read()
  146. if f.readLine() ~= nil then
  147. f.writeLine("\n\"" ..name.."\"{"..address.."}")
  148. else
  149. f.writeLine("\"" ..name.."\"{"..address.."}")
  150. end
  151.  
  152. f.close()
  153. end
  154. }
  155. }
  156. }
  157.  
  158. for k, v in ipairs(favAddresses) do
  159. table.insert(screen["options"],
  160. {
  161. option = v["title"],
  162. func = dialFav
  163. })
  164. end
  165.  
  166. table.insert(screen["options"],
  167. {
  168. option = "Back",
  169. func = startup
  170. })
  171. end
  172.  
  173. function dialManually()
  174. screenSelection = 1
  175. screen = {
  176. title = "Manual Dial",
  177. options = {
  178. {
  179. option = "Enter",
  180. func = function()
  181. local _, y = term.getSize()
  182. Print("ADDRESS:", 1, y - 1)
  183. term.setCursorPos(1, y)
  184. dial(read())
  185. startup()
  186. end
  187. },
  188. {
  189. option = "Back",
  190. func = startup
  191. }
  192. }
  193. }
  194. end
  195.  
  196. function parseTableFavorites()
  197. f = fs.open("favorites", fs.exists("favorites") and "r" or "rw")
  198. local text = f.readAll()
  199. for i in string.gmatch(text, "[^\r\n]+") do
  200. local title, address = nil
  201. local j = string.gmatch(i, "%b\"\"")
  202. j[0] = string.gsub(j[0], "%s$", "")
  203. j[0] = string.gsub(j[0], "^%s", "")
  204. j[0] = string.gsub(j[0], "\"", "")
  205. ftitle = j[0]
  206.  
  207. local j = string.gmatch(i, "%b{}")
  208. j[0] = string.gsub(j[0], "%s$", "")
  209. j[0] = string.gsub(j[0], "^%s", "")
  210. j[0] = string.gsub(j[0], "{", "")
  211. j[0] = string.gsub(j[0], "}", "")
  212. faddress = j[0]
  213.  
  214. table.insert(favAddresses,
  215. {
  216. title = ftitle,
  217. address = faddress
  218. })
  219. end
  220. f.close()
  221. end
  222.  
  223. function dialFav()
  224. local destination = tableAt(favAddresses, screenSelection - 1)
  225. dial(destination["address"])
  226. startup()
  227. end
  228.  
  229. function dial(address)
  230. local _, err = sg.dial(address)
  231. if err == "targetBusy" then
  232. local _, y = term.getSize()
  233. Print("Stargate is in use!", 1, y)
  234. sleep(2)
  235. elseif sg.energyAvailable() < sg.energyToDial(address) then
  236. local _, y = term.getSize()
  237. Print("Stargate has not enough energy!", 1, y)
  238. sleep(2)
  239. end
  240. end
  241.  
  242. function exitProgram()
  243. os.reboot()
  244. end
  245.  
  246. function awaitEvent()
  247. event, p1, p2, p3 = os.pullEvent()
  248. if event == "sgStargateStateChange" then
  249. state, engaged, direction = sg.stargateState()
  250. end
  251. end
  252.  
  253. function isSelectionChanged()
  254. if event == "key" then
  255. if p1 == keys.up then
  256. if screenSelection == 1 then
  257. screenSelection = tablelength(screen["options"])
  258. else
  259. screenSelection = screenSelection - 1
  260. end
  261. elseif p1 == keys.down then
  262. if screenSelection == tablelength(screen["options"]) then
  263. screenSelection = 1
  264. else
  265. screenSelection = screenSelection + 1
  266. end
  267. end
  268. end
  269. end
  270.  
  271. function isSelctionSelected()
  272. if event == "key" then
  273. if p1 == keys.enter then
  274. tableAt(screen["options"], screenSelection - 1)["func"]()
  275. end
  276. end
  277. end
  278.  
  279. parseTableFavorites()
  280. startup()
  281.  
  282. while true do
  283. awaitEvent()
  284. isSelectionChanged()
  285. isSelctionSelected()
  286. state, engaged, direction = sg.stargateState()
  287. drawScreen(screen)
  288. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement