Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Made by Alexmaster75
- local component = require("component")
- local event = require("event")
- local term = require("term")
- term.clear()
- print("Loading...")
- -- Peripherals
- local gpu = component.gpu
- local battery = {}
- local battery_present = true
- local c = 1
- for add, n in component.list("ntm_energy_storage") do
- battery[c] = component.proxy(add)
- c = c + 1
- end
- if (#battery >= 1) then
- battery = battery[1]
- else
- battery = nil
- battery_present = false
- end
- local turbines = {}
- local c = 1
- for add, n in component.list("ntm_gas_turbine") do
- turbines[c] = component.proxy(add)
- c = c + 1
- end
- -- Global variables
- local delta_program = 0.1
- local max = 0.8
- local min = 0.2
- local temp = {}
- for i=1,1,1 do
- temp[i] = 0
- end
- local battery_energy = {}
- local battery_percentage = 0.0
- local blink = true
- local key = {}
- local input = ""
- local auto = {}
- for i=1,#turbines,1 do
- auto[i] = true
- end
- local activation = false
- local turbine_index = 0
- local turbine_set = 0
- local keywords = {}
- local x, y = term.getCursor()
- -- Scales a number by orders of magnitude
- local function x3_scale(n)
- local scale = {" ", "k", "M", "G", "T"}
- local num = n
- local i = 1
- while (math.abs(num) >= 1000.0 and i < #scale) do
- num = num / 1000.0
- i = i + 1
- end
- return (("%5.1f %1s"):format(num, scale[i]))
- end
- -- Gets in-game time
- local function time(mask)
- return tonumber(os.date(mask))
- end
- -- Prints a divider given the character/string
- local function screen_divider(str)
- local W = term.window.width
- local X, Y = term.getCursor()
- for i=X,W-#str,#str do
- term.write(str)
- end
- X, Y = term.getCursor()
- if (X < W) then
- for i=1,W-X+1,1 do
- term.write(str:sub(i, i))
- end
- end
- if (#str == 1) then
- term.write(str)
- end
- print()
- end
- -- Segments a string containing spaces
- local function segment(string)
- local k_word = {}
- local k = 1
- local char = ""
- for i=1,string:len(),1 do
- char = string:sub(i,i)
- if (char == " " or char == nil) then
- k = k + 1
- else
- if (k_word[k] == nil) then k_word[k] = "" end
- k_word[k] = k_word[k] .. char
- end
- end
- return k_word
- end
- -- Starts all turbines
- local function start_all(turbine_array)
- local fluid = {}
- local fuel = false
- for i=1,#turbine_array,1 do
- fluid = {turbines[i].getFluid()}
- fuel = (fluid[1] / fluid[2] > 0.0 and fluid[3] / fluid[4] > 0.0)
- if (turbine_array[i].getState() == 0 and fuel) then
- turbine_array[i].start()
- end
- end
- return
- end
- -- Stops all turbines
- local function stop_all(turbine_array)
- for i=1,#turbine_array,1 do
- turbine_array[i].stop()
- end
- return
- end
- -- Prints a single turbine stats
- local function print_turbine(turbine, index)
- local fluid = {turbine.getFluid()}
- local energy = turbine.getPower()
- local num_status = turbine.getState()
- local status = ""
- if (num_status == -1) then
- status = "Starting..."
- elseif (num_status == 0) then
- status = "Offline"
- elseif (num_status == 1) then
- status = "Online"
- else
- status = "Error"
- end
- print(("%-10s: %11d "):format("Turbine n.", index))
- print(("%-10s: %11s "):format("Fuel type", turbine.getType()))
- print(("%-10s: %11s"):format("Status", status))
- print(("%-10s: %11d "):format("RPM", turbine.getThrottle()))
- print(("%-10s: %11.1f%1s "):format("Fuel", fluid[1] / fluid[2] * 100.0, "%"))
- print(("%-10s: %11.1f%1s "):format("Lubricant", fluid[3] / fluid[4] * 100.0, "%"))
- print(("%-10s: %11sHE "):format("Energy", x3_scale(energy)))
- print(("%-10s: %11.1f%1s "):format("Water", fluid[5] / fluid[6] * 100.0, "%"))
- print(("%-10s: %11.1f%1s "):format("Steam", fluid[7] / fluid[8] * 100.0, "%"))
- return
- end
- -- Screen setup
- term.clear()
- local w = term.window.width
- local h = term.window.height
- -- Main
- while (true) do
- -- Battery data update
- if (battery_present) then
- battery_energy = {battery.getInfo()}
- battery_percentage = battery_energy[1] / battery_energy[2]
- -- Battery auto regulation
- if (activation) then
- -- Max reached
- if (battery_percentage >= max) then
- stop_all(turbines)
- end
- -- Min reached
- if (battery_percentage <= min) then
- start_all(turbines)
- end
- end
- else
- battery_percentage = -0.01
- end
- -- Key acquisition
- key = {event.pull(0.05, "key")}
- -- Input acquisition
- if (key[3] ~= nil) then
- if (key[1] == "key_down") then
- if (key[3] == 0) then
- -- Reset
- input = ""
- elseif (key[3] == 8) then
- -- Backspace
- input = input:sub(1, -2)
- elseif (key[3] >= 32 and key[3] <= 126) then
- -- Characters input
- input = input .. string.char(key[3])
- elseif (key[3] == 13) then
- -- Lowers input characters and returns and keywords segmentation
- input = string.lower(input)
- keywords = segment(input)
- input = ""
- term.setCursor(x, y)
- for i=y,h-1,1 do
- screen_divider(" ")
- end
- term.setCursor(x, y)
- if (keywords[1] == "set") then
- turbine_set = tonumber(keywords[3])
- if (type(turbine_set) == "number") then
- if (keywords[2] == "max" and turbine_set > min) then
- max = (turbine_set > 1.0) and 1.0 or turbine_set
- print("Changed Max ESBSR setting to " .. max)
- elseif (keywords[2] == "min" and turbine_set < max) then
- min = (turbine_set < 0.0) and 0.0 or turbine_set
- print("Changed Min ESBSR setting to " .. min)
- elseif (keywords[2] == "throttle") then
- turbine_index = tonumber(keywords[4])
- if (keywords[4] == "all") then
- for i=1,#turbines,1 do
- turbines[i].setThrottle(turbine_set)
- end
- print(("Changed throttle to %.1f%1s for %2d turbines"):format(turbine_set, "%", #turbines))
- elseif (type(turbine_index) == "number") then
- if (turbine_index > 0 and turbine_index <= #turbines) then
- turbines[turbine_index].setThrottle(turbine_set)
- print(("Changed throttle to %.1f%1s for turbine n. %2d"):format(turbine_set, "%", turbine_index))
- end
- end
- else
- print("Usage:")
- print("'-max' -> Sets the maximum percentage of the ESBSR to stop. Needs a decimal number min < x <= 1")
- print("'-min' -> Sets the minimum percentage of the ESBSR to start. Needs a decimal number 0 <= x < max")
- print("'-throttle' -> Sets the throttle of the turbine selected. 'all' for all, 1<=x<=n for one")
- print("'-auto' -> Sets auto control for the turbine. 'all' for all, 0 <= x <= n for one")
- end
- elseif (keywords[2] == "auto") then
- turbine_index = tonumber(keywords[4])
- if (keywords[4] == "all") then
- for i=1,#turbines,1 do
- if (keywords[3] == "true") then
- auto[i] = true
- elseif (keywords[3] == "false") then
- auto[i] = false
- end
- end
- print(("Set AUTO to %5s for %2d turbines "):format(keywords[3], #turbines))
- elseif (type(turbine_index) == "number") then
- if (turbine_index > 0 and turbine_index <= #turbines) then
- if (keywords[3] == "true") then
- auto[turbine_index] = true
- elseif (keywords[3] == "false") then
- auto[turbine_index] = false
- end
- print(("Set AUTO to %5s for turbine n. %2d "):format(keywords[3], turbine_index))
- end
- end
- end
- elseif (keywords[1] == "get") then
- if (keywords[2] == "turbine") then
- turbine_index = tonumber(keywords[3])
- if (keywords[3] == "all") then
- for i=1,#turbines,1 do
- print_turbine(turbines[i], i)
- screen_divider("-")
- end
- elseif (type(turbine_index) == "number") then
- if (turbine_index > 0 and turbine_index <= #turbines) then
- print_turbine(turbines[turbine_index], turbine_index)
- end
- end
- elseif (keywords[2] == "auto") then
- turbine_index = tonumber(keywords[3])
- if (keywords[3] == "all") then
- for i=1,#turbines,1 do
- print(("Turbine n. %2d -> AUTO: %5s"):format(i, auto[i] and "true" or "false"))
- end
- elseif (type(turbine_index) == "number") then
- if (turbine_index > 0 and turbine_index <= #turbines) then
- print(("Turbine n. %2d -> AUTO: %5s"):format(turbine_index, auto[turbine_index] and "true" or "false"))
- end
- end
- end
- elseif (keywords[1] == "clear") then
- print()
- elseif (keywords[1] == "start") then
- turbine_index = tonumber(keywords[2])
- if (keywords[2] == "all") then
- start_all(turbines)
- print("Started " .. #turbines .. " turbines")
- elseif (type(turbine_index) == "number") then
- if (turbine_index > 0 and turbine_index <= #turbines) then
- turbines[turbine_index].start()
- print("Turbine n. " .. turbine_index .. " started")
- end
- else
- print("Usage:")
- print("'-all' -> Starts all turbines")
- print("'-n' -> Replace 'n' to start the desired turbine")
- print("Only offline turbines will be started")
- end
- elseif (keywords[1] == "stop") then
- activation = false
- turbine_index = tonumber(keywords[2])
- if (keywords[2] == "all") then
- stop_all(turbines)
- print("Stopped " .. #turbines .. " turbines")
- elseif (type(turbine_index) == "number") then
- if (turbine_index > 0 and turbine_index <= #turbines) then
- turbines[turbine_index].stop()
- print("Turbine n. " .. turbine_index .. " stopped")
- end
- else
- print("Usage:")
- print("'-all' -> Stops all turbines")
- print("'-n' -> Replace 'n' to stop the desired turbine")
- end
- elseif (keywords[1] == "shutdown") then
- activation = false
- print("Disabled ESBSR")
- elseif (keywords[1] == "startup") then
- activation = true
- print("Enabled ESBSR")
- elseif (keywords[1] == "exit") then
- stop_all(turbines)
- exit(0)
- elseif (keywords[1] == "help") then
- print("'get' -> Gets data from the system")
- print("'set' -> Sets data in the system")
- print("'start' -> Starts turbines")
- print("'stop' -> Stops turbines")
- print("'startup' -> Enables ESBSR control")
- print("'shutdown' -> Disables ESBSR control")
- print("(ESBSR = 'Energy Storage Block Self-Regulation')")
- print("'clear' -> Clears output")
- print("'exit' -> Exits the program and deactivates the turbines")
- print("'help' -> Shows this commands help list")
- print("Type only one of these keywords to get more info")
- else
- print("Not known command. Try 'help' for help.")
- end
- end
- end
- end
- term.setCursor(1, 3)
- io.write("> " .. input)
- screen_divider(" ")
- -- Auto regulation for turbines
- for i=1,#turbines,1 do
- turbines[i].setAuto(auto[i])
- end
- -- Screen update
- if (math.abs(time("%M") - temp[1]) >= 1) then
- term.setCursor(1, 1)
- print(("Heartbeat: %1s | Energy # Max: %5.1f%1s / Min: %5.1f%1s / Now: %5.1f%1s | ESBSR: %3s "):format(blink and "*" or " ", max * 100.0, "%", min * 100.0, "%", battery_percentage * 100.0, "%", activation and "ON" or "OFF"))
- screen_divider("=")
- term.setCursor(1, 4)
- screen_divider("=")
- if (not battery_present) then
- gpu.setForeground(0xFFE600)
- print("Warning: Energy Storage Block missing. Grid setups will be ignored.")
- gpu.setForeground(0xFFFFFF)
- end
- x, y = term.getCursor()
- -- Variables update
- temp[1] = time("%M")
- blink = not blink
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement