Advertisement
BlissSilence

Refined Storage Monitoring Testing

Jan 3rd, 2025 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. -- Sleep to let the pack start fully
  2. sleep(5)
  3. local monitors = {peripheral.find("monitor")}
  4.  
  5. local topMonitor = monitors[1] -- Main top monitor
  6. local rightMonitor = monitors[2] -- Modem-connected right monitor
  7. local leftMonitor = monitors[3] -- Left monitor (if needed later)
  8.  
  9. -- Set up peripherals
  10. local rs = nil -- Explicitly define rs to avoid nil errors
  11. while not rs do
  12. rs = peripheral.find("rsBridge")
  13. if not rs then
  14. if topMonitor then
  15. term.redirect(topMonitor)
  16. term.setBackgroundColor(colors.black)
  17. term.clear()
  18. term.setCursorPos(1, 1)
  19. term.setTextColor(colors.red)
  20. term.write("Waiting for Refined Storage...")
  21. end
  22. sleep(2)
  23. end
  24. end
  25.  
  26. -- Clear right and left monitors if they exist
  27. if rightMonitor then
  28. term.redirect(rightMonitor)
  29. term.setBackgroundColor(colors.black)
  30. term.clear()
  31. end
  32.  
  33. if leftMonitor then
  34. term.redirect(leftMonitor)
  35. term.setBackgroundColor(colors.black)
  36. term.clear()
  37. end
  38.  
  39. -- Default to top monitor for now
  40. term.redirect(topMonitor)
  41.  
  42. -- Color Palette
  43. local backgroundColor = colors.black
  44. local borderColor = colors.cyan
  45. local itemColor = colors.lightGray
  46. local amountColor = colors.orange
  47. local lowStockColor = colors.red
  48. local headerColor = colors.blue
  49. local increaseColor = colors.green
  50. local decreaseColor = colors.red
  51. local batteryColor = colors.green
  52. local batteryLowColor = colors.red
  53. local batteryFrameColor = colors.gray
  54. local fluidBarColor = colors.blue
  55. local fluidEmptyColor = colors.gray
  56.  
  57. local itemHistory = {}
  58. local arrowHistory = {}
  59. local arrowColorHistory = {}
  60.  
  61. -- Scaling and Layout
  62. function calculateScaling()
  63. topMonitor.setTextScale(1.8) -- Increased text scale for better readability
  64. w, h = topMonitor.getSize()
  65. end
  66.  
  67. -- Draw battery status on bottom monitor
  68. function drawBattery()
  69. if leftMonitor then
  70. term.redirect(leftMonitor)
  71. leftMonitor.setTextScale(1.0) -- Reset to ensure correct scaling
  72. local lw, lh = leftMonitor.getSize()
  73. term.redirect(leftMonitor)
  74. term.setBackgroundColor(colors.black)
  75.  
  76. local energy = rs.getEnergyStorage()
  77. local maxEnergy = rs.getMaxEnergyStorage()
  78. local energyUsage = rs.getEnergyUsage()
  79.  
  80. -- Display battery status text
  81. local statusText = string.format("FE Status: %d FE / %d FE Usage: %d FE", energy, maxEnergy, energyUsage)
  82. local startX = math.floor((lw - #statusText) / 2) + 1
  83. term.setCursorPos(startX, 1)
  84. term.setTextColor(colors.white)
  85. term.write(statusText)
  86.  
  87. local barWidth = 35
  88. local barHeight = 2
  89. local fill = math.floor((energy / maxEnergy) * barWidth)
  90. local batteryX = math.floor(lw / 2) - math.floor(barWidth / 2)
  91. local batteryY = math.floor(lh / 2)
  92.  
  93. -- Draw battery frame
  94. paintutils.drawBox(batteryX, batteryY, batteryX + barWidth, batteryY + barHeight, batteryFrameColor)
  95.  
  96. -- Draw battery fill
  97. local color = energy / maxEnergy < 0.2 and batteryLowColor or batteryColor
  98. paintutils.drawBox(batteryX + 1, batteryY + 1, batteryX + fill, batteryY + barHeight - 1, color)
  99. paintutils.drawBox(batteryX + fill + 1, batteryY + 1, batteryX + barWidth - 1, batteryY + barHeight - 1, batteryLowColor)
  100.  
  101. end
  102. end
  103.  
  104. -- Draw Item monitoring on right monitor
  105. local itemHistoryGraph = {}
  106. local barColorHistory = {}
  107. local previousItemCount = 0
  108. local maxGraphHeight = 6 -- Max height for right monitor graph
  109.  
  110. function drawItemGraph()
  111. if rightMonitor then
  112. term.redirect(rightMonitor)
  113. rightMonitor.setTextScale(0.6)
  114. local rw, rh = rightMonitor.getSize()
  115. term.setBackgroundColor(colors.black)
  116. term.clear()
  117.  
  118. -- Get total item count
  119. local totalItemCount = 0
  120. local rsitems = rs.listItems() or {}
  121. for _, item in pairs(rsitems) do
  122. totalItemCount = totalItemCount + item.amount
  123. end
  124.  
  125. -- Calculate difference from previous total
  126. local difference = totalItemCount - previousItemCount
  127. previousItemCount = totalItemCount
  128.  
  129. -- Store item count and bar color in history
  130. local scaleFactor = 10 -- Adjust scaling for better visibility
  131. local scaledDifference = math.floor(difference * scaleFactor)
  132.  
  133. -- Insert into history and apply color based on direction
  134. table.insert(itemHistoryGraph, scaledDifference)
  135. table.insert(barColorHistory, difference >= 0 and increaseColor or decreaseColor)
  136.  
  137. if #itemHistoryGraph > rw then
  138. table.remove(barColorHistory, 1)
  139. table.remove(itemHistoryGraph, 1)
  140. end
  141.  
  142. -- Draw graph bars
  143. local maxAmount = math.max(10, unpack(itemHistoryGraph))
  144. local barSpacing = math.max(1, math.floor(rw / maxGraphHeight))
  145. for i = math.max(1, #itemHistoryGraph - rw + 1), #itemHistoryGraph do
  146. local barX = rw - (#itemHistoryGraph - i) + 1
  147. local barHeight = math.floor((itemHistoryGraph[i] / maxAmount) * (rh * 0.8))
  148. local barColor = barColorHistory[i - math.max(1, #itemHistoryGraph - rw + 1) + 1] or increaseColor
  149. term.setBackgroundColor(barColor) -- Cap at 80% height
  150.  
  151. for y = rh, rh - barHeight + 1, -1 do
  152. term.setCursorPos(barX, y)
  153. term.write(" ")
  154. end
  155. end
  156.  
  157.  
  158. -- Display total item count
  159. term.setCursorPos(1, 1)
  160. term.setTextColor(colors.white)
  161. end
  162. end
  163.  
  164. -- Draw header
  165. function drawHeader(text)
  166. term.setBackgroundColor(headerColor)
  167. term.clearLine()
  168. term.setCursorPos(1, 1)
  169. term.setTextColor(colors.white)
  170. term.write(text)
  171. end
  172.  
  173. -- Draw footer
  174. function drawFooter()
  175. term.setBackgroundColor(headerColor)
  176. term.setCursorPos(1, h)
  177. term.clearLine()
  178. term.setTextColor(colors.white)
  179. term.write(" Updated: " .. os.date("%X"))
  180. end
  181.  
  182. -- Draw separator line
  183. function drawSeparator(y)
  184. paintutils.drawLine(1, y, w, y, borderColor)
  185. end
  186.  
  187. -- Draw item row with persistent up/down indicator and color
  188. function drawItemRow(y, itemName, itemAmount)
  189. if y < h then -- Prevent overwriting the footer
  190. local previousAmount = itemHistory[itemName] or itemAmount
  191. local arrow = arrowHistory[itemName] or " "
  192. local textColor = itemColor
  193. local arrowColor = arrowColorHistory[itemName] or amountColor
  194.  
  195. if itemAmount < 100 then
  196. textColor = lowStockColor
  197. end
  198.  
  199. if itemAmount > previousAmount then
  200. arrow = "^"
  201. arrowColor = increaseColor
  202. elseif itemAmount < previousAmount then
  203. arrow = "v"
  204. arrowColor = decreaseColor
  205. end
  206.  
  207. itemHistory[itemName] = itemAmount
  208. arrowHistory[itemName] = arrow
  209. arrowColorHistory[itemName] = arrowColor
  210.  
  211. term.setCursorPos(2, y)
  212. term.setBackgroundColor(backgroundColor)
  213. term.clearLine()
  214. term.setTextColor(textColor)
  215. term.write(itemName:gsub("[%[%]]", "")) -- Remove brackets
  216.  
  217. term.setCursorPos(w - #tostring(itemAmount) - 3, y)
  218. term.setTextColor(amountColor)
  219. term.write(tostring(itemAmount) .. " ")
  220. term.setTextColor(arrowColor)
  221. term.write(arrow)
  222. end
  223. end
  224.  
  225. -- Display items in list format with smooth scrolling
  226. function displayList()
  227. term.redirect(topMonitor)
  228. term.setBackgroundColor(backgroundColor)
  229. term.clear()
  230.  
  231. drawHeader(" Storage Overview ")
  232. drawSeparator(2)
  233.  
  234. local scrollOffset = 0
  235. local visibleRows = h - 4 -- Ensure footer space is not overwritten
  236.  
  237. while true do
  238. term.redirect(topMonitor)
  239. rsitems = rs.listItems() -- Refresh items every loop
  240. local filteredItems = {}
  241.  
  242. for _, item in pairs(rsitems) do
  243. if item.amount >= 32 then
  244. table.insert(filteredItems, item)
  245. end
  246. end
  247. table.sort(filteredItems, function(a, b) return a.amount > b.amount end)
  248.  
  249. term.setBackgroundColor(backgroundColor)
  250. term.clear()
  251. drawHeader(" Storage Overview ")
  252. drawSeparator(2)
  253. drawFooter()
  254.  
  255. local y = 3
  256. for i = 1 + scrollOffset, #filteredItems + scrollOffset do
  257. local index = ((i - 1) % #filteredItems) + 1 -- Loop smoothly
  258. if y < h then -- Ensure rows stop before footer
  259. drawItemRow(y, filteredItems[index].displayName, filteredItems[index].amount)
  260. end
  261. y = y + 1
  262. if y >= h then -- Stop drawing when footer is reached
  263. break
  264. end
  265. end
  266.  
  267. drawFooter()
  268.  
  269. sleep(0.35) -- Faster scroll speed
  270. scrollOffset = scrollOffset + 1
  271.  
  272. drawBattery()
  273. drawItemGraph()
  274. end
  275. end
  276.  
  277. -- Main execution loop
  278. function refreshDisplay()
  279. calculateScaling()
  280. displayList()
  281. end
  282.  
  283. -- Start display
  284. refreshDisplay()
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement