Advertisement
Killer_Klown_420

startup

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