Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ LanteaCraft ]]--
- --[[ ccDialer ]]--
- --[[ core ver. 1.0.0 ]]--
- --[[ ui ver. 1.0.0 ]]--
- --[[ by Dog ]]--
- --[[ aka HydrantHunter ]]--
- --[[ pastebin WN9wLUnu ]]--
- --[[ GPL v3 ]]--
- local ccDialVer = "1.0.01"
- --[[
- Tested with/requires:
- - Minecraft 1.6.4
- - LanteaCraft (RC1)
- - ComputerCraft ver. 1.61+ for MC 1.6.4
- - HTTP API enabled (for pastebin installer - not required for ccDHD)
- - 1 Advanced Portable Computer (color, mouse)
- - ccDHD running on an Advanced Computer (with a wireless modem)
- - gateLiaison running on a Computer (standard or advanced) with a wireless modem or a Wireless Turtle (standard or advanced)
- - ccDHD also supports the Biometric Lock (Fistprint Scanner) in GopherATL's Biolock MOD ver. 2.1.3 (updated for 1.6.4 by gamax92)
- ]]--
- -- AUTOMATIC/STATIC CONFIGURATION
- -- Default Settings
- local dialSettings = { pChannels={send=24242,recv=24747,},
- gateData="/data/DHDgates",
- }
- local allGates = { {addr="NOGATES",rating="U",note="Example gate",loc={x=99999,y=99999,z=99999,dim="Overworld",},}, }
- local netSide = "none"
- local longName, tY
- -- Peripherals
- local termX,termY = term.getSize() -- portable 26x20 / standard 51x19 / cBang & OneOS = 51x18
- local modem = nil -- Modem (for ccNet)
- -- Status Info
- local kernelState = true
- local runState = "init"
- local currentState = "ZZ"
- local tempState, currentEdit
- local err = "NO"
- local i,j,v,w
- -- Gate List
- local numPages,pageNum = 1,1
- -- Color Definitions
- local white = colors.white
- local lgray = colors.lightGray
- local gray = colors.gray
- local black = colors.black
- local brown = colors.brown
- local yellow = colors.yellow
- local orange = colors.orange
- local red = colors.red
- local magenta = colors.magenta
- local purple = colors.purple
- local blue = colors.blue
- local lblue = colors.lightBlue
- local cyan = colors.cyan
- local lime = colors.lime
- local green = colors.green
- -- END AUTOMATIC/STATIC CONFIGURATION
- local function pSync(pushpull) -- Sync allGates with an advanced computer running ccDHD
- modem.transmit(dialSettings.pChannels.send,dialSettings.pChannels.recv,pushpull)
- if pushpull == "pPull" then
- local netEvent = { os.pullEvent("modem_message") }
- if netEvent[3] == dialSettings.pChannels.recv and netEvent[4] == dialSettings.pChannels.send then
- local newGateData = netEvent[5]
- allGates = textutils.unserialize(newGateData)
- numPages = math.ceil(#allGates / 14)
- return
- end
- elseif pushpull == "pPush" then
- sleep(0.1)
- local pGateData = textutils.serialize(allGates)
- modem.transmit(dialSettings.pChannels.send,dialSettings.pChannels.recv,pGateData)
- return
- end
- end
- local function netSend(dataPack)
- modem.transmit(dialSettings.pChannels.send,dialSettings.pChannels.recv,dataPack)
- end
- local function saveData()
- if not fs.exists("/data") then fs.makeDir("/data") end
- local dhdConfig = fs.open("/data/DHDgates","w") or error("saveData(): Cannot open /data/DHDgates for writing", 2)
- dhdConfig.write(textutils.serialize(allGates))
- dhdConfig.close()
- end
- local function ingestData()
- if not fs.exists("/data/DHDgates") then return end
- local dhdConfig = fs.open("/data/DHDgates","r") or error("ingestData(): Cannot open /data/DHDgates for reading", 2)
- local dhdCfg = dhdConfig.readAll()
- dhdConfig.close()
- allGates = textutils.unserialize(dhdCfg)
- numPages = math.ceil(#allGates / 14)
- end
- local function assignColor(gateNumber)
- local colorBurst = { B = blue, H = lblue, V = brown, M = purple, S = green, C = orange, D = red, U = lgray, X = magenta }
- for v,w in pairs(colorBurst) do
- if allGates[gateNumber].rating == tostring(v) then
- return w
- end
- end
- end
- local function assignRating(gateNumber)
- local ratingBurst = { B = "Base/Outpost/Hub", H = "Home/Camp", V = "Village", M = "Misc/Special", S = "Safe", C = "Caution", D = "Danger", U = "Unknown", Z = "Zog" }
- for i,j in pairs(ratingBurst) do
- if allGates[gateNumber].rating == i then
- return j
- end
- end
- end
- local function drawElement(x,y,w,h,txColor,bgColor,text)
- local deText = tostring(text)
- term.setCursorPos(x,y)
- term.setBackgroundColor(bgColor)
- if w > #deText or h > 1 then -- We're 'drawing' something more than text
- for i = 1,h,1 do
- term.write(string.rep(" ",w)) -- Draw the 'element' (box/rectangle/line-seg)
- term.setCursorPos(x,y+i) --
- end
- end
- if deText ~= "" and deText ~= "nil" then
- term.setTextColor(txColor)
- if w < #deText then w = #deText end
- local xW = (x + w/2) - #deText/2
- local xH = y + h/2
- term.setCursorPos(xW,xH)
- term.write(deText)
- end
- end
- local function drawHeader()
- local title = "ccDialer " .. ccDialVer
- if termX < 19 then title = "ccDialer" end
- drawElement(1,1,termX,1,white,blue,title) -- title bar
- drawElement(termX-2,1,3,1,white,blue,"[ ]")
- drawElement(termX-1,1,1,1,red,blue,"X")
- if runState == "Dial" then
- local hSync = string.rep(" ",math.floor(termX/4 - 2)) .. "Sync" .. string.rep(" ",math.floor(termX/4 - 2))
- local hEnd = string.rep(" ",math.ceil(termX/4 - 4)) .. "End Call" .. string.rep(" ",math.ceil(termX/4 - 4))
- drawElement(1,2,#hSync,1,white,cyan,hSync)
- drawElement(termX/2,2,#hEnd,1,white,red,hEnd)
- elseif runState == "pSync" then
- drawElement(1,2,termX,1,lgray,gray,"Sync")
- elseif runState == "viewing" then
- drawElement(1,2,termX,1,lgray,gray,"Gate Info")
- end
- end
- local function drawPsyncUI()
- drawElement(2,5,termX-2,3,white,green,"GET gate list")
- drawElement(2,9,termX-2,3,white,orange,"SEND gate list")
- drawElement(2,13,termX-2,3,white,red,"C A N C E L")
- end
- local function drawNaviUI()
- local pNum = tostring(pageNum)
- if pageNum < 100 then pNum = "0" .. pNum end -- Add a "0" before double digit page numbers
- if pageNum < 10 then pNum = "0" .. pNum end -- Add another "0" before single digit page numbers
- if pageNum > 1 then
- drawElement((termX/2)-10,termY,7,1,gray,black,"<< BACK") -- Show "BACK" option if page number is > 1
- elseif pageNum == 1 then
- drawElement((termX/2)-10,termY,7,1,gray,black,string.rep(" ",7)) -- Overwrite "BACK" instead of clearing screen
- end
- if pageNum < numPages then
- drawElement(termX/2+4,termY,7,1,gray,black,"NEXT >>") -- Show "NEXT" option if page number is < numPages
- elseif pageNum == numPages then
- drawElement(termX/2+4,termY,7,1,gray,black,string.rep(" ",7)) -- Overwrite "NEXT" instead of clearing screen
- end
- drawElement((termX/2) - (math.floor(pNum:len()/2)),termY,3,1,lgray,black,pNum) -- Page Number
- end
- local function drawMainUI() -- Gate Address Book (Dial / Edit / Trim)
- local xPos, yPos = 2, 2
- local j = ((pageNum - 1) * 13) + pageNum
- local k = ((pageNum - 1) * 14)
- for i = j,#allGates,1 do
- if i > k and i < k + 8 then -- Column 1
- xPos, yPos = 4, 2
- elseif i > k + 6 and i < k + 13 then -- Column 2
- xPos, yPos = 15, -12
- end
- term.setCursorPos(xPos,yPos + (i - k) * 2)
- term.setBackgroundColor(assignColor(i))
- term.setTextColor(white)
- if i > (pageNum * 14) or i > #allGates then
- break
- else
- if #allGates[i].addr < 10 then
- term.write(string.rep(" ",math.ceil((9-#allGates[i].addr)/2)) .. allGates[i].addr .. string.rep(" ",math.floor((9-#allGates[i].addr)/2))) -- Dial
- else
- term.write("ERROR #10") -- ERROR #10 = address is 10 or more characters
- end
- end
- end
- term.setBackgroundColor(black)
- end
- local function drawCLI() -- Client Interface 'decider'
- drawHeader()
- if runState == "viewing" then return end
- if runState == "Dial" then
- drawMainUI()
- drawNaviUI()
- elseif runState == "pSync" then
- drawPsyncUI()
- end
- end
- local function viewGateEntry(dataBlock)
- currentEdit = dataBlock
- tempState = runState
- runState = "viewing"
- term.setBackgroundColor(black)
- term.clear()
- drawHeader()
- -- Display data for viewing/editing
- term.setBackgroundColor(black)
- if currentEdit <= #allGates then
- local teColor = assignColor(currentEdit)
- if teColor ~= nil then
- drawElement(2,4,#allGates[currentEdit].addr,1,teColor,black,allGates[currentEdit].addr)
- local ratingWord = assignRating(currentEdit)
- drawElement(2,6,#ratingWord,1,gray,black,ratingWord)
- drawElement(2,9,#allGates[currentEdit].note,1,white,black,allGates[currentEdit].note)
- drawElement(2,12,4,1,lgray,black,"Dim:")
- drawElement(7,12,#allGates[currentEdit].loc.dim,1,lblue,black,allGates[currentEdit].loc.dim)
- drawElement(2,13,2,1,lgray,black,"x:")
- drawElement(5,13,#tostring(allGates[currentEdit].loc.x),1,brown,black,allGates[currentEdit].loc.x)
- drawElement(2,14,2,1,lgray,black,"y:")
- drawElement(5,14,#tostring(allGates[currentEdit].loc.y),1,brown,black,allGates[currentEdit].loc.y)
- drawElement(2,15,2,1,lgray,black,"z:")
- drawElement(5,15,#tostring(allGates[currentEdit].loc.z),1,brown,black,allGates[currentEdit].loc.z)
- end
- end
- drawElement((termX/2)-8,termY-1,19,1,black,gray," < < < CLOSE > > > ")
- end
- local function goToPage()
- tempState = runState
- runState = "goPage"
- drawElement((termX/2)-7,8,15,1,white,gray," Go To Page: ")
- drawElement((termX/2)-7,9,15,2,white,gray,"")
- drawElement((termX/2)-6,9,13,1,white,black,"") -- input area bg
- term.setBackgroundColor(black)
- --local newPage = tonumber(readInput((termX/2-6),9,lime))
- term.setCursorPos((termX/2)-6,9)
- term.setTextColor(lime)
- local newPage = tonumber(read())
- drawElement((termX/2)-7,8,15,3,white,black,"")
- if newPage == nil then newPage = pageNum end
- if newPage < 1 then newPage = 1 end
- if newPage > numPages then newPage = numPages end
- if newPage == numPages then
- drawElement(2,4,termX,termY,white,black,"")
- end
- pageNum = newPage
- runState = tempState
- drawCLI()
- end
- local function keyClick()
- while true do
- local keyEvent = { os.pullEvent("key") }
- if keyEvent[2] == 16 or keyEvent[2] == 45 then -- q/x - Exit
- kernelState = false
- runState = "quit"
- break
- end
- if runState == "Dial" then
- if keyEvent[2] == 31 then -- s - pSync
- runState = "pSync"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- elseif keyEvent[2] == 18 then -- e - endCall
- netSend("endCall")
- elseif keyEvent[2] == 199 then -- HOME
- pageNum = 1
- drawCLI()
- elseif keyEvent[2] == 207 then -- END
- pageNum = numPages
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- end
- elseif runState == "pSync" then
- if keyEvent[2] == 23 or keyEvent[2] == 34 then -- i/g - Import/Get
- pSync("pPull")
- saveData()
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- elseif keyEvent[2] == 18 or keyEvent[2] == 31 or keyEvent[2] == 25 then -- e/s/p - Export/Send/Put
- pSync("pPush")
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- elseif keyEvent[2] == 46 then -- c - Cancel
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- end
- elseif runState == "viewing" then
- if keyEvent[2] == 46 then -- c - Close
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- end
- end
- end
- end
- local function mClick()
- while true do
- local clickEvent = { os.pullEvent("mouse_click") }
- local mButton = clickEvent[2]
- local mcX = clickEvent[3]
- local mcY = clickEvent[4]
- if mcY == 1 and mcX > termX - 3 and mcX <= termX then
- kernelState = false
- runState = "quit"
- break
- end
- if runState == "viewing" then
- if mcX > 0 and mcX < termX and mcY == termY-1 then
- runState = tempState
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- return
- end
- elseif runState == "Dial" then
- if mcY == 2 then
- if mcX < termX/2 then
- runState = "pSync"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- elseif mcX > termX/2 then
- netSend("endCall")
- end
- -- Dial a listed address or view it's info
- elseif mcY > 3 and mcY < 17 then
- local j = ((pageNum - 1) * 13) + pageNum
- local k = ((pageNum - 1) * 13)
- local l = ((pageNum - 1) * 14)
- for i = j,#allGates,1 do
- -- Addresses Column 1
- if mcX > 3 and mcX < 13 and mcY == 2 + ((i - l) * 2) and i <= #allGates then
- if mButton == 1 and allGates[i].addr ~= "NEWGATE" then
- netSend(allGates[i].addr)
- return
- elseif mButton == 2 then
- viewGateEntry(i)
- return
- end
- -- Addresses Column 2
- elseif mcX > 14 and mcX < 24 and mcY == 2 + ((i - l) * 2) and (i + 7) <= #allGates then
- if mButton == 1 and allGates[i].addr ~= "NEWGATE" then
- netSend(allGates[i + 7].addr)
- return
- elseif mButton == 2 then
- viewGateEntry(i + 7)
- return
- end
- end
- end
- -- Page Navigation via click
- elseif mcY == termY then -- Bottom row of screen
- if mcX < termX/2 - 3 then -- Page Back
- if pageNum > 1 then
- pageNum = pageNum - 1
- drawCLI()
- return
- end
- elseif mcX > termX/2 - 3 and mcX < termX/2 + 3 then -- Page Numbers (Go To Page dialogue)
- goToPage()
- elseif mcX > termX/2 + 3 then -- Page Forward
- if pageNum < numPages then
- pageNum = pageNum + 1
- end
- if pageNum == numPages then
- drawElement(2,4,termX,termY,white,black,"")
- end
- drawCLI()
- return
- end
- end
- elseif runState == "pSync" then
- if mcX > 2 and mcX < termX - 2 then
- if mcY > 4 and mcY < 8 then -- Import
- pSync("pPull")
- saveData()
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- elseif mcY > 8 and mcY < 12 then -- Export
- pSync("pPush")
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- elseif mcY > 12 and mcY < 16 then -- Cancel
- runState = "Dial"
- drawElement(2,4,termX,termY,white,black,"")
- drawCLI()
- end
- end
- end
- end
- end
- local function mScroll()
- while true do
- local scrollEvent = { os.pullEvent("mouse_scroll") }
- if runState == "Dial" then
- if scrollEvent[2] == -1 and pageNum > 1 then
- pageNum = pageNum - 1
- elseif scrollEvent[2] == 1 and pageNum < numPages then
- pageNum = pageNum + 1
- if pageNum == numPages then
- drawElement(2,4,termX,termY,white,black,"")
- end
- end
- drawCLI()
- end
- end
- end
- local function missingComponent()
- term.setBackgroundColor(black)
- term.clear()
- drawElement(2,2,11,1,red,black,"No wireless")
- drawElement(2,3,15,1,red,black,"modem detected!")
- drawElement(2,5,17,1,red,black,"ccDialer REQUIRES")
- drawElement(2,6,17,1,red,black,"a wireless modem.")
- term.setCursorPos(1,9)
- err = "YES"
- end
- local function tResize()
- while true do
- local tX,tY = term.getSize()
- if tX ~= termX or tY ~= termY then
- termX, termY = tX, tY
- --term.setBackgroundColor(black)
- --term.clear()
- drawCLI()
- end
- sleep(0.02)
- end
- end
- local function dialerKernel()
- if kernelState == true and runState ~= "goPage" then
- parallel.waitForAny(mClick,mScroll,keyClick) --,tResize
- end
- if runState == "quit" then
- kernelState = false
- end
- return kernelState
- end
- local function initMe()
- termX = 26
- term.setBackgroundColor(black)
- term.clear()
- drawElement(2,2,18,1,white,black,"Initializing . . .")
- local ccLabel = os.getComputerLabel()
- if ccLabel == nil or tostring(ccLabel) == "" or ccLabel == "Dialer" then
- os.setComputerLabel("ccDialer")
- end
- for _,side in ipairs(rs.getSides()) do
- if peripheral.isPresent(tostring(side)) then
- local perp = peripheral.getType(tostring(side))
- if tostring(perp) == "modem" then
- if peripheral.call(tostring(side),"isWireless") == true then
- modem = peripheral.wrap(side)
- modem.open(dialSettings.pChannels.recv)
- netSide = side
- break
- end
- end
- end
- end
- if netSide == "none" then missingComponent() runState = "quit" kernelState = false return end
- drawElement(2,10,25,1,white,black,"Ingesting gate data . . .")
- ingestData()
- term.clear()
- kernelState = true
- runState = "Dial"
- drawCLI()
- end
- initMe()
- repeat
- dialerKernel()
- if runState == "quit" then kernelState = false end
- if kernelState == false then
- term.setBackgroundColor(black)
- term.setTextColor(white)
- if err == "NO" then
- term.clear()
- term.setCursorPos(1,1)
- end
- if modem then modem.close(dialSettings.pChannels.recv) end
- modem = nil
- return
- end
- until kernelState == false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement