Advertisement
Guest User

mon.lua

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