From 724f1a99b6051b9e26dc2cfaded1d3626f01093d Mon Sep 17 00:00:00 2001 From: StormFusions <62036454+StormFusions@users.noreply.github.com> Date: Sat, 15 May 2021 15:43:16 +0100 Subject: [PATCH] Add files via upload --- lib/button.lua | 80 +++++++++++ lib/f.lua | 93 +++++++++++++ reactor.lua | 367 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 540 insertions(+) create mode 100644 lib/button.lua create mode 100644 lib/f.lua create mode 100644 reactor.lua diff --git a/lib/button.lua b/lib/button.lua new file mode 100644 index 0000000..05a2cc3 --- /dev/null +++ b/lib/button.lua @@ -0,0 +1,80 @@ + +local button = {} +local mon = peripheral.find("monitor") + +function clearTable() + button = {} +end + +function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color) + button[name] = {} + button[name]["title"] = title + button[name]["func"] = func + button[name]["active"] = false + button[name]["xmin"] = xmin + button[name]["ymin"] = ymin + button[name]["xmax"] = xmax + button[name]["ymax"] = ymax + button[name]["color"] = color + button[name]["elem"] = elem + button[name]["elem2"] = elem2 +end + +-- stuff and things for buttons + +function fill(text, color, bData) + mon.setBackgroundColor(color) + mon.setTextColor(colors.white) + local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2) + local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1 + for j = bData["ymin"], bData["ymax"] do + mon.setCursorPos(bData["xmin"], j) + if j == yspot then + for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do + if k == xspot then + mon.write(bData["title"]) + else + mon.write(" ") + end + end + else + for i = bData["xmin"], bData["xmax"] do + mon.write(" ") + end + end + end + mon.setBackgroundColor(colors.black) +end + +-- stuff and things for buttons + +function screen() + local currColor + for name,data in pairs(button) do + local on = data["active"] + currColor = data["color"] + fill(name, currColor, data) + end +end + +-- magical handler for clicky clicks + +function checkxy(x, y) + for name, data in pairs(button) do + if y>=data["ymin"] and y <= data["ymax"] then + if x>=data["xmin"] and x<= data["xmax"] then + data["func"](data["elem"], data["elem2"]) + --flash(data['name']) + return true + --data["active"] = not data["active"] + --print(name) + end + end + end + return false +end + +function clickEvent() + local myEvent={os.pullEvent("monitor_touch")} + checkxy(myEvent[3], myEvent[4]) +end \ No newline at end of file diff --git a/lib/f.lua b/lib/f.lua new file mode 100644 index 0000000..e28b771 --- /dev/null +++ b/lib/f.lua @@ -0,0 +1,93 @@ + +-- peripheral identification +-- +function periphSearch(type) + local names = peripheral.getNames() + local i, name + for i, name in pairs(names) do + if peripheral.getType(name) == type then + return peripheral.wrap(name) + end + end + return null +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_y(mon, x, y, length, color) + + if length < 0 then + length = 0 + end + + mon.monitor.setBackgroundColor(color) + for i=y, length do + mon.monitor.setCursorPos(x, 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) --backgoround bar + local barSize = math.floor((minVal/maxVal) * length) + draw_line(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 diff --git a/reactor.lua b/reactor.lua new file mode 100644 index 0000000..314cfc0 --- /dev/null +++ b/reactor.lua @@ -0,0 +1,367 @@ + +os.loadAPI("lib/f.lua") +os.loadAPI("lib/button.lua") + +local reactorSide = "back" +local fluxgateSide = "left" + +local targetStrength = 50 +local maxTemp = 8000 +local safeTemp = 3000 +local lowFieldPer = 15 + +local activateOnCharge = true + +local version = 0.1 + +local autoInputGate = 1 +local curInputGate = 222000 + +local mon, monitor, monX, monY + +local reactor +local fluxgate +local inputFluxgate + +local ri + +local action = "None since reboot" +local actioncolor = colors.gray +local emergencyCharge = false +local emergencyTemp = false + +monitor = f.periphSearch("monitor") +inputFluxgate = f.periphSearch("flux_gate") +fluxgate = peripheral.wrap(fluxgateSide) +reactor = peripheral.wrap(reactorSide) + +if monitor == null then + error("No valid monitor was found") +end + +if fluxgate == null then + error("No valid fluxgate was found") +end + +if reactor == null then + error("No reactor was found") +end + +monX, monY = monitor.getSize() +mon = {} +mon.monitor, mon.X, mon.Y = monitor, monX, monY + +function mon.clear() + mon.monitor.setBackgroundColor(colors.black) + mon.monitor.clear() + mon.monitor.setCursorPos(1,1) + button.screen() +end + +mon.clear() + +function save_config() + sw = fs.open("reactorconfig.txt", "w") + sw.writeLine(version) + sw.writeLine(autoInputGate) + sw.writeLine(curInputGate) + sw.close() +end + +function load_config() + sr = fs.open("reactorconfig.txt", "r") + version = sr.readLine() + autoInputGate = tonumber(sr.readLine()) + curInputGate = tonumber(sr.readLine()) + sr.close() +end + +if fs.exists("reactorconfig.txt") == false then + save_config() +else + load_config() +end + +function reset() + term.clear() + term.setCursorPos(1,1) +end + +function reactorStatus(r) + local tbl = {} + if r == "running" then + tbl = {"Online", colors.green} + elseif r == "cold" then + tbl = {"Offline", colors.gray} + elseif r =="warming_up" then + tbl = {"Charging", colors.orange} + elseif r == "cooling" then + tbl = {"Cooling Down", colors.blue} + else + tbl = {"Shutting Down", colors.red} + end + return tbl +end + +function reactorControl() + ri = reactor.getReactorInfo() + + if ri == nil then + error("Reactor not setup correctly") + end + + for k, v in pairs(ri) do + print(k..": "..tostring(v)) + end + print("Output Gate: ", fluxgate.getSignalLowFlow()) + print("Input Gate: ", inputFluxgate.getSignalLowFlow()) + + if emergencyCharge == true then + reactor.chargeReactor() + end + + if ri.status == "warming_up" then + inputFluxgate.setSignalLowFlow(900000) + emergencyCharge = false + end + + if emergencyTemp == true and ri.status == "stopping" and ri.temperature < safeTemp then + reactor.activateReactor() + emergencyTemp = false + end + + if ri.status == "warming_up" and activateOnCharge == true then + reactor.activateOnCharge() + end + + if ri.status == "running" then + if autoInputGate == 1 then + fluxval = ri.fieldDrainRate / (1 - (targetStrength/100)) + print("Target Gate: "..fluxval) + inputFluxgate.setSignalLowFlow(fluxval) + else + inputFluxgate.setSignalLowFlow(curInputGate) + end + end + + --safe guards + local fuelPercent + fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000)*.01 + + local fieldPercent + fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000)*.01 + + if fuelPercent <= 10 then + reactor.stopReactor() + actioncolor = colors.red + action = "Fuel is low. Refuel" + ActionMenu() + end + + if fieldPercent <= lowFieldPer and ri.status == "running" then + actioncolor = colors.red + action = "Field str < "..lowFieldPer.."%" + ActionMenu() + reactor.stopReactor() + reactor.chargeReactor() + emergencyCharge = true + end + + if ri.temperature > maxTemp then + reactor.stopReactor() + actioncolor = colors.red + action = "Reactor overheated" + ActionMenu() + emergencyTemp = true + end + + sleep(0.2) +end + +local MenuText = "Loading..." + +function toggleReactor() + ri = reactor.getReactorInfo() + + if ri.status == "running" then + reactor.stopReactor() + elseif ri.status == "stopping" then + reactor.activateReactor() + else + reactor.chargeReactor() + end +end + +function ActionMenu() + + button.setButton("action", action, buttonMain, 2, 23, monX-1, 25, 0, 0, colors.red) + +end + +function buttonControls() + + mon.clear() + button.clearTable() + + MenuText = "CONTROLS" + + local sLength = 4+(string.len("Toggle Reactor")+1) + + button.setButton("toggle", "Toggle Reactor", toggleReactor, 4, 28, sLength, 30, 0, 0, colors.blue) + + local sLength2 = (sLength+10+(string.len("Reboot")+1)) + button.setButton("reboot", "Reboot", rebootSystem, sLength+10, 28, sLength2, 30, 0, 0, colors.blue) + + local sLength3 = 4+(string.len("Back")+1) + button.setButton("back", "Back", buttonMain, 4, 32, sLength3, 34, 0, 0, colors.blue) + + button.screen() +end + +function changeOutputValue(num, val) + local cFlow = fluxgate.getSignalLowFlow() + + if val == 1 then + cFlow = cFlow+num + else + cFlow = cFlow-num + end + fluxgate.setSignalLowFlow(cFlow) +end + +function outputMenu() + + button.clearTable() + + MenuText = "OUTPUT" + + local sLengthX = monX-3-(string.len(">>>")+1) + local sLength = sLengthX+string.len(">>>")+1 + button.setButton("+100,000", ">>>", changeOutputValue, sLengthX, 28, sLength, 30, 100000, 1, colors.blue) + + local sLengthX2 = sLengthX-3-string.len(">>") + local sLength2 = sLengthX2+string.len(">>")+1 + button.setButton("+10,000", ">>", changeOutputValue, sLengthX2, 28, sLength2, 30, 10000, 1, colors.blue) + + local sLengthX3 = sLengthX2-3-string.len(">") + local sLength3 = sLengthX3+string.len(">")+1 + button.setButton("+1,000", ">", changeOutputValue, sLengthX3, 28, sLength3, 30, 1000, 1, colors.blue) + + + local nLength = 4+(string.len("<<<")+1) + button.setButton("-100,000", "<<<", changeOutputValue, 4, 28, nLength, 30, 100000, 0, colors.blue) + + local nLength2 = nLength+2+(string.len("<<")+1) + button.setButton("-10,000", "<<", changeOutputValue, nLength+2, 28, nLength2, 30, 10000, 0, colors.blue) + + local nLength3 = nLength2+2+(string.len("<")+1) + button.setButton("-1,000", "<", changeOutputValue, nLength2+2, 28, nLength3, 30, 1000, 0, colors.blue) + + local sLength4 = 4+(string.len("Back")+1) + button.setButton("back", "Back", buttonMain, 4, 32, sLength4, 34, 0, 0, colors.blue) + + +end + + +function buttonMain() + + mon.clear() + button.clearTable() + + MenuText = "MAIN MENU" + + local sLength = 4+(string.len("Controls")+1) + button.setButton("controls", "Controls", buttonControls, 4, 28, sLength, 30, 0, 0, colors.blue) + + local sLength2 = (sLength+13+(string.len("Output"))+1) + button.setButton("output", "Output", outputMenu, sLength+13, 28, sLength2, 30, 0, 0, colors.blue) + + button.screen() + +end + +buttonMain() + +function reactorInfoScreen() + + reset() + mon.clear() + + f.draw_text(mon, 2, 38, "Made by: StormFusions v"..version, colors.gray, colors.black) + + --Info Box + f.draw_line(mon, 2, 22, monX-2, colors.gray) + f.draw_line(mon, 2, 2, monX-2, colors.gray) + + f.draw_line_y(mon, 2, 2, 22, colors.gray) + f.draw_line_y(mon, monX-1, 2, 22, colors.gray) + f.draw_text(mon, 4, 2, " INFO ", colors.white, colors.black) + + --Button Box + f.draw_line(mon, 2, 26, monX-2, colors.gray) + f.draw_line(mon, 2, monY-1, monX-2, colors.gray) + + f.draw_line_y(mon, 2, 26, monY-1, colors.gray) + f.draw_line_y(mon, monX-1, 26, monY-1, colors.gray) + f.draw_text(mon, 4, 26, " "..MenuText.." ", colors.white, colors.black) + + --f.draw_text(mon, 5, 28, MenuText, colors.white, colors.black) + + ri = reactor.getReactorInfo() + + local stat = reactorStatus(ri.status) + + f.draw_text_lr(mon, 4, 4, 3, "Status:", stat[1], colors.white, stat[2], colors.black) + f.draw_text_lr(mon, 4, 5, 3, "Generation:", f.format_int(ri.generationRate).." rf/t", colors.white, colors.lime, colors.black) + + local tempColor = colors.red + if ri.temperature <= 5000 then tempColor = colors.green end + if ri.temperature >= 5000 and ri.temperature <= 6500 then tempColor = colors.orange end + f.draw_text_lr(mon, 4, 7, 3, "Temperature:", f.format_int(ri.temperature).."C", colors.white, tempColor, colors.black) + + f.draw_text_lr(mon, 4, 9, 3, "Output Gate:", f.format_int(fluxgate.getSignalLowFlow()).." rf/t", colors.white, colors.blue, colors.black) + f.draw_text_lr(mon, 4, 10, 3, "Input Gate:", f.format_int(inputFluxgate.getSignalLowFlow()).." rf/t", colors.white, colors.blue, colors.black) + + local satPercent + satPercent = math.ceil(ri.energySaturation / ri.maxEnergySaturation * 10000)*.01 + + f.draw_text_lr(mon, 4, 12, 3, "Energy Saturation:", satPercent.."%", colors.white, colors.green, colors.black) + f.progress_bar(mon, 4, 13, monX-7, satPercent, 100, colors.green, colors.white) + + local fieldPercent, fieldColor + fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000)*.01 + + fieldColor = colors.red + if fieldPercent >= 50 then fieldColor = colors.blue end + if fieldPercent < 50 and fieldPercent > 30 then fieldColor = colors.orange end + + if autoInputGate == 1 then + f.draw_text_lr(mon, 4, 15, 3, "Field Strength T:"..targetStrength, fieldPercent.."%", colors.white, fieldColor, colors.black) + else + f.draw_text_lr(mon, 4, 15, 3, "Field Strength:", fieldPercent.."%", colors.white, fieldColor, colors.black) + end + f.progress_bar(mon, 4, 16, monX-7, fieldPercent, 100, fieldColor, colors.white) + + local fuelPercent, fuelColor + fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000)*.01 + + fuelColor = colors.red + if fuelPercent >=70 then fuelColor = colors.green end + if fuelPercent < 70 and fuelPercent > 30 then fuelColor = colors.orange end + + f.draw_text_lr(mon, 4, 18, 3, "Fuel:", fuelPercent.."%", colors.white, fuelColor, colors.black) + + f.progress_bar(mon, 4, 19, monX-7, fuelPercent, 100, fuelColor, colors.white) + + sleep(1.25) + + while true do + parallel.waitForAny(reactorInfoScreen, reactorControl, button.clickEvent) + end + +end + +mon.monitor.setTextScale(0.5) + +reactorInfoScreen() \ No newline at end of file