koriar

safedr

Jan 18th, 2021 (edited)
981
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. print("Waiting 1 second to make sure information is ready")
  2. sleep(1)
  3. fine = true
  4.  
  5. targetOutput = 850000
  6. maxTemp = 7000
  7. minShieldPer = 30
  8. autoCharge = true
  9. autoActivate = true
  10. minFuelPer = 50
  11.  
  12. reac = peripheral.wrap("back")
  13. local inRate
  14.  
  15. outGate = peripheral.wrap("right")
  16.  
  17. monitor = peripheral.wrap("top")
  18.  
  19. if monitor == null then
  20.     monitor = periphSearch("monitor")
  21. end
  22.  
  23. if monitor ~= null then
  24.     term.redirect(monitor)
  25. end
  26.  
  27. if peripheral.wrap("top") then
  28.     monitor = peripheral.wrap("top")
  29.  
  30. end
  31.  
  32. function reactorShutdown()
  33.     outGate.setSignalLowFlow(0)
  34.     reac.stopReactor()
  35. end
  36.  
  37.  
  38. function periphSearch(type)
  39.    local names = peripheral.getNames()
  40.    local i, name
  41.    for i, name in pairs(names) do
  42.       if peripheral.getType(name) == type and name ~= "right" then
  43.          return peripheral.wrap(name)
  44.       end
  45.    end
  46.    return null
  47. end
  48.  
  49. inGate = periphSearch("flux_gate")
  50.  
  51. function safeCheck(rInfo)
  52.     if rInfo.fieldStrength == nil or rInfo.maxFieldStrength == nil then
  53.         print("Shield info nil for some reason. Sleeping for 5 seconds before continuing")
  54.         sleep(5)
  55.         rInfo = reac.getReactorInfo()
  56.     end
  57.     shieldPer = math.ceil(rInfo.fieldStrength / rInfo.maxFieldStrength * 10000) * .01
  58.    
  59.     if shieldPer <= minShieldPer and rInfo.status == "online" then
  60.         print("Shield is too weak.. Shutting down! ... I'm not sure what to do about this one!")
  61.         reactorShutdown()
  62.     end
  63.    
  64.     if rInfo.temperature > maxTemp then
  65.         print("Reactor is too hot! Shutting down temporarily")
  66.         reactorShutdown()
  67.     end
  68.  
  69.     if (rInfo.fuelConversion / rInfo.maxFuelConversion * 100) > minFuelPer then
  70.         print("Fuel conversion: ",rInfo.fuelConversion)
  71.         print("maxFuelConversion: ",rInfo.maxFuelConversion)
  72.         print("Fuel level is below ",minFuelPer,"% Please refuel after finished cooling.")
  73.         reactorShutdown()
  74.         fine = false
  75.     end
  76.  
  77. end
  78.  
  79. function stateCheck(rInfo)
  80.  
  81.     print("Status:",rInfo.status)
  82.     --Auto charge
  83.     if autoCharge == true and rInfo.status == "cold" or autoCharge == true and rInfo.status == "cooling" then
  84.         if rInfo.maxFuelConversion < 10000 then
  85.             print("I think you didn't put enough fuel in here. Double check that please")
  86.             return
  87.         else
  88.             print("Reactor offline. Starting reactor in 5 seconds.")
  89.             sleep(5)
  90.             reac.chargeReactor()
  91.         end
  92.     elseif autoCharge ~= true then
  93.         print("autoCharge Disabled. Leaving off")
  94.     end
  95.    
  96.     if autoCharge == true and rInfo.status == "stopping" and rInfo.temperature < (maxTemp * 0.9) then
  97.         print("Reactor is stopping... but it's cooled off a bit. Turning it back on in 5 seconds, hope you live!")
  98.         sleep(5)
  99.         reac.activateReactor()
  100.     end
  101.    
  102.     if rInfo.status == "warming_up" then
  103.         print("Setting input gate to 900k to speed up charging. Also clamping output.")
  104.         inGate.setSignalLowFlow(900000)
  105.         outGate.setSignalLowFlow(0)
  106.     end
  107.        
  108.     --Auto-activate
  109.     if autoActivate == true and rInfo.status == "warming_up" and rInfo.temperature >= 2000 then
  110.         print("Reactor Charged. Changing input gate and activating reactor")
  111.         inGate.setSignalLowFlow(rInfo.fieldDrainRate / (1 - (targetOutput/100)))
  112.         reac.activateReactor()
  113.     elseif autoActivate ~= true then
  114.         print("Auto activate disabled. Leaving charged")
  115.     end
  116.    
  117.     if rInfo.status == "running" then
  118.         inRate = rInfo.fieldDrainRate / (1 - (targetOutput/1000000))
  119.         --inRate = rInfo.fieldDrainRate / (1 - (targetOutput/100))
  120.         print("Net output is",math.floor((outGate.getFlow() - inRate)/1000)*1000)
  121.         print("Input gate set at",math.ceil(inRate))
  122.         inGate.setSignalLowFlow(inRate)
  123.         print("Output gate set at",targetOutput)
  124.         outGate.setSignalLowFlow(targetOutput)
  125.     end
  126.    
  127. end
  128.  
  129. while fine == true do
  130.     sleep(1)
  131.     term.clear()
  132.     local info = reac.getReactorInfo()
  133.     local timeLeft = (info.fuelConversion - info.maxFuelConversion) / info.fuelConversionRate * -100000 / 2
  134.     print("Temperature:",info.temperature,"C")
  135.     print("Shield Percent:",shieldPer,"%")
  136.     print("Saturation:",math.ceil(info.energySaturation / info.maxEnergySaturation * 10000)/100,"%")
  137.     print("Fuel percentage:",100 - math.ceil((info.fuelConversion / info.maxFuelConversion * 100)),"%")
  138.     print("Fuel time left:",math.ceil(timeLeft/60/60), "hours")
  139.     print("or",math.ceil(timeLeft/60/60/24),"days")
  140. --  print("Running state check")
  141.     stateCheck(info)
  142. --  print("Running safe check")
  143.     safeCheck(info)
  144.  
  145. end
  146. if fine == false then
  147.     shell.run("shutdown")
  148. end
  149.  
RAW Paste Data