Advertisement
CMastar

Stargate Control Public

Feb 8th, 2014
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.26 KB | None | 0 0
  1. function APILoader(apiName, apiPastebin) -- Used across several programs - but we can't use it from an API, because then how would we load it?
  2.         os.unloadAPI(apiName)
  3.         if fs.exists(apiName)
  4.         then
  5.                 os.loadAPI(apiName)
  6.         else
  7.                 shell.run("pastebin","get",apiPastebin,apiName)
  8.                 os.loadAPI(apiName)
  9.         end
  10. end
  11.  
  12.  
  13.  
  14. APILoader("commonFunctions","x5Ve1KSK")
  15. APILoader("gooey","KpJ8K6xm")
  16.  
  17. sleepTime = 5
  18. local gate
  19. local mon
  20.  
  21. function updateStatus()
  22.     -- Display for the monitor, showing basic status stuff. Limited by the limited functionallity of the gate.
  23.  
  24.     if gate.isDialing()
  25.     then
  26.         status = "Gate Dialing"
  27.     else if gate.isConnected()
  28.         then
  29.             status = "Connected"
  30.         else
  31.             status = "Inactive"
  32.         end
  33.     end
  34.  
  35.     if mon ~= nil
  36.     then
  37.         mon.clear()
  38.         mon.setCursorPos(1,1)
  39.         mon.write("Gate: " .. gate.getAddress())
  40.         mon.setCursorPos(1,2)
  41.         mon.write("Construction: ")
  42.         if gate.isComplete()
  43.         then
  44.             mon.write("complete")
  45.         else
  46.             mon.write("incomplete")
  47.         end
  48.         mon.setCursorPos(1,3)
  49.         mon.write("Status: " .. status)
  50.         mon.setCursorPos(1,4)
  51.         if gate.hasFuel()
  52.         then
  53.             mon.write("Fuel: OK")
  54.         else
  55.             mon.write("Fuel: None")
  56.         end
  57.     end
  58.  
  59. end
  60.  
  61. function Dial(address)
  62.     gate.dial(address)
  63.     term.clear()
  64.     term.setCursorPos(3,6)
  65.     term.write("Dialling " .. address)
  66.     updateStatus()
  67.     sleep(1)
  68. end
  69.  
  70. function Disconnect()
  71.     gate.disconnect()
  72.     term.clear()
  73.     term.setCursorPos(3,6)
  74.     term.write("Disconnecting")
  75.     updateStatus()
  76.     sleep(1)
  77. end
  78.  
  79. function AddGate()
  80.     term.clear()
  81.     term.setCursorPos(1,5)
  82.     term.write("Enter new gate name:")
  83.     gateName = read()
  84.     term.setCursorPos(1,6)
  85.     term.write("Enter new gate address:")
  86.     gateAddress = read()
  87.     term.setCursorPos(1,7)
  88.     if gate.isValidAddress
  89.     then
  90.         gateList[gateName] = gateAddress
  91.         term.write(gateAddress .. " added as " .. gateName)
  92.         term.setCursorPos(1,8)
  93.         term.write("Saving gate list")
  94.         commonFunctions.saveData(gateList, "gateList", false)
  95.     else
  96.         term.write(gateAddress .. " is not a valid address")
  97.     end
  98.     updateStatus()
  99.     sleep(2)
  100. end
  101.  
  102. function RemoveGate()
  103.     count = 0
  104.     gateItems = {}
  105.     for k, v in pairs(gateList) do
  106.         table.insert(gateItems,k)
  107.         count = count + 1
  108.     end
  109.     term.clear()
  110.     if count <= 0
  111.     then
  112.         print()
  113.         print("No gates to remove")
  114.     end
  115.     table.insert(gateItems,"Cancel")
  116.     menuResult = commonFunctions.menu(gateItems,"Select Gate to Remove")
  117.     term.clear()
  118.     print()
  119.     print("Removing " .. gateItems[menuResult] .. " from gate list")
  120.     gateList[gateItems[menuResult]] = nil
  121.     print("Saving new gate list")
  122.     commonFunctions.saveData(gateList, "gateList", false)
  123.     print("Done")
  124.     updateStatus()
  125.     sleep(2)
  126. end
  127.  
  128. function Wait() -- Keeps the secondary monitor up to date.
  129.     while true do
  130.         updateStatus()
  131.         sleep(sleepTime)
  132.     end
  133. end
  134.  
  135.  
  136.  
  137. function DisplayMenu()
  138.     while true do
  139.         menuItems = {}
  140.         table.insert(menuItems, {["text"] = "Disconnect", ["func"] = Disconnect})
  141.         table.insert(menuItems, {["text"] = "Settings", ["func"] = ShowSettings})
  142.         for gateName, gateAddress in pairs(gateList) do
  143.             table.insert(menuItems, {["text"] = ("Dial " .. gateName) , ["func"] = Dial, ["params"] = {gateAddress}})
  144.         end
  145.         if term.isColor()
  146.         then
  147.             gooey.clickMenu("Stargate Control",menuItems, term)
  148.         else
  149.             commonFunctions.menu(menuItems, "Stargate Control")
  150.         end
  151.     end
  152. end
  153.  
  154. function ShowSettings()
  155.     settingsItems = {
  156.                     {["text"] = ("Add new gate") , ["func"] = AddGate},
  157.                     {["text"] = ("Delete Gate") , ["func"] = RemoveGate},
  158.                     {["text"] = ("Download Gate List") , ["func"] = DownloadList},
  159.                     {["text"] = ("Upload Gate List") , ["func"] = UploadList},
  160.                     {["text"] = ("Change Stargate Used") , ["func"] = editPeripheral, ["params"] = {"stargate",gate}},
  161.                     {["text"] = ("Change Monitor Used") , ["func"] = editPeripheral, ["params"] = {"monitor",mon}},
  162.                     {["text"] = ("Main Menu"), ["func"] = sleep, ["params"] = 0} -- do nothing
  163.                     }
  164.     if term.isColor()
  165.     then
  166.         gooey.clickMenu("Settings Menu",settingsItems, term)
  167.     else
  168.         commonFunctions.menu(settingsItems,"Settings Menu")
  169.     end    
  170. end
  171.  
  172. function editPeripheral(peripheralType, wrappedPeripheral)
  173.     settings[peripheralType] = commonFunctions.selectPeripheral(peripheralType)
  174.     wrappedPeripheral = peripheral.wrap(settings[peripheralType])
  175.     commonFunctions.saveData(settings, "gateSettings", false)
  176.     term.clear()
  177.     print()
  178.     print("Set " .. peripheralType .. " at " .. settings[peripheralType])
  179.     updateStatus()
  180.     sleep(2)
  181. end
  182.  
  183. function UploadList()
  184.     term.clear()
  185.     print()
  186.     print("Uploading gate list to pastebin")
  187.     shell.run("pastebin","put","gateList")
  188.     print("Enter to continue")
  189.     read()
  190. end
  191.  
  192. function DownloadList()
  193.     term.clear()
  194.     print()
  195.     print("Enter pastebin code for list to download")
  196.     pastebinAddress = read()
  197.    
  198.     -- Make a backup, in case wrong/broken thing is downloaded
  199.     fs.delete("gateListBackup")
  200.     print("Backing up current Gate List as gateListBackup")
  201.     fs.copy("gateList","gateListBackup")
  202.     fs.delete("gateList")
  203.    
  204.     -- Get the new list and use it
  205.     shell.run("pastebin","get",pastebinAddress,"gateList")
  206.     print("Loading new stargate list")
  207.     print("If crashes occur, delete new gateList and revert to old (if any)")
  208.     gateList = commonFunctions.loadData("gateList")
  209.     if gateList == nil then
  210.         gateList = {}
  211.     end
  212.     print("Complete.")
  213.     updateStatus()
  214.     sleep(2)
  215. end
  216.    
  217.  
  218. function main()
  219.     -- License Stuff
  220.     print("Stargate Control RC1 by CMaster")
  221.     print("Controls a Lanteacraft stargate from a Computercraft computer")
  222.     print("Available to use by the Creactive Commons CC-BY-SA 4.0 License")
  223.     print("Basically, do what you want with it, provided you also use this license")
  224.     print("And include a credit to CMaster in it")
  225.     print("Bug reports on the Computercraft forum please")
  226.     -- Load up the stuff that needs loading
  227.    
  228.     print("Loading Settings")
  229.     settings = commonFunctions.loadData("gateSettings")
  230.     if settings == nil
  231.     then
  232.         settings = {}
  233.         print "No settings found. Beginning anew"
  234.     else
  235.         print "If the program crashes after this point, try deleting the file \"gateSettings\" and running again"
  236.     end
  237.    
  238.     -- Ensure that there is an attached stargate
  239.     if settings["stargate"] == nil or settings["stargate"] == "none"
  240.     then
  241.         print("No stargate saved. Auto-searching for stargates")
  242.         settings["stargate"] = commonFunctions.selectPeripheral("stargate")
  243.     end
  244.     while peripheral.getType(settings["stargate"]) ~= "stargate" do -- catches if for example the stargate has been removed or moved.
  245.         if settings["stargate"] == "none" or settings["stargate"] == nil then
  246.             return "Cannot continue - Attached Stargate required"
  247.         end
  248.         settings["stargate"] = commonFunctions.selectPeripheral("stargate")
  249.     end
  250.     print("Wrapping " ..settings["stargate"] .. " as connected gate" )
  251.     gate = peripheral.wrap(settings["stargate"])
  252.  
  253.     -- Sort out which monitor (if any) to use.
  254.     if settings["monitor"] == nil
  255.     then
  256.         print("No monitor saved. Auto-searching for monitors")
  257.         settings["monitor"] = commonFunctions.selectPeripheral("monitor")
  258.     else if peripheral.getType(settings["monitor"]) ~= "monitor" -- this could happen if monitor has been moved or removed.
  259.         then
  260.                 settings["monitor"] = commonFunctions.selectPeripheral("monitor")
  261.         end
  262.     end
  263.     if settings["monitor"] ~= nil and settings["monitor"] ~= "none"
  264.     then
  265.         print("Wrapping " ..settings["monitor"] .. " as connected monitor" )
  266.         mon = peripheral.wrap(settings["monitor"])
  267.         mon.write("Stargate Monitor")
  268.     else
  269.         print("No monitor used")
  270.     end
  271.    
  272.     -- Save all the changes (if any) made.
  273.     print("Saving settings")
  274.     commonFunctions.saveData(settings, "gateSettings", false)
  275.    
  276.     --[[ Feel free to add this commented block back in with your local gate list if you like
  277.     if not fs.exists("gateList")
  278.     then
  279.         print "Gate list not found. Downloading master."
  280.         shell.run("pastebin","get","Re9B7yGT","gateList")
  281.     end
  282.     ]]--
  283.     print("Loading gate list")
  284.     gateList = commonFunctions.loadData("gateList")
  285.    
  286.     if gateList == nil -- just making sure we can't have a crash as a consequence of this.
  287.     then
  288.         gateList = {}
  289.     end
  290.    
  291.     print("Initalization complete. Press enter to continue")
  292.     read()
  293.    
  294.     -- run the program
  295.     while true do
  296.         updateStatus()
  297.         parallel.waitForAny(DisplayMenu,Wait)
  298.     end
  299.     return("Got to the end somehow!")
  300. end
  301.  
  302. print(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement