mrjan123

Untitled

May 25th, 2022 (edited)
376
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- modifiable variables
  2. local reactorSide = "back"
  3. local fluxgateSide = "right"
  4.  
  5. local targetStrength = 50
  6. local maxTemperature = 8000
  7. local safeTemperature = 3000
  8. local lowestFieldPercent = 15
  9.  
  10. local activateOnCharged = 1
  11.  
  12. -- please leave things untouched from here on
  13. os.loadAPI("lib/f")
  14.  
  15. local version = "0.25"
  16. -- toggleable via the monitor, use our algorithm to achieve our target field strength or let the user tweak it
  17. local autoInputGate = 1
  18. local curInputGate = 222000
  19.  
  20. -- monitor
  21. local mon, monitor, monX, monY
  22.  
  23. -- peripherals
  24. local reactor
  25. local fluxgate
  26. local inputfluxgate
  27.  
  28. -- reactor information
  29. local ri
  30.  
  31. -- last performed action
  32. local action = "None since reboot"
  33. local emergencyCharge = false
  34. local emergencyTemp = false
  35.  
  36. monitor = f.periphSearch("monitor")
  37. inputfluxgate = f.periphSearch("flux_gate")
  38. fluxgate = peripheral.wrap(fluxgateSide)
  39. reactor = peripheral.wrap(reactorSide)
  40.  
  41. if monitor == null then
  42.     error("No valid monitor was found")
  43. end
  44.  
  45. if fluxgate == null then
  46.     error("No valid fluxgate was found")
  47. end
  48.  
  49. if reactor == null then
  50.     error("No valid reactor was found")
  51. end
  52.  
  53. if inputfluxgate == null then
  54.     error("No valid flux gate was found")
  55. end
  56.  
  57. monX, monY = monitor.getSize()
  58. mon = {}
  59. mon.monitor,mon.X, mon.Y = monitor, monX, monY
  60.  
  61. --write settings to config file
  62. function save_config()
  63.   sw = fs.open("config.txt", "w")  
  64.   sw.writeLine(version)
  65.   sw.writeLine(autoInputGate)
  66.   sw.writeLine(curInputGate)
  67.   sw.close()
  68. end
  69.  
  70. --read settings from file
  71. function load_config()
  72.   sr = fs.open("config.txt", "r")
  73.   version = sr.readLine()
  74.   autoInputGate = tonumber(sr.readLine())
  75.   curInputGate = tonumber(sr.readLine())
  76.   sr.close()
  77. end
  78.  
  79.  
  80. -- 1st time? save our settings, if not, load our settings
  81. if fs.exists("config.txt") == false then
  82.   save_config()
  83. else
  84.   load_config()
  85. end
  86.  
  87. function buttons()
  88.  
  89.   while true do
  90.     -- button handler
  91.     event, side, xPos, yPos = os.pullEvent("monitor_touch")
  92.  
  93.     -- output gate controls
  94.     -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
  95.     -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
  96.     if yPos == 8 then
  97.       local cFlow = fluxgate.getSignalLowFlow()
  98.       if xPos >= 2 and xPos <= 4 then
  99.         cFlow = cFlow-1000
  100.       elseif xPos >= 6 and xPos <= 9 then
  101.         cFlow = cFlow-10000
  102.       elseif xPos >= 10 and xPos <= 12 then
  103.         cFlow = cFlow-100000
  104.       elseif xPos >= 17 and xPos <= 19 then
  105.         cFlow = cFlow+100000
  106.       elseif xPos >= 21 and xPos <= 23 then
  107.         cFlow = cFlow+10000
  108.       elseif xPos >= 25 and xPos <= 27 then
  109.         cFlow = cFlow+1000
  110.       end
  111.       fluxgate.setSignalLowFlow(cFlow)
  112.     end
  113.  
  114.     -- input gate controls
  115.     -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
  116.     -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
  117.     if yPos == 10 and autoInputGate == 0 and xPos ~= 14 and xPos ~= 15 then
  118.       if xPos >= 2 and xPos <= 4 then
  119.         curInputGate = curInputGate-1000
  120.       elseif xPos >= 6 and xPos <= 9 then
  121.         curInputGate = curInputGate-10000
  122.       elseif xPos >= 10 and xPos <= 12 then
  123.         curInputGate = curInputGate-100000
  124.       elseif xPos >= 17 and xPos <= 19 then
  125.         curInputGate = curInputGate+100000
  126.       elseif xPos >= 21 and xPos <= 23 then
  127.         curInputGate = curInputGate+10000
  128.       elseif xPos >= 25 and xPos <= 27 then
  129.         curInputGate = curInputGate+1000
  130.       end
  131.       inputfluxgate.setSignalLowFlow(curInputGate)
  132.       save_config()
  133.     end
  134.  
  135.     -- input gate toggle
  136.     if yPos == 10 and ( xPos == 14 or xPos == 15) then
  137.       if autoInputGate == 1 then
  138.         autoInputGate = 0
  139.       else
  140.         autoInputGate = 1
  141.       end
  142.       save_config()
  143.     end
  144.  
  145.   end
  146. end
  147.  
  148. function drawButtons(y)
  149.  
  150.   -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
  151.   -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
  152.  
  153.   f.draw_text(mon, 2, y, " < ", colors.white, colors.gray)
  154.   f.draw_text(mon, 6, y, " <<", colors.white, colors.gray)
  155.   f.draw_text(mon, 10, y, "<<<", colors.white, colors.gray)
  156.  
  157.   f.draw_text(mon, 17, y, ">>>", colors.white, colors.gray)
  158.   f.draw_text(mon, 21, y, ">> ", colors.white, colors.gray)
  159.   f.draw_text(mon, 25, y, " > ", colors.white, colors.gray)
  160. end
  161. for k, v in pairs(monitor) do
  162.     print(k)
  163. end
  164.  
  165. function update()
  166.   while true do
  167.  
  168.     f.clear(mon)
  169.  
  170.     ri = reactor.getReactorInfo()
  171.    
  172.     -- print out all the infos from .getReactorInfo() to term
  173.  
  174.     if ri == nil then
  175.       error("reactor has an invalid setup")
  176.     end
  177.  
  178.     for k, v in pairs (ri) do
  179.       print(k.. ": ".. tostring(v))
  180.     end
  181.     print("Output Gate: ", fluxgate.getSignalLowFlow())
  182.     print("Input Gate: ", inputfluxgate.getSignalLowFlow())
  183.  
  184.     -- monitor output
  185.     local satPercent
  186.     satPercent = math.ceil(ri.energySaturation / ri.maxEnergySaturation * 10000)*.01
  187.     local fieldPercent, fieldColor
  188.     fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000)*.01
  189.     if ri.status == "cold" then
  190.         ri.status = "offline"
  191.     elseif ri.status == "warming_up" then
  192.         if satPercent >= 50 and fieldPercent >= 50 and ri.temperature >= 2005.48 then
  193.             ri.status = "charged"
  194.         else
  195.             ri.status = "charging"
  196.         end
  197.     elseif ri.status == "running" then
  198.         ri.status = "online"
  199.     end
  200.     local statusColor
  201.     statusColor = colors.red
  202.  
  203.     if ri.status == "online" or ri.status == "charged" then
  204.       statusColor = colors.green
  205.     elseif ri.status == "offline" then
  206.       statusColor = colors.gray
  207.     elseif ri.status == "charging" then
  208.       statusColor = colors.orange
  209.     end
  210.  
  211.     f.draw_text_lr(mon, 2, 2, 1, "Reactor Status", string.upper(ri.status), colors.white, statusColor, colors.black)
  212.  
  213.     local outputFlux = fluxgate.getSignalLowFlow()
  214.     local inputFlux = inputfluxgate.getSignalLowFlow()
  215.    
  216.     f.draw_text_lr(mon, 2, 3, 1, "Generation", f.format_int(ri.generationRate) .. " rf/t", colors.white, colors.lime, colors.black)
  217.     f.draw_text_lr(mon, 2, 4, 1, "Real Output", f.format_int(outputFlux - inputFlux) .. " rf/t", colors.white, colors.lime, colors.black)
  218.     local tempColor = colors.red
  219.     if ri.temperature <= 5000 then tempColor = colors.green end
  220.     if ri.temperature >= 5000 and ri.temperature <= 6500 then tempColor = colors.orange end
  221.     f.draw_text_lr(mon, 2, 6, 1, "Temperature", f.format_int(ri.temperature) .. "C", colors.white, tempColor, colors.black)
  222.  
  223.     f.draw_text_lr(mon, 2, 7, 1, "Output Gate", f.format_int(outputFlux) .. " rf/t", colors.white, colors.blue, colors.black)
  224.  
  225.     -- buttons
  226.     drawButtons(8)
  227.  
  228.     f.draw_text_lr(mon, 2, 9, 1, "Input Gate", f.format_int(inputFlux) .. " rf/t", colors.white, colors.blue, colors.black)
  229.  
  230.     if autoInputGate == 1 then
  231.       f.draw_text(mon, 14, 10, "AU", colors.white, colors.gray)
  232.     else
  233.       f.draw_text(mon, 14, 10, "MA", colors.white, colors.gray)
  234.       drawButtons(10)
  235.     end
  236.  
  237.     f.draw_text_lr(mon, 2, 11, 1, "Energy Saturation", satPercent .. "%", colors.white, colors.white, colors.black)
  238.     f.progress_bar(mon, 2, 12, mon.X-2, satPercent, 100, colors.blue, colors.gray)
  239.  
  240.     fieldColor = colors.red
  241.     if fieldPercent >= 50 then fieldColor = colors.green end
  242.     if fieldPercent < 50 and fieldPercent > 30 then fieldColor = colors.orange end
  243.  
  244.     if autoInputGate == 1 then
  245.       f.draw_text_lr(mon, 2, 14, 1, "Field Strength T:" .. targetStrength, fieldPercent .. "%", colors.white, fieldColor, colors.black)
  246.     else
  247.       f.draw_text_lr(mon, 2, 14, 1, "Field Strength", fieldPercent .. "%", colors.white, fieldColor, colors.black)
  248.     end
  249.     f.progress_bar(mon, 2, 15, mon.X-2, fieldPercent, 100, fieldColor, colors.gray)
  250.  
  251.     local fuelPercent, fuelColor
  252.  
  253.     fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000)*.01
  254.  
  255.     fuelColor = colors.red
  256.  
  257.     if fuelPercent >= 70 then fuelColor = colors.green end
  258.     if fuelPercent < 70 and fuelPercent > 30 then fuelColor = colors.orange end
  259.  
  260.     f.draw_text_lr(mon, 2, 17, 1, "Fuel ", fuelPercent .. "%", colors.white, fuelColor, colors.black)
  261.     f.progress_bar(mon, 2, 18, mon.X-2, fuelPercent, 100, fuelColor, colors.gray)
  262.  
  263.     f.draw_text_lr(mon, 2, 19, 1, "Action ", action, colors.gray, colors.gray, colors.black)
  264.  
  265.     -- actual reactor interaction
  266.     --
  267.     if emergencyCharge == true then
  268.       reactor.chargeReactor()
  269.     end
  270.    
  271.     -- are we charging? open the floodgates
  272.     if ri.status == "charging" then
  273.       inputfluxgate.setSignalLowFlow(900000)
  274.       emergencyCharge = false
  275.     end
  276.  
  277.     -- are we stopping from a shutdown and our temp is better? activate
  278.     if emergencyTemp == true and ri.status == "stopping" and ri.temperature < safeTemperature then
  279.       reactor.activateReactor()
  280.       emergencyTemp = false
  281.     end
  282.  
  283.     -- are we charged? lets activate
  284.     if ri.status == "charged" and activateOnCharged == 1 then
  285.       reactor.activateReactor()
  286.     end
  287.  
  288.     -- are we on? regulate the input fludgate to our target field strength
  289.     -- or set it to our saved setting since we are on manual
  290.     if ri.status == "online" then
  291.       if autoInputGate == 1 then
  292.         fluxval = ri.fieldDrainRate / (1 - (targetStrength/100) )
  293.         print("Target Gate: ".. fluxval)
  294.         inputfluxgate.setSignalLowFlow(fluxval)
  295.       else
  296.         inputfluxgate.setSignalLowFlow(curInputGate)
  297.       end
  298.     end
  299.  
  300.     -- safeguards
  301.     --
  302.    
  303.     -- out of fuel, kill it
  304.     if fuelPercent <= 10 then
  305.       reactor.stopReactor()
  306.       action = "Fuel below 10%, refuel"
  307.     end
  308.  
  309.     -- field strength is too dangerous, kill and it try and charge it before it blows
  310.     if fieldPercent <= lowestFieldPercent and ri.status == "online" then
  311.       action = "Field Str < " ..lowestFieldPercent.."%"
  312.       reactor.stopReactor()
  313.       reactor.chargeReactor()
  314.       emergencyCharge = true
  315.     end
  316.  
  317.     -- temperature too high, kill it and activate it when its cool
  318.     if ri.temperature > maxTemperature then
  319.       reactor.stopReactor()
  320.       action = "Temp > " .. maxTemperature
  321.       emergencyTemp = true
  322.     end
  323.  
  324.     sleep(0.5)
  325.   end
  326. end
  327.  
  328. parallel.waitForAny(buttons, update)
RAW Paste Data Copied