Skip to content

Instantly share code, notes, and snippets.

@maxihafer
Last active January 9, 2023 20:50
Show Gist options
  • Save maxihafer/61e9bd19f259edb7917d8aee21b830db to your computer and use it in GitHub Desktop.
Save maxihafer/61e9bd19f259edb7917d8aee21b830db to your computer and use it in GitHub Desktop.
CC-Controllers
local reactor = peripheral.find("fissionReactorLogicAdapter")
local turbine = peripheral.find("turbineValve")
local target_energy_percent = 0.9
data = {reactor = {}, turbine = {}}
data.print = function (self)
term.clear()
print("Temperature: "..data.reactor.temp.."K\n")
print("Burn Rate: "..data.reactor.cur_burn.."mb/t\n")
print("Waste: "..data.reactor.waste_percent.."\n")
print("Energy: "..data.turbine.energy_percent.."\n")
print("Steam: "..data.turbine.steam_percent.."\n")
print("Coolant: "..data.reactor.coolant_percent.."\n")
print("Damage: "..data.reactor.damage.."\n")
end
local function updateBurnRate()
if (data.turbine.energy_percent > target_energy_percent) then
reactor.setBurnRate(0)
else
reactor.setBurnRate(10)
end
end
local function shouldScram()
if (data.reactor.damage > 0) then
return true
elseif (data.reactor.coolant_percent < 0.9) then
return true
elseif (data.reactor.temp > 1000) then
return true
elseif (data.reactor.waste_percent > 0.1) then
return true
elseif (data.reactor.fuel_percent) < 0.5 then
return true
elseif (data.turbine.steam_percent) > 0.9 then
return true
else
return false
end
end
local function updateData()
data.reactor.damage = reactor.getDamagePercent()
data.reactor.max_burn = reactor.getMaxBurnRate()
data.reactor.coolant_percent = reactor.getCoolantFilledPercentage()
data.reactor.cur_burn = reactor.getActualBurnRate()
data.reactor.temp = reactor.getTemperature()
data.reactor.waste_percent = reactor.getWasteFilledPercentage()
data.reactor.fuel_percent = reactor.getFuelFilledPercentage()
data.turbine.steam_percent = turbine.getSteamFilledPercentage()
data.turbine.energy_percent = turbine.getEnergyFilledPercentage()
end
while (true) do
if not reactor or not turbine then
print("Reactor or turbine not found exiting")
return
end
updateData()
if shouldScram() and reactor.getStatus() then
reactor.scram()
end
updateBurnRate()
data:print()
os.sleep(5)
end
local dTank = peripheral.wrap("dynamicValve_0")
local tTank = peripheral.wrap("dynamicValve_1")
local monitor = peripheral.wrap("monitor_2")
local function valFmt(num)
rounded = math.floor(num * 10^2 + 0.5) / 10^2
if num <= 0 then
return "-"..rounded.."mb/t",colors.red
else
return "+"..rounded.."mb/t",colors.green
end
end
local function writeFmtRight(y,val)
message, color = valFmt(val)
oldX, oldY = monitor.getCursorPos()
oldColor = monitor.getTextColor()
width, height = monitor.getSize()
startX = width - message:len() - 1
monitor.setTextColor(color)
monitor.setCursorPos(startX,y)
monitor.write(message)
monitor.setTextColor(oldColor)
monitor.setCursorPos(oldX,oldY)
end
local function draw(tVal, dVal)
monitor.setTextScale(0.5)
monitor.clear()
local width, height = monitor.getSize()
monitor.setBackgroundColor(colors.black)
monitor.setTextColor(colors.white)
monitor.setCursorPos(2, height/2)
monitor.write("Tritium:")
writeFmtRight(height/2,tVal)
monitor.setCursorPos(2, height/2+1)
monitor.write("Deuterium:")
writeFmtRight(height/2+1, dVal)
end
local time = (os.time() * 1000 + 18000)%24000
local tLevel = tTank.getStored().amount
local dLevel = dTank.getStored().amount
while true do
local newTime = (os.time() * 1000 + 18000)%24000
local newTLevel = tTank.getStored().amount
local newDLevel = dTank.getStored().amount
local dT = newTime - time
local dTL = newTLevel - tLevel
local dDL = newDLevel - dLevel
draw(dTL/dT, dDL/dT)
time = newTime
tLevel = newTLevel
dLevel = newDLevel
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment