Light83

Stargate Lua (OpenComputer LanteaCraft)

Apr 19th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local internet = require("internet")
  4. local process = require("process")
  5. local event = require("event")
  6. local keyboard = require("keyboard")
  7. local shell = require("shell")
  8. local term = require("term")
  9. local text = require("text")
  10. local unicode = require("unicode")
  11. local sides = require("sides")
  12. local colors=require("colors")
  13.  
  14. local gml=require("gml")
  15.  
  16. local sg = component.stargate
  17. local gpu=component.gpu
  18. local w, h = gpu.getResolution()
  19. local fname = "GateInfo.txt"
  20. local filename = shell.resolve(fname)
  21. local connectAddress = ""
  22. local destAddress = {}
  23. local destName = {}
  24. local timerID = 0
  25. local fuelTimer = 0
  26. local running=process.running()
  27. local addressListAdd
  28. local addressListDelete
  29.  
  30. local function isAdvanced()
  31. return gpu.getDepth()
  32. end
  33.  
  34. local function strripos(s, delim)
  35. return s:match('^.*()'..delim)
  36. end
  37.  
  38. function loadAddressData()
  39. destName = {}
  40. destAddress = {}
  41. do
  42. local f = io.open(filename)
  43. if f then
  44. for line in f:lines() do
  45. line = text.trim(line)
  46. delimPos = strripos(line, ",")
  47. table.insert(destName, string.sub(line, 1, delimPos - 1))
  48. table.insert(destAddress, string.sub(line, delimPos + 1, string.len(line)))
  49. end
  50. f:close()
  51. end
  52. end
  53. if (#destName < 1) then
  54. destName = {" "}
  55. destAddress = {" "}
  56. end
  57. end
  58.  
  59. term.clear()
  60. local gui=gml.create("center", "center", 80, 25)
  61. gui.style=gml.loadStyle("cv.gss")
  62.  
  63. local function dialing()
  64. return tostring(sg.isDialing())
  65. end
  66.  
  67. local function connected()
  68. return tostring(sg.isConnected())
  69. end
  70.  
  71. local function fuelAvailable()
  72. return tostring(sg.hasFuel())
  73. end
  74.  
  75. local function valid(address)
  76. return "true"
  77. end
  78.  
  79. loadAddressData()
  80.  
  81. local addressListBoxGUI = gml.create("center", 18, 48, 16)
  82. local addressList=gui:addListBox(21, 7, 38, 8, destName)
  83. local addressLabel=gui:addLabel(19, 16, 41,"Destination: "..text.trim(destAddress[1]))
  84. local validLabel=gui:addLabel(19, 18, 41, "Valid: "..valid(text.trim(destAddress[1])))
  85.  
  86. local contentsLabel=gui:addLabel(5, 2, 15, "Gate Address:")
  87. local contentsActiveLabel=gui:addLabel(20, 2, 9, " ")
  88. contentsActiveLabel.text = text.trim(sg.getAddress())
  89. contentsActiveLabel["text-color"] = 0xFFFFFF
  90. contentsActiveLabel["text-background"] = 0x0000AA
  91. contentsActiveLabel:draw()
  92.  
  93. local fuelLabel=gui:addLabel(35, 2, 30, "Has Fuel:")
  94. local fuelLabelActive=gui:addLabel(48, 2, 7, "looking")
  95.  
  96. local dialingLabel=gui:addLabel(5, 4, 13,"Dialing: ")
  97. local dialingActiveLabel=gui:addLabel(20, 4, 7," ")
  98. local connectedLabel=gui:addLabel(35, 4, 17, "Connected: ")
  99. local connectedActiveLabel=gui:addLabel(48, 4, 7, " ")
  100.  
  101. local function hasFuel()
  102. if (text.trim(fuelAvailable()) == "false") then
  103. fuelLabelActive["text-color"] = 0xFFFFFF
  104. fuelLabelActive["text-background"] = 0xBB0000
  105. else
  106. fuelLabelActive["text-color"] = 0xFFFFFF
  107. fuelLabelActive["text-background"] = 0x009900
  108. end
  109. fuelLabelActive.text = fuelAvailable()
  110. fuelLabelActive:draw()
  111. end
  112.  
  113. function saveAddressData()
  114. do
  115. local f = io.open(filename, "w")
  116. if f then
  117. for a = 1, #destName do
  118. f:write(tostring(destName[a])..","..string.sub(tostring(destAddress[a]), 1, 9).."\n")
  119. end
  120. f:close()
  121. end
  122. end
  123. addressList:updateList(destName)
  124. end
  125.  
  126. local function cancelAddress()
  127. newAddressField.text = ""
  128. newGateAddressField.text = ""
  129. newAddressField:draw()
  130. newGateAddressField:draw()
  131. end
  132.  
  133. local function deleteAddress()
  134. for id, name in pairs(destName) do
  135. if text.trim(name) == text.trim(addressList:getSelected()) then
  136. addressID = id
  137. break
  138. end
  139. end
  140. if addressID ~= 0 then
  141. table.remove(destName, addressID)
  142. table.remove(destAddress, addressID)
  143. end
  144. addressList:updateList(destName)
  145. saveAddressData()
  146. end
  147.  
  148. local function setNewAddressData()
  149. if (string.len(text.trim(newAddressField.text)) > 1) then
  150. destName[#destName + 1] = text.trim(newAddressField.text)
  151. destAddress[#destAddress + 1] = text.trim(newGateAddressField.text)
  152. newAddressField.text = ""
  153. newGateAddressField.text = ""
  154. newAddressField:draw()
  155. newGateAddressField:draw()
  156. end
  157. saveAddressData()
  158. end
  159.  
  160. local function addAddress()
  161. gpu.setForeground(0xFFFFFF)
  162. gpu.setBackground(0x000000)
  163. term.clear()
  164. newAddressGUI = gml.create("center", "center", 50, 16)
  165. newAddressLabel=newAddressGUI:addLabel(2, 2, 13,"Address Name:")
  166. newAddressField=newAddressGUI:addTextField(15, 2, 30)
  167. newGateAddressLabel=newAddressGUI:addLabel(2, 4, 13,"Gate Address:")
  168. newGateAddressField=newAddressGUI:addTextField(15, 4, 10)
  169. newAddressSave=newAddressGUI:addButton(5, 6, 10, 1, "Save", setNewAddressData)
  170. newAddressCancel=newAddressGUI:addButton(20, 6, 10, 1, "Cancel", cancelAddress)
  171. newAddressClose=newAddressGUI:addButton(35, 6, 10, 1, "Close", newAddressGUI.close)
  172. newAddressGUI:run()
  173. term.clear()
  174. gui:draw()
  175. addressListBoxGUI:draw()
  176. addressList:draw()
  177. addressLabel:draw()
  178. addressListAdd:draw()
  179. addressListDelete:draw()
  180. validLabel:draw()
  181. dialingLabel:draw()
  182. dialingActiveLabel:draw()
  183. end
  184.  
  185. addressListAdd=gui:addButton(25, 20, 10, 1, "Add", addAddress)
  186. addressListDelete=gui:addButton(45, 20, 10, 1, "Delete", deleteAddress)
  187.  
  188. gui.onClose=function()
  189. event.cancel(fuelTimer)
  190. end
  191.  
  192. gui.onRun=function()
  193. fuelID = event.timer(20, hasFuel, math.huge)
  194. addressListBoxGUI:draw()
  195. addressList:draw()
  196. addressLabel:draw()
  197. addressListAdd:draw()
  198. addressListDelete:draw()
  199. validLabel:draw()
  200. dialingLabel:draw()
  201. dialingActiveLabel:draw()
  202. end
  203.  
  204. local function updateValues()
  205. event.cancel(timerID)
  206. connectedActiveLabel.text=string.sub(" "..connected().." ", 1, 7)
  207. dialingActiveLabel.text= string.sub(" "..dialing().." ", 1, 7)
  208.  
  209. if (text.trim(connected()) == "false") then
  210. connectedActiveLabel["text-color"] = 0xFFFFFF
  211. connectedActiveLabel["text-background"] = 0xBB0000
  212. else
  213. connectedActiveLabel["text-color"] = 0xFFFFFF
  214. connectedActiveLabel["text-background"] = 0x009900
  215. end
  216. if (text.trim(dialing()) == "false") then
  217. dialingActiveLabel["text-color"] = 0xFFFFFF
  218. dialingActiveLabel["text-background"] = 0xBB0000
  219. else
  220. dialingActiveLabel["text-color"] = 0xFFFFFF
  221. dialingActiveLabel["text-background"] = 0x009900
  222. end
  223.  
  224. connectedLabel:draw()
  225. connectedActiveLabel:draw()
  226. dialingLabel:draw()
  227. dialingActiveLabel:draw()
  228. end
  229.  
  230. local function updateDialAddress(info)
  231. local addressID = 0
  232. local addInfo = text.trim(info)
  233. connectAddress = ""
  234.  
  235. for id, name in pairs(destName) do
  236. if text.trim(name) == text.trim(addInfo) then
  237. addressID = id
  238. break
  239. end
  240. end
  241. if addressID ~= 0 then
  242. connectAddress = text.trim(destAddress[addressID])
  243. addressLabel.text="Address: "..destAddress[addressID]
  244. addressLabel:draw()
  245. end
  246. validLabel.text= "Valid: "..valid(connectAddress)
  247. validLabel:draw()
  248. end
  249.  
  250. local function disconnect()
  251. sg.disconnect()
  252. os.sleep(2)
  253. updateValues()
  254. end
  255.  
  256. local function dialGate()
  257. if (valid(connectAddress) == "true") then
  258. sg.dial(connectAddress)
  259. dialingActiveLabel["text-color"] = 0xFFFFFF
  260. dialingActiveLabel["text-background"] = 0x009900
  261. dialingActiveLabel.text= string.sub(" "..dialing().." ", 1, 7)
  262. dialingActiveLabel:draw()
  263. timerID = event.timer(26, updateValues, 1)
  264. end
  265. end
  266.  
  267. local function onAddressDial()
  268. updateDialAddress(addressList:getSelected())
  269. dialGate()
  270. end
  271.  
  272. local connect=gui:addButton(15,-1,10,1,"Connect",onAddressDial)
  273. local disconnect=gui:addButton(35,-1,10,1,"Disconnect", disconnect)
  274. local close=gui:addButton(55,-1,10,1,"Close",gui.close)
  275.  
  276. local function onAddressSelect(lb,prevIndex,selIndex)
  277. updateDialAddress(addressList:getSelected())
  278. end
  279.  
  280. addressList.onChange=onAddressSelect
  281.  
  282. addressList.onDoubleClick=function()
  283. updateDialAddress(addressList:getSelected())
  284. end
  285.  
  286. updateValues()
  287. gui:run()
  288.  
  289. gpu.setForeground(0xFFFFFF)
  290. gpu.setBackground(0x000000)
  291. term.clear()
Add Comment
Please, Sign In to add comment