Advertisement
AlexMastang

ic2-oc prg

Nov 23rd, 2023 (edited)
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.64 KB | None | 0 0
  1. local term = require("term")
  2. local component = require("component")
  3. local sides = require("sides")
  4. local shell = require("shell")
  5. local reactor = component.reactor
  6. local battery = component.ic2_te_cesu
  7. local rs = component.redstone
  8.  
  9. local cfg_n = "cfg.txt"
  10. local cfg
  11. local data = {}
  12. local reboot = 1
  13. local max_energy = 0.8
  14. local min_energy = 0.2
  15. local heat_thrsh = 0.1
  16. local rc_side = 4
  17. local st_side = 5
  18. local max_heat = 0.0
  19. local status = false
  20. local capacity = 0.0
  21. local bt_prc = 0.0
  22. local x
  23. local y
  24.  
  25. local t = 0.0
  26. local t_max = 10.0
  27. local ta = 0.1
  28.  
  29.  
  30. -- creates a divider printing
  31. local function divider(c)
  32.     for i=1,term.window.width do
  33.         term.write(c)
  34.     end
  35.     print()
  36. end
  37.  
  38. -- checks the existence of the given file
  39. local function file_exists(file)
  40.     local f = io.open(file, "rb")
  41.     if (f) then f:close() end
  42.     return f ~= nil
  43. end
  44.  
  45. -- gets all the lines from the given file
  46. local function lines_from(file)
  47.     local lines = {}
  48.     for line in io.lines(file) do
  49.         lines[#lines + 1] = line
  50.     end
  51.     return lines
  52. end
  53.  
  54. -- edits a specific line of the given file
  55. local function edit_line(inputFile, n, k)
  56.     local fileContent = {}
  57.     fileContent = lines_from(inputFile)
  58.  
  59.     fileContent[n] = k
  60.  
  61.     local f = io.open(inputFile, "w")
  62.     for i=1,#fileContent do
  63.         f:write(fileContent[i] .. "\n")
  64.     end
  65.     f:close()
  66. end
  67.  
  68. -- gets the real time in seconds
  69. local function rtime()
  70.     local ts = os.date("%X")
  71.     return ts
  72. end
  73.  
  74.  
  75. term.clear()
  76. term.setCursor(1, 1)
  77. divider("=")
  78. print(("[%s] Checking for a response from the peripherals.."):format(rtime()))
  79. x, y = term.getCursor()
  80. while (not (reactor and battery)) do
  81.     if (t > t_max) then
  82.         error("Too long without a response")
  83.     end
  84.     reactor = component.reactor
  85.     battery = component.ic2_te_cesu
  86.     term.setCursor(1, y)
  87.     print(("T: %.1f"):format(t))
  88.     t = t + 0.1
  89.     os.sleep(0.1)
  90. end
  91. print(("[%s] Done!"):format(rtime()))
  92.  
  93. -- if the program has arrived at this point then all the components are working properly
  94. -- but there's still a chance of not being truly online, so it waits for a valid response
  95. print(("[%s] Checking if it is online.."):format(rtime()))
  96. x, y = term.getCursor()
  97. t = 0.0
  98. while (reactor.getMaxHeat() == nil and battery.getCapacity() == nil) do
  99.     if (t > t_max) then
  100.         error("Too long without a response")
  101.     end
  102.     term.setCursor(1, y)
  103.     print(("T: %.1f"):format(t))
  104.     t = t + 0.1
  105.     os.sleep(0.1)
  106. end
  107. print(("[%s] Done!"):format(rtime()))
  108.  
  109. print(("[%s] Checking the config file.."):format(rtime()))
  110. -- reads (or creates is it doesn't exist) the config file
  111. if (not file_exists(cfg_n)) then
  112.     -- creates it
  113.     print(("[%s] Config file didn't found"):format(rtime()))
  114.     print(("[%s] Creating it.."):format(rtime()))
  115.     cfg = io.open(cfg_n, "w")
  116.     cfg:write(reboot .. "\n" .. max_energy .. "\n" .. min_energy .. "\n" .. heat_thrsh .. "\n" .. ta .. "\n" .. rc_side .. "\n" .. st_side)
  117.     cfg:close()
  118. else
  119.     -- reads it
  120.     print(("[%s] Config file found"):format(rtime()))
  121.     print(("[%s] Reading it.."):format(rtime()))
  122.     data = lines_from(cfg_n)
  123.     reboot = tonumber(data[1])
  124.     max_energy = tonumber(data[2])
  125.     min_energy = tonumber(data[3])
  126.     heat_thrsh = tonumber(data[4])
  127.     ta = tonumber(data[5])
  128.     rc_side = tonumber(data[6])
  129.     st_side = tonumber(data[7])
  130. end
  131. print(("[%s] Done!"):format(rtime()))
  132. -- then it restarts the entire program if necessary
  133. if (reboot ~= 0) then
  134.     edit_line(cfg_n, 1, 0)
  135.     shell.execute("reboot")
  136. end
  137.  
  138. print(("[%s] Setting up the values.."):format(rtime()))
  139.  
  140. max_heat = reactor.getMaxHeat()
  141. capacity = battery.getCapacity()
  142.  
  143. print(("[%s] All set!"):format(rtime()))
  144. print(("[%s] Program started"):format(rtime()))
  145.  
  146.  
  147. -- MAIN
  148. term.clear()
  149. print("IC2-OC PRG")
  150. divider("=")
  151. print(("> Min - Max Battery Energy: %d - %d%s"):format(min_energy*100, max_energy*100, "%"))
  152. divider("-")
  153. x, y = term.getCursor()
  154. while (true) do
  155.     status = reactor.producesEnergy()
  156.     bt_prc = battery.getEnergy() / capacity
  157.  
  158.     if (reactor.getHeat()/max_heat > heat_thrsh or rs.getInput(st_side) > 0) then
  159.         rs.setOutput(rc_side, 0)
  160.         break
  161.     end
  162.  
  163.     if (bt_prc > max_energy and status) then
  164.         rs.setOutput(rc_side, 0)
  165.     end
  166.     if (bt_prc < min_energy and not status) then
  167.         rs.setOutput(rc_side, 15)
  168.     end
  169.  
  170.     term.setCursor(1, y)
  171.     print(("Reactor Prod.: %d EU/t         "):format(reactor.getReactorEUOutput()))
  172.     print(("Battery Stor.: %.1f / %.1f kEU (%.1f%s)         "):format(battery.getEnergy()/1000, capacity/1000, bt_prc*100, "%"))
  173.    
  174.     os.sleep(ta)
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement