Advertisement
HydrantHunter

ccDialer 1.0

Jan 14th, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.25 KB | None | 0 0
  1. --[[   LanteaCraft     ]]--
  2. --[[        ccDialer   ]]--
  3. --[[  core ver. 1.0.0  ]]--
  4. --[[   ui ver. 1.0.0   ]]--
  5. --[[     by Dog        ]]--
  6. --[[ aka HydrantHunter ]]--
  7. --[[ pastebin WN9wLUnu ]]--
  8. --[[      GPL v3       ]]--
  9. local ccDialVer = "1.0.01"
  10. --[[
  11. Tested with/requires:
  12.   - Minecraft 1.6.4
  13.   - LanteaCraft (RC1)
  14.   - ComputerCraft ver. 1.61+ for MC 1.6.4
  15.     - HTTP API enabled (for pastebin installer - not required for ccDHD)
  16.     - 1 Advanced Portable Computer (color, mouse)
  17.     - ccDHD running on an Advanced Computer (with a wireless modem)
  18.     - gateLiaison running on a Computer (standard or advanced) with a wireless modem or a Wireless Turtle (standard or advanced)
  19.  
  20.   - ccDHD also supports the Biometric Lock (Fistprint Scanner) in GopherATL's Biolock MOD ver. 2.1.3 (updated for 1.6.4 by gamax92)
  21. ]]--
  22. -- AUTOMATIC/STATIC CONFIGURATION
  23. -- Default Settings
  24. local dialSettings = { pChannels={send=24242,recv=24747,},
  25.                        gateData="/data/DHDgates",
  26.                      }
  27. local allGates = { {addr="NOGATES",rating="U",note="Example gate",loc={x=99999,y=99999,z=99999,dim="Overworld",},}, }
  28. local netSide = "none"
  29. local longName, tY
  30. -- Peripherals
  31. local termX,termY = term.getSize() -- portable 26x20 / standard 51x19 / cBang & OneOS = 51x18
  32. local modem = nil                  -- Modem (for ccNet)
  33.  -- Status Info
  34. local kernelState = true
  35. local runState = "init"
  36. local currentState = "ZZ"
  37. local tempState, currentEdit
  38. local err = "NO"
  39. local i,j,v,w
  40.  -- Gate List
  41. local numPages,pageNum = 1,1
  42.  -- Color Definitions
  43. local white = colors.white
  44. local lgray = colors.lightGray
  45. local gray = colors.gray
  46. local black = colors.black
  47. local brown = colors.brown
  48. local yellow = colors.yellow
  49. local orange = colors.orange
  50. local red = colors.red
  51. local magenta = colors.magenta
  52. local purple = colors.purple
  53. local blue = colors.blue
  54. local lblue = colors.lightBlue
  55. local cyan = colors.cyan
  56. local lime = colors.lime
  57. local green = colors.green
  58. -- END AUTOMATIC/STATIC CONFIGURATION
  59.  
  60. local function pSync(pushpull) -- Sync allGates with an advanced computer running ccDHD
  61.   modem.transmit(dialSettings.pChannels.send,dialSettings.pChannels.recv,pushpull)
  62.   if pushpull == "pPull" then
  63.     local netEvent = { os.pullEvent("modem_message") }
  64.     if netEvent[3] == dialSettings.pChannels.recv and netEvent[4] == dialSettings.pChannels.send then
  65.       local newGateData = netEvent[5]
  66.       allGates = textutils.unserialize(newGateData)
  67.       numPages = math.ceil(#allGates / 14)
  68.       return
  69.     end
  70.   elseif pushpull == "pPush" then
  71.     sleep(0.1)
  72.     local pGateData = textutils.serialize(allGates)
  73.     modem.transmit(dialSettings.pChannels.send,dialSettings.pChannels.recv,pGateData)
  74.     return
  75.   end
  76. end
  77.  
  78. local function netSend(dataPack)
  79.   modem.transmit(dialSettings.pChannels.send,dialSettings.pChannels.recv,dataPack)
  80. end
  81.  
  82. local function saveData()
  83.   if not fs.exists("/data") then fs.makeDir("/data") end
  84.   local dhdConfig = fs.open("/data/DHDgates","w") or error("saveData(): Cannot open /data/DHDgates for writing", 2)
  85.   dhdConfig.write(textutils.serialize(allGates))
  86.   dhdConfig.close()
  87. end
  88.  
  89. local function ingestData()
  90.   if not fs.exists("/data/DHDgates") then return end
  91.   local dhdConfig = fs.open("/data/DHDgates","r") or error("ingestData(): Cannot open /data/DHDgates for reading", 2)
  92.   local dhdCfg = dhdConfig.readAll()
  93.   dhdConfig.close()
  94.   allGates = textutils.unserialize(dhdCfg)
  95.   numPages = math.ceil(#allGates / 14)
  96. end
  97.  
  98. local function assignColor(gateNumber)
  99.   local colorBurst = { B = blue, H = lblue, V = brown, M = purple, S = green, C = orange, D = red, U = lgray, X = magenta }
  100.   for v,w in pairs(colorBurst) do
  101.     if allGates[gateNumber].rating == tostring(v) then
  102.       return w
  103.     end
  104.   end
  105. end
  106.  
  107. local function assignRating(gateNumber)
  108.   local ratingBurst = { B = "Base/Outpost/Hub", H = "Home/Camp", V = "Village", M = "Misc/Special", S = "Safe", C = "Caution", D = "Danger", U = "Unknown", Z = "Zog" }
  109.   for i,j in pairs(ratingBurst) do
  110.     if allGates[gateNumber].rating == i then
  111.       return j
  112.     end
  113.   end
  114. end
  115.  
  116. local function drawElement(x,y,w,h,txColor,bgColor,text)
  117.   local deText = tostring(text)
  118.   term.setCursorPos(x,y)
  119.   term.setBackgroundColor(bgColor)
  120.   if w > #deText or h > 1 then       -- We're 'drawing' something more than text
  121.     for i = 1,h,1 do                  
  122.       term.write(string.rep(" ",w)) -- Draw the 'element' (box/rectangle/line-seg)
  123.       term.setCursorPos(x,y+i)      --
  124.     end
  125.   end
  126.   if deText ~= "" and deText ~= "nil" then
  127.     term.setTextColor(txColor)
  128.     if w < #deText then w = #deText end
  129.     local xW = (x + w/2) - #deText/2
  130.     local xH = y + h/2
  131.     term.setCursorPos(xW,xH)
  132.     term.write(deText)
  133.   end
  134. end
  135.  
  136. local function drawHeader()
  137.   local title = "ccDialer  " .. ccDialVer
  138.   if termX < 19 then title = "ccDialer" end
  139.   drawElement(1,1,termX,1,white,blue,title) -- title bar
  140.   drawElement(termX-2,1,3,1,white,blue,"[ ]")
  141.   drawElement(termX-1,1,1,1,red,blue,"X")
  142.   if runState == "Dial" then
  143.     local hSync = string.rep(" ",math.floor(termX/4 - 2)) .. "Sync" .. string.rep(" ",math.floor(termX/4 - 2))
  144.     local hEnd = string.rep(" ",math.ceil(termX/4 - 4)) .. "End Call" .. string.rep(" ",math.ceil(termX/4 - 4))
  145.     drawElement(1,2,#hSync,1,white,cyan,hSync)
  146.     drawElement(termX/2,2,#hEnd,1,white,red,hEnd)
  147.   elseif runState == "pSync" then
  148.     drawElement(1,2,termX,1,lgray,gray,"Sync")
  149.   elseif runState == "viewing" then
  150.     drawElement(1,2,termX,1,lgray,gray,"Gate Info")
  151.   end
  152. end
  153.  
  154. local function drawPsyncUI()
  155.   drawElement(2,5,termX-2,3,white,green,"GET gate list")
  156.   drawElement(2,9,termX-2,3,white,orange,"SEND gate list")
  157.   drawElement(2,13,termX-2,3,white,red,"C A N C E L")
  158. end
  159.  
  160. local function drawNaviUI()
  161.   local pNum = tostring(pageNum)
  162.   if pageNum < 100 then pNum = "0" .. pNum end -- Add a "0" before double digit page numbers
  163.   if pageNum < 10 then pNum = "0" .. pNum end  -- Add another "0" before single digit page numbers
  164.   if pageNum > 1 then
  165.     drawElement((termX/2)-10,termY,7,1,gray,black,"<< BACK") -- Show "BACK" option if page number is > 1
  166.   elseif pageNum == 1 then
  167.     drawElement((termX/2)-10,termY,7,1,gray,black,string.rep(" ",7)) -- Overwrite "BACK" instead of clearing screen
  168.   end
  169.   if pageNum < numPages then
  170.     drawElement(termX/2+4,termY,7,1,gray,black,"NEXT >>") -- Show "NEXT" option if page number is < numPages
  171.   elseif pageNum == numPages then
  172.     drawElement(termX/2+4,termY,7,1,gray,black,string.rep(" ",7)) -- Overwrite "NEXT" instead of clearing screen
  173.   end
  174.   drawElement((termX/2) - (math.floor(pNum:len()/2)),termY,3,1,lgray,black,pNum) -- Page Number
  175. end
  176.  
  177. local function drawMainUI() -- Gate Address Book (Dial / Edit / Trim)
  178.   local xPos, yPos = 2, 2
  179.   local j = ((pageNum - 1) * 13) + pageNum
  180.   local k = ((pageNum - 1) * 14)
  181.   for i = j,#allGates,1 do
  182.     if i > k and i < k + 8 then           -- Column 1
  183.       xPos, yPos = 4, 2
  184.     elseif i > k + 6 and i < k + 13 then  -- Column 2
  185.       xPos, yPos = 15, -12
  186.     end
  187.     term.setCursorPos(xPos,yPos + (i - k) * 2)
  188.     term.setBackgroundColor(assignColor(i))
  189.     term.setTextColor(white)
  190.     if i > (pageNum * 14) or i > #allGates then
  191.       break
  192.     else
  193.       if #allGates[i].addr < 10 then
  194.         term.write(string.rep(" ",math.ceil((9-#allGates[i].addr)/2)) .. allGates[i].addr .. string.rep(" ",math.floor((9-#allGates[i].addr)/2))) -- Dial
  195.       else
  196.         term.write("ERROR #10")  -- ERROR #10 = address is 10 or more characters
  197.       end
  198.     end
  199.   end
  200.   term.setBackgroundColor(black)
  201. end
  202.  
  203. local function drawCLI()   -- Client Interface 'decider'
  204.   drawHeader()
  205.   if runState == "viewing" then return end
  206.   if runState == "Dial" then
  207.     drawMainUI()
  208.     drawNaviUI()
  209.   elseif runState == "pSync" then
  210.     drawPsyncUI()
  211.   end
  212. end
  213.  
  214. local function viewGateEntry(dataBlock)
  215.   currentEdit = dataBlock
  216.   tempState = runState
  217.   runState = "viewing"
  218.   term.setBackgroundColor(black)
  219.   term.clear()
  220.   drawHeader()
  221.   -- Display data for viewing/editing
  222.   term.setBackgroundColor(black)
  223.   if currentEdit <= #allGates then
  224.     local teColor = assignColor(currentEdit)
  225.     if teColor ~= nil then
  226.       drawElement(2,4,#allGates[currentEdit].addr,1,teColor,black,allGates[currentEdit].addr)
  227.       local ratingWord = assignRating(currentEdit)
  228.       drawElement(2,6,#ratingWord,1,gray,black,ratingWord)
  229.       drawElement(2,9,#allGates[currentEdit].note,1,white,black,allGates[currentEdit].note)
  230.       drawElement(2,12,4,1,lgray,black,"Dim:")
  231.       drawElement(7,12,#allGates[currentEdit].loc.dim,1,lblue,black,allGates[currentEdit].loc.dim)
  232.       drawElement(2,13,2,1,lgray,black,"x:")
  233.       drawElement(5,13,#tostring(allGates[currentEdit].loc.x),1,brown,black,allGates[currentEdit].loc.x)
  234.       drawElement(2,14,2,1,lgray,black,"y:")
  235.       drawElement(5,14,#tostring(allGates[currentEdit].loc.y),1,brown,black,allGates[currentEdit].loc.y)
  236.       drawElement(2,15,2,1,lgray,black,"z:")
  237.       drawElement(5,15,#tostring(allGates[currentEdit].loc.z),1,brown,black,allGates[currentEdit].loc.z)
  238.     end
  239.   end
  240.   drawElement((termX/2)-8,termY-1,19,1,black,gray," < < < CLOSE > > > ")
  241. end
  242.  
  243. local function goToPage()
  244.   tempState = runState
  245.   runState = "goPage"
  246.   drawElement((termX/2)-7,8,15,1,white,gray,"  Go To Page:  ")
  247.   drawElement((termX/2)-7,9,15,2,white,gray,"")
  248.   drawElement((termX/2)-6,9,13,1,white,black,"") -- input area bg
  249.   term.setBackgroundColor(black)
  250.   --local newPage = tonumber(readInput((termX/2-6),9,lime))
  251.   term.setCursorPos((termX/2)-6,9)
  252.   term.setTextColor(lime)
  253.   local newPage = tonumber(read())
  254.   drawElement((termX/2)-7,8,15,3,white,black,"")
  255.   if newPage == nil then newPage = pageNum end
  256.   if newPage < 1 then newPage = 1 end
  257.   if newPage > numPages then newPage = numPages end
  258.   if newPage == numPages then
  259.     drawElement(2,4,termX,termY,white,black,"")
  260.   end
  261.   pageNum = newPage
  262.   runState = tempState
  263.   drawCLI()
  264. end
  265.  
  266. local function keyClick()
  267.   while true do
  268.     local keyEvent = { os.pullEvent("key") }
  269.     if keyEvent[2] == 16 or keyEvent[2] == 45 then -- q/x - Exit
  270.       kernelState = false
  271.       runState = "quit"
  272.       break
  273.     end
  274.     if runState == "Dial" then
  275.       if keyEvent[2] == 31 then -- s - pSync
  276.         runState = "pSync"
  277.         drawElement(2,4,termX,termY,white,black,"")
  278.         drawCLI()
  279.       elseif keyEvent[2] == 18 then -- e - endCall
  280.         netSend("endCall")
  281.       elseif keyEvent[2] == 199 then -- HOME
  282.         pageNum = 1
  283.         drawCLI()
  284.       elseif keyEvent[2] == 207 then -- END
  285.         pageNum = numPages
  286.         drawElement(2,4,termX,termY,white,black,"")
  287.         drawCLI()
  288.       end
  289.     elseif runState == "pSync" then
  290.       if keyEvent[2] == 23 or keyEvent[2] == 34 then -- i/g - Import/Get
  291.         pSync("pPull")
  292.         saveData()
  293.         runState = "Dial"
  294.         drawElement(2,4,termX,termY,white,black,"")
  295.         drawCLI()
  296.       elseif keyEvent[2] == 18 or keyEvent[2] == 31 or keyEvent[2] == 25 then -- e/s/p - Export/Send/Put
  297.         pSync("pPush")
  298.         runState = "Dial"
  299.         drawElement(2,4,termX,termY,white,black,"")
  300.         drawCLI()
  301.       elseif keyEvent[2] == 46 then -- c - Cancel
  302.         runState = "Dial"
  303.         drawElement(2,4,termX,termY,white,black,"")
  304.         drawCLI()
  305.       end
  306.     elseif runState == "viewing" then
  307.       if keyEvent[2] == 46 then -- c - Close
  308.         runState = "Dial"
  309.         drawElement(2,4,termX,termY,white,black,"")
  310.         drawCLI()
  311.       end
  312.     end
  313.   end
  314. end
  315.  
  316. local function mClick()
  317.   while true do
  318.     local clickEvent = { os.pullEvent("mouse_click") }
  319.     local mButton = clickEvent[2]
  320.     local mcX = clickEvent[3]
  321.     local mcY = clickEvent[4]
  322.     if mcY == 1 and mcX > termX - 3 and mcX <= termX then
  323.       kernelState = false
  324.       runState = "quit"
  325.       break
  326.     end
  327.     if runState == "viewing" then
  328.       if mcX > 0 and mcX < termX and mcY == termY-1 then
  329.         runState = tempState
  330.         drawElement(2,4,termX,termY,white,black,"")
  331.         drawCLI()
  332.         return
  333.       end
  334.     elseif runState == "Dial" then
  335.       if mcY == 2 then
  336.         if mcX < termX/2 then
  337.           runState = "pSync"
  338.           drawElement(2,4,termX,termY,white,black,"")
  339.           drawCLI()
  340.         elseif mcX > termX/2 then
  341.           netSend("endCall")
  342.         end
  343.        -- Dial a listed address or view it's info
  344.       elseif mcY > 3 and mcY < 17 then
  345.         local j = ((pageNum - 1) * 13) + pageNum
  346.         local k = ((pageNum - 1) * 13)
  347.         local l = ((pageNum - 1) * 14)
  348.         for i = j,#allGates,1 do
  349.             -- Addresses Column 1
  350.           if mcX > 3 and mcX < 13 and mcY == 2 + ((i - l) * 2) and i <= #allGates then
  351.             if mButton == 1 and allGates[i].addr ~= "NEWGATE" then
  352.               netSend(allGates[i].addr)
  353.               return
  354.             elseif mButton == 2 then
  355.               viewGateEntry(i)
  356.               return
  357.             end
  358.             -- Addresses Column 2
  359.           elseif mcX > 14 and mcX < 24 and mcY == 2 + ((i - l) * 2) and (i + 7) <= #allGates then
  360.             if mButton == 1 and allGates[i].addr ~= "NEWGATE" then
  361.               netSend(allGates[i + 7].addr)
  362.               return
  363.             elseif mButton == 2 then
  364.               viewGateEntry(i + 7)
  365.               return
  366.             end
  367.           end
  368.         end
  369.        -- Page Navigation via click
  370.       elseif mcY == termY then -- Bottom row of screen
  371.         if mcX < termX/2 - 3 then -- Page Back
  372.           if pageNum > 1 then
  373.             pageNum = pageNum - 1
  374.             drawCLI()
  375.             return
  376.           end
  377.         elseif mcX > termX/2 - 3 and mcX < termX/2 + 3 then -- Page Numbers (Go To Page dialogue)
  378.           goToPage()
  379.         elseif mcX > termX/2 + 3 then -- Page Forward
  380.           if pageNum < numPages then
  381.             pageNum = pageNum + 1
  382.           end
  383.           if pageNum == numPages then
  384.             drawElement(2,4,termX,termY,white,black,"")
  385.           end
  386.           drawCLI()
  387.           return
  388.         end
  389.       end
  390.     elseif runState == "pSync" then
  391.       if mcX > 2 and mcX < termX - 2 then
  392.         if mcY > 4 and mcY < 8 then -- Import
  393.           pSync("pPull")
  394.           saveData()
  395.           runState = "Dial"
  396.           drawElement(2,4,termX,termY,white,black,"")
  397.           drawCLI()
  398.         elseif mcY > 8 and mcY < 12 then -- Export
  399.           pSync("pPush")
  400.           runState = "Dial"
  401.           drawElement(2,4,termX,termY,white,black,"")
  402.           drawCLI()
  403.         elseif mcY > 12 and mcY < 16 then -- Cancel
  404.           runState = "Dial"
  405.           drawElement(2,4,termX,termY,white,black,"")
  406.           drawCLI()
  407.         end
  408.       end
  409.     end
  410.   end
  411. end
  412.  
  413. local function mScroll()
  414.   while true do
  415.     local scrollEvent = { os.pullEvent("mouse_scroll") }
  416.     if runState == "Dial" then
  417.       if scrollEvent[2] == -1 and pageNum > 1 then
  418.         pageNum = pageNum - 1
  419.       elseif scrollEvent[2] == 1 and pageNum < numPages then
  420.         pageNum = pageNum + 1
  421.         if pageNum == numPages then
  422.           drawElement(2,4,termX,termY,white,black,"")
  423.         end
  424.       end
  425.       drawCLI()
  426.     end
  427.   end
  428. end
  429.  
  430. local function missingComponent()
  431.   term.setBackgroundColor(black)
  432.   term.clear()
  433.   drawElement(2,2,11,1,red,black,"No wireless")
  434.   drawElement(2,3,15,1,red,black,"modem detected!")
  435.   drawElement(2,5,17,1,red,black,"ccDialer REQUIRES")
  436.   drawElement(2,6,17,1,red,black,"a wireless modem.")
  437.   term.setCursorPos(1,9)
  438.   err = "YES"
  439. end
  440.  
  441. local function tResize()
  442.   while true do
  443.     local tX,tY = term.getSize()
  444.     if tX ~= termX or tY ~= termY then
  445.       termX, termY = tX, tY
  446.       --term.setBackgroundColor(black)
  447.       --term.clear()
  448.       drawCLI()
  449.     end
  450.     sleep(0.02)
  451.   end
  452. end
  453.  
  454. local function dialerKernel()
  455.   if kernelState == true and runState ~= "goPage" then
  456.     parallel.waitForAny(mClick,mScroll,keyClick) --,tResize
  457.   end
  458.   if runState == "quit" then
  459.     kernelState = false
  460.   end
  461.   return kernelState
  462. end
  463.  
  464. local function initMe()
  465.   termX = 26
  466.   term.setBackgroundColor(black)
  467.   term.clear()
  468.   drawElement(2,2,18,1,white,black,"Initializing . . .")
  469.   local ccLabel = os.getComputerLabel()
  470.   if ccLabel == nil or tostring(ccLabel) == "" or ccLabel == "Dialer" then
  471.     os.setComputerLabel("ccDialer")
  472.   end
  473.   for _,side in ipairs(rs.getSides()) do
  474.     if peripheral.isPresent(tostring(side)) then
  475.       local perp = peripheral.getType(tostring(side))
  476.       if tostring(perp) == "modem" then
  477.         if peripheral.call(tostring(side),"isWireless") == true then
  478.           modem = peripheral.wrap(side)
  479.           modem.open(dialSettings.pChannels.recv)
  480.           netSide = side
  481.           break
  482.         end
  483.       end
  484.     end
  485.   end
  486.   if netSide == "none" then missingComponent() runState = "quit" kernelState = false return end
  487.   drawElement(2,10,25,1,white,black,"Ingesting gate data . . .")
  488.   ingestData()
  489.   term.clear()
  490.   kernelState = true
  491.   runState = "Dial"
  492.   drawCLI()
  493. end
  494.  
  495. initMe()
  496.  
  497. repeat
  498.   dialerKernel()
  499.   if runState == "quit" then kernelState = false end
  500.   if kernelState == false then
  501.     term.setBackgroundColor(black)
  502.     term.setTextColor(white)
  503.     if err == "NO" then
  504.       term.clear()
  505.       term.setCursorPos(1,1)
  506.     end
  507.     if modem then modem.close(dialSettings.pChannels.recv) end
  508.     modem = nil
  509.     return
  510.   end
  511. until kernelState == false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement