Advertisement
HydrantHunter

gateLiaison 1.1

Dec 24th, 2013
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.18 KB | None | 0 0
  1. --[[  LanteaCraft      ]]--
  2. --[[    Gate Liaison   ]]--
  3. --[[ by HydrantHunter  ]]--
  4. --[[     aka Dog       ]]--
  5. --[[  with help from   ]]--
  6. --[[  AfterLifeLochie  ]]--
  7. --[[ pastebin xSHHqL0h ]]--
  8. --[[      GPL v3       ]]--
  9. local gVer = "1.1.12"
  10. --[[
  11. Tested with/requires:
  12.   - Minecraft 1.6.4
  13.   - LanteaCraft (snapshot 82+ & RC1_15-RC1_22)
  14.   - ComputerCraft ver. 1.57+ for MC 1.6.4
  15.     - HTTP API enabled (for pastebin installer - not required for gateLiaison)
  16.     - 1 Computer (standard or advanced), 3 optional Monitors (standard or advanced) - probably best not to mix, though
  17.       - or 1 Wireless Turtle (standard or advanced), no monitors
  18.     - ccDHD running on an Advanced Computer with 4 optional Advanced Monitors
  19.       - ccDHD also supports GopherATL's BioLock ver. 2.1.3 Fistprint scanner (updated for 1.6.4 by gamax92)
  20. ]]--
  21. -- AUTOMATIC/STATIC CONFIGURATION (Part 1)
  22. local modem, modemSide
  23. local gate, thisGate
  24. local fuelGuage, shieldStatus
  25. local pData
  26. local gateStatus = "QRY"
  27. local dialAddress = "none"
  28. local currentState = "ZZ"
  29. local pollTimer, timePoller = 0,0
  30. local longName = ""
  31. local mon = "none"
  32. local gateSettings = { shield = "front",
  33.                        DHD = 99999,
  34.                        chan = { send = 1787, recv = 1776 },
  35.                        net = "C",
  36.                        pollTime = 2,
  37.                        last = "/data/lastCall",
  38.                      }
  39.  -- Color Definitions
  40. local white = colors.white
  41. local lgray = colors.lightGray
  42. local gray = colors.gray
  43. local black = colors.black
  44. local brown = colors.brown
  45. local yellow = colors.yellow
  46. local orange = colors.orange
  47. local red = colors.red
  48. local magenta = colors.magenta
  49. local purple = colors.purple
  50. local blue = colors.blue
  51. local lblue = colors.lightBlue
  52. local cyan = colors.cyan
  53. local lime = colors.lime
  54. local green = colors.green
  55. if not term.isColor() then
  56.   lgray = colors.white
  57.   gray = colors.black
  58.   brown = colors.white
  59.   yellow = colors.white
  60.   orange = colors.white
  61.   red = colors.white
  62.   magenta = colors.white
  63.   purple = colors.white
  64.   blue = colors.black
  65.   lblue = colors.white
  66.   cyan = colors.white
  67.   lime = colors.white
  68.   green = colors.white
  69. end
  70. -- END AUTOMATIC/STATIC CONFIGURATION (Part 1)
  71.  
  72. local function ingestData()
  73.   local gateConfig = fs.open("/data/gateSettings","r") or error("ingestData(): Unable to open /data/gateSettings for reading", 2)
  74.   local gateCfg = gateConfig.readAll()
  75.   gateSettings = textutils.unserialize(gateCfg)
  76.   gateConfig.close()
  77. end
  78.  
  79. local function saveData()
  80.   if not fs.exists("/data") then fs.makeDir("/data") end
  81.   local gateConfig = fs.open("/data/gateSettings","w") or error("saveData(): Unable to open /data/gateSettings for writing", 2)
  82.   gateConfig.write(textutils.serialize(gateSettings))
  83.   gateConfig.close()
  84. end
  85.  
  86. local function displayMarquee() -- Display the local address when there is no other information to display
  87.   if mon == "none" then return end
  88.   mon.setBackgroundColor(black)
  89.   mon.clear()
  90.   mon.setTextScale(2)
  91.   mon.setCursorPos(1,1)
  92.   mon.setTextColor(cyan)
  93.   mon.write("Stargate")
  94.   mon.setTextColor(yellow)
  95.   if #thisGate > 7 then
  96.     mon.setCursorPos(4,2)
  97.   else
  98.     mon.setCursorPos(2,2)
  99.   end
  100.   mon.write(longName)
  101. end
  102.  
  103. local function displayStatus(select)
  104.   if mon == "none" then return end
  105.   if select == "con" then       -- Connection status - dialAddress or incoming
  106.     mon.clear()
  107.     mon.setTextScale(2)
  108.     mon.setCursorPos(1,1)
  109.     if dialAddress ~= "none" then -- dialAddress
  110.       mon.setTextColor(lblue)
  111.       mon.write("Connected to")
  112.       mon.setTextColor(yellow)
  113.       if #dialAddress > 7 then
  114.         mon.setCursorPos(4,2)
  115.         mon.write(dialAddress)
  116.       else                      -- incoming
  117.         mon.setCursorPos(1,2)
  118.         local dA = { }
  119.         for i = 1,#dialAddress do
  120.           dA[i] = dialAddress:sub(i,i)
  121.           mon.write(" " .. dA[i])
  122.         end
  123.       end
  124.     else
  125.       mon.setTextColor(yellow)
  126.       mon.write(" Incoming")
  127.       mon.setCursorPos(4,2)
  128.       mon.write("Connection")
  129.     end
  130.   elseif select == "dial" then  -- Dial status - dialAddress or incoming
  131.     mon.clear()
  132.     mon.setTextScale(2)
  133.     mon.setCursorPos(1,1)
  134.     if dialAddress ~= "none" then
  135.       mon.setTextColor(lblue)
  136.       mon.write("Dialing")
  137.       mon.setTextColor(yellow)
  138.       if #dialAddress > 7 then
  139.         mon.setCursorPos(4,2)
  140.         mon.write(dialAddress)  -- dialAddress (8+ symbols)
  141.       else                      -- or longName (7 symbols)
  142.         mon.setCursorPos(1,2)
  143.         local dA = { }
  144.         for i = 1,#dialAddress do
  145.           dA[i] = dialAddress:sub(i,i)
  146.           mon.write(" " .. dA[i])
  147.         end
  148.       end
  149.     else                        -- incoming
  150.       mon.setTextColor(yellow)
  151.       mon.write(" Incoming")
  152.       mon.setCursorPos(4,2)
  153.       mon.write("Connection")
  154.     end
  155.   elseif select == "" or select == nil then -- Non-specified status - incoming or marquee
  156.     if gate.isDialing() == true and dialAddress == "" then -- incoming
  157.       mon.clear()
  158.       mon.setTextScale(2)
  159.       mon.setTextColor(yellow)
  160.       mon.setCursorPos(1,1)
  161.       mon.write(" Incoming")
  162.       mon.setCursorPos(4,2)
  163.       mon.write("Connection")
  164.     else
  165.       displayMarquee()          -- marquee
  166.     end
  167.   end
  168. end
  169.  
  170. local function drawElement(x,y,w,h,txtColor,bgdColor,text)
  171.   local deText = tostring(text)
  172.   term.setCursorPos(x,y)
  173.   term.setBackgroundColor(bgdColor)
  174.   if w > #deText or h > 1 then       -- We're 'drawing' something more than text
  175.     for i = 1,h,1 do                 --
  176.       term.write(string.rep(" ",w))  -- Draw the 'element' (box/rectangle/line-seg)
  177.       term.setCursorPos(x,y+i)       --
  178.     end
  179.   end
  180.   if deText ~= "" and deText ~= "nil" then
  181.     term.setTextColor(txtColor)
  182.     if w < #deText then w = #deText end -- Ensure minimum length
  183.     local xW = (x + math.floor(w/2)) - math.floor(#deText/2) -- Center the text horizontally
  184.     local xH = y + math.floor(h/2)   -- Center the text vertically
  185.     term.setCursorPos(xW,xH)
  186.     term.write(deText)
  187.   end
  188. end
  189.  
  190. local function drawTermScreen()
  191.   term.setBackgroundColor(black)
  192.   term.clear()
  193.   local stColor,sbColor
  194.   if mon == "none" then
  195.     sbColor = lblue
  196.     stColor = black
  197.   else
  198.     sbColor = blue
  199.     stColor = white
  200.   end
  201.   if fuelGuage == false then
  202.     sbColor = red
  203.     stColor = white
  204.   end
  205.   if not term.isColor() then
  206.     sbColor = white
  207.     stColor = black
  208.   end
  209.   local glHeaderTxt = " Gate Liaison" .. string.rep(" ",12) .. "ver. " .. gVer .. " "
  210.   drawElement(2,2,1,1,stColor,sbColor,glHeaderTxt) -- Header
  211.   drawElement(2,10,37,1,stColor,sbColor,"") -- lowline
  212.   local me = os.getComputerLabel()
  213.   drawElement(3,10,1,1,stColor,sbColor,me)
  214.   local cID = "cc # " .. tostring(os.getComputerID())
  215.   drawElement(38-(#cID),10,1,1,stColor,sbColor,cID)
  216.   drawElement(2,3,12,7,black,lgray,"") -- Information background
  217.   local readouts = { " Gate:", " State:", " Target:",  " Iris:", " Fuel:", " Status:", " Network:" }
  218.   for i = 1,7,1 do
  219.     drawElement(2,i+2,1,1,black,lgray,readouts[i])
  220.   end
  221.   if #thisGate == 7 then
  222.     drawElement(2,7,1,1,black,lgray," Shield:")
  223.   end
  224.   drawElement(17,3,1,1,yellow,black,thisGate)
  225.   local sTxtColor = blue
  226.   if gateStatus == "Disconnected" then
  227.     sTxtColor = green
  228.   elseif gateStatus == "Connected" then
  229.     sTxtColor = orange
  230.   elseif gateStatus == "Dialing" then
  231.     sTxtColor = lblue
  232.   end
  233.   if gateStatus == "Dialing" and dialAddress == "none" then
  234.     drawElement(17,4,1,1,sTxtColor,black,"Incoming Connection")
  235.   elseif gateStatus == "lockdown" then
  236.     drawElement(17,4,1,1,red,black,"!! LOCKDOWN !!")
  237.   else
  238.     drawElement(17,4,1,1,sTxtColor,black,gateStatus)
  239.   end
  240.   drawElement(17,5,1,1,brown,black,dialAddress)
  241.   term.setCursorPos(17,6)
  242.   if shieldStatus == true then
  243.     term.setTextColor(green)
  244.     if #thisGate == 7 then
  245.       term.write("ON    ")
  246.     elseif #thisGate > 7 then
  247.       term.write("CLOSED")
  248.     end
  249.   elseif shieldStatus == false then
  250.     term.setTextColor(orange)
  251.     if #thisGate == 7 then
  252.       term.write("OFF   ")
  253.     elseif #thisGate > 7 then
  254.       term.write("OPEN  ")
  255.     end
  256.   else
  257.     term.setTextColor(magenta)
  258.     term.write("ERROR ")
  259.   end
  260.   term.setCursorPos(17,7)
  261.   if fuelGuage == true then
  262.     term.setTextColor(green)
  263.     term.write("YES  ")
  264.   elseif fuelGuage == false then
  265.     term.setTextColor(red)
  266.     term.write("NO   ")
  267.   else
  268.     term.setTextColor(magenta)
  269.     term.write("ERROR")
  270.   end
  271.   drawElement(17,8,1,1,lblue,black,pData)
  272.   term.setCursorPos(17,9)
  273.   if gateSettings.net == "R" then
  274.     term.setTextColor(red)
  275.     term.write("redNet   ")
  276.     term.setTextColor(gray)
  277.     term.write("ccDHD ")
  278.     term.setTextColor(red)
  279.     term.write(tostring(gateSettings.DHD))
  280.   elseif gateSettings.net == "C" then
  281.     term.setTextColor(green)
  282.     term.write("ccNet ")
  283.     term.setTextColor(gray)
  284.     term.write("s:")
  285.     term.setTextColor(green)
  286.     term.write(tostring(gateSettings.chan.send))
  287.     term.setTextColor(gray)
  288.     term.write(" r:")
  289.     term.setTextColor(green)
  290.     term.write(tostring(gateSettings.chan.recv))
  291.   end
  292. end
  293.  
  294. local function netSend(dataPack)
  295.   if gateSettings.net == "R" then
  296.     if gateSettings.DHD ~= 99999 then
  297.       rednet.send(gateSettings.DHD,dataPack)
  298.     end
  299.   elseif gateSettings.net == "C" then
  300.     if gateSettings.chan.recv ~= 2 then
  301.       modem.transmit(gateSettings.chan.send,gateSettings.chan.recv,dataPack)
  302.     end
  303.   end
  304. end
  305.  
  306. local function updateStatus(special)
  307.   currentState = pData
  308.   fuelGuage = gate.hasFuel()
  309.   if gateSettings.shield == "iris" then
  310.     shieldStatus, _ = { pcall(gate.isIrisClosed) }
  311.   else
  312.     shieldStatus = rs.getOutput(gateSettings.shield)
  313.   end
  314.   if gateStatus == "allclear" then
  315.     shieldState = false
  316.     if gate.isConnected() then
  317.       gateStatus = "Connected"
  318.     else
  319.       gateStatus = "Disconnected"
  320.       dialAddress = "none"
  321.     end
  322.     if fuelGuage then
  323.       pData = "YY1"
  324.     else
  325.       pData = "YY0"
  326.     end
  327.   elseif gateStatus == "lockdown" then
  328.     if fuelGuage then
  329.       pData = "XX1"
  330.     else
  331.       pData = "XX0"
  332.     end
  333.     dialAddress = "none"
  334.   else
  335.     if shieldStatus then
  336.       pData = "1"
  337.     else
  338.       pData = "0"
  339.     end
  340.     if gate.isDialing() then
  341.       pData = pData .. "2"
  342.       gateStatus = "Dialing"
  343.       if mon ~= "none" then
  344.         displayStatus("dial")
  345.       end
  346.     else
  347.       if gate.isConnected() then
  348.         pData = pData .. "1"
  349.         gateStatus = "Connected"
  350.         if mon ~= "none" then
  351.           displayStatus("con")
  352.         end
  353.       else
  354.         gateStatus = "Disconnected"
  355.         dialAddress = "none"
  356.         pData = pData .. "0"
  357.       end
  358.     end
  359.     if fuelGuage then
  360.       pData = pData .. "1"
  361.     else
  362.       pData = pData .. "0"
  363.     end
  364.   end
  365.   if gateStatus ~= "Dialing" and gateStatus ~= "Connected" and gateStatus ~= "lockdown" then displayMarquee() end
  366.   if currentState ~= pData or tostring(special) == "QRY" then
  367.     netSend(pData)
  368.     drawTermScreen()
  369.   end
  370. end
  371.  
  372. local function recordSessionData()  -- Human readable log files (last gate & history)
  373.   if not fs.exists("/data") then fs.makeDir("/data") end
  374.   local dateStamp = textutils.formatTime(os.time(),false) .. " / Day " .. os.day()
  375.   local lastCall = fs.open(gateSettings.last,"w")
  376.   lastCall.writeLine(dateStamp)
  377.   lastCall.writeLine(dialAddress)
  378.   lastCall.close()
  379. end
  380.  
  381. local function shieldControl(state)
  382.   if state == "ON" then
  383.     if gateSettings.shield == "iris" then
  384.       pcall(gate.irisClose)
  385.     else
  386.       rs.setOutput(gateSettings.shield,true)
  387.     end
  388.     return
  389.   elseif state == "OFF" then
  390.     if gateSettings.shield == "iris" then
  391.       pcall(gate.irisOpen)
  392.     else
  393.       rs.setOutput(gateSettings.shield,false)
  394.     end
  395.     return
  396.   end
  397. end
  398.  
  399. local function lockDown()
  400.   gateStatus = "lockdown"
  401.   shieldControl("ON")
  402.   local onHook, _ = pcall(gate.disconnect)
  403.   if onHook then
  404.     dialAddress = "none"
  405.   end
  406.   updateStatus()
  407.   if mon ~= "none" then
  408.     mon.clear()
  409.     mon.setCursorPos(1,1)
  410.     mon.setTextColor(red)
  411.     mon.write("!! LOCKDOWN !!")
  412.   end
  413. end
  414.  
  415. local function dialOut(targetAddress)
  416.   dialAddress = tostring(targetAddress)
  417.   local dialGate, _ = pcall(gate.dial,dialAddress)
  418.   if dialGate then
  419.     gateStatus = "Dialing"
  420.     recordSessionData()
  421.     updateStatus()
  422.   end
  423.   return dialGate
  424. end
  425.  
  426. local function endCall()
  427.   local hangUp, _ = pcall(gate.disconnect)
  428.   if hangUp then
  429.     gateStatus = "Disconnected"
  430.     dialAddress = "none"
  431.   end
  432. end
  433.  
  434. local function doCommand(thisCommand)
  435.   if thisCommand == "lockdown" then
  436.     lockDown()
  437.     return
  438.   elseif thisCommand == "allclear" then
  439.     displayMarquee()
  440.     shieldControl("OFF")
  441.     gateStatus = "allclear"
  442.     dialAddress = "none"
  443.     updateStatus()
  444.     return
  445.   end
  446.   if thisCommand == "QRY" then
  447.     updateStatus("QRY")
  448.     return
  449.   end
  450.   if gateStatus ~= "lockdown" then
  451.     if thisCommand == "1stRun" then -- When ccDHD signifies its first run...
  452.       while true do
  453.         local myBoss = { os.pullEvent("modem_message") }  -- ...it then sends it's cc# and waits for the gate's address & cc #
  454.         if myBoss[3] == gateSettings.chan.recv and myBoss[4] == gateSettings.chan.send then
  455.           gateSettings.DHD = myBoss[5]
  456.           saveData()
  457.           break
  458.         end
  459.       end
  460.       local me = os.getComputerID()
  461.       local tempSettings = { addr = thisGate, gate = me }
  462.       sleep(0.1)
  463.       netSend(tempSettings)
  464.       return
  465.     elseif thisCommand == "redNet" then -- change to redNet
  466.       if gateSettings.net == "C" then modem.close(gateSettings.chan.recv) end
  467.       modem = nil
  468.       gateSettings.net = "R"
  469.       rednet.open(modemSide)
  470.       saveData()
  471.       drawTermScreen()
  472.       return
  473.     elseif thisCommand == "ccNet" then  -- change to ccNet
  474.       if gateSettings.net == "R" then rednet.close(modemSide) end
  475.       gateSettings.net = "C"
  476.       modem = peripheral.wrap(modemSide)
  477.       modem.open(gateSettings.chan.recv)
  478.       saveData()
  479.       drawTermScreen()
  480.       return
  481.     elseif thisCommand == "CS" then     -- change ccNet Receive Channel
  482.       local newCR = { os.pullEvent("modem_message") }
  483.       modem.close(gateSettings.chan.recv)
  484.       gateSettings.chan.recv = newCR[5]
  485.       modem.open(gateSettings.chan.recv)
  486.       saveData()
  487.       drawTermScreen()
  488.       return
  489.     elseif thisCommand == "CR" then     -- change ccNet Send Channel
  490.       local newCS = { os.pullEvent("modem_message") }
  491.       gateSettings.chan.send = newCS[5]
  492.       saveData()
  493.       drawTermScreen()
  494.       return
  495.     elseif thisCommand == "endCall" then
  496.       endCall()
  497.       updateStatus()
  498.       return
  499.     elseif thisCommand == "sON" then
  500.       shieldControl("ON")
  501.       updateStatus()
  502.       return
  503.     elseif thisCommand == "sOFF" then
  504.       shieldControl("OFF")
  505.       updateStatus()
  506.       return
  507.     elseif thisCommand == "restart" or thisCommand == "reset" then
  508.       os.reboot()
  509.     else
  510.       if dialOut(thisCommand) then
  511.         gateStatus = "Dialing"
  512.         updateStatus()
  513.         return
  514.       else
  515.         gateStatus = "Disconnected"
  516.         dialAddress = "none"
  517.         updateStatus()
  518.         return
  519.       end
  520.     end
  521.   end
  522. end
  523.  
  524. local function netReceive()
  525.   while true do
  526.     if gateSettings.net == "R" then
  527.       local eventNet = { rednet.receive() }
  528.       if tonumber(eventNet[1]) == gateSettings.DHD then
  529.         if eventNet[2] ~= nil and eventNet[2] ~= "" then
  530.           doCommand(eventNet[2])
  531.         end
  532.       end
  533.       return
  534.     elseif gateSettings.net == "C" then
  535.       local eventNet = { os.pullEvent("modem_message") }
  536.       if eventNet[3] == gateSettings.chan.recv and eventNet[4] == gateSettings.chan.send then
  537.         if eventNet[5] ~= nil and eventNet[5] ~= "" then
  538.           doCommand(eventNet[5])
  539.         end
  540.       end
  541.       return
  542.     end
  543.   end
  544. end
  545.  
  546. local function dataPoller()
  547.   while true do
  548.     local timerEvent = { os.pullEvent("timer") }
  549.     if timerEvent[2] == pollTimer then
  550.       timePoller = 0
  551.       updateStatus()
  552.       break
  553.     end
  554.   end
  555. end
  556.  
  557. local function gateKernel()
  558.   if timePoller == 0 then
  559.     pollTimer = os.startTimer(gateSettings.pollTime)
  560.     timerPoller = 1
  561.   end
  562.   parallel.waitForAny(netReceive,dataPoller)
  563. end
  564.  
  565. local function firstRun()
  566.   -- Set computer label
  567.   local ccLabel = os.getComputerLabel()
  568.   if ccLabel == nil or tostring(ccLabel) == "" or ccLabel == "Gate" then
  569.     os.setComputerLabel(thisGate .. " gate")
  570.   end
  571.   term.clear()
  572.   -- Select shield/redstone output side
  573.   --if #thisGate == 7 then                              -- implement when iris control is introduced?
  574.     drawElement(2,2,45,1,lblue,black,"Please select the shield/redstone output side")
  575.     drawElement(2,4,22,1,gray,black,"(select an unused side")
  576.     drawElement(2,5,21,1,gray,black,"if not using redstone")
  577.     drawElement(2,6,7,1,gray,black,"output)")
  578.     if term.isColor() then
  579.       -- Draw side list
  580.       local tPos = { }
  581.       local tSide = { }
  582.       if #thisGate == 7 then
  583.         tPos = { 5,7,9,11,13,15 }
  584.         tSide = { "top","bottom","front","back","left","right" }
  585.       elseif #thisGate > 7 then
  586.         tPos = { 5,7,9,11,13,15 }
  587.         tSide = { "top","bottom","front","back","left","right" }
  588.         --tPos = { 5,7,9,11,13,15,17 }
  589.         --tSide = { "top","bottom","front","back","left","right","iris" }
  590.       end
  591.       if #thisGate == 7 then
  592.         drawElement(26,4,8,13,white,gray,"")              -- Draw blank menu (snapshot 82+)
  593.       elseif #thisGate > 7 then
  594.         drawElement(26,4,8,13,white,gray,"")              -- Draw blank menu (RC1)
  595.       end
  596.       for j,k in ipairs(tSide) do                           --
  597.         local tSides = string.upper(k):sub(1,1) .. k:sub(2) -- Draw side labels in menu
  598.         drawElement(27,tPos[j],#tSides,1,white,gray,tSides) --
  599.       end
  600.       while true do
  601.         local newSide = { os.pullEvent("mouse_click") }   --
  602.         if newSide[3] > 26 and newSide[3] < 34 then       --
  603.           for j,k in ipairs(tSide) do                     -- Select shield (redstone output) side
  604.             if newSide[4] == tPos[j] then                 --          or internal iris
  605.               gateSettings.shield = k                     --
  606.               return false                                --
  607.             end
  608.           end
  609.         end
  610.       end
  611.     else
  612.       -- Select redstone output side
  613.       term.clear()
  614.       drawElement(2,2,45,1,white,black,"Please select the shield/redstone output side")
  615.       -- Draw side list
  616.       term.setTextColor(white)
  617.       local theseSides = { "top", "bottom", "left", "right", "front", "back" }
  618.       for l,m in ipairs(theseSides) do
  619.         local sSides = tostring(l) .. " = " .. string.upper(m):sub(1,1) .. m:sub(2)
  620.         drawElement(2,l+4,#sSides,1,white,black,sSides)  -- Side selection list for standard computers and turtles
  621.       end
  622.       term.setCursorPos(2,12)
  623.       while true do
  624.         local getSide = tonumber(read())
  625.         if getSide ~= nil then
  626.           if getSide > 0 and getSide < 7 then
  627.             for n,o in ipairs(theseSides) do
  628.               if getSide == n then
  629.                 gateSettings.shield = o
  630.               end
  631.             end
  632.             break
  633.           end
  634.         end
  635.       end
  636.     end
  637.   --end
  638. end
  639.  
  640. -- AUTOMATIC/STATIC CONFIGURATION (Part 2)
  641. local function initGate()
  642.   for k,v in ipairs(rs.getSides()) do
  643.         -- Stargate
  644.     if peripheral.getType(v) == "stargate" then
  645.       if not gate then
  646.         gate = peripheral.wrap(tostring(v))
  647.         break
  648.       end
  649.     elseif peripheral.getType(v) == "modem" then
  650.       if peripheral.call(tostring(v),"isWireless") == false then
  651.         local mPerps = peripheral.call(tostring(v),"getNamesRemote")
  652.         for _,mPerp in ipairs(mPerps) do
  653.           if tostring(mPerp):sub(1,8) == "stargate" then
  654.             if not gate then
  655.               gate = peripheral.wrap(mPerp)
  656.               break
  657.             end
  658.           end
  659.         end
  660.       end
  661.     end
  662.   end
  663.   if not gate then
  664.     term.setBackgroundColor(black)
  665.     term.setTextColor(red)
  666.     term.clear()
  667.     term.setCursorPos(1,1)
  668.     term.write("No Stargate detected!")
  669.     term.setCursorPos(1,3)
  670.     return
  671.   end
  672.   thisGate = gate.getAddress()
  673.   fuelGuage = gate.hasFuel()
  674.   if #thisGate > 7 then
  675.     longName = thisGate
  676.   else
  677.     local tG = { }
  678.     longName = ""
  679.     for i = 1,#thisGate,1 do
  680.       tG[i] = thisGate:sub(i,i)
  681.       if i < #thisGate then
  682.         longName = longName .. tG[i] .. " "
  683.       else
  684.         longName = longName .. tG[i]
  685.       end
  686.     end
  687.   end
  688. end
  689.  
  690. local function initMe()
  691.   initGate()
  692.   if not gate then return end
  693.   -- Check for first time install
  694.   if not fs.exists("/data/gateSettings") then
  695.     firstRun()
  696.     term.setBackgroundColor(black)
  697.     term.clear()
  698.     saveData()
  699.   end
  700.   ingestData()
  701.   modemSide = "none"
  702.   for k,v in ipairs(rs.getSides()) do
  703.         -- Modem
  704.     if peripheral.getType(v) == "modem" then
  705.       if peripheral.call(tostring(v),"isWireless") == true then
  706.         -- redNet
  707.         if gateSettings.net == "R" then
  708.           modemSide = v
  709.           rednet.open(v)
  710.         -- ccNet
  711.         elseif gateSettings.net == "C" then
  712.           modemSide = v
  713.           modem = peripheral.wrap(v)
  714.           modem.open(gateSettings.chan.recv)
  715.         end
  716.         -- Monitor
  717.       elseif peripheral.call(tostring(v),"isWireless") == false then
  718.         local mPerps = peripheral.call(tostring(v),"getNamesRemote")
  719.         for _,mPerp in ipairs(mPerps) do
  720.           if tostring(mPerp):sub(1,7) == "monitor" then
  721.             mon = peripheral.wrap(mPerp)
  722.           end
  723.         end
  724.       end
  725.     end
  726.   end
  727.   if modemSide == "none" then
  728.     term.clear()
  729.     term.setCursorPos(2,2)
  730.     term.setTextColor(red)
  731.     term.write("gateLiaison requires a wireless modem")
  732.     term.setCursorPos(1,4)
  733.     return
  734.   end
  735.   if gateSettings.shield == "iris" then
  736.     shieldStatus, _ = pcall(gate.isIrisClosed)
  737.   else
  738.     shieldStatus = rs.getOutput(gateSettings.shield)
  739.   end
  740.   if mon ~= "none" then
  741.     displayMarquee()
  742.   end
  743.   updateStatus()
  744. end
  745. -- END AUTOMATIC/STATIC CONFIGURATION (Part 2)
  746.  
  747. initMe()
  748. if not gate then return end
  749. if modemSide == "none" then return end
  750.  
  751. while true do
  752.   gateKernel()
  753. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement