Advertisement
Sokia

startupSuper

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