Advertisement
Wyvern67

eScan

Oct 16th, 2015
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.67 KB | None | 0 0
  1. local tick = 0.05
  2.  
  3. function writeLog(text)
  4.     if not fileName then
  5.         fileName = "undefined"
  6.     end
  7.     f = fs.open(fileName, "a")
  8.     f.writeLine(text)
  9.     f.close()
  10. end
  11.  
  12. debugMode = true
  13. function console(text, ligne)
  14.     local lx, ly = term.getCursorPos()
  15.     if ligne==nil then ligne=1 end
  16.     if debugMode == true then
  17.         term.setCursorPos(1,ligne)
  18.         term.clearLine()
  19.         term.write(text)
  20.     end
  21.     term.setCursorPos(lx, ly)
  22. end
  23.  
  24. local function compare(data1,data2)
  25.     return data1 > data2
  26. end
  27.  
  28. function tableSort(tToSort)
  29.     local copyToSort = {}
  30.     for i = 1, #tToSort do
  31.         copyToSort[i] = tToSort[i]
  32.     end
  33.     local len = #copyToSort
  34.     for j = 2, len do
  35.         local key = copyToSort[j]
  36.         local i = j - 1
  37.         while i > 0 and compare(copyToSort[i],key) do
  38.             copyToSort[i + 1] = copyToSort[i]
  39.             i = i - 1
  40.         end
  41.         copyToSort[i + 1] = key
  42.     end
  43.     return copyToSort
  44. end
  45.  
  46. function selectionMenu(title, ... )
  47.     tChoices = {}
  48.     if type(arg[1]) == "table" then
  49.         for i=1, #arg do
  50.             tChoices[i] = arg[i]
  51.             -- tChoices[1] = {a,b,c,d,e}
  52.             -- tChoices[2] = {1,2,3,4,5}
  53.         end
  54.     else
  55.         tChoices[1] = arg
  56.         -- tChoices[1] = {a,b,c,d,e}
  57.     end
  58.    
  59.     local nTermX, nTermY = term.getSize()
  60.     local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal
  61.     local tSeparatorLength = math.floor((nTermX-string.len(tostring(title)))/2)-1
  62.     local tSeparator = (" "):rep(tSeparatorLength)
  63.     print(tSeparator)
  64.     -- Do the above for the remaining
  65.  
  66.     local nSelection = 1 -- The current selection defaults at 1
  67.     while true do
  68.         term.setCursorPos(1, 1)
  69.         term.clear()
  70.         print(sSeperator)
  71.         term.write("|"..(" "):rep(tSeparatorLength))
  72.         term.write(tostring(title))
  73.         if tSeparatorLength%2 == 0 then
  74.             print(tSeparator.."|")
  75.         else
  76.             print(tSeparator.." |")
  77.         end
  78.         print(sSeperator)
  79.  
  80.         for nLine = 1, #tChoices[1] do -- Iterate through the possible potions, and print them, marking the chosen one
  81.             local sLine = " "
  82.             if nSelection == nLine then
  83.                 sLine = ">"
  84.             end
  85.             local sLineNum = tostring(nLine)
  86.             if #sLineNum < 2 then
  87.                 sLineNum = "0" .. sLineNum -- Prepend a 0 if it's too short
  88.             end
  89.             sLine = sLine .. "[" .. sLineNum .. "] "
  90.             for i=1,#tChoices do
  91.                 sLine = sLine .. tostring(tChoices[i][nLine]) .. " "
  92.                 -- [0] choix1 choix2 choixN
  93.             end
  94.             print(sLine) -- Print it
  95.         end
  96.         -- os.pullEvent keys: up - 200, down - 208, enter - 28
  97.         local sEvent, nKey = os.pullEvent("key") -- Using the 1.3 filtering; this will mean only "key" events will pass
  98.  
  99.         if nKey == 200 or nKey == 17 then -- Up/w key: move up the menu
  100.             if tChoices[1][nSelection - 1] then -- Check if we can move up
  101.                 nSelection = nSelection - 1
  102.             end
  103.             -- Ignore it otherwise
  104.         elseif nKey == 208 or nKey == 31  then -- Down/s key: move down the menu
  105.             if tChoices[1][nSelection + 1] then -- Check if we can move down
  106.                 nSelection = nSelection + 1
  107.             end
  108.         elseif nKey == 28 then -- Enter key: Selecting a choice
  109.             if tChoices[1][nSelection] then
  110.                 return nSelection, tChoices[1][nSelection]  -- Run the function associated with the action.
  111.             else
  112.                 print("Error: Selection out of bounds: ", nSelection)
  113.                 return false
  114.             end
  115.         end
  116.     end
  117. end
  118.  
  119. function searchTableFor(t, restrict)
  120.     ret = {}
  121.     for i,v in pairs(t) do
  122.         if string.find(tostring(v), restrict) then
  123.             table.insert(ret, v)
  124.         else
  125.            
  126.         end
  127.     end
  128.     return ret
  129. end
  130.  
  131. function readLogs()
  132.     files = fs.list("")
  133.     files = searchTableFor(files, ".log")
  134.     _, choice = selectionMenu("Choose a log file", files)
  135.    
  136.     f = fs.open(choice, "r")
  137.     assert(f)
  138.     line = ""
  139.     j = 0
  140.     k = 0
  141.    
  142.    
  143.     values = {}
  144.     while true do
  145.         line = f.readLine()
  146.         if line == nil then
  147.             break
  148.         end
  149.         j = j+1
  150.         values[j] = {}
  151.         for i,v in string.gmatch(line, "%d+") do
  152.             table.insert(values[j], i)
  153.             -- k = k+1
  154.             -- values[j][(k%3)+1] = i
  155.         end
  156.         table.insert(values[j], line)
  157.     end
  158.    
  159.     if string.find(pType, "tesseract") then
  160.         local freq = {}
  161.         local speed = {}
  162.         for i=1,#values do
  163.             freq[i] = values[i][1].." Hz"
  164.             speed[i] = values[i][2].." RF/t"
  165.         end
  166.         _, choice = selectionMenu("Frequency selection", freq, speed)
  167.         choice = string.reverse(string.sub(string.reverse(choice), 4))
  168.         print(tonumber(choice))
  169.         assert(e.setFrequency(tonumber(choice)), "Error: Couldn't change the tesseract frequency")
  170.     else  --assume enderchest/endertank
  171.         term.clear()
  172.         for i=1,#values do
  173.             e.setColors(2^values[i][1], 2^values[i][2], 2^values[i][3])
  174.             console("Apply a redstone signal to cycle through the log file")
  175.             console(values[i][4], 3)
  176.             while rs.getInput("front") == false do
  177.                 sleep()
  178.             end
  179.         end
  180.     end
  181. end
  182.  
  183. --[[
  184. if peripheral.getType(side) = "modem" then
  185.     names = peripheral.getNamesRemote()
  186.     if #names > 0 then
  187.         for i=1,#names do
  188.             types = peripheral.getTypeRemote(names[i])
  189.         end
  190.         if #names > 1 then
  191.             choice = selectionMenu(names)
  192.             if choice then
  193.                 pType = types[choice]
  194.             end
  195.         end
  196.     end
  197. end
  198. --]]
  199.  
  200. names = peripheral.getNames()
  201. if #names > 0 then
  202.     types = {}
  203.     if #names > 1 then
  204.         for i=1,#names do
  205.             types[i] = peripheral.getType(names[i])
  206.         end
  207.        
  208.         choice = selectionMenu("First, please select a peripheral", types)
  209.        
  210.         if choice then
  211.             pType = types[choice]
  212.             assert(pType)
  213.             e = peripheral.wrap(names[choice])
  214.             assert(e)
  215.         end
  216.     end
  217. end
  218.  
  219. _, action = selectionMenu("Action to perform", "Explore logs", "Bruteforce")
  220.  
  221. if action == "Explore logs" then
  222.     readLogs(e)
  223. else
  224.     local i = 1
  225.     fileName = ""
  226.     truncatedPType = string.reverse(string.sub(string.reverse(pType), -10))
  227.     repeat
  228.         fileName = truncatedPType.."_"..i..".log"
  229.         i = i+1
  230.     until fs.exists(fileName) == false
  231.  
  232.  
  233.    
  234.     if pType == "ender_tank" then
  235.         for a=0,15 do
  236.             for b=0,15 do
  237.                 for c=0,15 do
  238.                     e.setColors(2^a,2^b,2^c)
  239.                     tank = e.getTankInfo()[1]
  240.                     if tank.contents.amount > 0 then
  241.                         writeLog(a..","..b..","..c..","..tank.contents.name)
  242.                     end
  243.                 end
  244.                 sleep(tick)
  245.             end
  246.         end
  247.     elseif pType == "ender_chest" then
  248.         for a=0,15 do
  249.             for b=0,15 do
  250.                 for c=0,15 do
  251.                     e.setColors(2^a,2^b,2^c)
  252.                     inventory = e.getAllStacks()
  253.                     if #inventory > 0 then
  254.                         if inventory[1] ~= nil then
  255.                             writeLog(a..","..b..","..c..","..tostring(inventory[1].all().name))
  256.                         else
  257.                             writeLog(a..","..b..","..c..",unknown")
  258.                         end
  259.                     end
  260.                 end
  261.                 sleep(tick)
  262.             end
  263.         end
  264.     elseif pType == "tile_thermalexpansion_ender_tesseract_name" then
  265.         choice = selectionMenu("Please select an energy cell", types)
  266.         term.clear()
  267.        
  268.         if choice then
  269.             cell = peripheral.wrap(names[choice])
  270.             if type(cell.getEnergyStored) == "function" then
  271.                 function isGettingPower(accuracy)
  272.                     if type(accuracy) ~= "number" then
  273.                         local accuracy = 5
  274.                     end
  275.                     local stored = cell.getEnergyStored()
  276.                     for i=1,accuracy do
  277.                         sleep()
  278.                         if cell.getEnergyStored() ~= stored then
  279.                             return true
  280.                         end
  281.                     end
  282.                     return false
  283.                 end
  284.                 function getPowerIncomePerTick(accuracy, debugValue)
  285.                     if tonumber(accuracy) > 0 then
  286.                         local t0 = os.clock()
  287.                         local sum = 0
  288.                         local energyAtFirst = cell.getEnergyStored()
  289.                         for i=1,accuracy do
  290.                             sleep()
  291.                             console(debugValue.."/999 = "..(cell.getEnergyStored()-energyAtFirst)/i, 19)
  292.                         end
  293.                         local gain = cell.getEnergyStored() - energyAtFirst
  294.                         local timeSpan = os.clock() - t0
  295.                         return (gain/timeSpan) / 20
  296.                     else
  297.                         return false
  298.                     end
  299.                 end
  300.             else
  301.                 return false
  302.             end
  303.         end
  304.        
  305.         for i=1,999 do
  306.             e.setFrequency(i)
  307.             console(tostring(i).."/999 = 0", 19)
  308.             if getPowerIncomePerTick(1, i) == 0 then
  309.             else
  310.                 local espeed = getPowerIncomePerTick(10, i)
  311.                 writeLog(i..","..math.floor(espeed))
  312.             end
  313.         end
  314.         term.setCursorPos(1,1)
  315.     end
  316. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement