Advertisement
thelockj

Untitled

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