TyanColte

Powah Monitor (ComputerCraft)

Jan 22nd, 2025 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. -- This program monitors the power level of a Powah power cell
  2. -- connected via ComputerCraft networking cables and modems.
  3.  
  4. -- **Requires CC: Tweaked 1.113.1 and the Powah mod.**
  5. -- **Assumes a modem is attached to the BACK of the computer and connected to another modem
  6. -- attached to the Powah power cell named "powah_cell_modem".**
  7.  
  8. -- Set the remote modem's peripheral name
  9. local remotePeripheralName = "powah_cell_modem"
  10.  
  11. -- Set the modem channel (change if needed)
  12. local modemChannel = 100
  13.  
  14. -- Function to check and display the power level
  15. local function checkPower()
  16.   -- Get the current and max energy levels from the remote modem
  17.   local currentEnergy = peripheral.call(remotePeripheralName, "getEnergyStored")
  18.   local maxEnergy = peripheral.call(remotePeripheralName, "getMaxEnergyStored")
  19.  
  20.   -- Calculate the percentage of energy stored
  21.   local percentage = (currentEnergy / maxEnergy) * 100
  22.  
  23.   -- Clear the screen and display the information
  24.   term.clear()
  25.   term.setCursorPos(1, 1)
  26.   print("Powah Power Cell Monitor")
  27.   print("-----------------------")
  28.   print("Current Energy:", currentEnergy)
  29.   print("Max Energy:", maxEnergy)
  30.   print("Percentage:", string.format("%.2f%%", percentage))
  31. end
  32.  
  33. -- Function to set up the modem connection
  34. local function setupModem()
  35.   -- Get the modem peripheral from the BACK side
  36.   local modem = peripheral.wrap("back")
  37.  
  38.   -- Open the modem on the specified channel
  39.   modem.open(modemChannel)
  40.  
  41.   -- Wait for a connection to the remote modem (simplified)
  42.   while true do
  43.     if modem.isOpen(modemChannel) then
  44.       print("Modem is open on channel", modemChannel)
  45.       break -- Exit the loop once the modem is open
  46.     else
  47.       print("Modem not open on channel", modemChannel, ". Waiting...")
  48.     end
  49.     sleep(1)
  50.   end
  51. end
  52.  
  53. -- Setup the modem connection
  54. setupModem()
  55.  
  56. -- Main loop to continuously monitor the power level
  57. while true do
  58.   checkPower()
  59.   sleep(1) -- Check every second
  60. end
Advertisement
Add Comment
Please, Sign In to add comment