Koridev

induction_display

Apr 5th, 2025 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. -- matrix_dashboard.lua
  2. -- Zentraler Computer/Monitor-Skript für Mekanism Induction Matrix.
  3. -- Kommuniziert per rednet mit dem Induction-Client, zeigt ascii-grafische Anzeige.
  4.  
  5. -----------------------------------------
  6. -- 1) KONFIGURATION
  7. -----------------------------------------
  8. local modemSide = "bottom" -- Seite deines (Wireless/Wired) Modems
  9. local monSide = "top" -- Seite deines Monitors
  10. local clientID = 5 -- Computer-ID des matrix_client
  11. local refreshDelay = 3 -- Sekunden zwischen Updates
  12.  
  13. -----------------------------------------
  14. -- 2) INIT
  15. -----------------------------------------
  16. local modem = peripheral.wrap(modemSide)
  17. if not modem then
  18. error("Modem an Seite '"..modemSide.."' nicht gefunden!")
  19. end
  20. rednet.open(modemSide)
  21.  
  22. local mon = peripheral.wrap(monSide)
  23. if not mon then
  24. error("Monitor an Seite '"..monSide.."' nicht gefunden!")
  25. end
  26. mon.setTextScale(0.5)
  27.  
  28. local w, h = mon.getSize()
  29.  
  30. print("Matrix-Dashboard gestartet (ID: "..os.getComputerID()..")")
  31. print("Empfange Daten von Client:", clientID)
  32.  
  33. -----------------------------------------
  34. -- 3) GLOBALE DATEN
  35. -----------------------------------------
  36. -- Hier speichern wir die Werte, die wir vom Client bekommen
  37. local matrixData = {
  38. stored = 0,
  39. capacity = 1, -- capacity=1 um Division by zero zu vermeiden
  40. inputRate = 0,
  41. outputRate = 0
  42. }
  43.  
  44. -----------------------------------------
  45. -- 4) HILFSFUNKTIONEN
  46. -----------------------------------------
  47.  
  48. -- 4.1) Bildschirm löschen
  49. local function clear()
  50. mon.setBackgroundColor(colors.black)
  51. mon.clear()
  52. mon.setCursorPos(1,1)
  53. end
  54.  
  55. -- 4.2) Text zentriert ausgeben
  56. local function centerText(y, text, color)
  57. color = color or colors.white
  58. mon.setTextColor(color)
  59. local x = math.floor((w - #text) / 2)
  60. mon.setCursorPos(x+1, y)
  61. mon.write(text)
  62. end
  63.  
  64. -- 4.3) Zahl mit K/M/G/T abkürzen
  65. local function formatNumberShort(num)
  66. local absNum = math.abs(num)
  67. local sign = (num < 0) and "-" or ""
  68. if absNum >= 10^12 then
  69. return sign .. string.format("%.2fT", absNum / 10^12)
  70. elseif absNum >= 10^9 then
  71. return sign .. string.format("%.2fG", absNum / 10^9)
  72. elseif absNum >= 10^6 then
  73. return sign .. string.format("%.2fM", absNum / 10^6)
  74. elseif absNum >= 10^3 then
  75. return sign .. string.format("%.2fk", absNum / 10^3)
  76. else
  77. return tostring(num)
  78. end
  79. end
  80.  
  81. -- 4.4) ASCII-Farbiger Balken
  82. -- x, y = Startkoordinate
  83. -- width = Breite
  84. -- fillPercent = 0..1
  85. -- fillColor = z. B. colors.lime
  86. local function drawBar(x, y, width, fillPercent, fillColor)
  87. local fillCount = math.floor(width * fillPercent)
  88. mon.setCursorPos(x, y)
  89. mon.setBackgroundColor(fillColor)
  90. mon.write(string.rep(" ", fillCount))
  91. mon.setBackgroundColor(colors.gray)
  92. mon.write(string.rep(" ", width - fillCount))
  93. -- Hintergrund zurücksetzen
  94. mon.setBackgroundColor(colors.black)
  95. end
  96.  
  97. -----------------------------------------
  98. -- 5) DATEN VOM CLIENT HOLEN
  99. -----------------------------------------
  100. local function fetchMatrixData()
  101. rednet.send(clientID, {cmd="GET_DATA"}, "mekanism_matrix")
  102. local sender, resp, proto = rednet.receive("mekanism_matrix", 3)
  103. if sender == clientID and type(resp)=="table" then
  104. matrixData.stored = resp.stored or 0
  105. matrixData.capacity = resp.capacity or 1
  106. matrixData.inputRate = resp.inputRate or 0
  107. matrixData.outputRate = resp.outputRate or 0
  108. end
  109. end
  110.  
  111. -----------------------------------------
  112. -- 6) DASHBOARD-ZEICHNUNG
  113. -----------------------------------------
  114. local function drawDashboard()
  115. clear()
  116. centerText(1, "Mekanism Induction Matrix", colors.cyan)
  117.  
  118. -- Daten holen aus matrixData
  119. local stored = matrixData.stored
  120. local capacity = matrixData.capacity
  121. local inputRate = matrixData.inputRate
  122. local outputRate = matrixData.outputRate
  123.  
  124. -- Kurzformat
  125. local storedStr = formatNumberShort(stored)
  126. local capacityStr = formatNumberShort(capacity)
  127. local inStr = formatNumberShort(inputRate)
  128. local outStr = formatNumberShort(outputRate)
  129.  
  130. -- Füllstand in %
  131. local fillPercent = (capacity > 0) and (stored / capacity) or 0
  132.  
  133. mon.setCursorPos(2, 3)
  134. mon.setTextColor(colors.white)
  135. mon.write(("Energie: %s / %s FE"):format(storedStr, capacityStr))
  136.  
  137. mon.setCursorPos(2, 4)
  138. mon.write(("Input: %s FE/t, Output: %s FE/t"):format(inStr, outStr))
  139.  
  140. mon.setCursorPos(2, 6)
  141. mon.write("Füllstand:")
  142.  
  143. -- Farbauswahl basierend auf fillPercent
  144. local fillColor = colors.lime
  145. if fillPercent >= 0.75 then
  146. fillColor = colors.lime
  147. elseif fillPercent >= 0.5 then
  148. fillColor = colors.yellow
  149. elseif fillPercent >= 0.25 then
  150. fillColor = colors.orange
  151. else
  152. fillColor = colors.red
  153. end
  154.  
  155. -- Balken-Darstellung
  156. local barWidth = math.min(w - 3, 50)
  157. drawBar(2, 7, barWidth, fillPercent, fillColor)
  158.  
  159. local p100 = math.floor(fillPercent * 100)
  160. mon.setCursorPos(2, 9)
  161. mon.setTextColor(colors.white)
  162. mon.write(p100.."%")
  163. end
  164.  
  165. -----------------------------------------
  166. -- 7) HAUPT-LOOP
  167. -----------------------------------------
  168. local function mainLoop()
  169. while true do
  170. fetchMatrixData()
  171. drawDashboard()
  172. sleep(refreshDelay)
  173. end
  174. end
  175.  
  176. mainLoop()
  177.  
Advertisement
Add Comment
Please, Sign In to add comment