Advertisement
ashotnsta2012

Draconic Reactor Computercraft Controller

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