Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Author: Jack Dell
- -- Website: jackdell.ca
- -- Date: 2025-01-27
- -- Config variables
- local LOW_THRESHOLD_PERCENT = 0.3
- local HIGH_THRESHOLD_PERCENT = 0.85
- local LISTEN_CHANNEL = 1
- local RELAY_CHANNEL = 2
- local ACTION_RATE = 1
- -- When false, auto-manage the reactor otherwise run in manual mode (on/off)
- local system_override = false
- -- Connect to the reactor on the back, and modem on the top
- local reactor = peripheral.wrap('back') or error('Reactor not found on the back of computer')
- local modem = peripheral.wrap('top') or error('Modem not found on the top of computer')
- -- Bundles information about the reactor into a table and returns it
- function getReactorStats()
- local stats = {
- dynamic = not system_override,
- active = reactor.active(),
- temperature = reactor.fuelTemperature(),
- rf_output = reactor.battery().producedLastTick(),
- rf_stored = reactor.battery().stored(),
- rf_capacity = reactor.battery().capacity(),
- fuel_rate = reactor.fuelTank().burnedLastTick(),
- fuel_ammount = reactor.fuelTank().fuel(),
- fuel_capacity = reactor.fuelTank().capacity()
- }
- return stats
- end
- -- Handles the control of the reactor, turning it on if its below the rf stored threshold, off otherwise
- function manageReactor()
- while true do
- if system_override then
- os.sleep(ACTION_RATE)
- else
- local s = getReactorStats()
- if s.rf_stored < s.rf_capacity * LOW_THRESHOLD_PERCENT and not s.active then
- print('RF below threshold, enabling reactor')
- reactor.setActive(true)
- elseif s.rf_stored > s.rf_capacity * HIGH_THRESHOLD_PERCENT and s.active then
- print('RF threshold met, disabling reaction')
- reactor.setActive(false)
- end
- end
- os.sleep(ACTION_RATE)
- end
- end
- -- Gets the reactors information and relays it using a modem
- function relayStats()
- while true do
- local stats = getReactorStats()
- modem.transmit(RELAY_CHANNEL, LISTEN_CHANNEL, stats)
- os.sleep(ACTION_RATE)
- end
- end
- -- Updates the system_override variable and sets the reactor state
- function setSystemOverride(override, state)
- system_override = override
- reactor.setActive(state)
- print(string.format("System override: %s, Reactor active: %s", tostring(override), tostring(state)))
- end
- -- Handles processing incoming messages from the modem
- function handleMessage()
- while true do
- local _, _, recvChannel, _, message = os.pullEvent("modem_message")
- if recvChannel == LISTEN_CHANNEL
- and type(message) == 'table'
- and message.override ~= nil
- and message.state ~= nil
- then
- setSystemOverride(message.override, message.state)
- end
- end
- end
- -- Initalizer
- function init()
- modem.open(LISTEN_CHANNEL)
- parallel.waitForAll(handleMessage, relayStats, manageReactor)
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement