Advertisement
AlwayzPatty

Untitled

Nov 13th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 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", "w").close()
  132. end
  133. f = fs.open("favorites","r")
  134. if f then
  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. drawScreen(screen)
  142.  
  143. Print("Address:", 1, y - 1)
  144. term.setCursorPos(1, y)
  145. term.clearLine()
  146. local gaddress = read()
  147. if r.readLine() ~= "" then
  148. f.close()
  149. f = fs.open("favorites","a")
  150. f.writeLine("\n\"" ..name.."\"{"..gaddress.."}")
  151. else
  152. f.close()
  153. f = fs.open("favorites","a")
  154. f.writeLine("\"" ..name.."\"{"..gaddress.."}")
  155. end
  156. table.insert(favAddresses,
  157. {
  158. title = name,
  159. address = gaddress
  160. })
  161. f.close()
  162.  
  163. end
  164. dialFavoriteScreen()
  165. end
  166. }
  167. }
  168. }
  169.  
  170. for k, v in ipairs(favAddresses) do
  171. table.insert(screen["options"],
  172. {
  173. option = v["title"],
  174. func = dialFav
  175. })
  176. end
  177.  
  178. table.insert(screen["options"],
  179. {
  180. option = "Back",
  181. func = startup
  182. })
  183. end
  184.  
  185. function dialManually()
  186. screenSelection = 1
  187. screen = {
  188. title = "Manual Dial",
  189. options = {
  190. {
  191. option = "Enter",
  192. func = function()
  193. local _, y = term.getSize()
  194. Print("ADDRESS:", 1, y - 1)
  195. term.setCursorPos(1, y)
  196. dial(read())
  197. startup()
  198. end
  199. },
  200. {
  201. option = "Back",
  202. func = startup
  203. }
  204. }
  205. }
  206. end
  207.  
  208. function parseTableFavorites()
  209. if not fs.exists("favorites") then
  210. fs.open("favorites", "w").close()
  211. end
  212. f = fs.open("favorites", "r")
  213. if f then
  214. local text = f.readAll()
  215. for i in string.gmatch(text, "[^\r\n]+") do
  216. local title, address = nil
  217. local j = string.gmatch(i, "%b\"\"")
  218. j[0] = string.gsub(j[0], "%s$", "")
  219. j[0] = string.gsub(j[0], "^%s", "")
  220. j[0] = string.gsub(j[0], "\"", "")
  221. ftitle = j[0]
  222.  
  223. local j = string.gmatch(i, "%b{}")
  224. j[0] = string.gsub(j[0], "%s$", "")
  225. j[0] = string.gsub(j[0], "^%s", "")
  226. j[0] = string.gsub(j[0], "{", "")
  227. j[0] = string.gsub(j[0], "}", "")
  228. faddress = j[0]
  229.  
  230. table.insert(favAddresses,
  231. {
  232. title = ftitle,
  233. address = faddress
  234. })
  235. end
  236. f.close()
  237. end
  238. end
  239.  
  240. function dialFav()
  241. local destination = tableAt(favAddresses, screenSelection - 1)
  242. dial(destination["address"])
  243. startup()
  244. end
  245.  
  246. function dial(address)
  247. local _, err = sg.dial(address)
  248. if err == "targetBusy" then
  249. local _, y = term.getSize()
  250. Print("Stargate is in use!", 1, y)
  251. sleep(2)
  252. elseif sg.energyAvailable() < sg.energyToDial(address) then
  253. local _, y = term.getSize()
  254. Print("Stargate has not enough energy!", 1, y)
  255. sleep(2)
  256. end
  257. end
  258.  
  259. function exitProgram()
  260. os.reboot()
  261. end
  262.  
  263. function awaitEvent()
  264. event, p1, p2, p3 = os.pullEvent()
  265. if event == "sgStargateStateChange" then
  266. state, engaged, direction = sg.stargateState()
  267. drawScreen(screen)
  268. end
  269. end
  270.  
  271. function isSelectionChanged()
  272. if event == "key" then
  273. if p1 == keys.up then
  274. if screenSelection == 1 then
  275. screenSelection = tablelength(screen["options"])
  276. else
  277. screenSelection = screenSelection - 1
  278. end
  279. elseif p1 == keys.down then
  280. if screenSelection == tablelength(screen["options"]) then
  281. screenSelection = 1
  282. else
  283. screenSelection = screenSelection + 1
  284. end
  285. end
  286. end
  287. end
  288.  
  289. function isSelctionSelected()
  290. if event == "key" then
  291. if p1 == keys.enter then
  292. tableAt(screen["options"], screenSelection - 1)["func"]()
  293. end
  294. end
  295. end
  296.  
  297. parseTableFavorites()
  298. startup()
  299.  
  300. while true do
  301. awaitEvent()
  302. isSelectionChanged()
  303. isSelctionSelected()
  304. state, engaged, direction = sg.stargateState()
  305. drawScreen(screen)
  306. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement