Skip to content

Instantly share code, notes, and snippets.

@zmbush
Last active September 9, 2016 03:59
Show Gist options
  • Save zmbush/e5fa28d63ba73b5882f4b8d0c231e0fb to your computer and use it in GitHub Desktop.
Save zmbush/e5fa28d63ba73b5882f4b8d0c231e0fb to your computer and use it in GitHub Desktop.
Computercraft scripts
print("Starting up reactor control program")
reactorOn = false
previouslyStored = 0
function commas(number)
local number, k = tostring(number), nil
while true do
number, k = string.gsub(number, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k == 0) then break end
end
return number
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function drawBar(startx, starty, endx, endy, barPercent, barColor, borderColor)
paintutils.drawBox(startx, starty, endx, endy, borderColor)
paintutils.drawFilledBox(
startx + 1,
starty + 1 + (1 - barPercent)*(endy - starty - 1),
endx - 1,
endy - 1, barColor
)
term.setBackgroundColor(colors.black)
end
function getMonitor()
physicalMonitor = peripheral.find("monitor")
if not physicalMonitor then
return nil, nil, nil
end
physicalMonitor.setTextScale(2)
monitorPixelWidth, monitorPixelHeight = physicalMonitor.getSize()
monitor = window.create(physicalMonitor, 1, 1, monitorPixelWidth, monitorPixelHeight, false)
return monitor, monitorPixelWidth, monitorPixelHeight
end
while true do
os.startTimer(0.5)
power_cell = peripheral.find("draconic_rf_storage")
storage_percentage = power_cell.getEnergyStored() / power_cell.getMaxEnergyStored()
if storage_percentage < 0.70 then
reactorOn = true
end
if storage_percentage >= 0.90 then
reactorOn = false
end
redstone.setOutput("front", reactorOn)
if redstone.getInput("top") or redstone.getInput("back") then
shell.run("reboot")
end
monitor, monWidth, monHeight = getMonitor()
if monitor then
monitor.clear()
monitor.setCursorPos(1, 1)
monitor.setVisible(false)
oldTerm = term.redirect(monitor)
else
print("Unable to connect to monitor. Falling back to term.")
end
print("Storage: " .. round(storage_percentage * 100, 2) .. "% full")
print("Power gain: " .. commas((power_cell.getEnergyStored() - previouslyStored) / 10) .. " RF/t")
if reactorOn then
print("Reactor is ON")
else
print("Reactor is OFF")
end
if monitor then
drawBar(
monitorPixelWidth - 5, 2,
monitorPixelWidth - 1, monitorPixelHeight - 1,
storage_percentage,
colors.green, colors.white
)
term.redirect(oldTerm)
monitor.setVisible(true)
end
previouslyStored = power_cell.getEnergyStored()
os.pullEvent("timer")
end
shell.run("rm reactorControl.lua")
shell.run("rm startup")
shell.run("gist get e5fa28d63ba73b5882f4b8d0c231e0fb")
shell.run("reactorControl.lua")
print("Waiting 5 seconds before rebooting")
os.sleep(5)
shell.run("reboot")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment