Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ SGCraft ]]--
- --[[ ccDHD Basic ]]--
- --[[ by Dog ]]--
- --[[ a k a ]]--
- --[[ HydrantHunter ]]--
- --[[ pastebin: ]]--
- --[[ JYpfKQL6 ]]--
- local ccDHDbasicVer = "1.0.38"
- --[[
- Tested with/requires:
- - Minecraft 1.7.10+ AND ComputerCraft 1.75+ || SGCraft1.11.x-mc1.7.10+
- - An advanced computer connected directly or via modem/network cable to...
- - A stargate with a CC adapter
- Special thanks to theoriginalbit for his custom read function
- IMPORTANT NOTE: DO NOT EDIT this script - all variables are set automatically
- ]]--
- if not term.isColor() or pocket or turtle then error("Advanced computer required!\n", 0) end
- if not os.getComputerLabel() then os.setComputerLabel("ccDHD.cc#" .. tostring(os.getComputerID())) end
- local termX, termY = term.getSize()
- if termX ~= 51 or termY < 18 or termY > 19 then error("Screen size must be 51x18 or 51x19.\n", 0) end
- local sg = peripheral.find("stargate")
- if not sg then error("Unable to locate stargate!\n", 0) end
- local thisGate, nrgAvailable, irisState, dialTarget = sg.localAddress(), sg.energyAvailable(), sg.irisState(), sg.remoteAddress()
- if dialTarget == "" then dialTarget = "none" end
- local sgState, chevronsEngaged, callDirection = sg.stargateState()
- local MAXFUEL, abCount, gateChange, shift, gettingInput = 200000, 1, false, false, false
- local addressBook = { { name = thisGate, addr = thisGate, rating = "U", iris = "none", callDrop = false, note = "short note", loc = { x = 0, y = 0, z = 0, dim = "Unspecified" } } }
- do
- local sgData
- if fs.exists("/data/DHDgates") then
- addressBook[1] = nil
- sgData = fs.open("/data/DHDgates", "r")
- addressBook = textutils.unserialize(sgData.readAll())
- abCount = #addressBook
- else
- if not fs.exists("/data") then fs.makeDir("/data") end
- sgData = fs.open("/data/DHDgates", "w")
- sgData.write(textutils.serialize(addressBook))
- end
- sgData.close()
- local saveIt = false
- for i = 1, abCount do
- if addressBook[i].iris == "open" then
- addressBook[i].iris, saveIt = true, true
- elseif addressBook[i].iris == "close" then
- addressBook[i].iris, saveIt = false, true
- end
- end
- if saveIt then
- sgData = fs.open("/data/DHDgates", "w")
- sgData.write(textutils.serialize(addressBook))
- sgData.close()
- end
- end
- local currentSelection, numPerPage, pageNum, numPages, logPage, logPages, callHistory, help = 1, 4, 1, math.ceil(abCount / 4), 1, 1, { }, false
- local editEntry, drawLabels, drawDetails, drawStargateState, drawHelp, dialOut, button, updateTimer
- local classifications = {
- B = { order = 1, color = colors.blue, label = "Base/Outpost/Hub" };
- H = { order = 2, color = colors.lightBlue, label = "Home/Camp" };
- V = { order = 3, color = colors.brown, label = "Village" };
- M = { order = 4, color = colors.purple, label = "Misc/Special" };
- S = { order = 5, color = colors.green, label = "Safe/Secured" };
- C = { order = 6, color = colors.orange, label = "Caution" };
- D = { order = 7, color = colors.red, label = "Danger" };
- U = { order = 8, color = colors.lightGray, label = "Unk/Unclassified", long = "Unknown/Unclassified" };
- }
- --# Below is a subset of Dog's buttonBuddy API...
- do
- local bbButtons, bbButtonCount = { }, 0
- local validColors = { [1] = true; [2] = true; [4] = true; [8] = true; [16] = true; [32] = true; [64] = true; [128] = true; [256] = true; [512] = true; [1024] = true; [2048] = true; [4096] = true; [8192] = true; [16384] = true, [32768] = true; }
- button = {
- create = function(sText, bbGroup, x, y, w, h, mouseBtn, activate, funct, txtCol, bgCol, inactiveTxtCol, inactiveBgCol)
- if type(sText) == "string" and (type(bbGroup) == "string" or bbGroup == nil) and type(x) == "number" and type(y) == "number" and type(w) == "number" and type(h) == "number" and (type(mouseButton) == "number" or mouseButton == nil) and (type(funct) == "function" or funct == nil) and type(activate) == "boolean" then
- bbButtonCount = bbButtonCount + 1
- w = math.floor(math.min(math.max(1, math.max(#sText, w)), termX))
- h = math.floor(math.max(1, math.min(h, termY)))
- x = math.floor(math.max(1, math.min(x, termX - w + 1)))
- y = math.floor(math.max(1, math.min(y, termY - h + 1)))
- if mouseBtn then mouseBtn = math.floor(mouseBtn) mouseBtn = (mouseBtn > 0 and mouseBtn < 4) and mouseBtn or 1 end
- bbButtons[bbButtonCount] = {
- text = sText:sub(1, w);
- group = bbGroup;
- startX = x;
- startY = y;
- width = w;
- height = h;
- mouseButton = mouseBtn;
- active = activate;
- func = type(funct) == "function" and funct or function() return true end;
- txtColor = (type(txtCol) == "number" and validColors[txtCol]) and txtCol or colors.black;
- bgColor = (type(bgCol) == "number" and validColors[bgCol]) and bgCol or colors.white;
- inactiveTextColor = (type(inactiveTextCol) == "number" and validColors[inactiveTextCol]) and inactiveTextCol or colors.lightGray;
- inactiveBgColor = (type(inactiveBgCol) == "number" and validColors[inactiveBgCol]) and inactiveBgCol or colors.gray;
- }
- return true, bbButtonCount
- end
- return false
- end;
- check = function(x, y, mouseBtn, group)
- if type(x) == "number" and type(y) == "number" and (type(mouseBtn) == "number" or mouseBtn == nil) and (type(group) == "string" or group == nil) and x > 0 and x <= termX and y > 0 and y <= termY then
- local bbGroup, bbMBtn, startX, startY
- if mouseBtn then mouseBtn = math.floor(mouseBtn) mouseBtn = (mouseBtn > 0 and mouseBtn < 4) and mouseBtn or 1 end
- for id = 1, bbButtonCount do
- bbGroup, bbMBtn, startX, startY = bbButtons[id]["group"], bbButtons[id]["mouseButton"], bbButtons[id]["startX"], bbButtons[id]["startY"]
- if (group and group == bbGroup) or not bbGroup then
- if (mouseBtn and (bbMBtn == mouseBtn or not bbMBtn)) or (not mouseBtn and not bbMBtn) then
- if x >= startX and x <= startX + bbButtons[id]["width"] - 1 and y >= startY and y <= startY + bbButtons[id]["height"] - 1 then
- if bbButtons[id]["active"] then
- local success, returnData = bbButtons[id]["func"](id, mouseBtn)
- if type(success) == "boolean" then return success, id, returnData else return true, id, returnData or success end
- else
- return false, id
- end
- end
- end
- end
- end
- end
- return false
- end;
- render = function(target, groupOnly)
- local active, line, text, txtLine, txtLen, txtRow, startX, startY, buttonHeight, buttonWidth
- if type(target) == "number" and target > 0 and target <= bbButtonCount then
- startX, startY = bbButtons[target]["startX"], bbButtons[target]["startY"]
- buttonHeight, buttonWidth = bbButtons[target]["height"], bbButtons[target]["width"]
- text = bbButtons[target]["text"]
- txtLen, active = #text, bbButtons[target]["active"]
- txtLine = string.rep(" ", math.floor((buttonWidth - txtLen) / 2)) .. text .. string.rep(" ", math.ceil((buttonWidth - txtLen) / 2))
- term.setTextColor(active and bbButtons[target]["txtColor"] or bbButtons[target]["inactiveTextColor"])
- term.setBackgroundColor(active and bbButtons[target]["bgColor"] or bbButtons[target]["inactiveBgColor"])
- if buttonHeight == 1 then
- term.setCursorPos(startX, startY)
- term.write(txtLine)
- else
- line, txtRow = string.rep(" ", buttonWidth), startY + math.floor(buttonHeight / 2)
- for yPos = startY, startY + buttonHeight - 1 do
- term.setCursorPos(startX, yPos)
- term.write(yPos == txtRow and txtLine or line)
- end
- end
- return true, 1
- elseif type(target) == "string" or target == nil then
- local buttonsRendered, bbGroup = 0
- for id = 1, bbButtonCount do
- bbGroup = bbButtons[id]["group"]
- if (type(target) == "string" and bbGroup == target) or (not bbGroup and not groupOnly) then
- startX, startY = bbButtons[id]["startX"], bbButtons[id]["startY"]
- buttonHeight, buttonWidth = bbButtons[id]["height"], bbButtons[id]["width"]
- text = bbButtons[id]["text"]
- txtLen, active = #text, bbButtons[id]["active"]
- txtLine = string.rep(" ", math.floor((buttonWidth - txtLen) / 2)) .. text .. string.rep(" ", math.ceil((buttonWidth - txtLen) / 2))
- term.setTextColor(active and bbButtons[id]["txtColor"] or bbButtons[id]["inactiveTextColor"])
- term.setBackgroundColor(active and bbButtons[id]["bgColor"] or bbButtons[id]["inactiveBgColor"])
- if buttonHeight == 1 then
- term.setCursorPos(startX, startY)
- term.write(txtLine)
- else
- line, txtRow = string.rep(" ", buttonWidth), startY + math.floor(buttonHeight / 2)
- for yPos = startY, startY + buttonHeight - 1 do
- term.setCursorPos(startX, yPos)
- term.write(yPos == txtRow and txtLine or line)
- end
- end
- buttonsRendered = buttonsRendered + 1
- end
- end
- return true, buttonsRendered
- end
- return false, 0
- end;
- setFunction = function(id, funct)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(funct) == "function" or funct == nil) then
- bbButtons[id]["func"] = funct or function() return true end
- return true
- end
- return false
- end;
- setText = function(id, text)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and type(text) == "string" then
- bbButtons[id]["text"] = #text <= (termX - bbButtons[id]["startX"] + 1) and text or text:sub(1, termX - bbButtons[id]["startX"] + 1)
- bbButtons[id]["width"] = math.max(#bbButtons[id]["text"], bbButtons[id]["width"])
- return true
- end
- return false
- end;
- getText = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then return bbButtons[id]["text"] end
- end;
- setColors = function(id, txtColor, bgColor, inactiveTxtCol, inactiveBgCol)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and (txtColor or bgColor or inactiveTxtCol or inactiveBgCol) then
- local returnValue, changedColors = true, 0
- if txtColor then
- if type(txtColor) == "number" and validColors[txtColor] then bbButtons[id]["txtColor"] = txtColor changedColors = changedColors + 1 else returnValue = false end
- end
- if bgColor then
- if type(bgColor) == "number" and validColors[bgColor] then bbButtons[id]["bgColor"] = bgColor changedColors = changedColors + 1 else returnValue = false end
- end
- if inactiveTxtCol then
- if type(inactiveTxtCol) == "number" and validColors[inactiveTxtCol] then bbButtons[id]["inactiveTextColor"] = inactiveTxtCol changedColors = changedColors + 1 else returnValue = false end
- end
- if inactiveBgCol then
- if type(inactiveBgCol) == "number" and validColors[inactiveBgCol] then bbButtons[id]["inactiveBgColor"] = inactiveBgCol changedColors = changedColors + 1 else returnValue = false end
- end
- return returnValue, changedColors
- end
- return false, 0
- end;
- setActive = function(id, bState)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and type(bState) == "boolean" then bbButtons[id]["active"] = bState return true end
- return false
- end;
- getButtonCount = function()
- return bbButtonCount
- end;
- }
- end
- --# 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
- gettingInput = true
- 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 mouseLimit = _limit or 0
- 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
- local function historyHandler(value)
- redraw(' ')
- if value == -1 or value == 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 = historyPos + 1
- elseif historyPos == #_history then
- historyPos = nil
- end
- end
- if historyPos and #_history > 0 then
- input = string.sub(_history[historyPos], 1, _limit) or ""
- pos = #input
- if not _limit then mouseLimit = pos end
- else
- input = ""
- pos = 0
- if not _limit then mouseLimit = 0 end
- end
- 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
- code = code:upper()
- goodData = true
- end
- else
- goodData = true
- end
- if goodData then
- input = input:sub(1, pos) .. code .. input:sub(pos + 1)
- pos = pos + 1
- if not _limit then mouseLimit = math.min(mouseLimit + 1, sw - (sw - sx)) end
- 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))
- newWord[i] = symbols[glyph] and glyph or "?"
- end
- code = table.concat(newWord)
- end
- input = input:sub(1, pos) .. code .. input:sub(pos + 1)
- pos = pos + #code
- if not _limit then mouseLimit = math.min(mouseLimit + #code, sw - (sw - sx)) end
- elseif event == "key" then
- if code == keys.enter or code == keys.numPadEnter then
- 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)
- if not _limit then mouseLimit = math.max(mouseLimit - 1, 0) end
- elseif code == keys.delete and pos < #input then
- redraw(' ')
- input = input:sub(1, pos)..input:sub(pos + 2)
- if not _limit then mouseLimit = math.max(mouseLimit - 1, 0) end
- 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
- historyHandler(code)
- end
- elseif event == "mouse_click" and not _noMouse and ((mX < sx or mX >= sx + mouseLimit) or (mY ~= sy)) then
- break
- elseif event == "mouse_scroll" and _history then
- historyHandler(code)
- 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
- gettingInput = false
- return input
- end
- local function assignColor()
- return classifications[addressBook[currentSelection].rating].color or colors.lightGray
- end
- local function assignRating()
- return classifications[addressBook[currentSelection].rating].long or (classifications[addressBook[currentSelection].rating].label or "Unknown/Unclassified")
- end
- local function drawRatingList(rating)
- local txtColor
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.blue)
- term.setCursorPos(18, 6)
- term.write(" Classification ")
- paintutils.drawFilledBox(18, 7, 37, 16, colors.gray) --# menu body
- for k, v in pairs(classifications) do
- if rating == k then
- txtColor = rating == "U" and colors.white or v.color
- term.setCursorPos(37, v.order + 7)
- term.setBackgroundColor(v.color)
- term.write(" ") --# selected rating color pip
- else
- txtColor = colors.lightGray
- end
- term.setCursorPos(18, v.order + 7)
- term.setBackgroundColor(v.color)
- term.write(" ") --# color pip
- term.setCursorPos(20, v.order + 7)
- term.setTextColor(txtColor)
- term.setBackgroundColor(colors.gray)
- term.write(v.label)
- end
- end
- local function selectRating()
- local selected = false
- drawRatingList(addressBook[currentSelection].rating)
- while true do
- local event, data, x, y = os.pullEvent("mouse_click")
- if x > 17 and x < 38 and y > 5 and y < 17 then --# menu body (so clicking on the body doesn't make the menu disappear)
- if y > 7 and y < 16 and data == 1 then --# actual menu options
- for k, v in pairs(classifications) do
- if y == v.order + 7 then
- if addressBook[currentSelection].rating ~= k then
- addressBook[currentSelection].rating = k
- gateChange = true
- end
- selected = true
- break
- end
- end
- if selected then break end
- end
- else
- break
- end
- end
- return drawDetails(currentSelection, true)
- end
- local function repositionCursor(curX, curY, txtColor, bgColor)
- term.setTextColor(txtColor)
- term.setBackgroundColor(bgColor)
- term.setCursorPos(curX, curY)
- end
- local function drawSaveButton()
- term.setBackgroundColor(colors.orange)
- term.setTextColor(colors.black)
- term.setCursorPos(12, termY)
- term.write(" Click here to save Address Book ")
- end
- do
- local labelTable = {
- [1] = { 6, "Name:" };
- [2] = { 8, "Address:" };
- [3] = { 10, "Class:" };
- [4] = { 12, "Note:" };
- [5] = { 14, "Loc/Dim:" };
- [6] = { 15, "X: Y: Z: " };
- [7] = { 17, "Call Drop: Auto Iris:" };
- }
- drawLabels = function()
- term.setTextColor(colors.gray)
- for i = 1, 7 do
- term.setCursorPos(14, labelTable[i][1])
- term.write(labelTable[i][2])
- end
- end
- end
- local function drawSwitch(x, y, position)
- if type(x) == "number" and type(y) == "number" then
- x = math.floor(math.max(1, math.min(x, termX - 4))) --# Validate x coord
- y = math.floor(math.max(1, math.min(y, termY))) --# Validate y coord
- term.setCursorPos(x, y)
- if type(position) == "string" then --# Switch (Neutral)
- term.setBackgroundColor(colors.green)
- term.write(" ")
- term.setBackgroundColor(colors.gray)
- term.write(" ")
- term.setBackgroundColor(colors.red)
- term.write(" ")
- elseif type(position) == "boolean" then --# Switch (ON/OFF)
- term.setBackgroundColor(position and colors.green or colors.gray)
- term.write(" ")
- term.setBackgroundColor(position and colors.gray or colors.red)
- term.write(" ")
- end
- end
- end
- drawDetails = function(id, bClearRatingMenu)
- os.queueEvent("drawSGDialButton")
- if bClearRatingMenu then paintutils.drawFilledBox(18, 6, 37, 16, colors.black) paintutils.drawLine(34, 6, 37, 6, colors.lightGray) end
- term.setBackgroundColor(colors.black)
- if currentSelection == 0 or bClearRatingMenu then drawLabels() end
- currentSelection = id
- term.setTextColor(colors.cyan)
- term.setCursorPos(20, 6)
- term.write(addressBook[currentSelection].name .. string.rep(" ", 12 - #addressBook[currentSelection].name))
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.setCursorPos(23, 8)
- term.write(addressBook[currentSelection].addr .. " ")
- term.setTextColor(colors.gray)
- term.setCursorPos(34, 8)
- term.write(addressBook[currentSelection].addr == thisGate and "This Gate" or " ")
- local gRating = assignRating(currentSelection)
- term.setTextColor(assignColor(currentSelection))
- term.setCursorPos(23, 10)
- term.write(gRating .. string.rep(" ", 20 - #gRating))
- term.setTextColor(colors.white)
- term.setCursorPos(23, 12)
- term.write(addressBook[currentSelection].note:sub(1, 28) .. string.rep(" ", 29 - #addressBook[currentSelection].note:sub(1, 28)))
- term.setCursorPos(23, 13)
- if #addressBook[currentSelection].note > 28 then
- term.write(addressBook[currentSelection].note:sub(29, 43) .. string.rep(" ", 15 - #addressBook[currentSelection].note:sub(29, 43)))
- else
- term.write(string.rep(" ", 15))
- end
- if addressBook[currentSelection].note == "short note" then
- term.setCursorPos(34, 12)
- term.setTextColor(colors.gray)
- term.write("(43 characters)")
- end
- term.setTextColor(colors.brown)
- term.setCursorPos(23, 14)
- term.write(addressBook[currentSelection].loc.dim .. string.rep(" ", 29 - #addressBook[currentSelection].loc.dim))
- term.setTextColor(colors.lightGray)
- term.setCursorPos(16, 15)
- term.write(tostring(addressBook[currentSelection].loc.x):sub(1, 9) .. string.rep(" ", 9 - #tostring(addressBook[currentSelection].loc.x)))
- term.setCursorPos(29, 15)
- term.write(tostring(addressBook[currentSelection].loc.y):sub(1, 9) .. string.rep(" ", 9 - #tostring(addressBook[currentSelection].loc.y)))
- term.setCursorPos(42, 15)
- term.write(tostring(addressBook[currentSelection].loc.z):sub(1, 9) .. string.rep(" ", 9 - #tostring(addressBook[currentSelection].loc.z)))
- drawSwitch(25, 17, addressBook[currentSelection].callDrop)
- drawSwitch(42, 17, addressBook[currentSelection].iris)
- if gateChange then drawSaveButton() end
- end
- local function drawAddressButtons(override)
- local txtColor, bgColor = term.getTextColor(), term.getBackgroundColor()
- local curX, curY = term.getCursorPos()
- local yPos, line, textLine, addr = 5, " "
- pageNum = override or pageNum
- for i = ((pageNum - 1) * numPerPage) + 1, ((pageNum - 1) * numPerPage) + 4 do
- if i > abCount then
- term.setBackgroundColor(colors.black) --# instead of clearing the area before calling drawAddressButtons() on the last page
- textLine = line --# we draw blanks at the end of the list - this avoids flicker
- else
- term.setTextColor(i == currentSelection and colors.blue or colors.white)
- addr = addressBook[i].addr
- if (sgState == "Dialling" or sgState == "Dialing" or sgState == "Opening" or sgState == "Connected") and (addr == dialTarget or addr:sub(1, 7) == dialTarget or addr == dialTarget:sub(1, 7)) then
- term.setBackgroundColor(colors.lightBlue)
- else
- term.setBackgroundColor(i % 2 == 0 and colors.gray or colors.lightGray)
- end
- textLine = string.rep(" ", math.floor((12 - #addressBook[i].name) / 2)) .. addressBook[i].name .. string.rep(" " ,math.ceil((12 - #addressBook[i].name) / 2))
- end
- for j = 1, 3 do
- yPos = yPos + 1
- term.setCursorPos(1, yPos)
- term.write(j == 2 and textLine or line)
- end
- end
- if gettingInput then repositionCursor(curX, curY, txtColor, bgColor) end
- end
- local function drawFuelPercent()
- local txtColor, bgColor = term.getTextColor(), term.getBackgroundColor()
- local curX, curY = term.getCursorPos()
- term.setTextColor(colors.lightGray)
- term.setCursorPos(42, 4)
- term.write("Fuel: ")
- local fuelPercent = math.min(100, math.floor((nrgAvailable / MAXFUEL) * 100))
- local fuelColor = fuelPercent > 24 and colors.lime or colors.orange
- fuelColor = fuelPercent < 6 and colors.red or fuelColor
- term.setTextColor(fuelColor)
- term.write(tostring(fuelPercent))
- term.setTextColor(colors.lightGray)
- term.write("% ")
- if gettingInput then repositionCursor(curX, curY, txtColor, bgColor) end
- end
- do
- local stateTable = {
- Incoming = "In : ";
- Outgoing = "Out: ";
- Opening = colors.lightBlue;
- Connected = colors.lime;
- Closing = colors.orange;
- }
- drawStargateState = function()
- local txtColor, bgColor = term.getTextColor(), term.getBackgroundColor()
- local curX, curY = term.getCursorPos()
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(13, 4)
- term.write(string.rep(" ", termX - 11))
- if sgState == "Dialling" or sgState == "Dialing" or sgState == "Opening" or sgState == "Closing" or sgState == "Connected" then
- term.setCursorPos(14, 4)
- term.setTextColor(colors.lightGray)
- term.write(stateTable[callDirection] or "Unk: ")
- if sgState == "Dialling" or sgState == "Dialing" then
- term.write(dialTarget)
- term.setCursorPos(19, 4)
- term.setTextColor(colors.yellow)
- term.write(dialTarget:sub(1, chevronsEngaged))
- else
- term.setTextColor(stateTable[sgState])
- term.write(dialTarget)
- end
- for i = 1, abCount do
- if addressBook[i].addr == dialTarget then
- term.setCursorPos(28, 4)
- term.setTextColor(colors.lightGray)
- term.write("/" .. addressBook[i].name)
- break
- end
- end
- elseif sgState == "Offline" then
- term.setCursorPos(14, 4)
- term.setTextColor(colors.red)
- term.write("Gate OFF-LINE")
- end
- drawFuelPercent()
- if gettingInput then repositionCursor(curX, curY, txtColor, bgColor) end
- if currentSelection == 0 then paintutils.drawFilledBox(13, 6, termX, termY, colors.black) end --# if coming from the logs view, clear the data area
- end
- end
- local function drawHeader()
- paintutils.drawFilledBox(4, 1, termX - 3, 3, colors.blue)
- term.setTextColor(colors.lightBlue)
- local headerText = thisGate
- for i = 1, abCount do
- if addressBook[i].addr == thisGate then
- headerText = addressBook[i].name .. " (" .. thisGate .. ")"
- break
- end
- end
- term.setCursorPos(math.floor((termX - #headerText) / 2), 2)
- term.write(headerText)
- end
- do
- local helpTable = {
- [1] = { 6, "Middle click an entry to delete", "" };
- [2] = { 8, "Hold shift when scrolling to jump to", "the beginning or end of the list" };
- [3] = { 11, "Log: Click 'X' to clear the logs", "" };
- [4] = { 13, "Log: Left click an address in the log", " to dial" };
- [5] = { 16, "Log: Right click an address in the", " log to add to the address book" };
- }
- drawHelp = function()
- paintutils.drawFilledBox(13, 6, termX, termY, colors.black)
- term.setTextColor(colors.white)
- for i = 1, 5 do
- term.setCursorPos(14, helpTable[i][1])
- term.write(helpTable[i][2])
- term.setCursorPos(14, helpTable[i][1] + 1)
- term.write(helpTable[i][3])
- end
- end
- end
- local function drawLogData()
- local currentEntry = ((logPage - 1) * 12) + 1 --# Set the first entry to show (based on page number)
- local spacer, line, callHistoryCount = " ", string.rep(" ", termX - 11), #callHistory
- local callDir, callAddr, gName, addr
- for i = currentEntry, currentEntry + 11 do --# Display logs
- if i > callHistoryCount then --# if we've reached the end of the log then draw blank lines to clear entries from the previous page
- term.setCursorPos(13, i - currentEntry + 6)
- term.write(line)
- else
- callDir = callHistory[i]:sub(callHistory[i]:find("<") + 1, callHistory[i]:find(">") - 1) --# get call direction
- term.setTextColor((callDir == "Inbound" or callDir == "Incoming") and colors.lightGray or ((callDir == "Outbound" or callDir == "Outgoing") and colors.gray or colors.red))
- term.setCursorPos(14, i - currentEntry + 6) --# position entry
- term.write(callHistory[i]:sub(1, callHistory[i]:find("@") - 2) .. " ") --# write day
- term.setCursorPos(20, i - currentEntry + 6) --# position entry
- term.write(callHistory[i]:sub(callHistory[i]:find("@") + 2, callHistory[i]:find("M")) .. " ") --# write time
- term.setCursorPos(30, i - currentEntry + 6) --# position entry
- term.write(callDir .. " ") --# write direction
- term.setCursorPos(39, i - currentEntry + 6) --# position entry
- term.setTextColor((callDir == "Inbound" or callDir == "Incoming") and colors.lightBlue or ((callDir == "Outbound" or callDir == "Outgoing") and colors.cyan or colors.orange))
- callAddr = callHistory[i]:sub(callHistory[i]:find(">") + 2) --# get address
- gName = nil
- for j = 1, abCount do
- addr = addressBook[j].addr
- if addr == callAddr or addr:sub(1, 7) == callAddr or addr == callAddr:sub(1, 7) then
- gName = addressBook[j].name
- break
- end
- end
- term.write((gName or callAddr) .. string.rep(" ", 12 - (gName and #gName or #callAddr))) --# write address
- end
- end
- end
- local function drawLogs()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(13, 4)
- term.write(" Day Time Vector Address ")
- term.setCursorPos(12, termY)
- term.write(" << < > >> ")
- local pNum = tostring(logPage) .. " of " .. tostring(logPages)
- term.setCursorPos(math.floor((termX - #pNum) / 2) + 7, termY)
- term.write(pNum)
- term.setBackgroundColor(colors.black)
- if callHistory[1] then
- drawLogData()
- else
- term.setCursorPos(24, 10)
- term.setTextColor(colors.gray)
- term.write("No Logs")
- end
- end
- local function ingestLogs()
- if currentSelection ~= 0 or help then
- help = false
- currentSelection = 0
- button.setActive(1, false)
- drawAddressButtons()
- paintutils.drawFilledBox(13, 6, termX, termY - 1, colors.black)
- end
- for i = #callHistory, 1, -1 do
- callHistory[i] = nil
- end
- if fs.exists("/data/DHDhistory") then
- for log_entry in io.lines("/data/DHDhistory") do
- table.insert(callHistory, 1, log_entry)
- end
- end
- logPages = math.max(1, math.ceil(#callHistory / 12)) --# paginate call logs
- drawLogs()
- end
- local function changePage(id)
- pageNum = button.getText(id) == " \\/ " and (shift and numPages or math.min(pageNum + 1, numPages)) or (shift and 1 or math.max(pageNum - 1, 1))
- drawAddressButtons()
- return true
- end
- local function enableDisablePageButtons()
- for id = 1, button.getButtonCount() do
- if button.getText(id) == " /\\ " or button.getText(id) == " \\/ " then
- button.setActive(id, numPages > 1)
- button.render(id)
- end
- end
- end
- local function addAddress(id, mBtn, newAddress)
- local oldPages = numPages
- abCount = abCount + 1
- addressBook[abCount] = { name = newAddress or "NEW GATE", addr = newAddress or "ADDRESS", rating = "U", iris = "none", callDrop = false, note = newAddress and "Added from logs" or "short note", loc = { x = 0, y = 0, z = 0, dim = "Unspecified" } } --# ...create new entry
- numPages = math.ceil(abCount / numPerPage)
- if numPages > oldPages then enableDisablePageButtons() end
- gateChange = true
- if help then paintutils.drawFilledBox(13, 6, termX, termY, colors.black) help = false drawLabels() end --# if coming from help, clear the data area
- if currentSelection == 0 then drawStargateState() drawLabels() end
- currentSelection = abCount
- drawDetails(currentSelection)
- drawAddressButtons(numPages)
- return true
- end
- local function deleteAddress(entry)
- if abCount > 1 then
- local oldPages = numPages
- if currentSelection > entry or (currentSelection == entry and entry == abCount) then currentSelection = math.max(1, currentSelection - 1) end
- table.remove(addressBook, entry)
- abCount = abCount - 1
- numPages = math.ceil(abCount / numPerPage)
- pageNum = math.min(pageNum, numPages)
- if numPages < oldPages then enableDisablePageButtons() end
- gateChange = true
- drawAddressButtons()
- drawHeader()
- if currentSelection ~= 0 and not help then drawDetails(currentSelection) end
- return true
- end
- return false
- end
- do
- local dataTable = { --# color, x, y, clear line, character limit, symbol match, history entry
- name = { colors.cyan, 20, 6, " ", 12, false, { addressBook[currentSelection].addr, addressBook[currentSelection].name } };
- addr = { colors.yellow, 23, 8, " ", 9, true, { addressBook[currentSelection].addr } };
- note = { colors.white, 23, 12, string.rep(" ", 28), 43, false, { addressBook[currentSelection].note } };
- dim = { colors.brown, 23, 14, string.rep(" ", 19), 19, false, { "Overworld", "Nether", "The End", addressBook[currentSelection].loc.dim } };
- x = { colors.lightGray, 16, 15, " ", 9, false, { addressBook[currentSelection].loc.x } };
- y = { colors.lightGray, 29, 15, " ", 9, false, { addressBook[currentSelection].loc.y } };
- z = { colors.lightGray, 42, 15, " ", 9, false, { addressBook[currentSelection].loc.z } };
- }
- editEntry = function(which)
- term.setBackgroundColor(colors.black)
- term.setTextColor(dataTable[which][1])
- term.setCursorPos(dataTable[which][2], dataTable[which][3])
- term.write(dataTable[which][4])
- if which == "note" then
- term.setCursorPos(23, 13)
- term.write(string.rep(" ", 15))
- end
- term.setCursorPos(dataTable[which][2], dataTable[which][3])
- local userData = read(nil, dataTable[which][7], dataTable[which][5], nil, nil, dataTable[which][6])
- if which == "name" then
- if userData ~= "" and userData ~= addressBook[currentSelection].name then addressBook[currentSelection].name = userData gateChange = true drawHeader() drawAddressButtons() end
- elseif which == "addr" then
- if #userData == 7 or #userData == 9 and not userData:find("?") and userData ~= addressBook[currentSelection].addr then addressBook[currentSelection].addr = userData gateChange = true drawHeader() end
- elseif which == "note" then
- if userData ~= "" and userData ~= addressBook[currentSelection].note then addressBook[currentSelection].note = userData gateChange = true end
- elseif which == "dim" then
- if userData ~= "" and userData ~= addressBook[currentSelection].loc.dim then addressBook[currentSelection].loc.dim = userData gateChange = true end
- elseif which == "x" then
- local newX = tonumber(userData)
- if newX and newX ~= addressBook[currentSelection].loc.x then addressBook[currentSelection].loc.x = newX gateChange = true end
- elseif which == "y" then
- local newY = tonumber(userData)
- if newY and newY ~= addressBook[currentSelection].loc.y then addressBook[currentSelection].loc.y = newY gateChange = true end
- elseif which == "z" then
- local newZ = tonumber(userData)
- if newZ and newZ ~= addressBook[currentSelection].loc.z then addressBook[currentSelection].loc.z = newZ gateChange = true end
- end
- drawDetails(currentSelection)
- end
- end
- local function toggleIris()
- if irisState == "Closed" or irisState == "Closing" then
- sg.openIris()
- elseif irisState == "Open" or irisState == "Opening" then
- sg.closeIris()
- end
- end
- local function exitScreen()
- if help then --# Exit Help
- help = false
- paintutils.drawFilledBox(13, 6, termX, termY - 1, colors.black)
- if currentSelection == 0 then
- drawLogs()
- else
- drawLabels()
- drawDetails(currentSelection)
- drawAddressButtons()
- end
- elseif currentSelection == 0 and callHistory[1] then --# Clear Logs
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.setCursorPos(21, 8)
- term.write(" Clear Logs? ")
- paintutils.drawFilledBox(21, 9, 33, 11, colors.gray)
- term.setBackgroundColor(colors.green)
- term.setCursorPos(22, 10)
- term.write(" Yes ")
- term.setBackgroundColor(colors.red)
- term.setCursorPos(28, 10)
- term.write(" N o ")
- while true do
- local _, data, x, y = os.pullEvent("mouse_click")
- if y == 10 and x > 21 and x < 33 and data == 1 then
- if x > 21 and x < 27 then
- local logData = fs.open("/data/DHDhistory", "w")
- logData.close()
- paintutils.drawFilledBox(13, 6, termX, termY - 1, colors.black)
- ingestLogs()
- break
- elseif x > 27 and x < 33 then
- paintutils.drawFilledBox(20, 8, 33, 11, colors.black)
- drawLogData()
- break
- end
- end
- end
- elseif currentSelection == 0 then --# Exit Logs
- drawStargateState()
- drawDetails(((pageNum - 1) * numPerPage) + 1)
- drawAddressButtons()
- else --# Exit Program
- if gateChange then
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.setCursorPos(15, 8)
- term.write(" Save Address Book? ")
- paintutils.drawFilledBox(15, 9, 34, 11, colors.gray)
- term.setBackgroundColor(colors.green)
- term.setCursorPos(18, 10)
- term.write(" Yes ")
- term.setBackgroundColor(colors.red)
- term.setCursorPos(27, 10)
- term.write(" N o ")
- while true do
- local _, data, x, y = os.pullEvent("mouse_click")
- if y == 10 and x > 17 and x < 32 then
- if x > 17 and x < 23 then --# Yes
- local sgData = fs.open("/data/DHDgates", "w")
- sgData.write(textutils.serialize(addressBook))
- sgData.close()
- break
- elseif x > 26 and x < 32 then --# No
- break
- end
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- error()
- end
- end
- local function userInput()
- while true do
- local event, data, x, y = os.pullEvent()
- if event == "mouse_click" then
- local success, btnID, returnData = button.check(x, y, data) --# check pre-defined buttons (via buttonBuddy)
- if not success then --# if no pre-defined buttons were clicked, check the address book...
- if x < 13 and y > 5 and y < 18 then --# select a gate entry
- local entry = ((pageNum - 1) * numPerPage) + 1
- for i = 6, 17, 3 do
- if y == i or y == i + 1 or y == i + 2 then
- if data == 1 then
- if help then --# Exit help and display selected gate entry
- help = false
- paintutils.drawFilledBox(13, 6, termX, termY - 1, colors.black)
- drawLabels()
- end
- if currentSelection == 0 then drawStargateState() end --# if returning from the log screen, draw the stargate state
- drawDetails(entry)
- drawAddressButtons()
- paintutils.drawLine(12, termY, 13, termY, colors.black)
- elseif data == 3 then --# Delete gate entry
- deleteAddress(entry)
- end
- break
- end
- entry = entry + 1
- if entry > abCount then break end
- end
- elseif not help then
- if currentSelection ~= 0 and data == 1 then --# Edit gate details
- if y == 6 and x > 19 and x < 20 + #addressBook[currentSelection].name then --# Name
- editEntry("name")
- elseif y == 8 and x > 22 and x < 23 + #addressBook[currentSelection].addr then --# Address
- editEntry("addr")
- elseif y == 10 and x > 22 and x < 23 + #assignRating(addressBook[currentSelection].rating) then --# Rating
- selectRating()
- elseif y == 12 and x > 22 and x < 23 + #addressBook[currentSelection].note then --# Note
- editEntry("note")
- elseif y == 14 and x > 22 and x < 23 + #addressBook[currentSelection].loc.dim then --# Dimension
- editEntry("dim")
- elseif y == 15 and x > 15 and x < 42 + #tostring(addressBook[currentSelection].loc.z) then --# X, Y, Z
- if x > 15 and x < 16 + #tostring(addressBook[currentSelection].loc.x) then --# X Coord (x > 15 and x < 24)
- editEntry("x")
- elseif x > 28 and x < 29 + #tostring(addressBook[currentSelection].loc.y) then --# Y Coord (x > 28 and x < 39)
- editEntry("y")
- elseif x > 41 and x < 42 + #tostring(addressBook[currentSelection].loc.z) then --# Z Coord (x > 41 and x < 52)
- editEntry("z")
- end
- elseif y == 17 and x > 24 and x < 46 then --# Switches
- if x > 24 and x < 29 then --# callDrop
- addressBook[currentSelection].callDrop = not addressBook[currentSelection].callDrop
- gateChange = true
- drawSwitch(25, 17, addressBook[currentSelection].callDrop)
- elseif x > 41 and x < 46 then --# auto-iris
- if addressBook[currentSelection].iris == "none" then
- addressBook[currentSelection].iris = true
- elseif addressBook[currentSelection].iris then
- addressBook[currentSelection].iris = false
- else
- addressBook[currentSelection].iris = "none"
- end
- gateChange = true
- drawSwitch(42, 17, addressBook[currentSelection].iris)
- end
- if gateChange then drawSaveButton() end
- elseif y == termY and x > 11 and gateChange then --# Save address book
- local sgData = fs.open("/data/DHDgates", "w")
- sgData.write(textutils.serialize(addressBook))
- sgData.close()
- gateChange = false
- term.setBackgroundColor(colors.black)
- term.setCursorPos(12, termY)
- term.write(string.rep(" ", termX - 11))
- end
- elseif currentSelection == 0 then --# Logs
- if y == termY and x > 20 and x < 43 and data == 1 then
- if x > 20 and x < 23 then
- logPage = 1
- drawLogData()
- elseif x == 24 then
- logPage = math.max(1, logPage - 1)
- drawLogData()
- elseif x == 39 then
- logPage = math.min(logPage + 1, logPages)
- drawLogData()
- elseif x > 40 and x < 43 then
- logPage = logPages
- drawLogData()
- end
- elseif x > 38 and x < termX and y > 5 and y < 18 then
- local magicNumber, abEntry, callAddr, addr = ((logPage - 1) * 12) + 1, 0
- if callHistory[magicNumber + y - 6] then
- callAddr = callHistory[magicNumber + y - 6]:sub(callHistory[magicNumber + y - 6]:find(">") + 2) --# get logged address
- for i = 1, abCount do
- addr = addressBook[i].addr
- if addr == callAddr or addr:sub(1, 7) == callAddr or addr == callAddr:sub(1, 7) then
- abEntry = i
- break
- end
- end
- local redraw = false
- if data == 1 then --# dial out from logs
- if sgState == "Idle" then sg.dial(callAddr) end
- if abEntry == 0 then
- if sgState == "Idle" then redraw = true end
- else
- redraw = true
- end
- elseif data == 2 then --# add to addressBook from logs
- if abEntry == 0 then
- addAddress(nil, nil, callAddr)
- else
- redraw = true
- end
- end
- if redraw then
- drawStargateState()
- drawDetails(abEntry == 0 and 1 or abEntry)
- drawAddressButtons(abEntry == 0 and 1 or math.ceil(abEntry / numPerPage))
- paintutils.drawLine(12, termY, 13, termY, colors.black)
- end
- end
- end
- end
- end
- end
- elseif event == "mouse_scroll" then
- if currentSelection == 0 then
- logPage = data == 1 and (shift and logPages or math.min(logPage + 1, logPages)) or (shift and 1 or math.max(1, logPage - 1))
- drawLogData()
- else
- pageNum = data == 1 and (shift and numPages or math.min(pageNum + 1, numPages)) or (shift and 1 or math.max(1, pageNum - 1))
- drawAddressButtons()
- end
- elseif event == "key" and (data == keys.f1 or data == keys.home or data == keys["end"] or data == keys.pageUp or data == keys.pageDown or data == keys.leftShift or data == keys.rightShift) then
- if data == keys.home or data == keys["end"] then
- if currentSelection == 0 then
- logPage = data == keys.home and 1 or logPages
- drawLogData()
- else
- pageNum = data == keys.home and 1 or numPages
- drawAddressButtons()
- end
- elseif data == keys.pageUp or data == keys.pageDown then
- if currentSelection == 0 then
- logPage = data == keys.pageDown and math.min(logPage + 1, logPages) or math.max(1, logPage - 1)
- drawLogData()
- else
- pageNum = data == keys.pageDown and math.min(pageNum + 1, numPages) or math.max(1, pageNum - 1)
- drawAddressButtons()
- end
- elseif data == keys.f1 then
- help = not help
- if help then
- button.setActive(1, false)
- drawHelp()
- else
- paintutils.drawFilledBox(13, 6, termX, termY - 1, colors.black)
- if currentSelection == 0 then
- drawLogs()
- else
- drawLabels()
- drawDetails(currentSelection)
- drawAddressButtons()
- end
- end
- else
- shift = not shift --# if the running version of CC doesn't support key_up, this allows shift to be toggled on and off
- end
- elseif event == "key_up" and (data == keys.leftShift or data == keys.rightShift) then
- shift = false
- elseif event == "char" and data:lower() == "q" then
- exitScreen()
- end
- end
- end
- local function dataPoller()
- local _, timer, newFuel, update
- while true do
- _, timer = os.pullEvent("timer")
- if timer == updateTimer then
- newFuel = math.min(math.floor((nrgAvailable / MAXFUEL) * 100), 100)
- update = newFuel ~= fuelPercent
- fuelPercent = newFuel
- if update and currentSelection ~= 0 then term.setBackgroundColor(colors.gray) drawFuelPercent() end
- if sgState == "Connected" and callDirection == "Outgoing" then updateTimer = os.startTimer(5) end
- end
- end
- end
- local function monitorDialButtonRedraw()
- while true do
- os.pullEvent("drawSGDialButton")
- if currentSelection ~= 0 then
- local curX, curY = term.getCursorPos()
- local txtColor, bgColor = term.getTextColor(), term.getBackgroundColor()
- local nrgToDial, ableToDial = sg.energyToDial(addressBook[currentSelection].addr)
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(34, 6)
- if sgState == "Offline" then
- ableToDial = false
- term.setTextColor(colors.red)
- term.write(" O F F - L I N E ")
- elseif addressBook[currentSelection].addr == thisGate then
- ableToDial = false
- term.setTextColor(colors.orange)
- term.write(" Not Applicable ")
- elseif not nrgToDial then
- ableToDial = false
- term.setTextColor(colors.orange)
- term.write(" Invalid Address ")
- else
- ableToDial = (nrgToDial or 9999999999) <= nrgAvailable
- term.setTextColor(ableToDial and colors.lime or colors.orange)
- term.write(ableToDial and " Sufficient Fuel " or "Insufficient Fuel")
- end
- if sgState == "Dialling" or sgState == "Dialing" or sgState == "Opening" or sgState == "Connected" then
- button.setText(1, " END. ")
- button.setColors(1, nil, colors.orange)
- button.setFunction(1, function() sg.disconnect() end)
- button.setActive(1, true)
- else
- button.setText(1, " Dial ")
- button.setColors(1, nil, colors.green)
- button.setFunction(1, function() sg.dial(addressBook[currentSelection].addr) end)
- button.setActive(1, ableToDial)
- end
- button.render(1)
- if gettingInput then repositionCursor(curX, curY, txtColor, bgColor) end
- end
- end
- end
- local function monitorGate()
- local ableToDial, event, _, x, lastState, dialAddress
- while true do
- event, _, x, lastState = os.pullEvent()
- local curX, curY = term.getCursorPos()
- local txtColor, bgColor = term.getTextColor(), term.getBackgroundColor()
- if event == "sgDialIn" then
- dialTarget = sg.remoteAddress() --# dialTarget = x
- callDirection = "Incoming"
- sgState = "Dialing"
- if currentSelection ~= 0 then
- term.setCursorPos(14, 4)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- term.write("In : " .. dialTarget)
- os.queueEvent("drawSGDialButton")
- end
- for i = 1, abCount do
- if addressBook[i].addr == dialTarget or addressBook[i].addr:sub(1, 7) == dialTarget or addressBook[i].addr == dialTarget:sub(1, 7) then
- if currentSelection ~= 0 then term.write("/" .. addressBook[i].name) end
- --if currentSelection ~= 0 then drawDetails(i) end
- drawAddressButtons()
- if addressBook[i].callDrop then
- sg.disconnect()
- end
- if type(addressBook[i].iris) == "boolean" then
- if addressBook[i].iris then
- sg.openIris()
- else
- sg.closeIris()
- end
- end
- break
- end
- end
- elseif event == "sgDialOut" then
- dialTarget = sg.remoteAddress() --# dialTarget = x
- callDirection = "Outgoing"
- sgState = "Dialing"
- if currentSelection ~= 0 then
- term.setCursorPos(14, 4)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- term.write("Out: " .. dialTarget)
- for i = 1, abCount do
- if addressBook[i].addr == dialTarget then
- term.write("/" .. addressBook[i].name)
- break
- end
- end
- os.queueEvent("drawSGDialButton")
- end
- drawAddressButtons()
- elseif event == "sgChevronEngaged" then
- chevronsEngaged = x
- if chevronsEngaged == 1 and dialTarget == "none" then
- callDirection = "Outgoing"
- dialTarget = lastState .. string.rep("?", 8)
- if currentSelection ~= 0 then os.queueEvent("drawSGDialButton") end
- elseif x > 1 and dialTarget:find("?") then
- dialTarget = dialTarget:sub(1, chevronsEngaged - 1) .. lastState .. string.rep("?", 9 - chevronsEngaged)
- end
- if currentSelection ~= 0 then
- term.setCursorPos(chevronsEngaged == 1 and 14 or 19, 4)
- term.setBackgroundColor(colors.gray)
- if chevronsEngaged == 1 then
- term.setTextColor(colors.lightGray)
- term.write("Out: ")
- end
- term.setTextColor(colors.yellow)
- dialAddress = dialTarget:sub(1, chevronsEngaged)
- term.write(dialAddress)
- end
- sgState = "Dialing"
- elseif event == "sgChevronUnset" then
- chevronsEngaged = x
- if chevronsEngaged == 0 then
- sgState, dialTarget, callDirection = "Idle", "none", "none"
- if currentSelection ~= 0 then
- os.queueEvent("drawSGDialButton")
- term.setCursorPos(14, 4)
- term.setBackgroundColor(colors.gray)
- term.write(" ")
- end
- else
- dialTarget = dialTarget:sub(1, chevronsEngaged) .. string.rep("?", 9 - chevronsEngaged)
- if currentSelection ~= 0 then
- dialAddress = dialTarget:sub(1, chevronsEngaged)
- term.setCursorPos(19, 4)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.yellow)
- term.write(dialAddress .. " ")
- end
- end
- elseif event == "sgStargateStateChange" then
- term.setCursorPos(14, 4)
- term.setBackgroundColor(colors.gray)
- sgState = x
- if sgState == "Idle" or sgState == "Offline" then
- if currentSelection ~= 0 then
- term.write(string.rep(" ", 26))
- os.queueEvent("drawSGDialButton")
- if sgState == "Offline" then
- term.setCursorPos(14, 4)
- term.setTextColor(colors.red)
- term.write("Gate OFF-LINE")
- end
- drawFuelPercent()
- end
- chevronsEngaged = 0
- callDirection, dialTarget = "none", "none"
- drawAddressButtons()
- elseif (sgState == "Opening" or sgState == "Closing") and currentSelection ~= 0 then
- term.setTextColor(sgState == "Opening" and colors.lightBlue or colors.orange)
- term.setCursorPos(19, 4)
- term.write(dialTarget)
- elseif sgState == "Connected" then
- sgState, chevronsEngaged, callDirection = sg.stargateState() --# this is only necessary if ccDHDbasic is started while the gate is dialing (direction is only exposed upon connection currently)
- if callDirection == "Outgoing" then
- nrgAvailable = sg.energyAvailable()
- updateTimer = os.startTimer(5)
- if dialTarget:find("?") then dialTarget = dialTarget:sub(1, dialTarget:find("?") - 1) end
- end
- if currentSelection ~= 0 then
- term.setTextColor(colors.lime)
- term.setCursorPos(19, 4)
- term.write(dialTarget)
- for i = 1, abCount do
- if addressBook[i].addr == dialTarget or addressBook[i].addr:sub(1, 7) == dialTarget or addressBook[i].addr == dialTarget:sub(1, 7) then
- term.setTextColor(colors.yellow)
- term.write("/" .. addressBook[i].name)
- break
- end
- end
- drawFuelPercent()
- end
- local logAddress = dialTarget or "ERROR" --# set logAddress
- local callDir = callDirection == "Incoming" and "Incoming" or "Outgoing" --# set callDirection
- local callTime = textutils.formatTime(os.time(), false) --# format the time
- if #callTime == 7 then callTime = " " .. callTime end --# move single digit hour times to the right one space
- local dataLine = tostring(os.day()) .. " @ " .. callTime .. " <" .. callDir .. "> " .. logAddress
- local callArchive = fs.open("/data/DHDhistory", fs.exists("/data/DHDhistory") and "a" or "w")
- callArchive.writeLine(dataLine)
- callArchive.close()
- local previousCall = fs.open("/data/DHDlastCall", "w") --# record last call
- previousCall.writeLine(dataLine)
- previousCall.close()
- end
- elseif event == "sgIrisStateChange" then
- irisState = x
- if irisState == "Offline" then
- button.setActive(2, false)
- elseif irisState == "Open" or irisState == "Closed" then
- if lastState == "Offline" then button.setActive(2, true) end --# If the iris was previously offline then activate the button
- button.setColors(2, colors.white, irisState == "Open" and colors.orange or colors.green)
- elseif irisState == "Opening" or irisState == "Closing" then
- if lastState == "Offline" then button.setActive(2, true) end --# If the iris was previously offline then activate the button
- if (irisState == "Opening" and lastState == "Closing") or (irisState == "Closing" and lastState == "Opening") then
- button.setColors(2, nil, irisState == "Opening" and colors.green or colors.orange)
- end
- button.setColors(2, irisState == "Opening" and colors.orange or colors.green)
- end
- button.render(2)
- end
- if gettingInput then repositionCursor(curX, curY, txtColor, bgColor) end
- end
- end
- button.create(" Dial ", nil, 45, 8, 1, 3, 1, false, function() sg.dial(addressBook[currentSelection].addr) end, colors.white, colors.green)
- button.create(" I r i s ", nil, 1, 4, 12, 1, 1, irisState ~= "Offline", toggleIris, colors.white, (irisState == "Open" or irisState == "Closing") and colors.orange or colors.green, colors.gray, colors.lightGray)
- button.create("-", nil, 1, 1, 3, 3, 1, true, ingestLogs, colors.white, colors.lightBlue) --# logs button (center dash on burger)
- button.create(" X ", nil, termX - 2, 1, 3, 3, 1, true, exitScreen, colors.white, colors.red)
- button.create(" /\\ ", nil, 1, termY, 4, 1, 1, numPages > 1, changePage, colors.white, colors.cyan)
- button.create(" \\/ ", nil, 8, termY, 4, 1, 1, numPages > 1, changePage, colors.white, colors.cyan)
- button.create(" + ", nil, 5, termY, 1, 1, 1, true, addAddress, colors.white, colors.green)
- term.setBackgroundColor(colors.black)
- term.clear()
- drawHeader()
- button.render()
- term.setBackgroundColor(colors.lightBlue) --# Finish the burger for the logs button
- term.setTextColor(colors.white)
- term.setCursorPos(2, 1)
- write("-\n\n -")
- drawStargateState()
- drawAddressButtons()
- term.setBackgroundColor(colors.black)
- drawLabels()
- drawDetails(1)
- if sgState == "Connected" and callDirection == "Outgoing" then updateTimer = os.startTimer(5) end
- parallel.waitForAny(userInput, dataPoller, monitorDialButtonRedraw, monitorGate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement