Advertisement
AlexMastang

OC-HBM Computer Controlled Combined Cycle Turbine Program (CCCCTP)

May 17th, 2025 (edited)
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.48 KB | None | 0 0
  1. -- Made by Alexmaster75
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5. local term = require("term")
  6.  
  7. term.clear()
  8. print("Loading...")
  9.  
  10. -- Peripherals
  11. local gpu = component.gpu
  12. local battery = {}
  13. local battery_present = true
  14. local c = 1
  15. for add, n in component.list("ntm_energy_storage") do
  16.   battery[c] = component.proxy(add)
  17.   c = c + 1
  18. end
  19. if (#battery >= 1) then
  20.   battery = battery[1]
  21. else
  22.   battery = nil
  23.   battery_present = false
  24. end
  25. local turbines = {}
  26. local c = 1
  27. for add, n in component.list("ntm_gas_turbine") do
  28.   turbines[c] = component.proxy(add)
  29.   c = c + 1
  30. end
  31.  
  32. -- Global variables
  33. local delta_program = 0.1
  34. local max = 0.8
  35. local min = 0.2
  36.  
  37. local temp = {}
  38. for i=1,1,1 do
  39.   temp[i] = 0
  40. end
  41. local battery_energy = {}
  42. local battery_percentage = 0.0
  43. local blink = true
  44. local key = {}
  45. local input = ""
  46. local auto = {}
  47. for i=1,#turbines,1 do
  48.   auto[i] = true
  49. end
  50. local activation = false
  51. local turbine_index = 0
  52. local turbine_set = 0
  53. local keywords = {}
  54. local x, y = term.getCursor()
  55.  
  56.  
  57. -- Scales a number by orders of magnitude
  58. local function x3_scale(n)
  59.   local scale = {" ", "k", "M", "G", "T"}
  60.   local num = n
  61.  
  62.   local i = 1
  63.   while (math.abs(num) >= 1000.0 and i < #scale) do
  64.     num = num / 1000.0
  65.     i = i + 1
  66.   end
  67.  
  68.   return (("%5.1f %1s"):format(num, scale[i]))
  69. end
  70.  
  71. -- Gets in-game time
  72. local function time(mask)
  73.   return tonumber(os.date(mask))
  74. end
  75.  
  76. -- Prints a divider given the character/string
  77. local function screen_divider(str)
  78.   local W = term.window.width
  79.   local X, Y = term.getCursor()
  80.  
  81.   for i=X,W-#str,#str do
  82.     term.write(str)
  83.   end
  84.   X, Y = term.getCursor()
  85.   if (X < W) then
  86.     for i=1,W-X+1,1 do
  87.       term.write(str:sub(i, i))
  88.     end
  89.   end
  90.   if (#str == 1) then
  91.     term.write(str)
  92.   end
  93.   print()
  94. end
  95.  
  96. -- Segments a string containing spaces
  97. local function segment(string)
  98.   local k_word = {}
  99.   local k = 1
  100.   local char = ""
  101.  
  102.   for i=1,string:len(),1 do
  103.     char = string:sub(i,i)
  104.     if (char == " " or char == nil) then
  105.       k = k + 1
  106.     else
  107.       if (k_word[k] == nil) then k_word[k] = "" end
  108.       k_word[k] = k_word[k] .. char
  109.     end
  110.   end
  111.  
  112.   return k_word
  113. end
  114.  
  115. -- Starts all turbines
  116. local function start_all(turbine_array)
  117.   local fluid = {}
  118.   local fuel = false
  119.  
  120.   for i=1,#turbine_array,1 do
  121.     fluid = {turbines[i].getFluid()}
  122.     fuel = (fluid[1] / fluid[2] > 0.0 and fluid[3] / fluid[4] > 0.0)
  123.    
  124.     if (turbine_array[i].getState() == 0 and fuel) then
  125.       turbine_array[i].start()
  126.     end
  127.   end
  128.  
  129.   return
  130. end
  131.  
  132. -- Stops all turbines
  133. local function stop_all(turbine_array)
  134.   for i=1,#turbine_array,1 do
  135.     turbine_array[i].stop()
  136.   end
  137.  
  138.   return
  139. end
  140.  
  141. -- Prints a single turbine stats
  142. local function print_turbine(turbine, index)
  143.   local fluid = {turbine.getFluid()}
  144.   local energy = turbine.getPower()
  145.   local num_status = turbine.getState()
  146.   local status = ""
  147.  
  148.   if (num_status == -1) then
  149.     status = "Starting..."
  150.   elseif (num_status == 0) then
  151.     status = "Offline"
  152.   elseif (num_status == 1) then
  153.     status = "Online"
  154.   else
  155.     status = "Error"
  156.   end
  157.  
  158.   print(("%-10s: %11d "):format("Turbine n.", index))
  159.   print(("%-10s: %11s "):format("Fuel type", turbine.getType()))
  160.   print(("%-10s: %11s"):format("Status", status))
  161.   print(("%-10s: %11d "):format("RPM", turbine.getThrottle()))
  162.   print(("%-10s: %11.1f%1s "):format("Fuel", fluid[1] / fluid[2] * 100.0, "%"))
  163.   print(("%-10s: %11.1f%1s "):format("Lubricant", fluid[3] / fluid[4] * 100.0, "%"))
  164.   print(("%-10s: %11sHE "):format("Energy", x3_scale(energy)))
  165.   print(("%-10s: %11.1f%1s "):format("Water", fluid[5] / fluid[6] * 100.0, "%"))
  166.   print(("%-10s: %11.1f%1s "):format("Steam", fluid[7] / fluid[8] * 100.0, "%"))
  167.  
  168.   return
  169. end
  170.  
  171.  
  172. -- Screen setup
  173. term.clear()
  174. local w = term.window.width
  175. local h = term.window.height
  176. -- Main
  177. while (true) do
  178.   -- Battery data update
  179.   if (battery_present) then
  180.     battery_energy = {battery.getInfo()}
  181.     battery_percentage = battery_energy[1] / battery_energy[2]
  182.    
  183.     -- Battery auto regulation
  184.     if (activation) then
  185.       -- Max reached
  186.       if (battery_percentage >= max) then
  187.         stop_all(turbines)
  188.       end
  189.       -- Min reached
  190.       if (battery_percentage <= min) then
  191.         start_all(turbines)
  192.       end
  193.     end
  194.   else
  195.     battery_percentage = -0.01
  196.   end
  197.  
  198.   -- Key acquisition
  199.   key = {event.pull(0.05, "key")}
  200.  
  201.   -- Input acquisition
  202.   if (key[3] ~= nil) then
  203.     if (key[1] == "key_down") then
  204.       if (key[3] == 0) then
  205.         -- Reset
  206.         input = ""
  207.       elseif (key[3] == 8) then
  208.         -- Backspace
  209.         input = input:sub(1, -2)
  210.       elseif (key[3] >= 32 and key[3] <= 126) then
  211.         -- Characters input
  212.         input = input .. string.char(key[3])
  213.       elseif (key[3] == 13) then
  214.         -- Lowers input characters and returns and keywords segmentation
  215.         input = string.lower(input)
  216.         keywords = segment(input)
  217.         input = ""
  218.        
  219.         term.setCursor(x, y)
  220.         for i=y,h-1,1 do
  221.           screen_divider("      ")
  222.         end
  223.         term.setCursor(x, y)
  224.         if (keywords[1] == "set") then
  225.           turbine_set = tonumber(keywords[3])
  226.          
  227.           if (type(turbine_set) == "number") then
  228.             if (keywords[2] == "max" and turbine_set > min) then
  229.               max = (turbine_set > 1.0) and 1.0 or turbine_set
  230.               print("Changed Max ESBSR setting to " .. max)
  231.             elseif (keywords[2] == "min" and turbine_set < max) then
  232.               min = (turbine_set < 0.0) and 0.0 or turbine_set
  233.               print("Changed Min ESBSR setting to " .. min)
  234.             elseif (keywords[2] == "throttle") then
  235.               turbine_index = tonumber(keywords[4])
  236.              
  237.               if (keywords[4] == "all") then
  238.                 for i=1,#turbines,1 do
  239.                   turbines[i].setThrottle(turbine_set)
  240.                 end
  241.                 print(("Changed throttle to %.1f%1s for %2d turbines"):format(turbine_set, "%", #turbines))
  242.               elseif (type(turbine_index) == "number") then
  243.                 if (turbine_index > 0 and turbine_index <= #turbines) then
  244.                   turbines[turbine_index].setThrottle(turbine_set)
  245.                   print(("Changed throttle to %.1f%1s for turbine n. %2d"):format(turbine_set, "%", turbine_index))
  246.                 end
  247.               end
  248.             else
  249.               print("Usage:")
  250.               print("'-max' -> Sets the maximum percentage of the ESBSR to stop. Needs a decimal number min < x <= 1")
  251.               print("'-min' -> Sets the minimum percentage of the ESBSR to start. Needs a decimal number 0 <= x < max")
  252.               print("'-throttle' -> Sets the throttle of the turbine selected. 'all' for all, 1<=x<=n for one")
  253.               print("'-auto' -> Sets auto control for the turbine. 'all' for all, 0 <= x <= n for one")
  254.             end
  255.           elseif (keywords[2] == "auto") then
  256.             turbine_index = tonumber(keywords[4])
  257.            
  258.             if (keywords[4] == "all") then
  259.               for i=1,#turbines,1 do
  260.                 if (keywords[3] == "true") then
  261.                   auto[i] = true
  262.                 elseif (keywords[3] == "false") then
  263.                   auto[i] = false
  264.                 end
  265.               end
  266.               print(("Set AUTO to %5s for %2d turbines "):format(keywords[3], #turbines))
  267.             elseif (type(turbine_index) == "number") then
  268.               if (turbine_index > 0 and turbine_index <= #turbines) then
  269.                 if (keywords[3] == "true") then
  270.                   auto[turbine_index] = true
  271.                 elseif (keywords[3] == "false") then
  272.                   auto[turbine_index] = false
  273.                 end
  274.                 print(("Set AUTO to %5s for turbine n. %2d "):format(keywords[3], turbine_index))
  275.               end
  276.             end
  277.           end
  278.         elseif (keywords[1] == "get") then
  279.           if (keywords[2] == "turbine") then
  280.             turbine_index = tonumber(keywords[3])
  281.            
  282.             if (keywords[3] == "all") then
  283.               for i=1,#turbines,1 do
  284.                 print_turbine(turbines[i], i)
  285.                 screen_divider("-")
  286.               end
  287.             elseif (type(turbine_index) == "number") then
  288.               if (turbine_index > 0 and turbine_index <= #turbines) then
  289.                 print_turbine(turbines[turbine_index], turbine_index)
  290.               end
  291.             end
  292.           elseif (keywords[2] == "auto") then
  293.             turbine_index = tonumber(keywords[3])
  294.            
  295.             if (keywords[3] == "all") then
  296.               for i=1,#turbines,1 do
  297.                 print(("Turbine n. %2d -> AUTO: %5s"):format(i, auto[i] and "true" or "false"))
  298.               end
  299.             elseif (type(turbine_index) == "number") then
  300.               if (turbine_index > 0 and turbine_index <= #turbines) then
  301.                 print(("Turbine n. %2d -> AUTO: %5s"):format(turbine_index, auto[turbine_index] and "true" or "false"))
  302.               end
  303.             end
  304.           end
  305.         elseif (keywords[1] == "clear") then
  306.           print()
  307.         elseif (keywords[1] == "start") then
  308.           turbine_index = tonumber(keywords[2])
  309.           if (keywords[2] == "all") then
  310.             start_all(turbines)
  311.             print("Started " .. #turbines .. " turbines")
  312.           elseif (type(turbine_index) == "number") then
  313.             if (turbine_index > 0 and turbine_index <= #turbines) then
  314.               turbines[turbine_index].start()
  315.               print("Turbine n. " .. turbine_index .. " started")
  316.             end
  317.           else
  318.             print("Usage:")
  319.             print("'-all' -> Starts all turbines")
  320.             print("'-n' -> Replace 'n' to start the desired turbine")
  321.             print("Only offline turbines will be started")
  322.           end
  323.         elseif (keywords[1] == "stop") then
  324.           activation = false
  325.           turbine_index = tonumber(keywords[2])
  326.           if (keywords[2] == "all") then
  327.             stop_all(turbines)
  328.             print("Stopped " .. #turbines .. " turbines")
  329.           elseif (type(turbine_index) == "number") then
  330.             if (turbine_index > 0 and turbine_index <= #turbines) then
  331.               turbines[turbine_index].stop()
  332.               print("Turbine n. " .. turbine_index .. " stopped")
  333.             end
  334.           else
  335.             print("Usage:")
  336.             print("'-all' -> Stops all turbines")
  337.             print("'-n' -> Replace 'n' to stop the desired turbine")
  338.           end
  339.         elseif (keywords[1] == "shutdown") then
  340.           activation = false
  341.           print("Disabled ESBSR")
  342.         elseif (keywords[1] == "startup") then
  343.           activation = true
  344.           print("Enabled ESBSR")
  345.         elseif (keywords[1] == "exit") then
  346.           stop_all(turbines)
  347.           exit(0)
  348.         elseif (keywords[1] == "help") then
  349.           print("'get' -> Gets data from the system")
  350.           print("'set' -> Sets data in the system")
  351.           print("'start' -> Starts turbines")
  352.           print("'stop' -> Stops turbines")
  353.           print("'startup' -> Enables ESBSR control")
  354.           print("'shutdown' -> Disables ESBSR control")
  355.           print("(ESBSR = 'Energy Storage Block Self-Regulation')")
  356.           print("'clear' -> Clears output")
  357.           print("'exit' -> Exits the program and deactivates the turbines")
  358.           print("'help' -> Shows this commands help list")
  359.           print("Type only one of these keywords to get more info")
  360.         else
  361.           print("Not known command. Try 'help' for help.")
  362.         end
  363.       end
  364.     end
  365.   end
  366.   term.setCursor(1, 3)
  367.   io.write("> " .. input)
  368.   screen_divider(" ")
  369.  
  370.   -- Auto regulation for turbines
  371.   for i=1,#turbines,1 do
  372.     turbines[i].setAuto(auto[i])
  373.   end
  374.  
  375.   -- Screen update
  376.   if (math.abs(time("%M") - temp[1]) >= 1) then
  377.     term.setCursor(1, 1)
  378.     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"))
  379.     screen_divider("=")
  380.     term.setCursor(1, 4)
  381.     screen_divider("=")
  382.     if (not battery_present) then
  383.       gpu.setForeground(0xFFE600)
  384.       print("Warning: Energy Storage Block missing. Grid setups will be ignored.")
  385.       gpu.setForeground(0xFFFFFF)
  386.     end
  387.     x, y = term.getCursor()
  388.    
  389.     -- Variables update
  390.     temp[1] = time("%M")
  391.     blink = not blink
  392.   end
  393. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement