Advertisement
Wariie12

Induction Matrix control REDSTONE - ( Matrix )

Nov 10th, 2024 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.24 KB | None | 0 0
  1. -- Configuration
  2. local CONFIG = {
  3.     MONITOR_SIDE = "right",
  4.     MATRIX_SIDE = "back",
  5.     REDSTONE_SIDE = "front",
  6.     UPDATE_RATE = 0.5,
  7.     TEXT_SCALE = 0.5,
  8.     START_THRESHOLD = 10.0,   -- Start when power below 10%
  9.     STOP_THRESHOLD = 90.0,    -- Stop when power above 90%
  10.     ENERGY_TYPE = 'FE',
  11.     DEBUG = true,            -- Add debug output
  12.     DEFAULT_REDSTONE = true,
  13. }
  14.  
  15. -- Colors
  16. local COLORS = {
  17.     title = colors.yellow,
  18.     text = colors.white,
  19.     warning = colors.red,
  20.     good = colors.lime,
  21.     bar_empty = colors.gray,
  22.     bar_filled = colors.red,
  23.     header = colors.orange,
  24.     charging = colors.lime,
  25.     discharging = colors.orange
  26. }
  27.  
  28. -- Energy conversion setup
  29. local energy_type = 'J'
  30. local energy_convert = function(energy) return energy end
  31.  
  32. if mekanismEnergyHelper and mekanismEnergyHelper[('joulesTo%s'):format(CONFIG.ENERGY_TYPE)] then
  33.     energy_type = CONFIG.ENERGY_TYPE
  34.     energy_convert = mekanismEnergyHelper[('joulesTo%s'):format(CONFIG.ENERGY_TYPE)]
  35. end
  36.  
  37. local currentRedstoneState = CONFIG.DEFAULT_REDSTONE
  38.  
  39. -- Initialize peripherals
  40. local monitor = peripheral.wrap(CONFIG.MONITOR_SIDE)
  41. local matrix = peripheral.wrap(CONFIG.MATRIX_SIDE)
  42.  
  43. -- Set up monitor
  44. if monitor then
  45.     monitor.setTextScale(CONFIG.TEXT_SCALE)
  46. end
  47.  
  48. -- Display buffer to prevent flicker
  49. local displayBuffer = {}
  50. local oldBuffer = {}
  51.  
  52. function writeBuffer(x, y, text, color)
  53.     displayBuffer[y] = displayBuffer[y] or {}
  54.     for i = 1, #text do
  55.         displayBuffer[y][x + i - 1] = {
  56.             char = text:sub(i, i),
  57.             color = color
  58.         }
  59.     end
  60. end
  61.  
  62. function flushBuffer(monitor)
  63.     for y, row in pairs(displayBuffer) do
  64.         for x, cell in pairs(row) do
  65.             local oldCell = oldBuffer[y] and oldBuffer[y][x]
  66.             if not oldCell or oldCell.char ~= cell.char or oldCell.color ~= cell.color then
  67.                 monitor.setCursorPos(x, y)
  68.                 monitor.setTextColor(cell.color)
  69.                 monitor.write(cell.char)
  70.             end
  71.         end
  72.     end
  73.     oldBuffer = displayBuffer
  74.     displayBuffer = {}
  75. end
  76.  
  77. function formatEnergy(energy, decimals)
  78.     decimals = decimals or 1
  79.     local prefix = energy < 0 and '-' or ''
  80.     local amount = energy_convert(math.abs(energy))
  81.     local suffix = ''
  82.  
  83.     if amount >= 1000000000 then
  84.         amount = amount / 1000000000
  85.         suffix = 'G'
  86.     elseif amount >= 1000000 then
  87.         amount = amount / 1000000
  88.         suffix = 'M'
  89.     elseif amount >= 1000 then
  90.         amount = amount / 1000
  91.         suffix = 'k'
  92.     end
  93.  
  94.     return string.format('%s%.1f%s%s', prefix, amount, suffix, energy_type)
  95. end
  96.  
  97. function formatRate(rate)
  98.     local prefix = rate < 0 and '-' or ''
  99.     local amount = energy_convert(math.abs(rate))
  100.     local suffix = ''
  101.  
  102.     if amount >= 1000000000 then
  103.         amount = amount / 1000000000
  104.         suffix = 'G'
  105.     elseif amount >= 1000000 then
  106.         amount = amount / 1000000
  107.         suffix = 'M'
  108.     elseif amount >= 1000 then
  109.         amount = amount / 1000
  110.         suffix = 'k'
  111.     end
  112.  
  113.     return string.format('%s%.1f%s%s/t', prefix, amount, suffix, energy_type)
  114. end
  115.  
  116. function formatTime(seconds)
  117.     if seconds < 60 then
  118.         return string.format("%ds", math.floor(seconds))
  119.     elseif seconds < 3600 then
  120.         return string.format("%dm %ds", math.floor(seconds/60), math.floor(seconds%60))
  121.     else
  122.         local hours = math.floor(seconds/3600)
  123.         local minutes = math.floor((seconds%3600)/60)
  124.         return string.format("%dh%dm", hours, minutes)
  125.     end
  126. end
  127.  
  128. function drawProgressBar(x, y, width, percentage, barColor)
  129.     local filledWidth = math.floor((width * percentage) / 100)
  130.     writeBuffer(x, y, string.rep("#", filledWidth) .. string.rep("-", width - filledWidth), barColor)
  131. end
  132.  
  133. function getMatrixInfo()
  134.     if not matrix then return nil end
  135.  
  136.     local info = {
  137.         stored = matrix.getEnergy(),
  138.         capacity = matrix.getMaxEnergy(),
  139.         input = matrix.getLastInput(),
  140.         output = matrix.getLastOutput(),
  141.         transferCap = matrix.getTransferCap()
  142.     }
  143.  
  144.     info.percentage = (info.stored / info.capacity) * 100
  145.     info.change = info.input - info.output
  146.  
  147.     return info
  148. end
  149.  
  150. function display(info, redstoneStatus)
  151.     if not monitor or not info then return end
  152.  
  153.     -- Title
  154.     writeBuffer(2, 1, "Matrix Monitor", COLORS.title)
  155.  
  156.     -- Storage section
  157.     writeBuffer(2, 3, string.format("Storage: %d%%", math.floor(info.percentage)), COLORS.text)
  158.     local barColor = info.percentage < 25 and COLORS.warning or COLORS.good
  159.     drawProgressBar(2, 4, 30, info.percentage, barColor)
  160.  
  161.     -- Status section
  162.     writeBuffer(2, 6, "Status", COLORS.header)
  163.     writeBuffer(2, 7, string.format("Stored: %s", formatEnergy(info.stored)), COLORS.text)
  164.     writeBuffer(2, 8, string.format("Capacity: %s", formatEnergy(info.capacity)), COLORS.text)
  165.     writeBuffer(2, 9, string.format("Max IO: %s/t", formatEnergy(info.transferCap)), COLORS.text)
  166.  
  167.     -- Transfer section
  168.     writeBuffer(2, 11, "Transfer Rates", COLORS.header)
  169.     writeBuffer(2, 12, string.format("Input:  %s", formatRate(info.input)), COLORS.charging)
  170.     writeBuffer(2, 13, string.format("Output: %s", formatRate(info.output)), COLORS.discharging)
  171.     writeBuffer(2, 14, string.format("Max: %s", formatRate(info.transferCap)), COLORS.text)
  172.  
  173.     -- Usage section
  174.     writeBuffer(2, 16, "Usage Statistics", COLORS.header)
  175.     local inputPercent = (info.input / info.transferCap) * 100
  176.     local outputPercent = (info.output / info.transferCap) * 100
  177.     writeBuffer(2, 17, string.format("Input:  %.1f%%", inputPercent), COLORS.charging)
  178.     writeBuffer(2, 18, string.format("Output: %.1f%%", outputPercent), COLORS.discharging)
  179.  
  180.     -- Time section
  181.     writeBuffer(2, 20, "Time Estimates", COLORS.header)
  182.     if info.change > 0 then
  183.         local timeToFull = (info.capacity - info.stored) / (info.change * 20)
  184.         writeBuffer(2, 21, "Full: " .. formatTime(timeToFull), COLORS.text)
  185.     elseif info.change < 0 then
  186.         local timeToEmpty = info.stored / (math.abs(info.change) * 20)
  187.         writeBuffer(2, 21, "Empty: " .. formatTime(timeToEmpty), COLORS.warning)
  188.     else
  189.         writeBuffer(2, 21, "Stable", COLORS.text)
  190.     end
  191.  
  192.     -- Control Status
  193.     writeBuffer(2, 23, "Control Status", COLORS.header)
  194.     writeBuffer(2, 24, "Output: " .. (redstoneStatus and "ON" or "OFF"),
  195.         redstoneStatus and COLORS.charging or COLORS.discharging)
  196.  
  197.     flushBuffer(monitor)
  198. end
  199.  
  200. -- Add this function for debug output
  201. function debugPrint(message)
  202.     if CONFIG.DEBUG then
  203.         print(message)
  204.     end
  205. end
  206.  
  207. -- Replace the updateRedstone function with this fixed version:
  208. function updateRedstone(percentage)
  209.     debugPrint(string.format("Current power: %.1f%%", percentage))
  210.  
  211.     if type(percentage) ~= "number" then
  212.         debugPrint("Invalid percentage value")
  213.         return false
  214.     end
  215.  
  216.     -- Turn ON redstone when power is LOW
  217.     if percentage <= CONFIG.START_THRESHOLD then
  218.         if not currentRedstoneState then
  219.             debugPrint("Power low - Turning redstone ON")
  220.             currentRedstoneState = true
  221.             redstone.setOutput(CONFIG.REDSTONE_SIDE, true)
  222.         end
  223.         return currentRedstoneState
  224.     -- Turn OFF redstone when power is HIGH
  225.     elseif percentage >= CONFIG.STOP_THRESHOLD then
  226.         if currentRedstoneState then
  227.             debugPrint("Power high - Turning redstone OFF")
  228.             currentRedstoneState = false
  229.             redstone.setOutput(CONFIG.REDSTONE_SIDE, false)
  230.         end
  231.         return currentRedstoneState
  232.     end
  233.  
  234.     -- Keep current state for values between thresholds
  235.     debugPrint(string.format("Between thresholds - Keeping state: %s", currentRedstoneState and "ON" or "OFF"))
  236.     return currentRedstoneState
  237. end
  238.  
  239. -- Initialize redstone output
  240. print("Initializing redstone output...")
  241. currentRedstoneState = CONFIG.DEFAULT_REDSTONE
  242. redstone.setOutput(CONFIG.REDSTONE_SIDE, currentRedstoneState)
  243. debugPrint(string.format("Initial redstone state set to: %s", currentRedstoneState and "ON" or "OFF"))
  244.  
  245. -- Main loop
  246. print("Starting Matrix Monitor with Redstone Control")
  247. local lastUpdate = 0
  248. local updateThreshold = 0.1 -- Minimum time between updates
  249.  
  250. -- In the main loop, modify the redstone handling:
  251. while true do
  252.     if matrix and monitor then
  253.         local currentTime = os.epoch("local") / 1000
  254.         if currentTime - lastUpdate >= updateThreshold then
  255.             local info = getMatrixInfo()
  256.             if info then
  257.                 local redstoneStatus = updateRedstone(info.percentage)
  258.                 debugPrint(string.format("Redstone status: %s", redstoneStatus and "ON" or "OFF"))
  259.                 display(info, redstoneStatus)
  260.             end
  261.             lastUpdate = currentTime
  262.         end
  263.     else
  264.         -- Try to reconnect peripherals
  265.         matrix = peripheral.wrap(CONFIG.MATRIX_SIDE)
  266.         monitor = peripheral.wrap(CONFIG.MONITOR_SIDE)
  267.         if monitor then
  268.             monitor.setTextScale(CONFIG.TEXT_SCALE)
  269.             monitor.clear()
  270.             monitor.setCursorPos(1, 1)
  271.             monitor.setTextColor(COLORS.warning)
  272.             monitor.write("Reconnecting...")
  273.         end
  274.     end
  275.     os.sleep(CONFIG.UPDATE_RATE)
  276. end
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement