Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local term = require("term")
- local component = require("component")
- local sides = require("sides")
- local shell = require("shell")
- local reactor = component.reactor
- local battery = component.ic2_te_cesu
- local rs = component.redstone
- local cfg_n = "cfg.txt"
- local cfg
- local data = {}
- local reboot = 1
- local max_energy = 0.8
- local min_energy = 0.2
- local heat_thrsh = 0.1
- local rc_side = 4
- local st_side = 5
- local max_heat = 0.0
- local status = false
- local capacity = 0.0
- local bt_prc = 0.0
- local x
- local y
- local t = 0.0
- local t_max = 10.0
- local ta = 0.1
- -- creates a divider printing
- local function divider(c)
- for i=1,term.window.width do
- term.write(c)
- end
- print()
- end
- -- checks the existence of the given file
- local function file_exists(file)
- local f = io.open(file, "rb")
- if (f) then f:close() end
- return f ~= nil
- end
- -- gets all the lines from the given file
- local function lines_from(file)
- local lines = {}
- for line in io.lines(file) do
- lines[#lines + 1] = line
- end
- return lines
- end
- -- edits a specific line of the given file
- local function edit_line(inputFile, n, k)
- local fileContent = {}
- fileContent = lines_from(inputFile)
- fileContent[n] = k
- local f = io.open(inputFile, "w")
- for i=1,#fileContent do
- f:write(fileContent[i] .. "\n")
- end
- f:close()
- end
- -- gets the real time in seconds
- local function rtime()
- local ts = os.date("%X")
- return ts
- end
- term.clear()
- term.setCursor(1, 1)
- divider("=")
- print(("[%s] Checking for a response from the peripherals.."):format(rtime()))
- x, y = term.getCursor()
- while (not (reactor and battery)) do
- if (t > t_max) then
- error("Too long without a response")
- end
- reactor = component.reactor
- battery = component.ic2_te_cesu
- term.setCursor(1, y)
- print(("T: %.1f"):format(t))
- t = t + 0.1
- os.sleep(0.1)
- end
- print(("[%s] Done!"):format(rtime()))
- -- if the program has arrived at this point then all the components are working properly
- -- but there's still a chance of not being truly online, so it waits for a valid response
- print(("[%s] Checking if it is online.."):format(rtime()))
- x, y = term.getCursor()
- t = 0.0
- while (reactor.getMaxHeat() == nil and battery.getCapacity() == nil) do
- if (t > t_max) then
- error("Too long without a response")
- end
- term.setCursor(1, y)
- print(("T: %.1f"):format(t))
- t = t + 0.1
- os.sleep(0.1)
- end
- print(("[%s] Done!"):format(rtime()))
- print(("[%s] Checking the config file.."):format(rtime()))
- -- reads (or creates is it doesn't exist) the config file
- if (not file_exists(cfg_n)) then
- -- creates it
- print(("[%s] Config file didn't found"):format(rtime()))
- print(("[%s] Creating it.."):format(rtime()))
- cfg = io.open(cfg_n, "w")
- cfg:write(reboot .. "\n" .. max_energy .. "\n" .. min_energy .. "\n" .. heat_thrsh .. "\n" .. ta .. "\n" .. rc_side .. "\n" .. st_side)
- cfg:close()
- else
- -- reads it
- print(("[%s] Config file found"):format(rtime()))
- print(("[%s] Reading it.."):format(rtime()))
- data = lines_from(cfg_n)
- reboot = tonumber(data[1])
- max_energy = tonumber(data[2])
- min_energy = tonumber(data[3])
- heat_thrsh = tonumber(data[4])
- ta = tonumber(data[5])
- rc_side = tonumber(data[6])
- st_side = tonumber(data[7])
- end
- print(("[%s] Done!"):format(rtime()))
- -- then it restarts the entire program if necessary
- if (reboot ~= 0) then
- edit_line(cfg_n, 1, 0)
- shell.execute("reboot")
- end
- print(("[%s] Setting up the values.."):format(rtime()))
- max_heat = reactor.getMaxHeat()
- capacity = battery.getCapacity()
- print(("[%s] All set!"):format(rtime()))
- print(("[%s] Program started"):format(rtime()))
- -- MAIN
- term.clear()
- print("IC2-OC PRG")
- divider("=")
- print(("> Min - Max Battery Energy: %d - %d%s"):format(min_energy*100, max_energy*100, "%"))
- divider("-")
- x, y = term.getCursor()
- while (true) do
- status = reactor.producesEnergy()
- bt_prc = battery.getEnergy() / capacity
- if (reactor.getHeat()/max_heat > heat_thrsh or rs.getInput(st_side) > 0) then
- rs.setOutput(rc_side, 0)
- break
- end
- if (bt_prc > max_energy and status) then
- rs.setOutput(rc_side, 0)
- end
- if (bt_prc < min_energy and not status) then
- rs.setOutput(rc_side, 15)
- end
- term.setCursor(1, y)
- print(("Reactor Prod.: %d EU/t "):format(reactor.getReactorEUOutput()))
- print(("Battery Stor.: %.1f / %.1f kEU (%.1f%s) "):format(battery.getEnergy()/1000, capacity/1000, bt_prc*100, "%"))
- os.sleep(ta)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement