Advertisement
HydrantHunter

discoveryDialer 1

Jun 15th, 2014
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.10 KB | None | 0 0
  1. --[[ LanteaCraft & SGCraft ]]--
  2. --[[    DiscoveryDialer    ]]--
  3. --[[        by Dog         ]]--
  4. --[[         aka           ]]--
  5. --[[     HydrantHunter     ]]--
  6. --[[  pastebin: BiXu3DfW   ]]--
  7.  
  8. local tArgs = { ... }
  9. local gate = peripheral.find("stargate")
  10. if not gate then print("") print("No stargate found!") print("") return end
  11. local lcGate = pcall(gate.getAddress)
  12. local thisGate = lcGate and gate.getAddress() or gate.localAddress()
  13. if not lcGate and #thisGate == 7 then print("") print("discoDialer requires a 9 chevron gate.") print("") return end
  14. 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', '-', '+' }
  15. local startChevron = { [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, [6] = 1, [7] = 1, [8] = 1, [9] = 1 }
  16. local newAddress = 'AAAAAAAAA'
  17. local numDials, numGates, numErrors = 0, 0, 0
  18. local gateStatus, stopMe = false, false
  19.  
  20. local function loadSession()
  21.   local file = fs.open(".discoverySession", "r")
  22.   newAddress = file.readLine()
  23.   file.close()
  24. end
  25.  
  26. local function saveSession()
  27.   local file = fs.open(".discoverySession", "w")
  28.   file.writeLine(newAddress)
  29.   file.close()
  30. end
  31.  
  32. local function recordGate()
  33.   local file = fs.open("gateList", fs.exists("gateList") and "a" or "w")
  34.   file.writeLine(newAddress)
  35.   file.close()
  36. end
  37.  
  38. local function gateWatch()
  39.   while true do
  40.     local event, success, newState = os.pullEvent()
  41.     if lcGate then
  42.       if event == "sgIdle" then
  43.         return false
  44.       elseif event == "sgWormholeOpening" and success then
  45.         return true
  46.       end
  47.     else
  48.       if event == "sgStargateStateChange" and newState == "Idle" then
  49.         return false
  50.       elseif event == "sgStargateStateChange" and newState == "Connected" then
  51.         return true
  52.       end
  53.     end
  54.   end
  55. end
  56.  
  57. local function gateDialer()
  58.   while true do
  59.     local checkAddress, _ = pcall(gate.dial, newAddress)
  60.     print("Dialing " .. newAddress)
  61.     if checkAddress then
  62.       numDials = numDials + 1
  63.       gateStatus = gateWatch()
  64.       break
  65.     else
  66.       if lcGate then
  67.         print("Unable to dial")
  68.         numErrors = numErrors + 1
  69.         if numErrors >= 10 then numErrors = 0 return end
  70.         sleep(2)
  71.       else
  72.         numDials = numDials + 1
  73.         gateStatus = false
  74.         break
  75.       end
  76.     end
  77.   end
  78.   if gateStatus and ((lcGate and gate.isConnected()) or (not lgGate and gate.stargateState() == "Connected")) then
  79.     print("Found gate at " .. newAddress)
  80.     numGates = numGates + 1
  81.     recordGate(newAddress)
  82.     gateStatus = false
  83.     pcall(gate.disconnect)
  84.     sleep(2)
  85.   end
  86. end
  87.  
  88. local function gateScanner(chevronEIGHT, chevronNINE)
  89.   for chevronONE = startChevron[1], #symbols do
  90.     for chevronTWO = startChevron[2], #symbols do
  91.       for chevronTHREE = startChevron[3], #symbols do
  92.         for chevronFOUR = startChevron[4], #symbols do
  93.           for chevronFIVE = startChevron[5], #symbols do
  94.             for chevronSIX = startChevron[6], #symbols do
  95.               for chevronSEVEN = startChevron[7], #symbols do
  96.                 newAddress = symbols[chevronONE] .. symbols[chevronTWO] .. symbols[chevronTHREE] .. symbols[chevronFOUR] .. symbols[chevronFIVE] .. symbols[chevronSIX] .. symbols[chevronSEVEN] .. symbols[chevronEIGHT] .. symbols[chevronNINE]
  97.                 if newAddress ~= thisGate then
  98.                   print("Temporal buffer...")
  99.                   sleep(1)
  100.                   saveSession()
  101.                   gateDialer()
  102.                 end
  103.               end
  104.             end
  105.           end
  106.         end
  107.       end
  108.     end
  109.   end
  110. end
  111.  
  112. local function dimensionScanner()
  113.   if tArgs and tArgs[1] and type(tArgs[1]) == "string" and #tArgs[1] == 2 then
  114.     local chevronEIGHT, chevronNINE
  115.     for i = 1, #symbols do
  116.       if tArgs[1]:sub(1, 1) == symbols[i] then chevronEIGHT = i end
  117.       if tArgs[1]:sub(2) == symbols[i] then chevronNINE = i end
  118.     end
  119.     gateScanner(chevronEIGHT, chevronNINE)
  120.     stopMe = true
  121.   else
  122.     for chevronEIGHT = startChevron[8], #symbols do
  123.       for chevronNINE = startChevron[9], #symbols do
  124.         gateScanner(chevronEIGHT, chevronNINE)
  125.       end
  126.     end
  127.     stopMe = true
  128.   end
  129. end
  130.  
  131. local function quit()
  132.   local _, char = os.pullEvent("char")
  133.   if string.lower(char) == "q" then
  134.     pcall(gate.disconnect)
  135.     stopMe = true
  136.   end
  137. end
  138.  
  139. if fs.exists("warSession") then
  140.   fs.move("warSession", ".discoverySession")
  141. end
  142.  
  143. if fs.exists(".discoverySession") then
  144.   loadSession()
  145.   for i = 1, 9 do
  146.     for j = 1, #symbols do
  147.       if newAddress:sub(i, i) == symbols[j] then
  148.         startChevron[i] = j
  149.         break
  150.       end
  151.     end
  152.   end
  153. end
  154.  
  155. while true do
  156.   parallel.waitForAny(dimensionScanner, quit)
  157.   if stopMe or numErrors >= 10 then break end
  158. end
  159.  
  160. print("")
  161. print("")
  162. print("Dialed " .. tostring(numDials) .. " addresses in " .. tostring(numDials + numErrors) .. " tries")
  163. print("")
  164. print("Had " .. tostring(numErrors) .. " errors")
  165. print("")
  166. print("Found " .. tostring(numGates) .. " gates")
  167. print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement