Created
October 17, 2016 18:20
-
-
Save Anaerin/0da315dc71a626d4637a673817a8056e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Globals - Change these to suit your turbines and consumption needs. | |
RPMTarget = 1700 | |
StoredShutoff = 90 -- Capacitor amount, in % | |
StoredRestart = 10 -- Capacitor amount, in % | |
-- Set up variables | |
turbines = {} | |
local pers = peripheral.getNames() | |
local screenWidth | |
local screenHeight | |
calcHeight = 0 | |
calcWidth = 0 | |
textScale = 1 | |
-- Pad strings to width characters wide. | |
function padRight(toPad, width) | |
local output = string.rep(' ',width) | |
output = output .. toPad | |
return string.sub(output, 0-width) | |
end | |
-- Function to insert thousands commas. | |
function comma_value(amount) | |
local formatted = amount | |
while true do | |
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') | |
if (k==0) then | |
break | |
end | |
end | |
return formatted | |
end | |
-- Write temperatures in colour! | |
function writeTemperature(value) | |
if value > 1000 then | |
hdr.setTextColor(colors.red) | |
elseif value > 500 then | |
hdr.setTextColor(colors.yellow) | |
elseif value > 100 then | |
hdr.setTextColor(colors.green) | |
else | |
hdr.setTextColor(colors.lightGray) | |
end | |
hdr.write(comma_value(value).."C") | |
end | |
-- Write speed, with colour coding (ish) | |
function writeSpeed(value) | |
if value > 2000 then | |
tbn.setTextColor(colors.red) | |
elseif value > 1800 then | |
tbn.setTextColor(colors.yellow) | |
elseif value > 1700 then | |
tbn.setTextColor(colors.green) | |
elseif value > 1000 then | |
tbn.setTextColor(colors.lightGray) | |
elseif value > 800 then | |
tbn.setTextColor(colors.green) | |
elseif value > 100 then | |
tbn.setTextColor(colors.lightGray) | |
else | |
tbn.setTextColor(colors.yellow) | |
end | |
tbn.write(padRight(comma_value(math.floor(value)),5).."RPM") | |
end | |
-- Write information line about turbine. | |
function writeTurbineInfo(turbine) | |
if not turbine.getInductorEngaged() then | |
tbn.setBackgroundColor(colors.gray) | |
end | |
writeSpeed(turbine.getRotorSpeed()) | |
tbn.setBackgroundColor(colors.black) | |
tbn.setTextColor(colors.lightGray) | |
tbn.write(", ") | |
tbn.setTextColor(colors.yellow) | |
tbn.write(padRight(comma_value(string.format("%u", turbine.getEnergyProducedLastTick())),7)) | |
tbn.setTextColor(colors.lightGray) | |
tbn.write("RFt, ") | |
tbn.write(padRight(comma_value(math.floor(turbine.getEnergyStored())),9).."RF Stored") | |
end | |
function idleTurbine(turbine) | |
turbine.setInductorEngaged(false) | |
-- Disengage the coils | |
turbine.setFluidFlowRateMax(4) | |
-- 4mb/t of steam will keep a turbine idling if the coils aren't engaged. | |
end | |
function powerTurbine(turbine) | |
turbine.setInductorEngaged(true) | |
turbine.setFluidFlowRateMax(turbine.getFluidFlowRateMaxMax()) | |
end | |
function idleReactor(reactor) | |
if reactor.getFuelTemperature() > 150 then | |
-- Fuel is hotter than 150 degrees, more than enough to generate steam still. | |
-- Disable the reactor. | |
reactor.setActive(false) | |
else | |
if not reactor.getActive() then | |
-- Reactor wasn't active, probably cooling down. But now it's under temperature | |
-- So time to turn it back on to idle. | |
reactor.setActive(true) | |
end | |
reactor.setAllControlRodLevels(100) | |
for i=0,reactor.getNumberOfControlRods()-1 do | |
if reactor.getControlRodName(i) == "Idle" then | |
-- If this rod is called "Idle", the reactor maker wanted it to be a | |
-- little on when idling. set it to 95% inserted. | |
reactor.setControlRodLevel(i, 97) | |
end | |
end | |
end | |
end | |
function powerReactor(reactor) | |
if not reactor.getActive() then | |
reactor.setActive(true) | |
end | |
end | |
function setupScreenScaling() | |
-- Set up monitor | |
local screenWidth | |
local screenHeight | |
local widthNeeded = 52 | |
local heightNeeded = #turbines + 7 | |
local newScale = 0.5 | |
monitor.setTextScale(1) | |
screenWidth, screenHeight = monitor.getSize() | |
calcHeight = screenHeight | |
calcWidth = screenWidth | |
if calcHeight > heightNeeded then | |
while calcHeight > heightNeeded do | |
-- Scale up by 0.5 until height no longer fits... | |
newScale = newScale + 0.5 | |
if newScale > 5 then | |
newScale = 5 | |
print("Screen so tall we can't scale up to match it. Setting to largest value (5)") | |
break | |
end | |
calcHeight = math.floor(screenHeight / newScale) | |
calcWidth = math.floor(screenWidth / newScale) | |
print("Sizing to "..newScale..", "..calcWidth.."x"..calcHeight) | |
end | |
-- Then take it back a step. | |
newScale = newScale - 0.5 | |
if newScale < 0.5 then | |
newScale = 0.5 | |
end | |
calcHeight = math.floor(screenHeight / newScale) | |
calcWidth = math.floor(screenWidth / newScale) | |
else | |
newScale = 0.5 | |
end | |
print("Height determined scaling factor to be "..newScale) | |
if calcWidth < widthNeeded then | |
print("need more width, though.") | |
while calcWidth < widthNeeded do | |
-- Then scale down some more until width fits. | |
newScale = newScale - 0.5 | |
if newScale < 0.5 then | |
newScale = 0.5 | |
print("Screen too narrow to contain display. Scaling set to smallest possible value (0.5), aborting auto-size") | |
break | |
end | |
calcHeight = math.floor(screenHeight / newScale) | |
calcWidth = math.floor(screenWidth / newScale) | |
print("Sizing to "..newScale..", "..calcWidth.."x"..calcHeight) | |
end | |
else | |
print("Which seems to be wide enough too.") | |
end | |
-- Everything should fit now. | |
print("Monitor scaling factor determined to be "..newScale..", "..calcWidth.."x"..calcHeight) | |
monitor.setTextScale(newScale) | |
monitor.clear() | |
if hdr then | |
hdr.reposition(1, 1, calcWidth, 3) | |
tbn.reposition(1, 5, calcWidth, #turbines) | |
tot.reposition(1, calcHeight - 1, calcWidth, 2) | |
else | |
hdr = window.create(monitor, 1, 1, calcWidth, 3) | |
tbn = window.create(monitor, 1, 5, calcWidth, #turbines) | |
tot = window.create(monitor, 1, calcHeight - 1, calcWidth, 2) | |
end | |
end | |
-- Find attached peripherals | |
for i=1,#pers do | |
if peripheral.getType(pers[i]) == "BigReactors-Turbine" then | |
table.insert(turbines, peripheral.wrap(pers[i])) | |
end | |
end | |
monitor = peripheral.find("monitor") | |
reactor = peripheral.find("BigReactors-Reactor") | |
capacitor = peripheral.find("tile_blockcapacitorbank_name") | |
capacitorFullIdling = false | |
setupScreenScaling() | |
local trigger = os.startTimer(1) | |
-- Main loop. | |
while true do | |
-- Display reactor information | |
hdr.clear() | |
hdr.setCursorPos(1,1) | |
attenAmount = 0 | |
powerStoredPercent = math.floor((capacitor.getEnergyStored() / capacitor.getMaxEnergyStored()) * 100) | |
if reactor.getConnected() then | |
if powerStoredPercent < StoredRestart then | |
powerReactor(reactor) | |
capacitorFullIdling = false | |
end | |
if powerStoredPercent > StoredShutoff then | |
idleReactor(reactor) | |
capacitorFullIdling = true | |
end | |
if reactor.getActive() then | |
hdr.setTextColor(colors.green) | |
hdr.write("Online") | |
else | |
hdr.setTextColor(colors.lightGray) | |
hdr.write("Offline") | |
end | |
hdr.setTextColor(colors.lightGray) | |
hdr.write(", Producing ") | |
hdr.setTextColor(colors.yellow) | |
hdr.write(comma_value(reactor.getHotFluidProducedLastTick()) .. "mB") | |
hdr.setTextColor(colors.lightGray) | |
if reactor.getHotFluidType() then | |
hdr.write(" of " .. reactor.getHotFluidType()) | |
else | |
for i=1,#turbines do | |
if (turbines[i].getConnected() and turbines[i].getInputType()) then | |
hdr.write(" of " .. turbines[i].getInputType()) | |
break | |
end | |
end | |
end | |
hdr.write(" per tick") | |
hdr.setCursorPos(1,2) | |
hdr.write("Using " .. (math.floor(reactor.getFuelConsumedLastTick()*1000)/1000) .. "mB of ") | |
hdr.write("fuel at " .. math.floor(reactor.getFuelReactivity()) .. "% reactivity") | |
hdr.setCursorPos(1,3) | |
hdr.write("Fuel: ") | |
writeTemperature(math.floor(reactor.getFuelTemperature())) | |
hdr.setTextColor(colors.lightGray) | |
hdr.write(", Casing: ") | |
writeTemperature(math.floor(reactor.getCasingTemperature())) | |
else | |
hdr.setTextColor(colors.red) | |
hdr.write("Unable to connect to Reactor.") | |
end | |
tbn.clear() | |
local powerTotal = 0 | |
local powerStored = 0 | |
local turbinesActive = 0 | |
local turbinesIdle = 0 | |
for i=1,#turbines do | |
tbn.setCursorPos(1,i) | |
tbn.setTextColor(colors.lightGray) | |
if turbines[i].getConnected() then | |
if capacitorFullIdling then | |
idleTurbine(turbines[i]) | |
else | |
powerTurbine(turbines[i]) | |
end | |
if turbines[i].getActive() then | |
tbn.setTextColor(colors.green) | |
else | |
tbn.setTextColor(colors.yellow) | |
end | |
tbn.write("Turbine "..i) | |
tbn.setTextColor(colors.lightGray) | |
tbn.write(": ") | |
powerTotal = powerTotal + turbines[i].getEnergyProducedLastTick() | |
powerStored = powerStored + turbines[i].getEnergyStored() | |
writeTurbineInfo(turbines[i]) | |
else | |
tbn.setTextColor(colors.red) | |
tbn.write("Turbine "..i.." INVALID") | |
end | |
end | |
-- Slow the reactor, if necessary. | |
tot.clear() | |
tot.setCursorPos(1,1) | |
tot.setTextColor(colors.lightGray) | |
tot.write(turbinesActive - turbinesIdle.."/"..#turbines.." Active.") | |
tot.write(" RF/Ingot: "..comma_value(math.floor((powerTotal/reactor.getFuelConsumedLastTick())*1000))) | |
tot.setCursorPos(1,2) | |
tot.write(turbinesIdle.."/"..#turbines.." Idle. ") | |
tot.write("Total: ") | |
tot.setTextColor(colors.yellow) | |
tot.write(padRight(comma_value(math.floor(powerTotal)),9)) | |
tot.setTextColor(colors.lightGray) | |
tot.write("RFt, Stored: ") | |
tot.setTextColor(colors.yellow) | |
tot.write(padRight(comma_value(math.floor(powerStored)),10)) | |
tot.setTextColor(colors.lightGray) | |
tot.write("RF") | |
tot.write(" Capacitor "..powerStoredPercent.."%") | |
local event = os.pullEvent() | |
if event == "timer" then | |
trigger = os.startTimer(1) | |
elseif event == "term_resize" then | |
setupScreenScaling() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment