Advertisement
Guest User

mon.lua

a guest
May 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. -- modifiablew variables
  2. local targetStrength = 50
  3. local maxTemperature = 8000
  4. local safeTemperature = 3000
  5. local lowestFieldPercent = 15
  6. local targetSaturation = 50
  7.  
  8. local activateOnCharged = 1
  9.  
  10. -- please leave things untouched from here on
  11. local component = require("component")
  12. local fs = require("filesystem")
  13. local term = require("term")
  14. local serial = require("serialization")
  15.  
  16. local version = "0.1"
  17. local cfgPath = "/etc/reactor_config.cfg"
  18. -- toggleable via the monitor, use our algorithm to achieve our target field strength or let the user tweak it
  19. local autoInputGate = 1
  20. local autoOutputGate = 1
  21. local curInputGate = 222000
  22.  
  23. -- monitor
  24. local mon, monitor, monX, monY
  25.  
  26. -- peripherals
  27. local monitor = component.proxy(component.get("46ac1d01", "screen"))
  28. local reactor = component.proxy(component.list("draconic_reactor")())
  29. local fluxgate = component.proxy(component.get("a804cf49", "flux_gate"))
  30. local inputfluxgate = component.proxy(component.get("747f39d9", "flux_gate"))
  31.  
  32. -- reactor information
  33. local ri
  34.  
  35. -- last performed action
  36. local action = "None since reboot"
  37. local emergencyCharge = false
  38. local emergencyTemp = false
  39.  
  40. if monitor == null then
  41.   error("No valid monitor was found")
  42. end
  43.  
  44. if fluxgate == null then
  45.   error("No valid flux gate was found")
  46. end
  47.  
  48. if reactor == null then
  49.   error("No valid reactor was fouind")
  50. end
  51.  
  52. if inputfluxgate == null then
  53.   error("No valid flux gate was found")
  54. end
  55.  
  56. -- TODO: monitor dimensions
  57. -- monX, monY = monitor.getSize()
  58. -- mon = {}
  59. -- mon.monitor, mon.X, mon.Y = monitor, monX, monY
  60.  
  61.  
  62. -- write settings to config file
  63. function save_config()
  64.   local cfg = {}
  65.   cfg.version = version
  66.   cfg.autoInputGate = autoInputGate
  67.   cfg.curInputGate = curInputGate
  68.  
  69.   sw = fs.open(cfgPath, "w")
  70.   sw:write(serial.serialize(cfg))
  71.   sw:close()
  72. end
  73.  
  74.  
  75. -- read settings from file
  76. function load_config()
  77.   sw = fs.open(cfgPath, "r")
  78.   local data = sw:read(fs.size(cfgPath))
  79.   local cfg = serial.unserialize(data)
  80.   sw:close()
  81.  
  82.   version = cfg.version
  83.   autoInputGate = cfg.autoInputGate
  84.   curInputGate = cfg.curInputGate
  85. end
  86.  
  87. -- 1st time? save our settings, if not, load our settings
  88. if fs.exists(cfgPath) == false then
  89.   save_config()
  90. else
  91.   load_config()
  92. end
  93.  
  94. function update()
  95.   while true do
  96.     term.clear()
  97.     ri = reactor.getReactorInfo()
  98.  
  99.     -- print out all the infos from .getReactorInfo() to term
  100.    
  101.     if ri == nil then
  102.       error("reactor has an invalid setup")
  103.     end
  104.  
  105.     for k, v in pairs(ri) do
  106.       print(tostring(k).. ": " .. tostring(v))
  107.     end
  108.  
  109.     print("Output Gate: ", fluxgate.getSignalLowFlow())
  110.     print("Input Gate: ", inputfluxgate.getSignalLowFlow())
  111.  
  112.  
  113.     -- actual reactor interaction
  114.     --
  115.  
  116.     if emergencyCharge == true then
  117.       reactor.chargeReactor()
  118.     end
  119.  
  120.     -- are we charging? open the floodgates
  121.     if ri.status == "warming_up" then
  122.       inputfluxgate.setSignalLowFlow(900000)
  123.       emergencyCharge = false
  124.     end
  125.  
  126.     -- are we stopping from a shutdown and our temp is better? activate
  127.     if emergencyTemp == true and ri.status == "cooling" and ri.temperature < safeTemperature then
  128.       reactor.activatereactor()
  129.       emergencyTemp = false
  130.     end
  131.  
  132.     -- are we charged? lets activate
  133.     if ri.status == "warming_up" and ri.temperature >= 2000 then
  134.       reactor.activateReactor()
  135.       print("Activating!!!")
  136.     end
  137.  
  138.     -- are we on? regulate the input floodgate to our target field strength
  139.     -- or set it to our saved setting since we are on manual
  140.     if ri.status == "running" then
  141.       if autoInputGate == 1 then
  142.         fluxval = ri.fieldDrainRate / (1 - (targetStrength/100))
  143.         print("Target Gate: " .. fluxval)
  144.         inputfluxgate.setSignalLowFlow(fluxval)
  145.       else
  146.         inputfluxgate.setSignalLowFlow(curInputGate)
  147.       end
  148.  
  149.       if autoOutputGate == 1 then
  150.         local rate = ((targetSaturation * 10000000) - ri.energySaturation) / ri.temperature
  151.         local target = ri.generationRate - rate
  152.      
  153.         print("Target output gate: " .. target)
  154.  
  155.         fluxgate.setSignalLowFlow(target)
  156.       end
  157.     end
  158.  
  159.  
  160.     -- safeguards
  161.     --
  162.  
  163.  
  164.     -- out of fuel, kill it
  165.     local fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000) * .01
  166.  
  167.     if fuelPercent <= 10 then
  168.       reactor.stopReactor()
  169.       action = "Fuel below 10%, refuel"
  170.     end
  171.  
  172.     -- field strength is too dangerous, kill it and try and charge it before it blows
  173.     local fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000) * .01
  174.  
  175.     if fieldPercent <= lowestFieldPercent and ri.status == "running" then
  176.       action = "Field Str < " .. lowestFieldPercent .. "%"
  177.       reactor.stopReactor()
  178.       reactor.chargeReactor()
  179.       emergencyCharge = true
  180.     end
  181.  
  182.     -- temperature too high, kill it and activate it when its cool
  183.     if ri.temperature > maxTemperature then
  184.       reactor.stopReactor()
  185.       action = "Temp > " .. maxTemperature
  186.       emergencyTemp = true
  187.     end
  188.  
  189.     os.sleep(0.1)
  190.   end
  191. end
  192.  
  193. update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement