Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local thread = require("thread")
- local server_channel = 23
- local client_channel = 43
- local function wait_for_component(name)
- if component.isAvailable(name) then
- return component.getPrimary(name)
- end
- while true do
- local event, component_name = event.pull(math.huge, "component_available")
- if component_name == name then
- return component.getPrimary(name)
- end
- end
- end
- local reactor = wait_for_component("nc_fusion_reactor")
- local modem = wait_for_component("modem")
- modem.open(server_channel)
- modem.open(client_channel)
- local reactor_commands = {
- ["is_processing"] = function()
- return "0"
- end;
- ["disable"] = function()
- reactor.deactivate()
- end;
- ["enable"] = function()
- reactor.activate()
- end;
- }
- local commands = {
- ["reactor"] = function(subcommand, ...)
- local command = reactor_commands[subcommand]
- if command then
- return command(...)
- else
- print("No reactor subcommand '" .. subcommand .. "'")
- end
- end;
- }
- local last_step
- local function process_message(event, local_address, remote_address, port, distance, message, step)
- if step == last_step then return end
- last_step = step
- if port ~= server_channel then return end
- local args = {}
- for i in message:gmatch("[^:]+") do
- table.insert(args, i)
- end
- local command = args[1]
- table.remove(args, 1)
- local handler = commands[command]
- if handler then
- local response = handler(table.unpack(args))
- if response then
- modem.broadcast(client_channel, "response:" .. response)
- print("Broadcasted response")
- end
- else
- print("No command '" .. command .. "'")
- end
- end
- local hit_threshold = false
- local function check()
- local efficiency = reactor.getEfficiency()
- modem.broadcast(client_channel, "event:reactor:efficiency:" .. tostring(efficiency))
- modem.broadcast(client_channel, "event:reactor:energy_change:" .. tostring(reactor.getEnergyChange()))
- modem.broadcast(client_channel, "event:reactor:process_power:" .. tostring(reactor.getReactorProcessPower()))
- if reactor.isProcessing() then
- if efficiency > 95 then
- print("Deactivating")
- reactor.deactivate()
- hit_threshold = true
- end
- else
- if hit_threshold then
- if efficiency < 90 then
- print("Activating")
- reactor.activate()
- hit_threshold = false
- end
- end
- end
- end
- function start()
- stop()
- messsage_listener = event.listen("modem_message", process_message)
- main_thread = thread.create(function()
- while true do
- if reactor.isHotEnough() then
- print("Starting")
- reactor.activate()
- break
- end
- os.sleep(1)
- end
- while true do
- os.sleep(1)
- check()
- end
- end)
- end
- function stop()
- if messsage_listener then
- event.cancel(messsage_listener)
- messsage_listener = nil
- end
- if main_thread then
- main_thread:kill()
- main_thread = nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement