Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. local component = require("component")
  2. local string = require("string")
  3. local term = require("term")
  4. local os = require("os")
  5. local gpu = component.gpu
  6. local interface = {}
  7. interface = component.proxy(component.me_interface.address)
  8.  
  9. --Prints all keys in a table.
  10. function printTableKeys(table, prop)
  11. for k,v in pairs(table) do
  12. print("--->", k)
  13. end
  14. end
  15.  
  16. --Combines two tables. Returns a new table with the contents of the originals
  17. function combineTables(tables)
  18. local out = {}
  19.  
  20. for k,t in pairs(tables) do
  21. for j,item in pairs(t) do
  22. table.insert(out, item)
  23. end
  24. end
  25.  
  26. return out
  27. end
  28.  
  29. --Checks to see if item is a value in the table
  30. function isIn(table, item)
  31. for k,v in pairs(table) do
  32. if(v == item) then return true end
  33. end
  34. return false
  35. end
  36.  
  37. function plotLine(x0, y0, x1, y1, color)
  38. if(math.abs(y1 - y0) < abs(x1 - x0)) then
  39. if x0 > x1 then
  40. plotLineLow(x1, y1, x0, y0, color)
  41. else
  42. plotLineLow(x0, y0, x1, y1, color)
  43. end
  44. else
  45. if y0 > y1 then
  46. plotLineHigh(x1, y1, x0, y0, color)
  47. else
  48. plotLineHigh(x0, y0, x1, y1, color)
  49. end
  50. end
  51. end
  52.  
  53. function plotLineLow(x0, y0, x1, y1, color)
  54. local dx = x1 - x0
  55. local dy = y1 - y0
  56.  
  57. local yi = 1
  58.  
  59. if(dy < 0) then
  60. yi = -1
  61. dy = -dy
  62. end
  63.  
  64. local D = 2*dy - dx
  65. local y = y0
  66.  
  67. for x=x0,x1 do
  68. plot(x, y, color)
  69.  
  70. if D > 0 then
  71. y = y + yi
  72. D = D - 2*dx
  73. end
  74.  
  75. D = D + 2*dy
  76. end
  77. end
  78.  
  79. function plotLineHigh(x0, y0, x1, y1, color)
  80. local dx = x1 - x0
  81. local dy = y1 - y0
  82.  
  83. local xi = 1
  84.  
  85. if(dx < 0) then
  86. yx = -1
  87. dx = -dx
  88. end
  89.  
  90. local D = 2*dx - dy
  91. local x = x0
  92.  
  93. for y=y0,y1 do
  94. plot(x, y, color)
  95.  
  96. if D > 0 then
  97. x = x + xi
  98. D = D - 2*dy
  99. end
  100.  
  101. D = D + 2*dx
  102. end
  103. end
  104.  
  105. function plot(x, y, color)
  106. gpu.fill(x, y, 1, 1, color)
  107. end
  108.  
  109. --Items to track. Split up into multiple lists for readability, combined later on
  110. local metals = {"Copper Ingot", "Iron Ingot", "Tin Ingot", "Gold Ingot", "Zinc Ingot", "Silver Ingot", "Nickel Ingot"}
  111. local dusts = {"Redstone Dust", "Glowstone Dust", "Sulfur"}
  112. local other = {"Ender Pearl", "Nether Quartz", "Certeus Quartz", "Charcoal", "Coal", "Diamond", "Rubber Wood", "Sugarcane"}
  113.  
  114. local all = {}
  115. local totals = {}
  116. local rates = {}
  117. local accelerations = {}
  118.  
  119. --Initialize all items that we will be analyzing
  120. for _,item in pairs(combineTables({metals, dusts, other})) do
  121. all[item] = 0
  122. rates[item] = 0
  123. end
  124.  
  125. --+ is a blue color, - is a red color. vt100 color codes
  126. colors = {["+"] = "\27[34m", ["-"] = "\27[31m"}
  127.  
  128. --Prints all items that we will be tracking. Wait for a keypress
  129. printTableKeys(all, "")
  130. io.read()
  131.  
  132. --Make the window smaller, increasing text size.
  133. gpu.setResolution(80, 25)
  134.  
  135. --Get the cutoff (# items below which a name will turn red), and timeout (seconds per refresh)
  136. term.clear()
  137. print("Cutoff: ")
  138. local cutoff = tonumber(io.read())
  139.  
  140. print("Timeout: (seconds)")
  141. local timeout = tonumber(io.read())
  142.  
  143. --Main program loop. Manages refreshes, coloring, and table generation
  144. while true do
  145. term.clear()
  146. gpu.setForeground(0xFFFFFF)
  147.  
  148. --Table header
  149. io.write(string.format("%-20s\tCount\titem/%ss\titem/%ss^2\n", "Item",timeout, timeout))
  150.  
  151. items = {}
  152.  
  153. --
  154. for i,item in pairs(interface.getItemsInNetwork()) do
  155. if(type(item) == "table") then
  156. if(all[item.label] ~= nil) then
  157. local rate = item.size - all[item.label]
  158. local dRate = rate - rates[item.label]
  159.  
  160. all[item.label] = item.size
  161. rates[item.label] = rate
  162. accelerations[item.label] = dRate
  163.  
  164. items[#items + 1] = {["size"]=item.size, ["rate"]=rate, ["dRate"]=dRate, ["label"]=item.label}
  165. end
  166. end
  167. end
  168.  
  169. table.sort(items, function(a, b)
  170. return a.size > b.size
  171. end)
  172.  
  173. for k,item in pairs(items) do
  174. local coloredName = ""
  175. if(item.size < cutoff) then
  176. coloredName = colors["-"] .. tostring(item.label)
  177. else
  178. coloredName = colors["+"] .. tostring(item.label)
  179. end
  180.  
  181. local coloredRate = ""
  182. if(item.rate < 0) then
  183. coloredRate = colors["-"] .. "(" .. tostring(item.rate) .. ")"
  184. else
  185. coloredRate = colors["+"] .. "(+" .. tostring(item.rate) .. ")"
  186. end
  187.  
  188. local coloredDRate = ""
  189. if(item.dRate < 0) then
  190. coloredDRate = colors["-"] .. "(-" .. tostring(item.dRate) .. ")"
  191. else
  192. coloredDRate = colors["+"] .. "(++" .. tostring(item.dRate) .. ")"
  193. end
  194.  
  195. io.write(string.format("%-20s", coloredName) .. "\t\t" .. tostring(item.size) .. "\t" .. coloredRate .. "\t" .. coloredDRate .. "\n")
  196. end
  197. os.sleep(timeout)
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement