Advertisement
AlwayzPatty

Untitled

Nov 13th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 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 (x - string.len(text)) / 2
  67. end
  68.  
  69. function getCenterY()
  70. local _,y = term.getSize()
  71. return 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 tableAtKey(T, key, value)
  91. for _, v in pairs(T) do
  92. if v[key] == value then
  93. return v
  94. end
  95. end
  96. end
  97.  
  98. function startup()
  99. screenSelection = 1
  100. screen = {
  101. title = "Stargate Control Program",
  102. options = {
  103. {
  104. option = "Dial Favorite",
  105. func = dialFavoriteScreen
  106. },
  107. {
  108. option = "Dial Manually",
  109. func = dialManually
  110. },
  111. {
  112. option = "Exit",
  113. func = exitProgram
  114. }
  115. }
  116. }
  117. end
  118.  
  119. function dialFavoriteScreen()
  120. screenSelection = 1
  121. screen = {
  122. title = "Dial Favorites",
  123. options = {
  124. {
  125. option = "Add Favorite",
  126. func = function()
  127. if not fs.exists("favorites") then
  128. fs.open("favorites", "w").close()
  129. end
  130. f = fs.open("favorites","r")
  131. if f then
  132. local _, y = term.getSize()
  133.  
  134. Print("Name:", 1, y - 1)
  135. term.setCursorPos(1, y)
  136. term.clearLine()
  137. local name = read()
  138. drawScreen(screen)
  139.  
  140. Print("Address:", 1, y - 1)
  141. term.setCursorPos(1, y)
  142. term.clearLine()
  143. local gaddress = read()
  144. local firstLine = f.readLine()
  145. f.close()
  146. f = fs.open("favorites","a")
  147. if firstLine ~= "" or firstLine ~= nil then
  148. f.writeLine("\n\"" ..name.."\"{"..gaddress.."}")
  149. else
  150. f.writeLine("\"" ..name.."\"{"..gaddress.."}")
  151. end
  152. table.insert(favAddresses,
  153. {
  154. title = name,
  155. address = gaddress
  156. })
  157. f.close()
  158.  
  159. end
  160. dialFavoriteScreen()
  161. end
  162. }
  163. }
  164. }
  165.  
  166. for k, v in ipairs(favAddresses) do
  167. table.insert(screen["options"],
  168. {
  169. option = v["title"],
  170. func = dialFav
  171. })
  172. end
  173.  
  174. table.insert(screen["options"],
  175. {
  176. option = "Back",
  177. func = startup
  178. })
  179. end
  180.  
  181. function dialManually()
  182. screenSelection = 1
  183. screen = {
  184. title = "Manual Dial",
  185. options = {
  186. {
  187. option = "Enter",
  188. func = function()
  189. local _, y = term.getSize()
  190. Print("ADDRESS:", 1, y - 1)
  191. term.setCursorPos(1, y)
  192. dial(read())
  193. startup()
  194. end
  195. },
  196. {
  197. option = "Back",
  198. func = startup
  199. }
  200. }
  201. }
  202. end
  203.  
  204. function parseTableFavorites()
  205. if not fs.exists("favorites") then
  206. fs.open("favorites", "w").close()
  207. end
  208. f = fs.open("favorites", "r")
  209. if f then
  210. local text = f.readAll()
  211. for i in string.gmatch(text, "[^\r\n]+") do
  212. if i ~= "" then
  213. local title, address = nil
  214. local j = string.match(i, "%b\"\"")
  215. j = string.gsub(j, "%s$", "")
  216. j = string.gsub(j, "^%s", "")
  217. j = string.gsub(j, "\"", "")
  218. ftitle = j
  219.  
  220. local j = string.match(i, "%b{}")
  221. j = string.gsub(j, "%s$", "")
  222. j = string.gsub(j, "^%s", "")
  223. j = string.gsub(j, "{", "")
  224. j = string.gsub(j, "}", "")
  225. faddress = j
  226.  
  227. table.insert(favAddresses,
  228. {
  229. title = ftitle,
  230. address = faddress
  231. })
  232. end
  233. end
  234. f.close()
  235. end
  236. end
  237.  
  238. function dialFav()
  239. local destination = tableAt(favAddresses, screenSelection - 2)
  240. dial(destination["address"])
  241. startup()
  242. end
  243.  
  244. function dial(address)
  245. local _, err = sg.dial(address)
  246. if err == "targetBusy" then
  247. local _, y = term.getSize()
  248. Print("Stargate is in use!", 1, y)
  249. sleep(2)
  250. elseif sg.energyAvailable() < sg.energyToDial(address) then
  251. local _, y = term.getSize()
  252. Print("Stargate has not enough energy!", 1, y)
  253. sleep(2)
  254. end
  255. end
  256.  
  257. function exitProgram()
  258. os.reboot()
  259. end
  260.  
  261. function awaitEvent()
  262. event, p1, p2, p3 = os.pullEvent()
  263. end
  264.  
  265. function isSelectionChanged()
  266. if event == "key" then
  267. if p1 == keys.up then
  268. if screenSelection == 1 then
  269. screenSelection = tablelength(screen["options"])
  270. else
  271. screenSelection = screenSelection - 1
  272. end
  273. elseif p1 == keys.down then
  274. if screenSelection == tablelength(screen["options"]) then
  275. screenSelection = 1
  276. else
  277. screenSelection = screenSelection + 1
  278. end
  279. end
  280. end
  281. end
  282.  
  283. function checkForGateControls()
  284. if state ~= "Idle" and screen["title"] == "Stargate Control Program" then
  285. table.insert(screen["options"], 3,
  286. {
  287. option = "Close Gate",
  288. func = function()
  289. sg.disconnect()
  290. startup()
  291. end
  292. })
  293. end
  294. end
  295.  
  296. function isSelctionSelected()
  297. if event == "key" then
  298. if p1 == keys.enter then
  299. tableAt(screen["options"], screenSelection - 1)["func"]()
  300. elseif p1 == keys.backspace then
  301. local back = tableAtKey(screen["options"], "option", "Back")
  302. if back then
  303. back["func"]()
  304. end
  305. end
  306. end
  307. end
  308.  
  309. parseTableFavorites()
  310. startup()
  311.  
  312. while true do
  313. awaitEvent()
  314. isSelectionChanged()
  315. isSelctionSelected()
  316. state, engaged, direction = sg.stargateState()
  317. checkForGateControls()
  318. drawScreen(screen)
  319. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement