Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Default settings, do not change
- local options = {
- -- Unique identifier for this matrix on rednet, required for rednet functionality
- rednet_identifier = '',
- -- Energy type being displayed (J, FE)
- energy_type = 'FE',
- -- Update frequency, in seconds
- update_frequency = 1,
- -- Text scale on the monitor
- text_scale = 1,
- -- Output debug data to the computer's internal display
- debug = true,
- }
- --------------------------------------------------
- --- Internal variables, DO NOT CHANGE
- --------------------------------------------------
- --- This will be used as the installer source (Pastebin)
- local INSTALLER_ID = 'LMdUZY4Z'
- --- Supported energy suffixes
- local energy_suffixes = { 'k', 'M', 'G', 'T', 'P' }
- --- Supported time periods when converting seconds
- local time_periods = {
- { 'weeks', 604800 },
- { 'days', 86400 },
- { 'hours', 3600 },
- { 'minutes', 60 },
- { 'seconds', 1 },
- }
- --- This is our Induction Matrix, we'll auto-detect it later
- local cube = nil
- --- This is our Monitor, we'll auto-detect it later
- local monitor = nil
- --------------------------------------------------
- --- Helper functions
- --------------------------------------------------
- --- Holds the current buffer of data being printed
- local machine_term = term.current()
- local print_buffer = {}
- --- Writes data to the output monitor buffer
- function print_r (text)
- table.insert(print_buffer, text)
- end
- --- Writes formatted data to the output monitor buffer
- function print_f (format, ...)
- print_r(string.format(format, ...))
- end
- --- Writes the buffer into the output monitor
- function print_flush ()
- if monitor then
- -- Redirects writes to monitor (if any)
- if monitor then
- term.redirect(monitor)
- end
- -- Clears terminal
- term.clear()
- term.setCursorPos(1, 1)
- -- Writes new data
- print(table.concat(print_buffer or {}, '\n'))
- -- Redirects writes back to computer (if using monitor)
- if monitor then
- term.redirect(machine_term)
- end
- end
- -- Clears buffer
- print_buffer = {}
- end
- --- Writes debug info to the machine
- function debug (...)
- if options.debug then
- print(...)
- end
- end
- --- Rounds a number with N decimals
- function round_decimal (number, decimals)
- local multiplier = math.pow(10, decimals or 0)
- return math.floor(number * multiplier) / multiplier
- end
- --- Rounds a percentage (0..1) to a number of decimals
- function round_percentage (number, decimals)
- return ('%s%%'):format(round_decimal(100 * number, decimals or 1))
- end
- function print_cube_info (cube_info)
- print_r('Energy Production Control')
- print_r('------------------')
- print_r('')
- print_f('Power : %s', energy_string(cube_info.energy_stored))
- print_f('Limit : %s', energy_string(cube_info.energy_capacity))
- print_f('Charge: %s', round_percentage(cube_info.energy_percentage))
- print_r('')
- print_f('Input : %s/t', energy_string(cube_info.io_input))
- print_f('Output: %s/t', energy_string(cube_info.io_output))
- print_f('Max IO: %s/t', energy_string(cube_info.io_capacity))
- print_f('Stat: %s/t', cube_info.gen_stats)
- print_r('')
- end
- -- Detects peripherals
- monitor = peripheral.find('monitor')
- if monitor then
- debug('Monitor detected, enabling output!')
- monitor.setTextScale(options.text_scale)
- else
- debug('No monitor detected, entering headless mode!')
- end
- --------------------------------------------------
- --- Main runtime
- --------------------------------------------------
- debug('Entering main loop...')
- --- This will be updated after every energy collection, it is used to calculate how much power is actually being added/removed from the system
- local energy_stored_previous = nil
- while true do
- local status, err = pcall(function ()
- -- Attempts to auto-detect missing Cube
- if not Cube then
- Cube = peripheral.find('mekanism:ultimate_energy_cube')
- -- Checks if it worked
- if not Cube then
- error('Cube not connected!')
- end
- end
- --- Save status
- local charge_status = 1
- --- This is our main information
- local cube_info = {
- energy_stored = cube.getEnergy(),
- energy_capacity = cube.getMaxEnergy(),
- energy_percentage = cube.getEnergyFilledPercentage(),
- io_input = cube.getLastInput(),
- io_output = cube.getLastOutput(),
- io_capacity = cube.getTransferCap(),
- gen_stats = gen_stats_input
- }
- if charge_status == 1 then
- if energy_capacity < 50 then
- redstone.setOutput("right", false)
- gen_stats_input = "Charging ..."
- charge_status = 2
- end
- else
- if energy_capacity > 90 then
- redstone.setOutput("right", true)
- gen_stats_input = "Idle"
- charge_status = 1
- end
- end
- if not energy_stored_previous then
- energy_stored_previous = cube_info.energy_stored
- end
- -- Calculates power changes and adds them to our information
- cube_info.change_interval = options.update_frequency
- cube_info.change_amount = cube_info.energy_stored - energy_stored_previous
- cube_info.change_amount_per_second = cube_info.change_amount / options.update_frequency
- -- General stats
- cube_info.is_charging = cube_info.change_amount > 0
- cube_info.is_discharging = cube_info.change_amount < 0
- -- Sets the new "previous" value
- energy_stored_previous = cube_info.energy_stored
- -- Prints the matrix information
- print_cube_infoinfo(cube_info)
- end)
- -- Checks for errors (might be disconnected)
- if not status then
- -- Clears buffer first
- print_buffer = {}
- -- Shows error message
- print_r('Error reading data')
- print_r('Check connections.')
- print_r('------------------')
- print_r(err)
- end
- -- Outputs text to screen
- print_flush()
- -- Waits for next cycle
- os.sleep(options.update_frequency)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement