Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Better reactor program
- local reactor = peripheral.wrap("back")
- local monitor = peripheral.find("monitor")
- --Functions at the top are lowest level
- --Functions at the bottom are highest level
- local function clearMonitor()
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.black)
- end
- local function checkReactor()
- if reactor == nil then
- error("Failed to find the reactor!")
- elseif reactor.getConnected() == false then
- error("Failed to connect to the reactor!")
- end
- end
- local function checkMonitor()
- if monitor == nil then
- error("Failed to find the monitor!")
- end
- end
- local function round(num)
- return math.floor(num + 0.5)
- end
- local function exact_send(x, y, msg)
- monitor.setCursorPos(x,y)
- monitor.write(msg)
- end
- local function f_send(pos, y, msg)
- local length = string.len(msg)
- local width, height = monitor.getSize()
- if pos == "RIGHT" then
- exact_send(width-length, y, msg)
- elseif pos == "CENTER" then
- local cx = round(width/2)-round(length/2)
- exact_send(cx, y, msg)
- elseif pos == "LEFT" then
- exact_send(1, y, msg)
- else
- error("Invalid argument: "..pos)
- end
- end
- function c_send(y, msg)
- f_send("CENTER", y, msg)
- end
- --x can be a # or a position ('LEFT', 'RIGHT', 'CENTER')
- function send(x, y, msg)
- if type(x) == "string" then
- f_send(x, y, msg)
- return
- end
- exact_send(x, y, msg)
- end
- local function draw_bar(x, y, percent)
- local bar = "["
- local limit = round(percent/10)
- for i=0, limit, 1 do
- bar = bar.."="
- end
- for i=limit, 10, 1 do
- bar = bar.." "
- end
- bar = bar.."]"
- send(x, y, bar)
- end
- function main()
- checkReactor()
- checkMonitor()
- clearMonitor()
- local energy, percent_energy, capacity
- while true do
- energy = reactor.getEnergyStored()
- percent_energy = energy/100000
- capacity = round((100-percent_energy)*1000)/1000
- reactor.setAllControlRodLevels(percent_energy)
- c_send(2, "Reactor Energy")
- c_send(3, "--------------")
- c_send(4, percent_energy.."%")
- draw_bar("CENTER", 5, percent_energy)
- c_send(7, "Reactor Capacity")
- c_send(8, "----------------")
- c_send(9, capacity.."%")
- draw_bar("CENTER", 10, capacity)
- os.sleep(1)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement