Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem = peripheral.find("modem")
- mon = peripheral.find("monitor")
- modem.open(24816)
- local maxPower = 2140000000000
- local monitorWidth = mon.getSize()
- mon.clear()
- --adds commas to long numbers
- 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
- --rounds number to specified number of decimals
- function round(val, decimal)
- if (decimal) then
- return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
- else
- return math.floor(val+0.5)
- end
- end
- --draws bars
- function drawBar(barColor, BGColor, maxLength, barLength, startX, startY)
- --draws the background color
- mon.setCursorPos(startX, startY)
- mon.setBackgroundColor(BGColor)
- for len=1,maxLength do
- mon.write(" ")
- end
- --draws the bar
- mon.setCursorPos(startX, startY)
- mon.setBackgroundColor(barColor)
- for length=1,barLength do
- mon.write(" ")
- end
- end
- --writes text
- function writeText(startX, startY, text, FGColor, BGColor, clear)
- mon.setCursorPos(startX, startY)
- mon.setTextColor(FGColor)
- mon.setBackgroundColor(BGColor)
- if clear then
- mon.clearLine()
- end
- mon.write(text)
- end
- --calculates order of magnitude for large numbers and returns smaller number and prefix
- function enshrinkenNumber(number, decimal)
- local levels = 0
- local remainder = number
- local failout = false
- while (remainder > 1000) do
- remainder = remainder / 1000
- levels = levels+1
- if (levels > 10) or (remainder < 0) then
- return "",""
- end
- end
- if decimal then
- remainder = round(remainder, decimal)
- end
- if levels == 0 then
- return remainder, ""
- elseif levels == 1 then
- return remainder, "k"
- elseif levels == 2 then
- return remainder, "M"
- elseif levels == 3 then
- return remainder, "G"
- elseif levels == 4 then
- return remainder, "T"
- elseif levels == 5 then
- return remainder, "P"
- elseif levels == 6 then
- return remainder, "E"
- elseif levels == 7 then
- return remainder, "Z"
- elseif levels == 8 then
- return remainder, "Y"
- else
- return nil, nil
- end
- end
- --formats a number of seconds into a more usable format HH:MM:SS
- function formatTime(time)
- time = math.floor(time)
- local hours = math.floor(time/3600)
- local minutes = math.floor(time/60 - (hours*60))
- local seconds = math.floor(time - (hours*3600) - (minutes*60))
- return (hours..":"..minutes..":"..seconds)
- end
- --draws the current power bar
- function drawPowerLevel(currE, row)
- --gets the percent fill to 2 decimals
- local fillPercent = (round((currE / maxPower), 4)) * 100
- local maxBarLength = monitorWidth - 2
- local barLength = math.floor((fillPercent * maxBarLength) / 100)
- local barColor
- if fillPercent<10 then
- barColor = colors.red
- elseif fillPercent < 25 then
- barColor = colors.yellow
- elseif fillPercent <= 100 then
- barColor = colors.lime
- else
- barColor = colors.gray
- end
- drawBar(barColor, colors.gray, maxBarLength, barLength, 2, row)
- drawBar(barColor, colors.gray, maxBarLength, barLength, 2, row+1)
- local currENum, currEPrefix = enshrinkenNumber(currE, 3)
- local maxNum, maxPrefix = enshrinkenNumber(maxPower, 3)
- local text = "Energy: "..currENum.." "..currEPrefix.."rf / "..maxNum.." "..maxPrefix.."rf"
- writeText(1, row+3, text, colors.white, colors.black, true)
- writeText(1, row+4, "Fill percentage: "..fillPercent.."%", colors.white, colors.black, true)
- end
- --draws power in/out
- function drawPowerFlux(deltaE, row)
- writeText(1, row, "Energy Flux: ", colors.white, colors.black, true)
- local textColor
- local symbol
- if deltaE>0 then
- textColor = colors.lime
- symbol = "+"
- elseif deltaE<0 then
- textColor = colors.red
- symbol = "-"
- else
- textColor = colors.white
- symbol = ""
- end
- writeText(14, row, symbol..deltaE, textColor, colors.black)
- local currPos = mon.getCursorPos()
- writeText(currPos+1, row, "rf/t", colors.white, colors.black)
- end
- --draws the uranium level in the reactor
- function drawUraniumLevel(count, row)
- count = round(count/1000, 3)
- local volNum, volPrefix = enshrinkenNumber(count, 3)
- local text = "Total Nuclear Fuel: "..volNum.." "..volPrefix.."b"
- writeText(1, row, text, colors.white, colors.black, true)
- end
- --draws runtime for reactor
- function drawReactorTime(runTime, row)
- local text = "Run Time: "..formatTime(runTime)
- writeText(1, row, text, colors.white, colors.black, true)
- end
- --draws total power expected from reactor given current fuel
- function drawTotalPower(totalPower, row)
- mon.clearLine(row)
- local totalNum, totalPrefix = enshrinkenNumber(totalPower, 3)
- local text = "Total Power: "..totalNum.." "..totalPrefix.."rf"
- writeText(1, row, text, colors.white, colors.black, true)
- end
- while true do
- event = {os.pullEvent("modem_message")}
- if event[4] == 1 then
- drawPowerLevel(event[5], 2)
- elseif event[4] == 2 then
- drawPowerFlux(event[5], 7)
- elseif event[4] == 3 then
- drawUraniumLevel(event[5], 9)
- elseif event[4] == 4 then
- drawReactorTime(event[5], 10)
- elseif event[4] == 5 then
- drawTotalPower(event[5], 11)
- else
- os.sleep(0.1)
- end
- end
Add Comment
Please, Sign In to add comment