Advertisement
HydrantHunter

discoveryDialer 2

Jul 13th, 2015
1,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 KB | None | 0 0
  1. --[[ LanteaCraft & SGCraft ]]--
  2. --[[   DiscoveryDialer 2   ]]--
  3. --[[        by Dog         ]]--
  4. --[[         aka           ]]--
  5. --[[     HydrantHunter     ]]--
  6. --[[  pastebin: yg67N4UT   ]]--
  7.  
  8. if not os.getComputerLabel() then
  9.   os.setComputerLabel("CC#" .. tostring(os.getComputerID()))
  10. end
  11. local gate = peripheral.find("stargate")
  12. if not gate then
  13.   lcGate = true
  14.   gate = peripheral.find("StargateBase")
  15.   if gate then
  16.     if not gate.isValid() then gate = false end
  17.   end
  18. end
  19. if not gate then error("No stargate found!", 0) end
  20. local thisGate = lcGate and gate.getStargateAddressString() or gate.localAddress()
  21. local nineChevs = #thisGate == 9
  22. local symbols = { 'A', 'B', 'C', 'D', 'E' ,'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '+' }
  23. local startChevron = { [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, [6] = 1, [7] = 1, [8] = 1, [9] = 1 }
  24. local newAddress = nineChevs and 'AAAAAAAAA' or 'AAAAAAA'
  25. local numDials, numGates, numErrors = 0, 0, 0
  26. local gateStatus, continueDialing = false, true
  27. local conTimer
  28.  
  29. local function hangUp()
  30.   if lcGate then
  31.     --local onHook = pcall(gate.disengageStargate)
  32.     pcall(gate.disengageStargate)
  33.     local numChevrons = gate.getActivatedChevrons()
  34.     if numChevrons > 0 then
  35.       for i = 1, numChevrons + 1 do --# had to add the + 1 so quitting would work correctly
  36.         --onHook = pcall(gate.deactivateChevron)
  37.         --if not onHook then break end
  38.         pcall(gate.deactivateChevron)
  39.       end
  40.     end
  41.   else
  42.     gate.disconnect()
  43.   end
  44. end
  45.  
  46. local function gateWatch()
  47.   local event, timerName
  48.   while continueDialing do
  49.     event, timerName = os.pullEvent()
  50.     if event == "connect" then
  51.       return true
  52.     elseif event == "timer" and timerName == conTimer then
  53.       hangUp()
  54.       return false
  55.     end
  56.   end
  57. end
  58.  
  59. local function gateDialer()
  60.   local checkAddress
  61.   while continueDialing do
  62.     print("Dialing " .. newAddress)
  63.     if lcGate then
  64.       for i = 1, #newAddress do
  65.         --[[
  66.         if continueDialing then
  67.           checkAddress = pcall(gate.selectGlyph, newAddress:sub(i, i))
  68.         else
  69.           return
  70.         end
  71.         if checkAddress then
  72.           if continueDialing then
  73.             checkAddress = pcall(gate.activateChevron)
  74.           else
  75.             return
  76.           end
  77.           if checkAddress then
  78.             os.pullEvent("engageGlyph")
  79.           else
  80.             hangUp()
  81.             break
  82.           end
  83.         else
  84.           hangUp()
  85.           break
  86.         end
  87.         ]]--
  88.         if continueDialing then
  89.           pcall(gate.selectGlyph, newAddress:sub(i, i))
  90.           pcall(gate.activateChevron)
  91.           os.pullEvent("engageGlyph")
  92.         else
  93.           return
  94.         end
  95.       end
  96.       if continueDialing then
  97.         --checkAddress = pcall(gate.engageStargate)
  98.         pcall(gate.engageStargate)
  99.         conTimer = os.startTimer(2)
  100.       else
  101.         return
  102.       end
  103.     else
  104.       checkAddress = gate.dial(newAddress)
  105.     end
  106.     if continueDialing then
  107.       if checkAddress then
  108.         numDials = numDials + 1
  109.         gateStatus = lcGate and gateWatch() or true
  110.         --if lcGate then gateStatus = gateWatch() else gateStatus = true end
  111.         break
  112.       else
  113.         if lcGate then
  114.           --print("Unable to dial")
  115.           --numErrors = numErrors + 1
  116.           --if numErrors >= 10 then continueDialing = false return end
  117.           --sleep(2)
  118.           numDials = numDials + 1
  119.           gateStatus = gateWatch()
  120.         else
  121.           numDials = numDials + 1
  122.           gateStatus = false
  123.           break
  124.         end
  125.       end
  126.     else
  127.       return
  128.     end
  129.   end
  130.   --if gateStatus and (lcGate or (not lcGate and gate.stargateState() == "Connected")) then
  131.   if gateStatus then
  132.     print("Found gate at " .. newAddress)
  133.     numGates = numGates + 1
  134.     local file = fs.open("gateList", fs.exists("gateList") and "a" or "w")
  135.     file.writeLine(newAddress)
  136.     file.close()
  137.     gateStatus = false
  138.     if lcGate then
  139.       pcall(gate.disengageStargate)
  140.       os.pullEvent("disconnect")
  141.     else
  142.       gate.disconnect()
  143.     end
  144.     --sleep(2)
  145.   end
  146. end
  147.  
  148. local function gateScanner()
  149.   for chevronONE = startChevron[1], #symbols do
  150.     for chevronTWO = startChevron[2], #symbols do
  151.       for chevronTHREE = startChevron[3], #symbols do
  152.         for chevronFOUR = startChevron[4], #symbols do
  153.           for chevronFIVE = startChevron[5], #symbols do
  154.             for chevronSIX = startChevron[6], #symbols do
  155.               for chevronSEVEN = startChevron[7], #symbols do
  156.                 if nineChevs then
  157.                   for chevronEIGHT = startChevron[8], #symbols do
  158.                     for chevronNINE = startChevron[9], #symbols do
  159.                       newAddress = symbols[chevronONE] .. symbols[chevronTWO] .. symbols[chevronTHREE] .. symbols[chevronFOUR] .. symbols[chevronFIVE] .. symbols[chevronSIX] .. symbols[chevronSEVEN] .. symbols[chevronEIGHT] .. symbols[chevronNINE]
  160.                       if newAddress ~= thisGate then
  161.                         print("Temporal buffer...")
  162.                         sleep(1)
  163.                         local file = fs.open(".discoverySession", "w")
  164.                         file.writeLine(newAddress)
  165.                         file.close()
  166.                         gateDialer()
  167.                       else
  168.                         print("Skipping my gate...")
  169.                       end
  170.                     end
  171.                   end
  172.                 else
  173.                   newAddress = symbols[chevronONE] .. symbols[chevronTWO] .. symbols[chevronTHREE] .. symbols[chevronFOUR] .. symbols[chevronFIVE] .. symbols[chevronSIX] .. symbols[chevronSEVEN]
  174.                   if newAddress ~= thisGate then
  175.                     print("Temporal buffer...")
  176.                     sleep(1)
  177.                     local file = fs.open(".discoverySession", "w")
  178.                     file.writeLine(newAddress)
  179.                     file.close()
  180.                     gateDialer()
  181.                   else
  182.                     print("Skipping my gate...")
  183.                   end
  184.                 end
  185.               end
  186.             end
  187.           end
  188.         end
  189.       end
  190.     end
  191.   end
  192. end
  193.  
  194. local function quit()
  195.   while true do
  196.     local _, char = os.pullEvent("char")
  197.     if string.lower(char) == "q" then
  198.       continueDialing = false
  199.       hangUp()
  200.       return
  201.     end
  202.   end
  203. end
  204.  
  205. if fs.exists("warSession") then
  206.   fs.move("warSession", ".discoverySession")
  207. end
  208.  
  209. if fs.exists(".discoverySession") then
  210.   local file = fs.open(".discoverySession", "r")
  211.   newAddress = file.readLine()
  212.   file.close()
  213.   for i = 1, nineChevs and 9 or 7 do
  214.     for j = 1, #symbols do
  215.       if newAddress:sub(i, i) == symbols[j] then
  216.         startChevron[i] = j
  217.         break
  218.       end
  219.     end
  220.   end
  221. end
  222.  
  223. parallel.waitForAny(gateScanner, quit)
  224.  
  225. print("\n")
  226. print("Dialed " .. tostring(numDials) .. " addresses in " .. tostring(numDials + numErrors) .. " tries\n")
  227. print("Had " .. tostring(numErrors) .. " errors\n")
  228. print("Found " .. tostring(numGates) .. " gates\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement