Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local reactorName = "BigReactors-Reactor_3"
- local monitorName = "monitor_3"
- local powerMonitorName = "right"
- local autoScaling = true
- local surplusThreshold = 0.7 -- Percentage of internal buffer where below the reactor will generate a surplus of energy
- local allowedSurplusStatic = 50 -- RF/t delta surplus when above surplus threshold
- local allowedSurplusGrowth = 250 -- RF/t of delta surplus if reactor below surplus threshold
- -- util
- function percentage(value, max)
- return math.ceil(value / max * 10000) * 0.01
- end
- function convertTimeToTick(time)
- return (time * 1000 + 18000) % 24000
- end
- function getDeltaTicks(lastTick)
- local now = os.time()
- now = convertTimeToTick(now)
- if now < lastTick then lastTick = lastTick - 24000 end
- local delta = now - lastTick
- if delta < 1 then delta = 1 end
- return delta
- end
- function round(value, precision)
- local multiplier = 10 ^ precision
- if multiplier > 1 then
- return math.ceil(value * multiplier) / multiplier
- else
- return math.ceil(value * multiplier)
- end
- end
- -- formatting
- function format_int(number)
- if number == nil then number = 0 end
- local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
- -- reverse the int-string and append a comma to all blocks of 3 digits
- int = int:reverse():gsub("(%d%d%d)", "%1,")
- -- reverse the int-string back remove an optional comma and put the
- -- optional minus and fractional part back
- return minus .. int:reverse():gsub("^,", "") .. fraction
- end
- -- monitor related
- --display text text on monitor, "mon" peripheral
- function draw_text(mon, x, y, text, text_color, bg_color)
- mon.monitor.setBackgroundColor(bg_color)
- mon.monitor.setTextColor(text_color)
- mon.monitor.setCursorPos(x,y)
- mon.monitor.write(text)
- end
- function draw_text_right(mon, offset, y, text, text_color, bg_color)
- mon.monitor.setBackgroundColor(bg_color)
- mon.monitor.setTextColor(text_color)
- mon.monitor.setCursorPos(mon.X-string.len(tostring(text))-offset,y)
- mon.monitor.write(text)
- end
- function draw_text_lr(mon, x, y, offset, text1, text2, text1_color, text2_color, bg_color)
- draw_text(mon, x, y, text1, text1_color, bg_color)
- draw_text_right(mon, offset, y, text2, text2_color, bg_color)
- end
- --draw line on computer terminal
- function draw_line(mon, x, y, length, color)
- if length < 0 then
- length = 0
- end
- mon.monitor.setBackgroundColor(color)
- mon.monitor.setCursorPos(x,y)
- mon.monitor.write(string.rep(" ", length))
- end
- function draw_line_vertical(mon, x, y, length, color)
- if length < 0 then
- length = 0
- end
- mon.monitor.setBackgroundColor(color)
- for i = 0, length - 1 do
- mon.monitor.setCursorPos(x,y + i)
- mon.monitor.write(" ")
- end
- end
- --create progress bar
- --draws two overlapping lines
- --background line of bg_color
- --main line of bar_color as a percentage of minVal/maxVal
- function progress_bar(mon, x, y, length, minVal, maxVal, bar_color, bg_color)
- draw_line(mon, x, y, length, bg_color) --background bar
- local barSize = math.floor((minVal/maxVal) * length)
- draw_line(mon, x, y, barSize, bar_color) --progress so far
- end
- -- create stacked progress bar
- function stacked_progress_bar(mon, x, y, length, a_val, b_val, maxVal, a_color, b_color, bg_color)
- draw_line(mon, x, y, length, bg_color) --background bar
- local a_size = math.floor((a_val/maxVal) * length)
- local b_size = math.ceil((b_val/maxVal) * length)
- draw_line(mon, x, y, a_size, a_color)
- draw_line(mon, x + a_size, y, b_size, b_color)
- end
- function vertical_progress_bar(mon, x, y, length, minVal, maxVal, bar_color, bg_color)
- draw_line_vertical(mon, x, y, length, bg_color) --background bar
- local barSize = math.floor((minVal/maxVal) * length)
- draw_line_vertical(mon, x, y, barSize, bar_color) --progress so far
- end
- function clear(mon)
- term.clear()
- term.setCursorPos(1,1)
- mon.monitor.setBackgroundColor(colors.black)
- mon.monitor.clear()
- mon.monitor.setCursorPos(1,1)
- end
- -- monitor
- local mon, monitor, monX, monY
- monitor = peripheral.wrap(monitorName)
- monX, monY = monitor.getSize()
- mon = {}
- mon.monitor, mon.X, mon.Y = monitor, monX, monY
- -- reactor
- local reactor = peripheral.wrap(reactorName)
- local maxHeat = 2000
- local maxEnergy = 10000000
- local controlRods = {}
- for i = 1, reactor.getNumberOfControlRods() do
- controlRods[i] = i - 1 -- Control rod indices start at zero
- end
- -- Power monitor
- local powerMonitor = peripheral.wrap(powerMonitorName)
- function getMostInsertedFuelRod()
- local selected = -1
- local previousBestInsertion = -10
- for _, rodIndex in ipairs(controlRods) do
- if reactor.getControlRodLevel(rodIndex) > previousBestInsertion then
- selected = rodIndex
- previousBestInsertion = reactor.getControlRodLevel(rodIndex)
- end
- end
- return selected, previousBestInsertion
- end
- function getLeastInsertedFuelRod()
- local selected = -1
- local previousBestInsertion = 110
- for _, rodIndex in ipairs(controlRods) do
- if reactor.getControlRodLevel(rodIndex) < previousBestInsertion then
- selected = rodIndex
- previousBestInsertion = reactor.getControlRodLevel(rodIndex)
- end
- end
- return selected, previousBestInsertion
- end
- function scaleUp()
- local rod, amount = getMostInsertedFuelRod()
- print(rod, amount)
- if amount > 0 then reactor.setControlRodLevel(rod, amount - 1) end
- end
- function scaleDown()
- local rod, amount = getLeastInsertedFuelRod()
- if amount < 100 then reactor.setControlRodLevel(rod, amount + 1) end
- end
- -- Autoscaling
- function autoscale()
- if not autoScaling then
- return
- end
- while true do
- local energy = reactor.getEnergyStored()
- local produced = reactor.getEnergyProducedLastTick()
- local consumed = powerMonitor.getAverageEnergySent()
- local deltaEnergy = produced - consumed
- if deltaEnergy < 0 then
- scaleUp()
- elseif energy < maxEnergy * surplusThreshold then
- local acceptableAddition = allowedSurplusGrowth
- if deltaEnergy < acceptableAddition then
- scaleUp()
- elseif deltaEnergy > acceptableAddition then
- scaleDown()
- end
- elseif deltaEnergy > allowedSurplusStatic then
- scaleDown()
- end
- sleep(0.1)
- end
- end
- function drawScreen()
- mon.monitor.setTextScale(0.5)
- mon.monitor.setTextScale(1)
- local lastTick = convertTimeToTick(os.time())
- local previousEnergy = reactor.getEnergyStored()
- while true do
- clear(mon)
- print("working")
- if reactor == nil or not reactor.getConnected() then
- draw_text(mon, 1, 1, "No reactor found", colors.red, colors.black)
- return
- end
- -- Reactor status
- local status = "INACTIVE"
- local statusColor = colors.red
- local active = reactor.getActive()
- if active then
- status = "ACTIVE"
- statusColor = colors.lime
- end
- draw_text_lr(mon, 2, 2, 1, "Reactor Status", status, colors.white, statusColor, colors.black)
- -- Power generation
- local energy = reactor.getEnergyStored()
- local produced = reactor.getEnergyProducedLastTick()
- local consumed = powerMonitor.getAverageEnergySent()
- local deltaEnergy = produced - consumed
- draw_text_lr(mon, 2, 4, 1, "Delta power", format_int(round(deltaEnergy, 0)) .. " rf/t", colors.white, colors.green, colors.black)
- -- Casing
- local fuelTemp = reactor.getFuelTemperature()
- local fuelTemperature = reactor.getFuelTemperature()
- local fuelColor = colors.red
- if fuelTemperature < 1200 then fuelColor = colors.orange end
- if fuelTemperature < 1000 then fuelColor = colors.yellow end
- if fuelTemperature < 750 then fuelColor = colors.lime end
- local casingTemp = reactor.getCasingTemperature()
- local casingColor = colors.red
- if casingTemp < 1200 then casingColor = colors.orange end
- if casingTemp < 1000 then casingColor = colors.yellow end
- if casingTemp < 750 then casingColor = colors.lime end
- draw_text_lr(mon, 2, 6, 1, "Fuel " .. round(fuelTemp, 0) .. " C", "Casing " .. round(casingTemp, 0) .. " C", fuelColor, casingColor, colors.black)
- -- Fuel and waste
- local fuelAmount = reactor.getFuelAmount()
- local wasteAmount = reactor.getWasteAmount()
- local maxFuelAmount = reactor.getFuelAmountMax()
- local fuelConsumed = reactor.getFuelConsumedLastTick()
- draw_text_lr(mon, 2, 8, 1, "Core fuel", format_int(round(fuelConsumed, 3)) .. " mB/t", colors.white, colors.white, colors.black)
- stacked_progress_bar(mon, 2, 9, mon.X-2, wasteAmount, fuelAmount, maxFuelAmount, colors.blue, colors.yellow, colors.gray)
- -- Control rods
- draw_text(mon, 2, 11, "Control rods", colors.white, colors.black)
- for _, rodIndex in ipairs(controlRods) do
- local insertion = reactor.getControlRodLevel(rodIndex)
- vertical_progress_bar(mon, 2 + rodIndex * 2, 12, 7, insertion, 100, colors.lightGray, colors.gray)
- end
- -- Fuel temperature
- local fuelTemperature = reactor.getFuelTemperature()
- local fuelColor = colors.red
- if fuelTemperature < 1200 then fuelColor = colors.orange end
- if fuelTemperature < 1000 then fuelColor = colors.yellow end
- if fuelTemperature < 750 then fuelColor = colors.lime end
- -- Casing temperature
- -- Autoscaling
- local autoScalingText = "Autoscaling on"
- local autoScalingColor = colors.green
- if not autoScaling then
- autoScalingText = "Autoscaling off"
- autoScalingColor = colors.gray
- end
- draw_text_right(mon, 0, mon.Y, autoScalingText, autoScalingColor, colors.black)
- sleep(1)
- end
- end
- parallel.waitForAll(autoscale, drawScreen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement