Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ LanteaCraft ]]--
- --[[ and SGCraft ]]--
- --[[ ccDHD Lite ]]--
- --[[ by Dog ]]--
- --[[ a k a ]]--
- --[[ HydrantHunter ]]--
- --[[ pastebin: ]]--
- --[[ W3AxUYJE ]]--
- local ccDHDliteVer = "1.5.02"
- --[[
- Tested with/requires:
- - Minecraft 1.7.10
- - LanteaCraft-1.7.10-70 || SGCraft1.95-mc1.7.10
- - ComputerCraft 1.73, 1.74, 1.75
- - An advanced computer
- - A stargate with a CC adapter
- ]]--
- local termX, termY = term.getSize()
- local gate, screen, currentGate, incomingAddress, assignColor, assignRating, drawRatingList, gateStateChange
- local kernelState, irisState, lcGate, gateChange, addrRedraw = false, false, false, false, false
- local gatePage, gatePages, logPage, logPages, chevronNumber = 1, 1, 1, 1, 0
- local callHistory, guiElements = { }, { }
- local addressBook = { { name = "NEW GATE", addr = "ADDRESS", rating = "U", iris = "none", note = "short note", loc = { x = 99999, y = 99999, z = 99999, dim = "Overworld", }, }, }
- local thisGate, gateStatus, gateTarget, cachedInput = "!!ERROR!!", "Idle", "No Target", ""
- local gateData, historyData = "/data/DHDgates", "/data/DHDhistory"
- --# custom read function courtesy of theoriginalbit (modified by Dog)
- local function read( _mask, _history, _limit, _noTerminate, _noMouse, gField )
- if _mask and type(_mask) ~= "string" then
- error("Invalid parameter #1: Expected string, got "..type(_mask), 2)
- end
- if _history and type(_history) ~= "table" then
- error("Invalid parameter #2: Expected table, got "..type(_history), 2)
- end
- if _limit and type(_limit) ~= "number" then
- error("Invalid parameter #3: Expected number, got "..type(_limit), 2)
- end
- if _noTerminate and type(_noTerminate) ~= "boolean" then
- error("Invalid argument #4: Expected boolean, got "..nativeType(_noTerminate), 2)
- end
- if _noMouse and type(_noMouse) ~= "boolean" then
- error("Invalid argument #5: Expected boolean, got "..nativeType(_noMouse), 2)
- end
- if gField and type(gField) ~= "boolean" then
- error("Invalid argument #6: Expected boolean, got "..nativeType(gField), 2)
- end
- term.setCursorBlink(true)
- local symbols = { ["A"] = true, ["B"] = true, ["C"] = true, ["D"] = true, ["E"] = true , ["F"] = true, ["G"] = true, ["H"] = true, ["I"] = true, ["J"] = true, ["K"] = true, ["L"] = true, ["M"] = true, ["N"] = true, ["O"] = true, ["P"] = true, ["Q"] = true, ["R"] = true, ["S"] = true, ["T"] = true, ["U"] = true, ["V"] = true, ["W"] = true, ["X"] = true, ["Y"] = true, ["Z"] = true, ["0"] = true, ["1"] = true, ["2"] = true, ["3"] = true, ["4"] = true, ["5"] = true, ["6"] = true, ["7"] = true, ["8"] = true, ["9"] = true, ["-"] = true, ["+"] = true }
- local input = ""
- local pos = 0
- local historyPos = nil
- local pullEvent = _noTerminate and os.pullEventRaw or os.pullEvent
- local sw, sh = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw( _special )
- local scroll = (sx + pos >= sw and (sx + pos) - sw or 0)
- local replace = _special or _mask
- local output = replace and (string.rep( replace, math.ceil(#input / #replace) - scroll )):sub(1, #input) or input:sub(scroll + 1)
- term.setCursorPos( sx, sy )
- term.write( output )
- term.setCursorPos( sx + pos - scroll, sy )
- end
- local nativeScroll = term.scroll
- term.scroll = function( _n ) local ok, err = pcall( function() return nativeScroll( _n ) end ) if ok then sy = sy - _n return err end error( err, 2 ) end
- while true do
- local event, code, mX, mY = pullEvent()
- if event == "char" and (not _limit or #input < _limit) then
- local goodData = false
- if gField then
- if symbols[code:upper()] then
- goodData = true
- end
- else
- goodData = true
- end
- if goodData then
- input = input:sub(1, pos) .. code .. input:sub(pos + 1)
- pos = pos + 1
- cachedInput = input
- end
- elseif event == "paste" and (not _limit or #input < _limit) then
- if _limit and #input + #code > _limit then
- code = code:sub(1, _limit - #input)
- end
- if gField then
- local newWord, glyph = { }, ""
- for i = 1, #code do
- glyph = string.upper(code:sub(i, i))
- if symbols[glyph] then
- newWord[i] = glyph
- else
- newWord[i] = "?"
- end
- end
- code = table.concat(newWord)
- end
- input = input:sub(1, pos) .. code .. input:sub(pos + 1)
- pos = pos + #code
- cachedInput = input
- elseif event == "key" then
- if code == keys.enter or code == keys.numPadEnter then
- cachedInput = ""
- break
- elseif code == keys.backspace and pos > 0 then
- redraw(' ')
- input = input:sub(1, math.max(pos - 1, 0))..input:sub(pos + 1)
- pos = math.max(pos - 1, 0)
- cachedInput = input
- elseif code == keys.delete and pos < #input then
- redraw(' ')
- input = input:sub(1, pos)..input:sub(pos + 2)
- cachedInput = input
- elseif code == keys.home then
- pos = 0
- elseif code == keys["end"] then
- pos = #input
- elseif code == keys.left and pos > 0 then
- pos = math.max(pos - 1, 0)
- elseif code == keys.right and pos < #input then
- pos = math.min(pos + 1, #input)
- elseif _history and code == keys.up or code == keys.down then
- redraw(' ')
- if code == keys.up then
- if not historyPos then
- historyPos = #_history
- elseif historyPos > 1 then
- historyPos = historyPos - 1
- end
- else
- if historyPos ~= nil and historyPos < #_history then
- historyPos = math.max(historyPos + 1, #_history)
- elseif historyPos == #_history then
- historyPos = nil
- end
- end
- if historyPos and #_history > 0 then
- input = string.sub(_history[historyPos], 1, _limit) or ""
- pos = #input
- cachedInput = input
- else
- input = ""
- pos = 0
- cachedInput = input
- end
- end
- elseif event == "mouse_click" and ((mX < sx or mX >= sx + _limit) or (mY ~= sy)) and not _noMouse then
- cachedInput = ""
- break
- end
- redraw(_mask)
- end
- term.scroll = nativeScroll
- term.setCursorBlink(false)
- if sy + 1 > sh then
- term.scroll(sy + 1 - sh)
- term.setCursorPos(1, sy)
- else
- term.setCursorPos(1, sy + 1)
- end
- return input
- end
- --# newButton function courtesy of theoriginalbit (modified by Dog)
- local function newButton(x, y, w, h, text, bc, tc, action, name, mbtn, callback)
- w = math.max(type(w) == "number" and w or 1, 1)
- h = math.max(type(h) == "number" and h or 1, 1)
- mbtn = (type(mbtn) == "number" and mbtn >= 1 and mbtn <= 3) and mbtn or 1
- action = action or function() end
- callback = type(callback) == "function" and callback or function() end
- name = (type(name) == "string" and #name > 0) and name or "noName"
- local hx = x + math.ceil((w - #text) / 2)
- local hy = y + math.ceil(h / 2) - 1
- local enabled = true
- local dbc, dtc = colors.lightGray, colors.gray
- return {
- getType = function()
- return "button"
- end;
- getName = function()
- return name
- end;
- setDisabledColors = function(bc, tc)
- dbc = bc
- dtc = tc
- end;
- setText = function(t)
- text = t or ""
- hx = x + math.ceil((w - #text) / 2) --# recalculate offset
- end;
- setEnabled = function(e)
- enabled = e == true
- end;
- render = function()
- term.setBackgroundColor(enabled and bc or dbc)
- term.setTextColor(enabled and tc or dtc)
- for i = 0, h - 1 do
- term.setCursorPos(x, y + i)
- write(string.rep(' ', w))
- end
- term.setCursorPos(hx, hy)
- write(text)
- end;
- processEvent = function(event, button, xPos, yPos)
- if enabled and event == "mouse_click" and button == mbtn and
- xPos >= x and xPos <= (x + w - 1) and
- yPos >= y and yPos <= (y + h - 1) then
- local modeSwitch = action()
- --callback(name)
- if modeSwitch then return true else return false end
- end
- end;
- }
- end
- local function saveData(fileName)
- fileName = fileName or gateData
- if fs.exists("/disk") and fileName == ("/disk/data/DHDgates") then
- if not fs.exists("/disk/data") then fs.makeDir("/disk/data") end
- end
- local dhdData = fs.open(fileName, "w")
- dhdData.write(textutils.serialize(addressBook))
- if fileName == gateData then gateChange = false end
- dhdData.close()
- end
- local function ingestData(fileName)
- if fileName == "logs" then
- if fs.exists(historyData) then
- for log_entry in io.lines(historyData) do
- table.insert(callHistory, 1, log_entry)
- end
- end
- logPage, logPages = 1, math.max(1, math.ceil(#callHistory / 11)) --# paginate call logs
- else
- fileName = fileName or gateData
- if fs.exists(fileName) then
- for i = #addressBook, 1, -1 do
- addressBook[i] = nil
- end
- local dhdData = fs.open(fileName, "r")
- addressBook = textutils.unserialize(dhdData.readAll())
- dhdData.close()
- gatePage, gatePages = 1, math.ceil(#addressBook / 21) --# paginate address book
- for i = 1, #addressBook do
- if not addressBook[i].iris then
- addressBook[i].iris = "none"
- saveAB = true
- end
- end
- if saveAB and fileName == gateData then saveData(gateData) end
- if fileName ~= gateData then gateChange = true end
- end
- end
- end
- local function mergeData() --# merge two address books into one
- if not fs.exists("/disk/data/DHDgates") then return end
- local dhdData = fs.open("/disk/data/DHDgates", "r")
- local newGates = textutils.unserialize(dhdData.readAll())
- dhdData.close()
- local matchFound = false --# use matchFound to indicate if a matching gate was found or not
- for i = 1, #newGates do --# start cycling through the 'new' list of gates
- for j = 1, #addressBook do --# search the address book for a matching address
- if newGates[i].addr == addressBook[j].addr then --# if the gate is already in the address book...
- matchFound = true --# ...set matchFound to true and...
- break --# stop the address book search loop and move on to the next gate in the 'new' list
- end
- end
- if not matchFound then --# if a match wasn't found...
- addressBook[#addressBook + 1] = { } --# initialize a new address book entry
- for k, v in pairs(newGates[i]) do --# loop through the gate entries...
- addressBook[#addressBook][k] = v --# ...and add them to the new address book entry
- end
- gateChange = true --# indicate that address book data has changed
- else
- matchFound = false --# if a match was found, reset the variable for the next iteration of the loop
- end
- end
- gatePages = math.ceil(#addressBook / 24) --# repaginate gates
- end
- local function recordSessionData() --# Human readable log files
- local logAddress = gateTarget or "ERROR" --# set logAddress
- logAddress = #logAddress == 7 and logAddress .. thisGate:sub(8, 9) or logAddress --# if logAddress is 7 characters (it is in the same dimension) add thisGate's dimension to the end for logging
- local callDirection = incomingAddress and "Inbound" or "Outbound" --# set callDirection
- local tTime = textutils.formatTime(os.time(), false) --# format the time
- if #tTime == 7 then tTime = " " .. tTime end --# move single digit hour times to the right one space
- local callArchive = fs.open(historyData, fs.exists(historyData) and "a" or "w")
- callArchive.writeLine(tostring(os.day()) .. " @ " .. tTime .. " <" .. callDirection .. "> " .. logAddress)
- callArchive.close()
- end
- do
- local colorBurst = {
- B = { colors.blue, "Base/Outpost/Hub" },
- H = { colors.lightBlue, "Home/Camp" },
- V = { colors.brown, "Village" },
- M = { colors.purple, "Misc/Special" },
- S = { colors.green, "Safe/Secured" },
- C = { colors.orange, "Caution" },
- D = { colors.red, "Danger" },
- U = { colors.lightGray, "Unknown/Unclassified" }
- }
- assignColor = function(gateNumber)
- return colorBurst[addressBook[gateNumber].rating][1]
- end
- assignRating = function(gateNumber)
- return colorBurst[addressBook[gateNumber].rating][2]
- end
- end
- local function clearCallHistory()
- for i = #callHistory, 1, -1 do
- callHistory[i] = nil
- end
- end
- local function clearScreen()
- term.setBackgroundColor(colors.black)
- term.clear()
- end
- local function clearLowerScreen(bgColor)
- --paintutils.drawFilledBox(1, 4, termX, termY, bgColor or colors.black)
- term.setBackgroundColor(bgColor or colors.black)
- local line = string.rep(" ", termX)
- for i = 4, termY do --# clear the display area
- term.setCursorPos(1, i)
- term.write(line)
- end
- end
- local function clearDataArea(bgColor)
- --paintutils.drawFilledBox(17, 5, termX, termY, bgColor or colors.black)
- term.setBackgroundColor(bgColor or colors.black)
- local line = string.rep(" ", 33)
- for i = 5, termY - 1 do --# clear the data area
- term.setCursorPos(17, i)
- term.write(line)
- end
- end
- local function clearABPopUp()
- --paintutils.drawFilledBox(15, 10, 35, 17, colors.black)
- term.setBackgroundColor(colors.black)
- local line = string.rep(" ", 21)
- for i = 10, 17 do
- term.setCursorPos(15, i)
- term.write(line)
- end
- end
- local function clearExodusPopUp()
- --paintutils.drawFilledBox(20, 9, 38, 12, colors.black)
- term.setBackgroundColor(colors.black)
- local line = string.rep(" ", 19)
- for i = 9, 12 do
- term.setCursorPos(20, i)
- term.write(line)
- end
- end
- local function clearPagePopUp(bgColor)
- --paintuitls.drawFilledBox(screen == "main" and 29 or 22, termY - 3, screen == "main" and 37 or 30, termY - 1, bgColor or colors.black)
- term.setBackgroundColor(bgColor or colors.black)
- for i = 1, 3 do
- term.setCursorPos(screen == "main" and 29 or 22, (termY - 4) + i)
- term.write(" ")
- end
- end
- local function redrawInput()
- term.setCursorPos(screen == "gatePage" and 31 or 24, termY - 2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(cachedInput)
- end
- local function highlightAddressButton()
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.lime)
- term.setCursorPos(2, 13)
- term.write("Addr Book")
- end
- local function drawIrisSwitch(switchPos)
- term.setCursorPos(31, 5)
- if switchPos == "none" then
- term.setBackgroundColor(colors.green)
- term.write(" ")
- term.setBackgroundColor(colors.gray)
- term.write(" ")
- term.setBackgroundColor(colors.red)
- term.write(" ")
- elseif switchPos == "open" then
- term.setBackgroundColor(colors.green)
- term.write(" ")
- term.setBackgroundColor(colors.gray)
- term.write(" ")
- elseif switchPos == "close" then
- term.setBackgroundColor(colors.gray)
- term.write(" ")
- term.setBackgroundColor(colors.red)
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- end
- local function drawClearLogsPopUp()
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.setCursorPos((termX / 2) - 5, math.floor(termY / 2) - 2)
- term.write(" Clear Logs? ")
- --paintutils.drawFilledBox((termX / 2) - 5, math.floor(termY / 2) - 1, (termX / 2) + 7, math.floor(termY /2 ) + 1, colors.gray)
- term.setBackgroundColor(colors.gray)
- local line = string.rep(" ", 13)
- for i = math.floor(termY / 2) - 1, math.floor(termY / 2) + 1 do
- term.setCursorPos((termX / 2) - 5, i)
- term.write(line)
- end
- term.setBackgroundColor(colors.green)
- term.setCursorPos((termX / 2) - 4, math.floor(termY / 2))
- term.write(" YES ")
- term.setBackgroundColor(colors.red)
- term.setCursorPos((termX / 2) + 2, math.floor(termY / 2))
- term.write(" N O ")
- end
- local function drawExodusPopUp()
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.setCursorPos(20, 9)
- term.write(" Save Addr Book? ")
- --paintutils.drawFilledBox(20, 10, 38, 12, colors.gray)
- term.setBackgroundColor(colors.gray)
- local line = string.rep(" ", 19)
- for i = 10, 12 do
- term.setCursorPos(20, i)
- term.write(line)
- end
- term.setBackgroundColor(colors.green)
- term.setCursorPos(22, 11)
- term.write(" Save ")
- term.setBackgroundColor(colors.orange)
- term.setCursorPos(31, 11)
- term.write(" Quit ")
- end
- local function drawAddressBookManagementPopUp()
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.setCursorPos(16, 10)
- term.write(" Address Book Mgt. ")
- --paintutils.drawFilledBox(16, 11, 34, 17, colors.gray)
- term.setBackgroundColor(colors.gray)
- local line = string.rep(" ", 19)
- for i = 11, 17 do
- term.setCursorPos(16, i)
- term.write(line)
- end
- for _, button in pairs(guiElements.addrBookMgt) do
- if button.getName() == "ABSave" then
- button.setEnabled(gateChange)
- elseif button.getName() == "ABMerge" then
- button.setEnabled(fs.exists("/disk/data/DHDgates"))
- elseif button.getName() == "ABImport" then
- button.setEnabled(fs.exists("/disk/data/DHDgates"))
- elseif button.getName() == "ABExport" then
- button.setEnabled(fs.exists("/disk"))
- end
- button.render()
- end
- end
- local function drawPagePopUp()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- for i = 1, 3 do
- term.setCursorPos(screen == "gatePage" and 29 or 22, termY - 4 + i)
- term.write(i == 1 and " :Page: " or " ")
- end
- term.setBackgroundColor(colors.black)
- term.setCursorPos(screen == "gatePage" and 30 or 23, termY - 2)
- term.write(" ")
- end
- local function drawHeader()
- term.setTextColor(colors.white)
- local line = string.rep(" ", 48)
- for i = 1, 3 do
- term.setCursorPos(1, i)
- term.setBackgroundColor(colors.blue)
- term.write(line)
- term.setBackgroundColor(colors.red)
- term.write(i == 2 and " X " or " ")
- end
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.lightBlue)
- term.setCursorPos(22, 2)
- term.write(thisGate)
- end
- local function drawFooter()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- term.setCursorPos(screen == "main" and 13 or 1, termY)
- term.write(string.rep(" ", screen == "main" and termX - 12 or termX))
- local cX = screen == "main" and (math.ceil(termX / 2) - (#tostring(gatePage) + #tostring(gatePages) + 4) / 2) + 7 or (math.ceil(termX / 2) - (#tostring(logPage) + #tostring(logPages) + 4) / 2)
- term.setCursorPos(cX, termY)
- term.write(screen == "main" and (tostring(gatePage) .. " of " .. tostring(gatePages)) or (tostring(logPage) .. " of " .. tostring(logPages)))
- for _, button in pairs(screen == "main" and guiElements.mainFooter or guiElements.logButtons) do
- button.render()
- end
- end
- local function drawHelpScreen()
- clearLowerScreen(colors.white)
- --paintutils.drawFilledBox(1, 4, 15, termY, colors.gray)
- term.setBackgroundColor(colors.gray)
- local line = string.rep(" ", 15)
- for i = 4, termY do
- term.setCursorPos(1, i)
- term.write(line)
- end
- term.setTextColor(colors.black)
- term.setCursorPos(2, 5)
- term.write("Main Screen")
- term.setCursorPos(2, 10)
- term.write("Addr Book Btn")
- term.setCursorPos(4, 12)
- term.write("(popup)")
- term.setCursorPos(2, 16)
- term.write("Log Screen")
- term.setTextColor(colors.lightGray)
- term.setCursorPos(2, 6)
- term.write("Left click")
- term.setCursorPos(2, 7)
- term.write("Right click")
- term.setCursorPos(2, 8)
- term.write("Middle click")
- term.setCursorPos(2, 11)
- term.write("Right click")
- term.setCursorPos(2, 13)
- term.write("Import/Export")
- term.setCursorPos(2, 14)
- term.write("Merge! Button")
- term.setCursorPos(2, 17)
- term.write("Left click")
- term.setCursorPos(2, 18)
- term.write("Right click")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(17, 6)
- term.write("an address to dial")
- term.setCursorPos(17, 7)
- term.write("an address to view/edit")
- term.setCursorPos(17, 8)
- term.write("an address to delete")
- term.setCursorPos(17, 11)
- term.write("Save/update the Address Book")
- term.setCursorPos(17, 13)
- term.write("Copy Addr Book FROM/TO floppy disk")
- term.setCursorPos(17, 14)
- term.write("Import only new gates from floppy")
- term.setCursorPos(17, 17)
- term.write("an address to dial")
- term.setCursorPos(17, 18)
- term.write("an address to add to Address Book")
- end
- local function drawLogScreen()
- if logPage == logPages and logPages > 1 then
- clearLowerScreen(colors.white)
- end
- if not callHistory[1] then
- term.setCursorPos(2, 6)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.write("No Logs")
- for _, button in pairs(guiElements.logButtons) do
- button.setEnabled(false)
- end
- return
- end
- term.setBackgroundColor(colors.gray) --#
- term.setTextColor(colors.white) --#
- term.setCursorPos(1, 4) --# Header
- term.write(string.rep(" ", termX)) --#
- term.setCursorPos(1, 5) --#
- term.write(" Day Time Vector Address ") --#
- for _, button in pairs(guiElements.logButtons) do
- button.setEnabled(true)
- end
- drawFooter()
- term.setBackgroundColor(colors.white)
- local currentEntry = ((logPage - 1) * 11) + 1 --# Set the first entry to show (based on page number)
- local spacer = string.rep(" ", 9)
- for i = currentEntry, currentEntry + 10 do --# Display logs
- local callDir = callHistory[i]:sub(string.find(callHistory[i], "<") + 1, string.find(callHistory[i], ">") - 1)
- term.setTextColor(callDir == "Inbound" and colors.gray or colors.lightGray)
- term.setCursorPos(2, i - currentEntry + 7) --# position entry
- term.write(callHistory[i]:sub(1, string.find(callHistory[i], "@") - 2) .. " ") --# write day
- term.setCursorPos(9, i - currentEntry + 7) --# position entry
- term.write(callHistory[i]:sub(string.find(callHistory[i], "@") + 2, string.find(callHistory[i], "M"))) --# write time
- term.setCursorPos(20, i - currentEntry + 7) --# position entry
- term.write(callDir .. " ") --# write direction
- term.setCursorPos(31, i - currentEntry + 7) --# position entry
- local callAddr = callHistory[i]:sub(string.find(callHistory[i], ">") + 2) --# set address
- term.setTextColor(callDir == "Inbound" and colors.cyan or colors.lightBlue)
- term.write(callAddr .. spacer) --# write address (and clear space for truncated entries)
- if i >= #callHistory then break end --# if we've reached the end of the log then stop the loop
- end
- end
- do
- local ratings = {
- [1] = { rating = "B", color = colors.blue, desc = "Base/Outpost/Hub" },
- [2] = { rating = "H", color = colors.lightBlue, desc = "Home/Camp" },
- [3] = { rating = "V", color = colors.brown, desc = "Village" },
- [4] = { rating = "M", color = colors.purple, desc = "Misc/Special" },
- [5] = { rating = "S", color = colors.green, desc = "Safe/Secured" },
- [6] = { rating = "C", color = colors.orange, desc = "Caution" },
- [7] = { rating = "D", color = colors.red, desc = "Danger" },
- [8] = { rating = "U", color = colors.lightGray, desc = "Unk/Unclassified" },
- }
- drawRatingList = function(gRating)
- local rColor
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.blue)
- term.setCursorPos(18, 6)
- term.write(" Classification ")
- --paintutils.drawFilledBox(18, 7, 37, 16, colors.gray)
- term.setBackgroundColor(colors.gray)
- local line = string.rep(" ", 20)
- for i = 7, 16 do --# menu body
- term.setCursorPos(18, i)
- term.write(line)
- end
- for i = 1, #ratings do
- if gRating == ratings[i].rating then
- rColor = gRating == "U" and colors.white or ratings[i].color
- term.setBackgroundColor(ratings[i].color)
- term.setCursorPos(37, i + 7)
- term.write(" ") --# current rating color pip
- else
- rColor = colors.lightGray
- end
- term.setBackgroundColor(ratings[i].color)
- term.setCursorPos(18, i + 7)
- term.write(" ") --# color pip
- term.setBackgroundColor(colors.gray)
- term.setTextColor(rColor)
- term.setCursorPos(20, i + 7)
- term.write(ratings[i].desc)
- end
- end
- end
- local function drawGateData()
- term.setTextColor(colors.yellow)
- term.setCursorPos(11, 5)
- term.write(addressBook[currentGate].name .. string.rep(" ", 9 - #addressBook[currentGate].name))
- term.setCursorPos(13, 13)
- term.write(addressBook[currentGate].loc.dim .. string.rep(" ", 38 - #addressBook[currentGate].loc.dim))
- local gateNum = tostring(currentGate)
- term.setCursorPos(termX - 6, 5)
- term.setTextColor(colors.white)
- term.write(gateNum .. string.rep(" ", 6 - #gateNum))
- term.setCursorPos(8, 9)
- term.write(addressBook[currentGate].note:sub(1, 43) .. string.rep(" ", 43 - #addressBook[currentGate].note:sub(1, 43)))
- term.setTextColor(assignColor(currentGate))
- term.setCursorPos(11, 7)
- term.write(addressBook[currentGate].addr .. " ")
- local ratingWord = assignRating(currentGate)
- term.setCursorPos(18, 11)
- term.write(ratingWord .. string.rep(" ", 14))
- drawIrisSwitch(addressBook[currentGate].iris)
- term.setCursorPos(25, 7)
- if addressBook[currentGate].addr == thisGate or addressBook[currentGate].addr == thisGate:sub(1, 7) then
- term.setTextColor(colors.gray)
- term.write("< This Gate >")
- else
- term.write(" ")
- end
- term.setTextColor(colors.lightGray)
- term.setCursorPos(5, 15)
- term.write(tostring(addressBook[currentGate].loc.x) .. string.rep(" ", 9 - #tostring(addressBook[currentGate].loc.x)))
- term.setCursorPos(19, 15)
- term.write(tostring(addressBook[currentGate].loc.y) .. string.rep(" ", 9 - #tostring(addressBook[currentGate].loc.y)))
- term.setCursorPos(33, 15)
- term.write(tostring(addressBook[currentGate].loc.z) .. string.rep(" ", 9 - #tostring(addressBook[currentGate].loc.z)))
- end
- local function drawGateLabels()
- clearLowerScreen()
- term.setTextColor(colors.gray)
- term.setCursorPos(2, 5)
- term.write("Name: Iris:")
- term.setCursorPos(termX - 8, 5)
- term.write("# ")
- term.setCursorPos(2, 7)
- term.write("Address:")
- term.setCursorPos(2, 9)
- term.write("Note:")
- term.setCursorPos(2, 11)
- term.write("Classification:")
- term.setCursorPos(2, 13)
- term.write("Dimension:")
- term.setCursorPos(2, 15)
- term.write("x: y: z:")
- drawGateData()
- end
- local function drawAddressList()
- local xPos, yPos = 17, 5
- local magicNumber = ((gatePage - 1) * 20) + gatePage --# 21 gates/page
- for i = magicNumber, #addressBook do
- if addressBook[i].addr == thisGate or addressBook[i].addr == thisGate:sub(1, 7) then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- elseif gateTarget ~= "No Target" and ((addressBook[i].addr == gateTarget) or (#gateTarget == 7 and addressBook[i].addr:sub(1, 7) == gateTarget and addressBook[i].addr:sub(8, 9) == thisGate:sub(8, 9)) or (#addressBook[i].addr == 7 and addressBook[i].addr == gateTarget:sub(1, 7) and gateTarget:sub(8, 9) == thisGate:sub(8, 9))) then
- term.setBackgroundColor(assignColor(i))
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(assignColor(i))
- term.setTextColor(colors.white)
- end
- term.setCursorPos(xPos, yPos)
- term.write(string.rep(" ", math.floor((9 - #addressBook[i].name) / 2)) .. addressBook[i].name .. string.rep(" ", math.ceil((9 - #addressBook[i].name) / 2)))
- yPos = yPos + 2
- if yPos >= termY - 1 then xPos = xPos + 11 yPos = 5 end
- if xPos >= termX - 10 then break end
- end
- addrRedraw = false
- end
- local function drawIrisStatus()
- paintutils.drawLine(10, 9, 11, 9, irisState and colors.lime or colors.orange)
- end
- local function drawDHDStatus()
- local fuelState, fuelPercent
- if lcGate then
- fuelState = gate.hasFuel()
- term.setBackgroundColor(fuelState and colors.white or colors.red)
- else
- fuelPercent = math.floor((gate.energyAvailable() / 203842) * 100)
- term.setBackgroundColor(fuelPercent > 5 and colors.white or colors.red)
- end
- local line = string.rep(" ", 12)
- for i = 4, 7 do
- term.setCursorPos(1, i)
- term.write(line)
- end
- term.setTextColor(colors.blue)
- term.setCursorPos(2, 5)
- term.write(gateStatus)
- if lcGate then
- term.setTextColor(gateStatus == "Dialing" and colors.lightGray or colors.gray)
- else
- term.setTextColor((gateStatus == "Dialing" or gateStatus == "Incoming") and colors.lightGray or colors.gray)
- end
- term.setCursorPos(2, 6)
- if lcGate then
- if fuelState then
- term.write(gateTarget)
- else
- term.write(gateTarget == "No Target" and "No Fuel" or gateTarget)
- end
- else
- if fuelPercent > 5 then
- term.write(gateTarget)
- else
- term.write(gateTarget == "No Target" and "Low Fuel" or gateTarget)
- end
- end
- if addrRedraw then drawAddressList() end
- if screen == "gatePage" then redrawInput() end
- end
- local function drawDHDControls()
- for _, button in pairs(guiElements.mainButtons) do
- button.render()
- end
- if gateChange then highlightAddressButton() end
- drawIrisStatus()
- end
- local function drawLeftPane()
- drawDHDStatus()
- --paintutils.drawFilledBox(1, 8, 12, termY, colors.gray)
- term.setBackgroundColor(colors.gray)
- local line = string.rep(" ", 12)
- for i = 8, termY do
- term.setCursorPos(1, i)
- term.write(line)
- end
- drawDHDControls()
- end
- local function drawMainScreen()
- drawLeftPane()
- drawFooter()
- drawAddressList()
- end
- local function flashDial(gName, gX, gY, gateNum)
- local gColor = assignColor(gateNum)
- local gLabel = string.rep(" ", math.floor((9 - #gName) / 2)) .. gName .. string.rep(" ", math.ceil((9 - #gName) / 2))
- term.setBackgroundColor(colors.black)
- term.setTextColor(gColor)
- term.setCursorPos(gX, gY)
- term.write(gLabel)
- sleep(0.1)
- term.setBackgroundColor(gColor)
- term.setTextColor(colors.white)
- term.setCursorPos(gX, gY)
- term.write(gLabel)
- addrRedraw = true
- end
- local function flashChoice(fX, fY, txtCol, bgCol, fText)
- term.setBackgroundColor(bgCol)
- term.setTextColor(txtCol)
- term.setCursorPos(fX, fY)
- term.write(fText)
- sleep(0.1)
- term.setBackgroundColor(txtCol)
- term.setTextColor(bgCol)
- term.setCursorPos(fX, fY)
- term.write(fText)
- end
- local function addNewAddress(newEntry, fast)
- if #addressBook < 23976 then
- local newAddress = { name = newEntry or "NEW GATE", addr = newEntry or "ADDRESS", rating = "U", iris = "none", note = "short note", loc = { x = 99999, y = 99999, z = 99999, dim = "Overworld", }, }
- addressBook[#addressBook + 1] = newAddress
- gatePages = math.ceil(#addressBook / 21)
- gateChange = true
- if fast then
- drawAddressList()
- drawFooter()
- highlightAddressButton()
- else
- screen = "gate"
- currentGate = #addressBook
- drawGateLabels()
- end
- end
- end
- local function hangUp()
- local onHook, _ = pcall(gate.disconnect)
- if onHook then
- gateTarget = "No Target"
- incomingAddress = nil
- end
- end
- local function dialOut(addr)
- local ok, _ = pcall(gate.dial, addr)
- if ok then
- gateTarget = addr
- gateStatus = "Dialing"
- drawDHDStatus()
- end
- end
- local function toggleIris()
- if lcGate then
- irisState = not irisState
- else
- term.setBackgroundColor(colors.lightBlue)
- term.setCursorPos(2, 9)
- if irisState then
- term.setTextColor(colors.orange)
- pcall(gate.openIris)
- else
- term.setTextColor(colors.green)
- pcall(gate.closeIris)
- end
- term.write("I r i s")
- end
- end
- local function irisStateChange() --# SGCraft
- while true do
- local irisEvent, _, newState, oldState = os.pullEvent("sgIrisStateChange")
- if newState == "Closed" or newState == "Open" or newState == "Offline" then
- term.setBackgroundColor(colors.lightBlue)
- term.setCursorPos(2, 9)
- term.setTextColor(colors.white)
- term.write("I r i s")
- if newState == "Closed" then
- irisState = true
- else
- irisState = false
- end
- drawIrisStatus()
- if screen == "gatePage" then redrawInput() end
- end
- end
- end
- do
- local states = {
- Idle = "Idle",
- Dialling = "Dialing",
- Opening = "Dialing",
- Connected = "Connected",
- Closing = "Closing",
- Offline = "Idle",
- }
- gateStateChange = function() --# SGCraft
- while true do
- local sgEvent, _, new_State, old_State = os.pullEvent("sgStargateStateChange")
- gateStatus = states[new_State]
- if gateStatus == "Closing" or gateStatus == "Idle" then
- gateTarget = "No Target"
- incomingAddress = nil
- chevronNumber = 0
- end
- if gateStatus == "Connected" then
- recordSessionData()
- end
- if (gateStatus == "Idle" or gateStatus == "Connected") and (screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus") then
- drawAddressList()
- end
- if gateStatus ~= "Dialing" and (screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus") then
- drawDHDStatus()
- end
- end
- end
- end
- local function chevronEncoder() --# SGCraft
- local fuelPercent = math.floor((gate.energyAvailable() / 203842) * 100)
- local sgEvent, _, chevron
- while true do
- sgEvent, _, chevronNumber, chevron = os.pullEvent("sgChevronEngaged")
- if screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus" then
- term.setCursorPos(2, 6)
- term.setBackgroundColor(fuelPercent > 5 and colors.white or colors.red)
- term.setTextColor(colors.gray)
- term.write(gateTarget:sub(1, chevronNumber))
- if incomingAddress then
- term.setTextColor(colors.lightGray)
- term.write(gateTarget:sub(chevronNumber + 1, #gateTarget))
- end
- if screen == "gatePage" then redrawInput() end
- end
- end
- end
- local function gateIdle() --# LanteaCraft
- while true do
- os.pullEvent("sgIdle")
- gateStatus = "Idle"
- gateTarget = "No Target"
- incomingAddress = nil
- chevronNumber = 0
- if screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus" then
- drawDHDStatus()
- end
- end
- end
- local function gateConnect() --# LanteaCraft
- while true do
- os.pullEvent("sgWormholeOpening")
- gateStatus = "Connected"
- gateTarget = incomingAddress or gateTarget
- recordSessionData()
- if screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus" then
- if incomingAddress then drawAddressList() end
- if screen == "gatePage" then drawPagePopUp() end
- drawDHDStatus()
- end
- end
- end
- local function gateDisconnect() --# LanteaCraft
- while true do
- os.pullEvent("sgWormholeClosing")
- gateStatus = "Closing"
- gateTarget = "No Target"
- incomingAddress = nil
- chevronNumber = 0
- if screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus" then
- drawAddressList()
- if screen == "gatePage" then drawPagePopUp() end
- drawDHDStatus()
- end
- end
- end
- local function incomingCall() --# LanteaCraft & SGCraft
- local sgEvent, _
- while true do
- if lcGate then
- local _, incomingChevron = os.pullEvent("sgIncoming")
- incomingAddress = incomingAddress and incomingAddress .. incomingChevron or incomingChevron
- else
- sgEvent, _, incomingAddress = os.pullEvent("sgDialIn")
- end
- gateStatus = "Incoming"
- gateTarget = incomingAddress
- for i = 1, #addressBook do
- if addressBook[i].addr == incomingAddress or (addressBook[i].addr:sub(1, 7) == incomingAddress and addressBook[i].addr:sub(8, 9) == thisGate:sub(8, 9)) then
- if addressBook[i].iris == "open" then
- pcall(gate.openIris)
- elseif addressBook[i].iris == "close" then
- pcall(gate.closeIris)
- end
- break
- end
- end
- if screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus" then
- if not lcGate then addrRedraw = true end
- drawDHDStatus()
- end
- end
- end
- local function outgoingCall() --# LanteaCraft
- local _
- while true do
- _, chevronNumber = os.pullEvent("sgChevronEncode")
- if chevronNumber == #gateTarget and (screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus") then
- drawDHDStatus()
- else
- if screen == "main" or screen == "gatePage" or screen == "manage" or screen == "exodus" then
- term.setCursorPos(2, 6)
- local fuelState = gate.hasFuel()
- term.setBackgroundColor(fuelState and colors.white or colors.red)
- term.setTextColor(colors.gray)
- term.write(gateTarget:sub(1, chevronNumber))
- if screen == "gatePage" then redrawInput() end
- end
- end
- end
- end
- local function inputExodusMouse()
- while true do
- local _, button, x, y = os.pullEvent("mouse_click")
- if y > 8 and y < 13 and x > 19 and x < 39 then
- if y == 11 and x > 21 and x < 28 and button == 1 then
- flashChoice(22, 11, colors.green, colors.white, " Save ")
- saveData(gateData)
- kernelState = false
- return
- elseif y == 11 and x > 30 and x < 37 and button == 1 then
- flashChoice(31, 11, colors.orange, colors.white, " Quit ")
- kernelState = false
- return
- end
- else
- screen = "main"
- clearExodusPopUp()
- drawAddressList()
- return
- end
- end
- end
- local function inputHelpMouse()
- while true do
- local _, button, x, y = os.pullEvent("mouse_click")
- if x > termX - 3 and y < 4 and button == 1 then
- screen = "main"
- clearLowerScreen()
- drawMainScreen()
- return
- end
- end
- end
- local function inputHelpKey()
- while true do
- local _, data = os.pullEvent("key")
- if data == keys.f1 then
- screen = "main"
- clearLowerScreen()
- drawMainScreen()
- return
- end
- end
- end
- local function inputLogChangePageKey()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(24, termY - 2)
- local newPage = tonumber(read(nil, nil, 4))
- logPage = newPage or logPage
- logPage = math.floor(math.max(1, math.min(logPage, logPages)))
- screen = "logs"
- clearPagePopUp(colors.white)
- drawLogScreen()
- end
- local function inputLogScreenMouse()
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- if x > termX - 3 and y < 4 and button == 1 then
- clearCallHistory()
- screen = "main"
- clearLowerScreen()
- drawMainScreen()
- return
- elseif x > 20 and x < 31 and y == termY and callHistory[1] then
- flashChoice((math.ceil(termX / 2) - (#tostring(logPage) + #tostring(logPages) + 4) / 2), termY, colors.gray, colors.lightGray, tostring(logPage) .. " of " .. tostring(logPages))
- screen = "logPage"
- drawPagePopUp()
- return
- elseif y > 6 and y < termY - 1 and x > 30 and x < 40 then
- local currentEntry = ((logPage - 1) * 11) + 1 --# Set the first entry to show (based on page number)
- if callHistory[currentEntry + y - 7] then
- local targetAddress = callHistory[currentEntry + y - 7]:sub(string.find(callHistory[currentEntry + y - 7], ">") + 2, #callHistory[currentEntry + y - 7])
- if button == 1 and (#targetAddress == 7 or #targetAddress == 9) then
- clearCallHistory()
- screen = "main"
- dialOut(targetAddress)
- clearLowerScreen()
- drawMainScreen()
- elseif button == 2 and (#targetAddress == 7 or #targetAddress == 9) then
- clearCallHistory()
- addNewAddress(targetAddress)
- end
- return
- end
- else
- local modeSwitch = false
- for _, click in pairs(guiElements.logButtons) do
- modeSwitch = click.processEvent(event, button, x, y)
- if modeSwitch then return end
- end
- end
- end
- end
- local function inputLogScreenScroll()
- while true do
- local _, data = os.pullEvent("mouse_scroll")
- logPage = data == 1 and math.min(logPage + 1, logPages) or math.max(1, logPage - 1)
- drawLogScreen()
- end
- end
- local function inputLogScreenKey()
- while true do
- local _, data = os.pullEvent("key") --# pageUp, pageDn, Home, End
- if data == keys.pageUp then
- logPage = math.min(logPages, logPage + 1)
- elseif data == keys.pageDown then
- logPage = math.max(1, logPage - 1)
- elseif data == keys.home then
- logPage = 1
- elseif data == keys["end"] then
- logPage = logPages
- end
- drawLogScreen()
- end
- end
- local function inputClearLogsMouse()
- while true do
- local _, button, x, y = os.pullEvent("mouse_click")
- if y == math.floor(termY / 2) then
- if x > (termX / 2) - 5 and x < (termX / 2) and button == 1 then --# yes
- flashChoice((termX / 2) - 4, math.floor(termY / 2), colors.green, colors.white, " YES ")
- screen = "logs"
- local clearLog = fs.open(historyData, "w")
- clearLog.close()
- clearCallHistory()
- logPage, logPages = 1, 1
- clearLowerScreen(colors.white)
- drawLogScreen()
- return
- elseif x > (termX / 2) and x < (termX / 2) + 6 and button == 1 then --# no
- flashChoice((termX / 2) + 2, math.floor(termY / 2), colors.red, colors.white, " N O ")
- screen = "logs"
- clearLowerScreen(colors.white)
- drawLogScreen()
- return
- end
- end
- end
- end
- local function inputManageAddressBookMouse()
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local modeSwitch = false
- for _, click in pairs(guiElements.addrBookMgt) do --# process buttons
- modeSwitch = click.processEvent(event, button, x, y) --# carry out action for clicked button
- if modeSwitch then return end
- end
- end
- end
- local function inputGateRatingMouse()
- local selected = false
- local gRatings = { "B", "H", "V", "M", "S", "C", "D", "U" }
- while true do
- local _, button, x, y = os.pullEvent("mouse_click")
- if x > 17 and x < 38 and y > 5 and y < 17 and button == 1 then
- for i = 1, 8 do
- if y == i + 7 then
- if addressBook[currentGate].rating ~= gRatings[i] then
- addressBook[currentGate].rating = gRatings[i]
- gateChange = true
- end
- selected = true
- break
- end
- end
- if selected then break end
- else
- break
- end
- end
- --paintutils.drawFilledBox(18, 6, 37, 17, colors.black)
- term.setBackgroundColor(colors.black)
- local line = string.rep(" ", 20)
- for i = 6, 17 do --# clear menu body
- term.setCursorPos(18, i)
- term.write(line)
- end
- term.setTextColor(colors.gray)
- term.setCursorPos(30, 15)
- term.write("z:")
- drawGateData()
- screen = "gate"
- end
- local function inputViewGateMouse()
- while true do
- local _, button, x, y = os.pullEvent("mouse_click")
- if x > termX - 3 and y < 4 and button == 1 then
- screen = "main"
- currentGate = nil
- clearLowerScreen()
- drawMainScreen()
- return
- elseif y == 5 and button == 1 then --# Name, gate order
- if x > 10 and x < 20 then --# Name
- term.setTextColor(colors.gray)
- term.setCursorPos(11, 5)
- term.write(addressBook[currentGate].name)
- term.setTextColor(colors.white)
- term.setCursorPos(11, 5)
- local newName = read(nil, nil, 9)
- if newName ~= "" then
- addressBook[currentGate].name = newName
- gateChange = true
- end
- term.setCursorPos(11, 5)
- term.write(string.rep(" ", 9))
- term.setTextColor(colors.yellow)
- term.setCursorPos(11, 5)
- term.write(addressBook[currentGate].name)
- elseif x > 30 and x < 35 then --# iris blacklist/whitelist
- if addressBook[currentGate].iris == "none" then
- addressBook[currentGate].iris = "open"
- elseif addressBook[currentGate].iris == "open" then
- addressBook[currentGate].iris = "close"
- elseif addressBook[currentGate].iris == "close" then
- addressBook[currentGate].iris = "none"
- end
- drawIrisSwitch(addressBook[currentGate].iris)
- elseif x > termX - 7 and x < termX - 6 + #tostring(currentGate) then --# change entry #
- term.setCursorPos(termX - 6, 5)
- term.write(string.rep(" ", 6))
- term.setTextColor(colors.gray)
- term.setCursorPos(termX - 6, 5)
- term.write(tostring(currentGate))
- term.setTextColor(colors.white)
- term.setCursorPos(termX - 6, 5)
- local newPos = tonumber(read(nil, nil, 3))
- if newPos then
- local tempGate = { } --# = addressBook[currentGate]
- for k, v in pairs(addressBook[currentGate]) do
- tempGate[k] = v
- end
- table.remove(addressBook, currentGate)
- table.insert(addressBook, newPos, tempGate)
- currentGate = newPos
- end
- term.setCursorPos(termX - 6, 5)
- term.write(string.rep(" ", 6))
- drawGateData()
- end
- elseif y == 7 and x > 10 and x < 20 and button == 1 then --# Address
- term.setTextColor(colors.gray)
- term.setCursorPos(11, 7)
- term.write(addressBook[currentGate].addr)
- term.setTextColor(colors.white)
- term.setCursorPos(11, 7)
- local newAddress = string.upper(read(nil, nil, 9, nil, nil, true))
- local nameChange = false
- if newAddress and (#newAddress == 7 or #newAddress == 9) then
- addressBook[currentGate].addr = newAddress
- local gNames = { "NEW GATE", "New Gate", "Name" }
- for _, tName in ipairs(gNames) do
- if addressBook[currentGate].name == tName then
- addressBook[currentGate].name = newAddress
- nameChange = true
- break
- end
- end
- gateChange = true
- end
- if nameChange then
- term.setCursorPos(11, 5)
- term.write(string.rep(" ", 9))
- term.setTextColor(colors.yellow)
- term.setCursorPos(11, 5)
- term.write(addressBook[currentGate].name)
- end
- term.setCursorPos(11, 7)
- term.write(string.rep(" ", 9))
- term.setTextColor(assignColor(currentGate))
- term.setCursorPos(11, 7)
- term.write(addressBook[currentGate].addr)
- elseif y == 9 and x > 7 and x < 8 + #addressBook[currentGate].note:sub(1, 43) and button == 1 then --# Note
- term.setTextColor(colors.gray)
- term.setCursorPos(8, 9)
- term.write(addressBook[currentGate].note:sub(1, 43))
- term.setTextColor(colors.white)
- term.setCursorPos(8, 9)
- local newNote = read(nil, nil, 43)
- if newNote ~= "" then
- addressBook[currentGate].note = newNote
- gateChange = true
- end
- term.setCursorPos(8, 9)
- term.write(string.rep(" ", 43))
- term.setCursorPos(8, 9)
- term.write(addressBook[currentGate].note:sub(1, 43))
- elseif y == 11 and x > 17 and x < 18 + #assignRating(currentGate) and button == 1 then --# Classification
- screen = "rating"
- drawRatingList(addressBook[currentGate].rating)
- return
- elseif y == 13 and x > 12 and x < 13 + #addressBook[currentGate].loc.dim and button == 1 then --# Dimension
- term.setTextColor(colors.gray)
- term.setCursorPos(13, 13)
- term.write(addressBook[currentGate].loc.dim)
- term.setTextColor(colors.white)
- term.setCursorPos(13, 13)
- local newDim = read(nil, nil, 19)
- if newDim ~= "" then
- addressBook[currentGate].loc.dim = newDim
- gateChange = true
- end
- term.setCursorPos(13, 13)
- term.write(string.rep(" ", 25))
- term.setTextColor(colors.yellow)
- term.setCursorPos(13, 13)
- term.write(addressBook[currentGate].loc.dim)
- elseif y == 15 and button == 1 then --# x, y, z coords
- if x > 4 and x < 5 + #tostring(addressBook[currentGate].loc.x) then
- term.setTextColor(colors.gray)
- term.setCursorPos(5, 15)
- term.write(tostring(addressBook[currentGate].loc.x))
- term.setTextColor(colors.white)
- term.setCursorPos(5, 15)
- local newX = tonumber(read(nil, nil, 9))
- if newX then
- addressBook[currentGate].loc.x = newX
- gateChange = true
- end
- term.setCursorPos(5, 15)
- term.write(string.rep(" ", 9))
- term.setTextColor(colors.lightGray)
- term.setCursorPos(5, 15)
- term.write(tostring(addressBook[currentGate].loc.x))
- elseif x > 18 and x < 19 + #tostring(addressBook[currentGate].loc.y) then
- term.setTextColor(colors.gray)
- term.setCursorPos(19, 15)
- term.write(tostring(addressBook[currentGate].loc.y))
- term.setTextColor(colors.white)
- term.setCursorPos(19, 15)
- local newY = tonumber(read(nil, nil, 9))
- if newY then
- addressBook[currentGate].loc.y = newY
- gateChange = true
- end
- term.setCursorPos(19, 15)
- term.write(string.rep(" ", 9))
- term.setTextColor(colors.lightGray)
- term.setCursorPos(19, 15)
- term.write(tostring(addressBook[currentGate].loc.y))
- elseif x > 32 and x < 33 + #tostring(addressBook[currentGate].loc.z) then
- term.setTextColor(colors.gray)
- term.setCursorPos(33, 15)
- term.write(tostring(addressBook[currentGate].loc.z))
- term.setTextColor(colors.white)
- term.setCursorPos(33, 15)
- local newZ = tonumber(read(nil, nil, 9))
- if newZ then
- addressBook[currentGate].loc.z = newZ
- gateChange = true
- end
- term.setCursorPos(33, 15)
- term.write(string.rep(" ", 9))
- term.setTextColor(colors.lightGray)
- term.setCursorPos(33, 15)
- term.write(tostring(addressBook[currentGate].loc.z))
- end
- end
- end
- end
- local function inputViewGateScroll()
- while true do
- local _, data = os.pullEvent("mouse_scroll")
- currentGate = data == 1 and math.min(#addressBook, currentGate + 1) or math.max(1, currentGate - 1)
- term.setCursorBlink(false)
- drawGateData()
- end
- end
- local function changePage()
- if gatePage == gatePages and gatePages > 1 then clearDataArea() end
- drawAddressList()
- drawFooter()
- end
- local function inputMainChangePageKey()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(31, termY - 2)
- local newPage = tonumber(read(nil, nil, 4))
- gatePage = newPage or gatePage
- gatePage = math.floor(math.max(1, math.min(gatePage, gatePages)))
- screen = "main"
- clearPagePopUp()
- if gatePage == gatePages and gatePages > 1 then clearDataArea() end
- drawAddressList()
- drawFooter()
- end
- local function inputMainScreenMouse()
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local xPos, yPos = 17, 5
- local magicNumber = ((gatePage - 1) * 20) + gatePage
- if x > termX - 3 and y < 4 and button == 1 then --# 'X' (quit)
- if gateChange then
- screen = "exodus"
- drawExodusPopUp()
- return
- end
- kernelState = false
- return
- elseif x > 27 and x < 38 and y == termY then
- flashChoice((math.ceil(termX / 2) - (#tostring(gatePage) + #tostring(gatePages) + 4) / 2) + 7, termY, colors.gray, colors.lightGray, tostring(gatePage) .. " of " .. tostring(gatePages))
- screen = "gatePage"
- drawPagePopUp()
- return
- end
- for i = magicNumber, #addressBook do
- if x >= xPos and x <= xPos + 8 and y == yPos then
- if button == 1 then --# dial address
- if (#addressBook[i].addr == 9 and addressBook[i].addr == thisGate) or (#addressBook[i].addr == 7 and addressBook[i].addr == thisGate:sub(1, 7)) then
- return --# thisGate - don't dial
- else
- flashDial(addressBook[i].name, xPos, yPos, i)
- dialOut(addressBook[i].addr) --# dial out
- return
- end
- elseif button == 2 then --# view/edit address
- flashDial(addressBook[i].name, xPos, yPos, i)
- screen = "gate"
- currentGate = i
- drawGateLabels()
- return
- elseif button == 3 and #addressBook > 1 then --# delete address
- flashDial(addressBook[i].name, xPos, yPos, i)
- table.remove(addressBook, i)
- gatePages = math.ceil(#addressBook / 21)
- gatePage = math.min(gatePage, gatePages)
- if gatePage == gatePages then clearDataArea() end
- drawAddressList()
- drawFooter()
- highlightAddressButton()
- gateChange = true
- return
- end
- else
- yPos = yPos + 2
- if yPos >= termY - 1 then xPos = xPos + 11 yPos = 5 end
- if xPos >= termX - 5 then break end
- end
- end
- local modeSwitch = false
- for _, click in pairs(guiElements.mainButtons) do --# process buttons
- modeSwitch = click.processEvent(event, button, x, y) --# carry out action for clicked button
- if modeSwitch then return end
- end
- for _, click in pairs(guiElements.mainFooter) do
- click.processEvent(event, button, x, y)
- end
- end
- end
- local function inputMainScreenScroll()
- while true do
- local _, data = os.pullEvent("mouse_scroll")
- gatePage = data == 1 and math.min(gatePage + 1, gatePages) or math.max(1, gatePage - 1)
- changePage()
- end
- end
- local function inputMainScreenKey()
- while true do
- local _, data = os.pullEvent("key") --# pageUp, pageDn, Home, End, F1
- if data == keys.pageUp then
- gatePage = math.min(gatePages, gatePage + 1)
- changePage()
- elseif data == keys.pageDown then
- gatePage = math.max(1, gatePage - 1)
- changePage()
- elseif data == keys.home then
- gatePage = 1
- changePage()
- elseif data == keys["end"] then
- gatePage = gatePages
- changePage()
- elseif data == keys.f1 then
- screen = "help"
- drawHelpScreen()
- end
- end
- end
- local function dhdKernel()
- while kernelState do
- if lcGate then
- if screen == "main" then
- parallel.waitForAny(inputMainScreenMouse, inputMainScreenScroll, inputMainScreenKey, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "gate" then
- parallel.waitForAny(inputViewGateMouse, inputViewGateScroll, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "gatePage" then
- parallel.waitForAny(inputMainChangePageKey, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "rating" then
- parallel.waitForAny(inputGateRatingMouse, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "manage" then
- parallel.waitForAny(inputManageAddressBookMouse, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "logs" then
- parallel.waitForAny(inputLogScreenMouse, inputLogScreenScroll, inputLogScreenKey, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "logPage" then
- parallel.waitForAny(inputLogChangePageKey, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "clearLogs" then
- parallel.waitForAny(inputClearLogsMouse, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "help" then
- parallel.waitForAny(inputHelpMouse, inputHelpKey, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- elseif screen == "exodus" then
- parallel.waitForAny(inputExodusMouse, incomingCall, outgoingCall, gateIdle, gateConnect, gateDisconnect)
- end
- else
- if screen == "main" then
- parallel.waitForAny(inputMainScreenMouse, inputMainScreenScroll, inputMainScreenKey, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "gate" then
- parallel.waitForAny(inputViewGateMouse, inputViewGateScroll, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "gatePage" then
- parallel.waitForAny(inputMainChangePageKey, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "rating" then
- parallel.waitForAny(inputGateRatingMouse, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "manage" then
- parallel.waitForAny(inputManageAddressBookMouse, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "logs" then
- parallel.waitForAny(inputLogScreenMouse, inputLogScreenScroll, inputLogScreenKey, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "logPage" then
- parallel.waitForAny(inputLogChangePageKey, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "clearLogs" then
- parallel.waitForAny(inputClearLogsMouse, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "help" then
- parallel.waitForAny(inputHelpMouse, inputHelpKey, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- elseif screen == "exodus" then
- parallel.waitForAny(inputExodusMouse, incomingCall, chevronEncoder, gateStateChange, irisStateChange)
- end
- end
- end
- end
- local function errorMessage(which)
- clearScreen()
- term.setCursorPos(2, 2)
- if which == "color" then
- term.write("This program requires an advanced computer")
- elseif which == "gate" then
- term.setTextColor(colors.red)
- term.write("Unable to locate a stargate")
- term.setTextColor(colors.white)
- end
- term.setCursorPos(1, 5)
- end
- local function initGate()
- gate = peripheral.find("stargate")
- if type(gate) == "table" then
- lcGate = pcall(gate.getAddress)
- if lcGate then
- irisState = false
- thisGate = gate.getAddress()
- if gate.isDialing() then
- gateStatus = "Dialing"
- gateTarget = "........."
- elseif gate.isConnected() then
- gateStatus = "Connected"
- gateTarget = "........."
- else
- gateStatus = "Idle"
- end
- else
- local gateState, callDirection
- gateState, chevronNumber, callDirection = gate.stargateState()
- if gateState == "Offline" then return false end
- thisGate = gate.localAddress()
- local irisStatus = gate.irisState()
- local irisStates = {
- Offline = "Open",
- Open = "Open",
- Opening = "Closed",
- Closed = "Closed",
- Closing = "Open",
- }
- for iState, iNewStatus in pairs(irisStates) do
- if irisStatus == iState then
- if iNewStatus == "Open" then
- irisState = false
- else
- irisState = true
- end
- break
- end
- end
- local states = {
- Idle = "Idle",
- Dialling = "Dialing",
- Opening = "Dialing",
- Connected = "Connected",
- Closing = "Closing",
- }
- gateStatus = states[gateState]
- if gateStatus == "Dialing" or gateStatus == "Connected" then
- gateTarget = gate.remoteAddress()
- if callDirection == "Incoming" then incomingAddress = gateTarget end
- if callDirection == "Incoming" and gateStatus == "Dialing" then gateStatus = "Incoming" end
- end
- end
- return true
- else
- return false
- end
- end
- local function initMe()
- if not term.isColor() then errorMessage("color") return false end
- if not initGate() then errorMessage("gate") return false end
- if not fs.exists(gateData) then
- if not fs.exists("/data") then fs.makeDir("/data") end
- addressBook[1].name = thisGate
- addressBook[1].addr = thisGate
- saveData(gateData)
- end
- ingestData(gateData)
- screen = "main"
- kernelState = true
- guiElements = {
- mainButtons = {
- newButton(1, 9, 11, 1, "I r i s ", colors.lightBlue, colors.white, function() toggleIris() drawIrisStatus() end, "Iris", 1);
- newButton(1, 11, 11, 1, "END Call ", colors.orange, colors.black, function() if gateStatus == "Dialing" or gateStatus == "Connected" or gateStatus == "Incoming" then flashChoice(1, 11, colors.orange, colors.black, " END Call ") hangUp() drawDHDStatus() end end, "EndCall", 1);
- newButton(1, 13, 11, 1, "Addr Book", colors.blue, colors.white, function() flashChoice(1, 13, colors.blue, gateChange and colors.lime or colors.white, " Addr Book ") screen = "manage" drawAddressBookManagementPopUp() return true end, "AddrBook", 1);
- newButton(1, 13, 11, 1, "Addr Book", colors.blue, colors.white, function() if gateChange then flashChoice(1, 13, colors.blue, colors.white, " Addr Book ") saveData(gateData) end end, "SaveAB", 2);
- newButton(1, 15, 11, 1, "New Gate ", colors.green, colors.white, function() flashChoice(1, 15, colors.green, colors.white, " New Gate ") addNewAddress() end, "NewGate", 1);
- newButton(1, 15, 11, 1, "New Gate ", colors.green, colors.white, function() flashChoice(1, 15, colors.green, colors.white, " New Gate ") addNewAddress(nil, true) end, "NewGateFast", 3);
- newButton(1, 17, 11, 1, "Logs ", colors.yellow, colors.gray, function() flashChoice(1, 17, colors.yellow, colors.gray, " Logs ") screen = "logs" ingestData("logs") clearLowerScreen(colors.white) drawLogScreen() return true end, "Logs", 1);
- newButton(1, termY, 11, 1, "[F1] Help", colors.gray, colors.lightGray, function() screen = "help" drawHelpScreen() return true end, "Help", 1);
- },
- mainFooter = {
- newButton(22, termY, 2, 1, "<<", colors.gray, colors.lightGray, function() gatePage = 1 changePage() end, "firstPage", 1);
- newButton(25, termY, 1, 1, "<", colors.gray, colors.lightGray, function() gatePage = math.max(1, gatePage - 1) changePage() end, "pageMinus", 1);
- newButton(40, termY, 1, 1, ">", colors.gray, colors.lightGray, function() gatePage = math.min(gatePages, gatePage + 1) changePage() end, "pagePlus", 1);
- newButton(42, termY, 2, 1, ">>", colors.gray, colors.lightGray, function() gatePage = gatePages changePage() end, "lastPage", 1);
- },
- logButtons = {
- newButton(termX - 7, 5, 7, 1, "CLEAR", colors.lightGray, colors.black, function() flashChoice(termX - 7, 5, colors.lightGray, colors.black, " CLEAR ") screen = "clearLogs" drawClearLogsPopUp() return true end, "logPageClear", 1);
- newButton(14, termY, 2, 1, "<<", colors.gray, colors.lightGray, function() logPage = 1 drawLogScreen() end, "logPageHome", 1);
- newButton(17, termY, 1, 1, "<", colors.gray, colors.lightGray, function() logPage = math.max(1, logPage - 1) drawLogScreen() end, "logPageMinus", 1);
- newButton(34, termY, 1, 1, ">", colors.gray, colors.lightGray, function() logPage = math.min(logPages, logPage + 1) clearLowerScreen(colors.white) drawLogScreen() end, "logPagePlus", 1);
- newButton(36, termY, 2, 1, ">>", colors.gray, colors.lightGray, function() logPage = logPages clearLowerScreen(colors.white) drawLogScreen() end, "logPageEnd", 1);
- },
- addrBookMgt = {
- newButton(17, 12, 8, 1, "Import", colors.yellow, colors.black, function() flashChoice(17, 12, colors.yellow, colors.black, " Import ") ingestData("/disk/data/DHDgates") clearDataArea() drawAddressList() highlightAddressButton() drawAddressBookManagementPopUp() end, "ABImport", 1);
- newButton(26, 12, 8, 1, "Export", colors.orange, colors.black, function() flashChoice(26, 12, colors.orange, colors.black, " Export ") saveData("/disk/data/DHDgates") drawAddressBookManagementPopUp() end, "ABExport", 1);
- newButton(17, 14, 8, 1, "Merge!", colors.purple, colors.white, function() flashChoice(17, 14, colors.purple, colors.white, " Merge! ") mergeData() if gatePage == gatePages then drawAddressList() end if gateChange then highlightAddressButton() end drawAddressBookManagementPopUp() end, "ABMerge", 1);
- newButton(26, 14, 8, 1, "Save", colors.green, colors.white, function() flashChoice(26, 14, colors.green, colors.white, " Save ") saveData(gateData) drawDHDControls() drawAddressBookManagementPopUp() end, "ABSave", 1);
- newButton(22, 16, 7, 1, "Close", colors.red, colors.white, function() flashChoice(22, 16, colors.red, colors.white, " Close ") screen = "main" clearABPopUp() drawAddressList() return true end, "ABClose", 1);
- },
- }
- return true
- end
- if not initMe() then return end
- clearScreen()
- drawHeader()
- drawMainScreen()
- dhdKernel()
- clearScreen()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement