Skip to content

Instantly share code, notes, and snippets.

@Leo1003
Last active February 8, 2020 14:42
Show Gist options
  • Save Leo1003/fefc257e622e5e6a3a1572d44a796afe to your computer and use it in GitHub Desktop.
Save Leo1003/fefc257e622e5e6a3a1572d44a796afe to your computer and use it in GitHub Desktop.
NuclearCraft Fission Reactor controller for OpenComputers
local component = require("component")
local event = require("event")
local term = require("term")
local thread = require("thread")
local function eprint(s)
io.stderr:write(s.."\n")
end
local DETECT_TIME = 1
local REFRESH_TIME = 0.5
local ENERGY_CRIT_RATIO = 0.5
local HEAT_CRIT_RATIO = 0.25
local stopping = false
local function handler()
event.pull("interrupted")
eprint("Stopping Controller...")
stopping = true
end
local function control(reactor)
local max_energy = reactor.getMaxEnergyStored()
local max_heat = reactor.getMaxHeatLevel()
while not stopping do
local energy = reactor.getEnergyStored()
local heat = reactor.getHeatLevel()
local energy_ratio = energy / max_energy
local heat_ratio = heat / max_heat
if heat_ratio < HEAT_CRIT_RATIO and energy_ratio < ENERGY_CRIT_RATIO then
if not reactor.isProcessing() then
reactor.activate()
end
else
if reactor.isProcessing() then
reactor.deactivate()
end
end
os.sleep(DETECT_TIME)
end
reactor.deactivate()
end
local function reactor_info(r)
print(string.format("Size: %d x %d x %d", r.getLengthX(), r.getLengthY(), r.getLengthZ()))
print(string.format("Cells: %d", r.getNumberOfCells()))
print(string.format("EnergyCapacity: %d", r.getMaxEnergyStored()))
print(string.format("HeatCapacity: %d", r.getMaxHeatLevel()))
print(string.format("Efficiency: %f", r.getEfficiency()))
print(string.format("HeatMultiplier: %f", r.getHeatMultiplier()))
end
local function write_color(s, fg, bg)
local gpu = component.gpu
if fg ~= nil then
gpu.setForeground(fg)
end
if bg ~= nil then
gpu.setBackground(bg)
end
term.write(s, false)
end
local h = thread.create(handler)
threads = {}
managed_reactors = {}
for address, _ in component.list("nc_fission_reactor") do
eprint(string.format("Found reactor %s", address))
local reactor = component.proxy(address)
if reactor.isComplete() then
reactor_info(reactor)
table.insert(managed_reactors, reactor)
local t = thread.create(control, reactor)
table.insert(threads, t)
else
eprint("Reactor is incomplete!")
end
end
-- Print Current Status
local reactor_num = #managed_reactors
while not stopping do
for _, r in pairs(managed_reactors) do
local energy = r.getEnergyStored()
local max_energy = r.getMaxEnergyStored()
local heat = r.getHeatLevel()
local max_heat = r.getMaxHeatLevel()
local running = r.isProcessing()
term.clearLine()
write_color(string.format("%.8s: ", r.address), 0xFFFF00)
if running then
write_color("ON ", 0x00FF00)
else
if r.getFissionFuelTime() == 0 then
write_color("Fuel", 0xFF7F00)
else
write_color("OFF ", 0xFF0000)
end
end
write_color(" -- ", 0xFFFFFF)
write_color(string.format("Energy: %d/%d ", energy, max_energy), 0x66DBFF)
write_color(string.format("Heat: %d/%d", heat, max_heat), 0xFF4900)
write_color("\n", 0xFFFFFF)
end
os.sleep(REFRESH_TIME)
local _, cur_y = term.getCursor()
term.setCursor(1, cur_y - reactor_num)
end
component.gpu.setForeground(0xFFFFFF)
component.gpu.setBackground(0x000000)
local _, cur_y = term.getCursor()
term.setCursor(1, cur_y + reactor_num)
thread.waitForAll(threads)
eprint("Controller Exit")
os.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment