ksaw000

Untitled

Apr 8th, 2026 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. --Start Config--
  2. --Valid values are "RF", "J", and "EU".
  3. displayedEnergyType = "RF"
  4. batteryEnergyType = "RF"
  5. debug = 0
  6. updateDelay = 4
  7. normalizeAmount = 100
  8. modemChannel = 5695
  9.  
  10. bgColor = colors.black
  11. txtColor = colors.red
  12. pgFromColor = colors.red
  13. pgToColor = colors.green
  14. pgTextColor = colors.black
  15. --End Config--
  16.  
  17. function clear()
  18.  if mon.isColor() == true then
  19.   mon.setBackgroundColor(bgColor)
  20.  end
  21.  mon.clear()
  22.  mon.setCursorPos(1,1)
  23. end
  24.  
  25. function drawTextLeft(y, text, color_txt, color_bg)
  26.  if mon.isColor() == true then
  27.   mon.setTextColor(color_txt)
  28.   mon.setBackgroundColor(color_bg)
  29.  end
  30.  mon.setCursorPos(2,y)
  31.  mon.write(text)
  32. end
  33.  
  34. function drawTextRight(y, text, color_txt, color_bg)
  35.  if mon.isColor() == true then
  36.   mon.setTextColor(color_txt)
  37.   mon.setBackgroundColor(color_bg)
  38.  end
  39.  if monX - #text >= 1 then
  40.   mon.setCursorPos(monX - #text, y)
  41.   mon.write(text)
  42.  else
  43.   mon.setCursorPos(1, y)
  44.   mon.write("Number is too big!")
  45.  end
  46. end
  47.  
  48. function drawTextCenter(y, text, color_txt, color_bg)
  49.  if mon.isColor() == true then
  50.   mon.setTextColor(color_txt)
  51.   mon.setBackgroundColor(color_bg)
  52.  end
  53.  mon.setCursorPos(monX/2 - #text/2 + 1, y)
  54.  mon.write(text)
  55. end
  56.  
  57. function drawLine(x, y, length, size, color_bar)
  58.  for yPos = y, y+size-1 do
  59.   if mon.isColor() == true then
  60.    mon.setBackgroundColor(color_bar)
  61.   end
  62.   mon.setCursorPos(x, yPos)
  63.   mon.write(string.rep(" ", length))
  64.  end
  65. end
  66.  
  67. function drawProg(x, y, name, length, size, minVal, maxVal)
  68.  drawLine(x, y, length, size, pgFromColor)
  69.  local barSize = math.floor((minVal/maxVal)*length)
  70.  drawLine(x, y, barSize, size, pgToColor)
  71.  local text = name .." ".. math.floor((minVal/maxVal)*100) .."%"
  72.  drawTextCenter(y+size/2, text, pgTextColor, pgToColor)
  73. end
  74.  
  75. function split(str, delimiter)
  76.  local result = {}
  77.  local from = 1
  78.  local d_from, d_to = string.find(str, delimiter, from)
  79.  while d_from do
  80.   table.insert(result, string.sub(str, from, d_from - 1))
  81.   from = d_to + 1
  82.   d_from, d_to = string.find(str, delimiter, from)
  83.  end
  84.  table.insert(result, string.sub(str, from))
  85.  return result
  86. end
  87.  
  88. function formatnum(number)
  89.  local result = number
  90.  while true do
  91.   result, s = string.gsub(result, "^(-?%d+)(%d%d%d)", '%1,%2')
  92.   if s == 0 then break
  93.   end
  94.  end
  95.  return result
  96. end
  97.  
  98. function convertEnergy(number)
  99.  energyRatio = 1
  100.  if batteryEnergyType == "J" then
  101.   if displayedEnergyType == "RF" then energyRatio = 0.4 end
  102.   if displayedEnergyType == "EU" then energyRatio = 0.1 end
  103.  elseif batteryEnergyType == "RF" then
  104.   if displayedEnergyType == "J" then energyRatio = 2.5 end
  105.   if displayedEnergyType == "EU" then energyRatio = 0.25 end
  106.  elseif batteryEnergyType == "EU" then
  107.   if displayedEnergyType == "RF" then energyRatio = 4 end
  108.   if displayedEnergyType == "J" then energyRatio = 10 end
  109.  else error("Unsupported energy type: \""..batteryEnergyType.."\" for the battery.") end
  110.  return string.format("%.2f",math.floor(tonumber(number) * energyRatio))
  111. end
  112.  
  113. function getEnergyPerTick(energy)
  114.  local input = {}
  115.  if prevenergy[1] == nil then
  116.   prevenergy[1] = energy
  117.  end
  118.  local limit = math.min(normalizeAmount, #prevenergy)
  119.  input[1] = math.floor((energy - prevenergy[1]) / (updateDelay))
  120.  for i=2,limit do
  121.   input[i] = math.floor((prevenergy[i-1] - prevenergy[i]) / (updateDelay))
  122.  end
  123.  for i=0,limit-1 do
  124.   local ii = 1+limit-i
  125.   prevenergy[ii] = prevenergy[ii-1]
  126.  end
  127.  prevenergy[1] = energy
  128.  calcenergy = input[1]
  129.  for i=2,#input do
  130.   calcenergy = calcenergy + input[i]
  131.  end
  132.  return math.floor(input[1]), math.floor((calcenergy) / limit)
  133. end
  134.  
  135. function startup()
  136.  modem = peripheral.find("modem")
  137.  mon = peripheral.find("monitor")
  138.  if mon == nil then
  139.    mon = term
  140.  end
  141.  monX, monY = mon.getSize()
  142.  prevDayEnergy = -1
  143.  currentDayEnergy = -1
  144.  ticksRun = 0
  145.  prevenergy = {}
  146.  running = true
  147.  modem.open(modemChannel)
  148.  print("Listening for transmission...")
  149. end
  150.  
  151. function shutdown()
  152.  modem.close(modemChannel)
  153.  if term.isColor() == true then
  154.   term.setBackgroundColor(colors.black)
  155.   term.setTextColor(colors.white)
  156.  end
  157.  term.clear()
  158.  term.setCursorPos(1,1)
  159.  term.write("Terminated Energy Monitor.")
  160.  term.setCursorPos(1,2)
  161.  term.write("Come back soon!")
  162.  term.setCursorPos(1,3)
  163. end
  164.  
  165. function pullEvent()
  166.  event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEventRaw()
  167.  if event == "modem_message" then
  168.   if message ~= nil then
  169.    updateMonitor(split(message,";"))
  170.   end
  171.  elseif event == "terminate" then
  172.   running = false
  173.  end
  174. end
  175.  
  176. function updateMonitor(tbl)
  177.  if tbl ~= nil then
  178.   if mon ~= term then
  179.    term.clear()
  180.    term.setCursorPos(1,1)
  181.    print("Recieving info...")
  182.    if debug == 1 then
  183.     print("Debug:")
  184.        for i=1,#prevenergy do
  185.         print("prevenergy"..i.." = "..prevenergy[i])
  186.        end
  187.    end
  188.   end
  189.   local energy = 0
  190.   local max = 0
  191.   for x,y in pairs(tbl) do
  192.    if x == 1 then energy = y
  193.    elseif x == 2 then max = y
  194.    end
  195.   end
  196.   if energy ~= 0 and prevDayEnergy == -1 then
  197.    prevDayEnergy = energy
  198.    currentDayEnergy = energy
  199.   end
  200.   ticksRun = ticksRun + updateDelay
  201.   if (ticksRun >= 24000) then
  202.    ticksRun = 0
  203.    prevDayEnergy = currentDayEnergy
  204.    currentDayEnergy = energy
  205.   end
  206.   local percent = math.floor((energy/max)*100)
  207.   local dailyEnergy = currentDayEnergy - prevDayEnergy
  208.   input, inputNormalized = getEnergyPerTick(energy)
  209.   energy = convertEnergy(energy)
  210.   print("energy" .. type(energy) .. energy)
  211.   max = convertEnergy(max)
  212.   print("max" .. type(max) .. max)
  213.   input = convertEnergy(input)
  214.   inputNormalized = convertEnergy(inputNormalized)
  215.   dailyEnergy = convertEnergy(dailyEnergy)
  216.   clear()
  217.   drawTextLeft(2, "Current Power:", txtColor, bgColor)
  218.   drawTextRight(2, formatnum(energy).." "..displayedEnergyType, txtColor, bgColor)
  219.   drawTextLeft(4, "Max Power:", txtColor, bgColor)
  220.   drawTextRight(4, formatnum(max).." "..displayedEnergyType, txtColor, bgColor)
  221.   drawTextLeft(6,"Input/Output: ", txtColor, bgColor)
  222.   drawTextRight(6, formatnum(input).." "..displayedEnergyType.."/t", txtColor, bgColor)
  223.   drawTextLeft(8,"Input/Output (Normalized): ", txtColor, bgColor)
  224.   drawTextRight(8, formatnum(inputNormalized).." "..displayedEnergyType.."/t", txtColor, bgColor)
  225.   drawTextLeft(10, "Loss/Gain from Prev. Day:", txtColor, bgColor)
  226.   drawTextRight(10, formatnum(dailyEnergy).." "..displayedEnergyType, txtColor, bgColor)
  227.   drawProg(2, 12, "Stored: ", monX-2, 3, percent, 100, pgToColor, pgFromColor)
  228.  end
  229. end
  230.  
  231. modem, mon, monX, monY, prevenergy, running = nil
  232.  
  233. startup()
  234. while running do
  235.  pullEvent()
  236. end
  237. shutdown()
Advertisement
Add Comment
Please, Sign In to add comment