Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sg = peripheral.find("stargate")
- local event, p1, p2, p3 = nil
- local screenSelection = 1
- local screen = nil
- local prevX, prevY = nil
- local state, engaged, direction = sg.stargateState()
- local favAddresses = {}
- function resetScreen()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- end
- function PrintInline(text, x, y)
- term.setCursorPos(prevX + x, prevY + y)
- term.write(text)
- prevX = prevX + x
- prevY = prevY + y
- end
- function Print(text, x, y)
- term.setCursorPos(x, y)
- term.clearLine()
- term.write(text)
- prevX = x
- prevY = y
- end
- function drawScreen(screenSettings)
- resetScreen()
- term.setBackgroundColor(colors.white)
- for i = 1, 3, 1 do
- term.setCursorPos(1, i)
- term.clearLine()
- end
- Print(screenSettings["title"], getCenterX(screenSettings["title"]), 2)
- term.setBackgroundColor(colors.gray)
- --For each option of a screen
- local line = getCenterY() - math.floor(tablelength(screenSettings["options"]) / 2)
- local curSel = 1
- for k, v in ipairs(screenSettings["options"]) do
- local label = v["option"]
- if curSel == screenSelection then
- term.setTextColor(colors.white)
- Print("[ ", getCenterX("[ " .. label .. " ]"), line)
- term.setTextColor(colors.black)
- PrintInline(label, string.len("[ "), 0, true)
- term.setTextColor(colors.white)
- PrintInline(" ]", string.len(label), 0, true)
- term.setTextColor(colors.black)
- else
- Print(label, getCenterX(label), line)
- end
- line = line + 1
- curSel = curSel + 1
- end
- end
- function getCenterX(text)
- local x,_ = term.getSize()
- return math.floor((x / 2) - (string.len(text) / 2))
- end
- function getCenterY()
- local _,y = term.getSize()
- return math.floor(y / 2)
- end
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function tableAt(T, index)
- local i = 0
- for _, v in pairs(T) do
- if i == index then
- return v
- end
- i = i + 1
- end
- end
- function startup()
- screenSelection = 1
- screen = {
- title = "Stargate Control Program",
- options = {
- {
- option = "Dial Favorite",
- func = dialFavoriteScreen
- },
- {
- option = "Dial Manually",
- func = dialManually
- },
- {
- option = "Exit",
- func = exitProgram
- }
- }
- }
- if state ~= "Idle" then
- table.insert(screen["options"], 3,
- {
- option = "Close Gate",
- func = function()
- sg.disconnect()
- startup()
- end
- })
- end
- end
- function dialFavoriteScreen()
- screenSelection = 1
- screen = {
- title = "Dial Favorites",
- options = {
- {
- option = "Add Favorite",
- func = function()
- if not fs.exists("favorites") then
- fs.open("favorites", "rw").close()
- end
- f = fs.open("favorites","ra")
- local _, y = term.getSize()
- Print("Name:", 1, y - 1)
- term.setCursorPos(1, y)
- term.clearLine()
- local name = read()
- Print("Address:", 1, y - 1)
- term.setCursorPos(1, y)
- term.clearLine()
- local address = read()
- if f.readLine() ~= nil then
- f.writeLine("\n\"" ..name.."\"{"..address.."}")
- else
- f.writeLine("\"" ..name.."\"{"..address.."}")
- end
- f.close()
- end
- }
- }
- }
- for k, v in ipairs(favAddresses) do
- table.insert(screen["options"],
- {
- option = v["title"],
- func = dialFav
- })
- end
- table.insert(screen["options"],
- {
- option = "Back",
- func = startup
- })
- end
- function dialManually()
- screenSelection = 1
- screen = {
- title = "Manual Dial",
- options = {
- {
- option = "Enter",
- func = function()
- local _, y = term.getSize()
- Print("ADDRESS:", 1, y - 1)
- term.setCursorPos(1, y)
- dial(read())
- startup()
- end
- },
- {
- option = "Back",
- func = startup
- }
- }
- }
- end
- function parseTableFavorites()
- f = fs.open("favorites", fs.exists("favorites") and "r" or "rw")
- local text = f.readAll()
- for i in string.gmatch(text, "[^\r\n]+") do
- local title, address = nil
- local j = string.gmatch(i, "%b\"\"")
- j[0] = string.gsub(j[0], "%s$", "")
- j[0] = string.gsub(j[0], "^%s", "")
- j[0] = string.gsub(j[0], "\"", "")
- ftitle = j[0]
- local j = string.gmatch(i, "%b{}")
- j[0] = string.gsub(j[0], "%s$", "")
- j[0] = string.gsub(j[0], "^%s", "")
- j[0] = string.gsub(j[0], "{", "")
- j[0] = string.gsub(j[0], "}", "")
- faddress = j[0]
- table.insert(favAddresses,
- {
- title = ftitle,
- address = faddress
- })
- end
- f.close()
- end
- function dialFav()
- local destination = tableAt(favAddresses, screenSelection - 1)
- dial(destination["address"])
- startup()
- end
- function dial(address)
- local _, err = sg.dial(address)
- if err == "targetBusy" then
- local _, y = term.getSize()
- Print("Stargate is in use!", 1, y)
- sleep(2)
- elseif sg.energyAvailable() < sg.energyToDial(address) then
- local _, y = term.getSize()
- Print("Stargate has not enough energy!", 1, y)
- sleep(2)
- end
- end
- function exitProgram()
- os.reboot()
- end
- function awaitEvent()
- event, p1, p2, p3 = os.pullEvent()
- if event == "sgStargateStateChange" then
- state, engaged, direction = sg.stargateState()
- end
- end
- function isSelectionChanged()
- if event == "key" then
- if p1 == keys.up then
- if screenSelection == 1 then
- screenSelection = tablelength(screen["options"])
- else
- screenSelection = screenSelection - 1
- end
- elseif p1 == keys.down then
- if screenSelection == tablelength(screen["options"]) then
- screenSelection = 1
- else
- screenSelection = screenSelection + 1
- end
- end
- end
- end
- function isSelctionSelected()
- if event == "key" then
- if p1 == keys.enter then
- tableAt(screen["options"], screenSelection - 1)["func"]()
- end
- end
- end
- parseTableFavorites()
- startup()
- while true do
- awaitEvent()
- isSelectionChanged()
- isSelctionSelected()
- state, engaged, direction = sg.stargateState()
- drawScreen(screen)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement