Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function APILoader(apiName, apiPastebin) -- Used across several programs - but we can't use it from an API, because then how would we load it?
- os.unloadAPI(apiName)
- if fs.exists(apiName)
- then
- os.loadAPI(apiName)
- else
- shell.run("pastebin","get",apiPastebin,apiName)
- os.loadAPI(apiName)
- end
- end
- APILoader("commonFunctions","x5Ve1KSK")
- APILoader("gooey","KpJ8K6xm")
- sleepTime = 5
- local gate
- local mon
- function updateStatus()
- -- Display for the monitor, showing basic status stuff. Limited by the limited functionallity of the gate.
- if gate.isDialing()
- then
- status = "Gate Dialing"
- else if gate.isConnected()
- then
- status = "Connected"
- else
- status = "Inactive"
- end
- end
- if mon ~= nil
- then
- mon.clear()
- mon.setCursorPos(1,1)
- mon.write("Gate: " .. gate.getAddress())
- mon.setCursorPos(1,2)
- mon.write("Construction: ")
- if gate.isComplete()
- then
- mon.write("complete")
- else
- mon.write("incomplete")
- end
- mon.setCursorPos(1,3)
- mon.write("Status: " .. status)
- mon.setCursorPos(1,4)
- if gate.hasFuel()
- then
- mon.write("Fuel: OK")
- else
- mon.write("Fuel: None")
- end
- end
- end
- function Dial(address)
- gate.dial(address)
- term.clear()
- term.setCursorPos(3,6)
- term.write("Dialling " .. address)
- updateStatus()
- sleep(1)
- end
- function Disconnect()
- gate.disconnect()
- term.clear()
- term.setCursorPos(3,6)
- term.write("Disconnecting")
- updateStatus()
- sleep(1)
- end
- function AddGate()
- term.clear()
- term.setCursorPos(1,5)
- term.write("Enter new gate name:")
- gateName = read()
- term.setCursorPos(1,6)
- term.write("Enter new gate address:")
- gateAddress = read()
- term.setCursorPos(1,7)
- if gate.isValidAddress
- then
- gateList[gateName] = gateAddress
- term.write(gateAddress .. " added as " .. gateName)
- term.setCursorPos(1,8)
- term.write("Saving gate list")
- commonFunctions.saveData(gateList, "gateList", false)
- else
- term.write(gateAddress .. " is not a valid address")
- end
- updateStatus()
- sleep(2)
- end
- function RemoveGate()
- count = 0
- gateItems = {}
- for k, v in pairs(gateList) do
- table.insert(gateItems,k)
- count = count + 1
- end
- term.clear()
- if count <= 0
- then
- print()
- print("No gates to remove")
- end
- table.insert(gateItems,"Cancel")
- menuResult = commonFunctions.menu(gateItems,"Select Gate to Remove")
- term.clear()
- print()
- print("Removing " .. gateItems[menuResult] .. " from gate list")
- gateList[gateItems[menuResult]] = nil
- print("Saving new gate list")
- commonFunctions.saveData(gateList, "gateList", false)
- print("Done")
- updateStatus()
- sleep(2)
- end
- function Wait() -- Keeps the secondary monitor up to date.
- while true do
- updateStatus()
- sleep(sleepTime)
- end
- end
- function DisplayMenu()
- while true do
- menuItems = {}
- table.insert(menuItems, {["text"] = "Disconnect", ["func"] = Disconnect})
- table.insert(menuItems, {["text"] = "Settings", ["func"] = ShowSettings})
- for gateName, gateAddress in pairs(gateList) do
- table.insert(menuItems, {["text"] = ("Dial " .. gateName) , ["func"] = Dial, ["params"] = {gateAddress}})
- end
- if term.isColor()
- then
- gooey.clickMenu("Stargate Control",menuItems, term)
- else
- commonFunctions.menu(menuItems, "Stargate Control")
- end
- end
- end
- function ShowSettings()
- settingsItems = {
- {["text"] = ("Add new gate") , ["func"] = AddGate},
- {["text"] = ("Delete Gate") , ["func"] = RemoveGate},
- {["text"] = ("Download Gate List") , ["func"] = DownloadList},
- {["text"] = ("Upload Gate List") , ["func"] = UploadList},
- {["text"] = ("Change Stargate Used") , ["func"] = editPeripheral, ["params"] = {"stargate",gate}},
- {["text"] = ("Change Monitor Used") , ["func"] = editPeripheral, ["params"] = {"monitor",mon}},
- {["text"] = ("Main Menu"), ["func"] = sleep, ["params"] = 0} -- do nothing
- }
- if term.isColor()
- then
- gooey.clickMenu("Settings Menu",settingsItems, term)
- else
- commonFunctions.menu(settingsItems,"Settings Menu")
- end
- end
- function editPeripheral(peripheralType, wrappedPeripheral)
- settings[peripheralType] = commonFunctions.selectPeripheral(peripheralType)
- wrappedPeripheral = peripheral.wrap(settings[peripheralType])
- commonFunctions.saveData(settings, "gateSettings", false)
- term.clear()
- print()
- print("Set " .. peripheralType .. " at " .. settings[peripheralType])
- updateStatus()
- sleep(2)
- end
- function UploadList()
- term.clear()
- print()
- print("Uploading gate list to pastebin")
- shell.run("pastebin","put","gateList")
- print("Enter to continue")
- read()
- end
- function DownloadList()
- term.clear()
- print()
- print("Enter pastebin code for list to download")
- pastebinAddress = read()
- -- Make a backup, in case wrong/broken thing is downloaded
- fs.delete("gateListBackup")
- print("Backing up current Gate List as gateListBackup")
- fs.copy("gateList","gateListBackup")
- fs.delete("gateList")
- -- Get the new list and use it
- shell.run("pastebin","get",pastebinAddress,"gateList")
- print("Loading new stargate list")
- print("If crashes occur, delete new gateList and revert to old (if any)")
- gateList = commonFunctions.loadData("gateList")
- if gateList == nil then
- gateList = {}
- end
- print("Complete.")
- updateStatus()
- sleep(2)
- end
- function main()
- -- License Stuff
- print("Stargate Control RC1 by CMaster")
- print("Controls a Lanteacraft stargate from a Computercraft computer")
- print("Available to use by the Creactive Commons CC-BY-SA 4.0 License")
- print("Basically, do what you want with it, provided you also use this license")
- print("And include a credit to CMaster in it")
- print("Bug reports on the Computercraft forum please")
- -- Load up the stuff that needs loading
- print("Loading Settings")
- settings = commonFunctions.loadData("gateSettings")
- if settings == nil
- then
- settings = {}
- print "No settings found. Beginning anew"
- else
- print "If the program crashes after this point, try deleting the file \"gateSettings\" and running again"
- end
- -- Ensure that there is an attached stargate
- if settings["stargate"] == nil or settings["stargate"] == "none"
- then
- print("No stargate saved. Auto-searching for stargates")
- settings["stargate"] = commonFunctions.selectPeripheral("stargate")
- end
- while peripheral.getType(settings["stargate"]) ~= "stargate" do -- catches if for example the stargate has been removed or moved.
- if settings["stargate"] == "none" or settings["stargate"] == nil then
- return "Cannot continue - Attached Stargate required"
- end
- settings["stargate"] = commonFunctions.selectPeripheral("stargate")
- end
- print("Wrapping " ..settings["stargate"] .. " as connected gate" )
- gate = peripheral.wrap(settings["stargate"])
- -- Sort out which monitor (if any) to use.
- if settings["monitor"] == nil
- then
- print("No monitor saved. Auto-searching for monitors")
- settings["monitor"] = commonFunctions.selectPeripheral("monitor")
- else if peripheral.getType(settings["monitor"]) ~= "monitor" -- this could happen if monitor has been moved or removed.
- then
- settings["monitor"] = commonFunctions.selectPeripheral("monitor")
- end
- end
- if settings["monitor"] ~= nil and settings["monitor"] ~= "none"
- then
- print("Wrapping " ..settings["monitor"] .. " as connected monitor" )
- mon = peripheral.wrap(settings["monitor"])
- mon.write("Stargate Monitor")
- else
- print("No monitor used")
- end
- -- Save all the changes (if any) made.
- print("Saving settings")
- commonFunctions.saveData(settings, "gateSettings", false)
- --[[ Feel free to add this commented block back in with your local gate list if you like
- if not fs.exists("gateList")
- then
- print "Gate list not found. Downloading master."
- shell.run("pastebin","get","Re9B7yGT","gateList")
- end
- ]]--
- print("Loading gate list")
- gateList = commonFunctions.loadData("gateList")
- if gateList == nil -- just making sure we can't have a crash as a consequence of this.
- then
- gateList = {}
- end
- print("Initalization complete. Press enter to continue")
- read()
- -- run the program
- while true do
- updateStatus()
- parallel.waitForAny(DisplayMenu,Wait)
- end
- return("Got to the end somehow!")
- end
- print(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement