Advertisement
themath1234

[Minecraft] [ComputerCraft] Control Fission Reactor

Jun 10th, 2023 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.33 KB | Software | 0 0
  1. local reactor
  2. local mon
  3. local alarmPos = 'back'
  4. local monX
  5. local monY
  6. local pathConfig = '/config/reactor.cfg'
  7. local burnRate = 0
  8.  
  9. -------------------FORMATTING-------------------------------
  10. term.clear()
  11.  
  12. function clear()
  13.   mon.setBackgroundColor(colors.black)
  14.   mon.clear()
  15.   mon.setCursorPos(1,1)
  16. end
  17.  
  18. --display text on computer's terminal screen
  19. function draw_text_term(x, y, text, text_color, bg_color)
  20.   term.setTextColor(text_color)
  21.   term.setBackgroundColor(bg_color)
  22.   term.setCursorPos(x,y)
  23.   write(text)
  24. end
  25.  
  26. --display text text on monitor, "mon" peripheral
  27. function draw_text(x, y, text, text_color, bg_color)
  28.   mon.setBackgroundColor(bg_color)
  29.   mon.setTextColor(text_color)
  30.   mon.setCursorPos(x,y)
  31.   mon.write(text)
  32. end
  33.  
  34. --draw line on computer terminal
  35. function draw_line(x, y, length, color)
  36.     mon.setBackgroundColor(color)
  37.     mon.setCursorPos(x,y)
  38.     mon.write(string.rep(" ", length))
  39. end
  40.  
  41. --draw line on computer terminal
  42. function draw_line_term(x, y, length, color)
  43.     term.setBackgroundColor(color)
  44.     term.setCursorPos(x,y)
  45.     term.write(string.rep(" ", length))
  46. end
  47.  
  48. --main line of bar_color as a percentage of minVal/maxVal
  49. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  50.   draw_line(x, y, length, bg_color) --background bar
  51.   local barSize = math.floor((minVal/maxVal) * length)
  52.   draw_line(x, y, barSize, bar_color) --progress so far
  53. end
  54.  
  55. --same as above but on the computer terminal
  56. function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  57.   draw_line_term(x, y, length, bg_color) --background bar
  58.   local barSize = math.floor((minVal/maxVal) * length)
  59.   draw_line_term(x, y, barSize, bar_color)  --progress so far
  60. end
  61.  
  62. ----------------------------------------------------------
  63.  
  64. -- search for monitor and return it
  65. function monitorSearch()
  66.    local names = peripheral.getNames()
  67.    local i, name
  68.    for i, name in pairs(names) do
  69.       if peripheral.getType(name) == "monitor" then
  70.         test = name
  71.          return peripheral.wrap(name)
  72.       else
  73.          --return null
  74.       end
  75.    end
  76. end
  77.  
  78. -- search for reactor and return it
  79. function reactorSearch()
  80.     local names = peripheral.getNames()
  81.     local i, name
  82.     for i, name in pairs(names) do
  83.         if peripheral.getType(name) == "fissionReactorLogicAdapter" then
  84.             return peripheral.wrap(name)
  85.         end
  86.     end
  87. end
  88.  
  89. ----- REACTOR SETUP -----------------------------------
  90.  
  91. reactor = reactorSearch()
  92. mon = monitorSearch()
  93.  
  94. function operationMonitor()
  95.    if mon == null then
  96.       draw_text_term(1, 7, "Error:", colors.red, colors.black)
  97.       draw_text_term(1, 8, "Could not connect to a monitor. Place a 3x3 advanced monitor", colors.red, colors.black)
  98.       draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
  99.       wait = read()
  100.       setup_wizard()
  101.   else
  102.       monX, monY = mon.getSize()
  103.       draw_text_term(27, 6, "success", colors.lime, colors.black)
  104.       sleep(0.5)
  105.   end
  106. end
  107.  
  108. -- Enable or disable reactor - true = ON | false = OFF
  109. function enableReactor(state)
  110.   if not reactor then
  111.     return false
  112.   end
  113.  
  114.   local minPercentageToStart = 20 -- 20% minimum
  115.   local currentWastePercentage = math.floor(reactor.getWasteFilledPercentage() * 100)
  116.   local coolantFilled = math.floor(reactor.getCoolantFilledPercentage()* 100)
  117.   local fuelFilled = math.floor(reactor.getFuelFilledPercentage()* 100)
  118.   local currentTemp = math.floor(reactor.getTemperature())
  119.  
  120.   -- Condições de segurança para ativação
  121.   local canOperate = coolantFilled >= minPercentageToStart and fuelFilled >= minPercentageToStart and currentWastePercentage <= 90 and currentTemp < 800
  122.  
  123.   -- Condições de desligamento de emergência
  124.   local emergencyShutdown = currentTemp >= 1000 or
  125.                            currentWastePercentage > 90 or
  126.                            coolantFilled < minPercentageToStart or
  127.                            fuelFilled < minPercentageToStart
  128.  
  129.   local currentStateReactor = reactor.getStatus()
  130.  
  131.   if state and canOperate then
  132.     if not currentStateReactor then
  133.       reactor.activate()
  134.       return true
  135.     end
  136.   elseif emergencyShutdown or not state then
  137.     if currentStateReactor then
  138.       reactor.scram()
  139.       return false
  140.     end
  141.   end
  142.  
  143.   return currentStateReactor
  144. end
  145.  
  146. function controlBurnRate()
  147.   if not reactor then
  148.     return
  149.   end
  150.  
  151.   local currentTemp = math.floor(reactor.getTemperature())
  152.   local currentPercentDamage = reactor.getDamagePercent()
  153.   local targetBurnRate = burnRate -- valor padrão
  154.  
  155.   if currentTemp >= 1300 or currentPercentDamage >= 30 then
  156.     targetBurnRate = 0
  157.   elseif currentTemp >= 1200 or currentPercentDamage >= 20 then
  158.     targetBurnRate = math.min(10, burnRate)
  159.   elseif currentTemp >= 1150 or currentPercentDamage >= 10 then
  160.     targetBurnRate = math.min(20, burnRate)
  161.   elseif currentTemp >= 1000 or currentPercentDamage >= 5 then
  162.     targetBurnRate = math.min(30, burnRate)
  163.   end
  164.  
  165.   if reactor.getActualBurnRate() ~= targetBurnRate then
  166.     reactor.setBurnRate(targetBurnRate)
  167.   end
  168. end
  169.  
  170. function waste()
  171.   if not reactor then
  172.     return
  173.   end
  174.  
  175.   local currentPercentageWaste = math.floor(reactor.getWasteFilledPercentage() * 100)
  176.   local currentStateReactor = reactor.getStatus()
  177.  
  178.   if currentPercentageWaste >= 90 and currentStateReactor then
  179.     reactor.scram()
  180.     return false
  181.   end
  182.  
  183.   return true
  184. end
  185.  
  186. -- Loading config
  187. function loadConfig()
  188.   local config = {}
  189.   sr = fs.open(pathConfig, "r")
  190.     burnRate = tonumber(sr.readLine())
  191.   sr.close()
  192. end
  193.  
  194.  
  195. -- Set burn rate
  196. function setBurnRate(rate)
  197.   reactor.setBurnRate(rate)
  198. end
  199.  
  200.  
  201. -- Waste control  
  202. function waste()
  203.   if not reactor then
  204.     return
  205.   end
  206.  
  207.   local currentPercentageWaste = math.floor(reactor.getWasteFilledPercentage() * 100)
  208.   local currentStateReactor = reactor.getStatus()
  209.  
  210.   if currentPercentageWaste >= 90 and currentStateReactor then
  211.     reactor.scram()
  212.     return false
  213.   end
  214.  
  215.   return true
  216. end
  217.  
  218. -- Enable or disable siren melt alert
  219. function meltAlert(state)
  220.   redstone.setOutput(alarmPos, state)
  221. end
  222.  
  223. enableReactor(false) -- Reactor OFF
  224.  
  225. -- Reactor monitoring
  226. function homepage()
  227.    while true do
  228.     if not reactor then
  229.       draw_text_term(1, 1, "Error: Reactor not found!", colors.red, colors.black)
  230.       sleep(1)
  231.       return
  232.     end
  233.  
  234.     local currentStateReactor = reactor.getStatus()
  235.     local currentPercentageFuel = math.floor(reactor.getFuelFilledPercentage() * 100)
  236.     local currentPercentageCoolant = math.floor(reactor.getCoolantFilledPercentage() * 100)
  237.     local currentPercentageWaste = math.floor(reactor.getWasteFilledPercentage() * 100)
  238.     local currentTemp = math.floor(reactor.getTemperature())
  239.     local currentPercentDamage = reactor.getDamagePercent()
  240.     local tempVariable = 950
  241.     local shouldBeRunning = currentTemp < tempVariable
  242.     local shouldBeOff = currentTemp >= tempVariable
  243.  
  244.     if shouldBeRunning then
  245.       enableReactor(true)
  246.     elseif shouldBeOff then
  247.       enableReactor(false)
  248.     end
  249.  
  250.     -- Control burn rate
  251.     controlBurnRate()
  252.     -- Waste control
  253.     waste()
  254.  
  255.     -- Melt Alert
  256.     if currentTemp >= 1200 or currentPercentDamage >= 30 then
  257.       meltAlert(true)
  258.  
  259.       draw_text(1,14,'Melt Alert!!!', colors.red, colors.black)
  260.       draw_text_term(1,14,'Melt Alert!!!', colors.red, colors.black)
  261.     else
  262.       meltAlert(false)
  263.     end
  264.  
  265.    
  266.     -- Display Status
  267.     local stateReactorStr = currentStateReactor and 'ONLINE' or 'OFFLINE'
  268.  
  269.     term.clear()
  270.     clear()
  271.    
  272.     draw_line(1, 1, monX, currentStateReactor and colors.lime or colors.red)
  273.     draw_line_term(1, 1, 55, currentStateReactor and colors.lime or colors.red)
  274.  
  275.     draw_text_term(2,3,'State:', colors.lime, colors.black)
  276.     draw_text_term(9,3,stateReactorStr, colors.white, colors.black)
  277.     draw_text(2,3,'State:', colors.lime, colors.black)
  278.     draw_text(9,3,stateReactorStr, colors.white, colors.black)
  279.  
  280.     draw_text_term(2,4,'Fuel Level:', colors.lime, colors.black)
  281.     draw_text_term(14,4,currentPercentageFuel ..'%', colors.white, colors.black)
  282.     draw_text(2, 4, "Fuel Level:", colors.lime, colors.black)
  283.     draw_text(14, 4, currentPercentageFuel ..'%', colors.white, colors.black)
  284.  
  285.     draw_text_term(2,5,'Coolant:', colors.lime, colors.black)
  286.     draw_text_term(11,5,currentPercentageCoolant .. '%', colors.white, colors.black)
  287.     draw_text(2, 5, "Coolant:", colors.lime, colors.black)
  288.     draw_text(11, 5, currentPercentageCoolant .. '%', colors.white, colors.black)
  289.  
  290.     draw_text_term(2,6,'Temp:', colors.lime, colors.black)
  291.     draw_text_term(8,6,currentTemp .. 'K', colors.white, colors.black)
  292.     draw_text(2, 6, "Temp:", colors.lime, colors.black)
  293.     draw_text(8, 6, currentTemp .. 'K', colors.white, colors.black)
  294.  
  295.     draw_text_term(2,7,'Damage:', colors.lime, colors.black)
  296.     draw_text_term(10,7,currentPercentDamage..'%', colors.white, colors.black)
  297.     draw_text(2, 7, "Damage:", colors.lime, colors.black)
  298.     draw_text(10, 7, currentPercentDamage..'%', colors.white, colors.black)
  299.  
  300.     draw_text_term(2,8,'Burn Rate:', colors.lime, colors.black)
  301.     draw_text_term(13,8,reactor.getActualBurnRate()  ..' mB/t', colors.white, colors.black)
  302.     draw_text(2, 8, "Burn Rate:", colors.lime, colors.black)
  303.     draw_text(13, 8, reactor.getActualBurnRate() ..' mB/t', colors.white, colors.black)
  304.  
  305.     draw_text_term(2,9,'Heating Rate:', colors.lime, colors.black)
  306.     draw_text_term(16,9,reactor.getHeatingRate() ..' mB/t', colors.white, colors.black)
  307.     draw_text(2, 9, "Heating Rate:", colors.lime, colors.black)
  308.     draw_text(16, 9, reactor.getHeatingRate() ..' mB/t', colors.white, colors.black)
  309.  
  310.     draw_text_term(2,10,'Waster Level:', colors.lime, colors.black)
  311.     draw_text_term(16,10,currentPercentageWaste ..'%', colors.white, colors.black)
  312.     draw_text(2, 10, "Waster Level:", colors.lime, colors.black)
  313.     draw_text(16, 10, currentPercentageWaste ..'%', colors.white, colors.black)
  314.  
  315.     sleep(0.5)
  316.   end
  317. end
  318.  
  319. -- Inicialização
  320. function initialize()
  321.   reactor = reactorSearch()
  322.   if not reactor then
  323.     error("No reactor found!")
  324.     return false
  325.   end
  326.  
  327.   mon = monitorSearch()
  328.   loadConfig()
  329.   operationMonitor()
  330.  
  331.   -- Começa com o reator desligado por segurança
  332.   enableReactor(false)
  333.  
  334.   return true
  335. end
  336.  
  337. -- Start the program
  338. if initialize() then
  339.   homepage()
  340. end
Tags: minecraft lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement