Advertisement
emprizr

AE2 Potion Graph Generator

May 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. -- Initial Config
  2. local storage_side = "top"
  3. local display_side = "monitor_6"
  4.  
  5. local s = peripheral.wrap(storage_side)
  6. local d = peripheral.wrap(display_side)
  7.  
  8. local storage_values = {}
  9.  
  10. -- DONT TOUCH THIS
  11. d.setTextScale(0.5)
  12. local width, height = d.getSize()
  13.  
  14. -- CHANGE THESE AS YOU LIKE
  15. -- Color variables
  16. local text_color = colors.white
  17. local background_color = colors.black
  18.  
  19. -- Variables for Graph X and Y Axis
  20. local base_row = height - 2
  21. local base_col = 3
  22. -- Graph Labels
  23. local x_label = "Time"
  24. local y_label = "Potions"
  25.  
  26. d.clear()
  27.  
  28. graph_x_start = height - base_row + 2
  29. graph_x_max = width - base_col
  30. graph_y_start = height - base_col
  31. graph_y_max = 1
  32.  
  33. d.setTextColor(text_color)
  34. d.setBackgroundColor(background_color)
  35.  
  36. function debug()
  37. for j=1, #storage_values do
  38.    print("values["..(j-1).."] = "..storage_values[j])
  39. end
  40. end
  41.  
  42. function getNbrOfPots()
  43.     local stored = s.getStoredItems()
  44.     table.insert(storage_values, stored.qty)
  45.     if table.getn(storage_values) == nbr_of_stored_values + 1 then
  46.         table.remove(storage_values, 1)
  47.     end
  48.     debug()
  49. end
  50.  
  51. function calcMeanSS(list, period)
  52.     sum = 0
  53.     for i=1, #list do
  54.         sum = sum + list[i]
  55.     end
  56.     mean = sum / #list
  57.    
  58.     sumsq = 0
  59.     for j=1, #list do
  60.         sq = list[j] - mean
  61.         sq = sq * sq
  62.         sumsq = sumsq + sq
  63.     end
  64.     mean_ss = sumsq / #list
  65.    
  66.     return mean_ss
  67. end
  68.  
  69. function calcPPM(list, period)
  70.     first = list[1]
  71.     last = list[period]
  72.    
  73.     diff = last - first
  74.    
  75.     ppm = 60/period
  76.    
  77.     return diff * ppm
  78. end
  79.  
  80. function normalizeList(list)
  81.     local result = {}
  82.     local min = list[1]
  83.     local max = list[#list]
  84.    
  85.     for i, value in pairs(list) do
  86.        
  87.         if value > max then
  88.             max = value
  89.         end
  90.        
  91.         if value < min then
  92.             min = value
  93.         end
  94.     end
  95.    
  96.     local range = max - min
  97.  
  98.    
  99.     for j = 1, #list do
  100.         result[j] = list[j] - min + 1
  101.     end
  102.    
  103.    
  104.     return result
  105. end
  106.  
  107. function displayPots()
  108.     printGraph(normalizeList(storage_values))
  109.    
  110. --    d.setTextColor(colors.red)
  111.     printCenter("PPM: "..math.floor(calcPPM(storage_values, #storage_values)), 0)
  112. --    d.setTextColor(text_color)
  113. end
  114.  
  115.  
  116. function printAt(str, x, y)
  117.     d.setCursorPos(x, y)
  118.     d.write(str)
  119. end
  120.  
  121. function printCenter(str, yOffset)
  122.     centerX = math.floor(width/2)
  123.     centerY = math.floor(height/2) + yOffset
  124.  
  125.     offsetX = math.floor(#str/2)
  126.  
  127.     printAt(str, centerX - offsetX, centerY)
  128. end
  129.  
  130. function printChartBars()
  131.    -- Print X Axis
  132.    for x=base_col, width - 1 do
  133.        printAt("-",x, base_row)
  134.    end
  135.    printAt(">", width, base_row)
  136.    
  137.    -- Print X Label
  138.    center_x = math.floor((width - base_col )/2) - math.floor(#x_label/2)
  139.    printAt(x_label, center_x, base_row + 1)
  140.    
  141.    -- Print Y Axis
  142.    printAt("^", base_col,1)
  143.    for y=2, base_row - 1 do
  144.        printAt("|", base_col, y)
  145.    end
  146.  
  147.    -- Print Y label (horizontally)
  148.    center_y = math.floor((height - base_col)/2)
  149.    label_y = center_y - math.floor(#y_label/2)
  150.        
  151.    label_idx = 1      
  152.    for i=1, #y_label do
  153.         c = y_label:sub(label_idx, label_idx)
  154.         printAt(c, math.floor(base_col/2), i + label_y)    
  155.         label_idx = label_idx + 1
  156.    end
  157. end
  158.  
  159. function clearGraph()
  160.     for x=graph_x_start, graph_x_max do
  161.         for y=1, graph_y_start do
  162.             printAt(" ", x, y)
  163.         end
  164.     end
  165. end
  166.  
  167. function printGraph(list)
  168.     -- X is static-isch
  169.     clearGraph()
  170.     y_base = graph_y_start
  171.     idx = 1
  172.     for x=graph_x_start, graph_x_max do
  173.         sign = ""
  174.         if list[idx] then
  175.             printGraphPoint("=", x, y_base - list[idx])
  176.         end    
  177.         idx = idx + 1
  178.     end
  179. end
  180.  
  181. function printGraphPoint(content, x, y)
  182. --    d.setTextColor(colors.green)
  183.     printAt(content, x, y)
  184. --    d.setTextColor(text_color)
  185. end
  186.  
  187. printChartBars()
  188. while true do
  189.     getNbrOfPots()
  190.     displayPots()
  191.     os.sleep(1)
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement