Advertisement
Guest User

nrg

a guest
Jun 26th, 2017
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.99 KB | None | 0 0
  1. -- NRG
  2. -- Energy control system for Big Reactors / Mekanism
  3. -- in 1.7.10 Pack
  4. -- version 0.1
  5. -- Alex Shaffer : alex@nosuch.org
  6.  
  7. local nrg = {}
  8.  
  9. function nrg.gridManageStandby()
  10.  
  11.   nrg.reactorShouldBeOff()
  12.   nrg.turbineCoilsShouldBeOff()
  13.  
  14.   if nrg.energyPercentCapacity <= nrg.config.spinUpPercent then
  15.  
  16.     nrg.status.current = nrg.const.gridStatus.activate
  17.  
  18.   end
  19.  
  20. end
  21.  
  22. function nrg.gridManageActivate()
  23.  
  24.   nrg.reactorShouldBeOn()
  25.   nrg.turbineCoilsShouldBeOff()
  26.  
  27.   nrg.status.current = nrg.const.gridStatus.spinUp
  28.  
  29.  
  30. end
  31.  
  32. function nrg.gridManageSpinUp()
  33.  
  34.   nrg.reactorShouldBeOn()
  35.  
  36.   if nrg.energyPercentCapacity >= nrg.config.spinDownPercent then
  37.  
  38.     nrg.status.current = nrg.const.gridStatus.spinDown
  39.  
  40.   else
  41.    
  42.     if nrg.turbinesShouldBeRPM(nrg.const.coilOnThreshold, true) then
  43.    
  44.       nrg.status.current = nrg.const.gridStatus.charging
  45.    
  46.     end
  47.    
  48.   end
  49.  
  50.  
  51. end
  52.  
  53. function nrg.gridManageCharging()
  54.  
  55.   nrg.reactorShouldBeOn()
  56.   nrg.turbinesShouldBeRPM(nrg.const.coilOffThreshold, true)
  57.  
  58.   if nrg.energyPercentCapacity >= nrg.config.spinDownPercent then
  59.  
  60.     nrg.status.current = nrg.const.gridStatus.spinDown
  61.  
  62.   end
  63.  
  64. end
  65.  
  66.  
  67. function nrg.gridManageSpinDown()
  68.  
  69.   nrg.reactorShouldBeOn()
  70.   nrg.turbineCoilsShouldBeOff()
  71.   if nrg.turbinesShouldBeRPM(nrg.const.coilSpinUpThreshhold) then
  72.       nrg.status.current = nrg.const.gridStatus.deactivate
  73.   end
  74.  
  75. end
  76.  
  77. function nrg.gridManageDeactivate()
  78.  
  79.   nrg.reactorShouldBeOff()
  80.   nrg.turbineCoilsShouldBeOff()
  81.  
  82. end
  83.  
  84.  
  85.  
  86. function nrg.constInit()
  87.  
  88.   nrg.const = {}
  89.  
  90.   nrg.const.BRmaxEnergyStorage = 10000000
  91.  
  92.   nrg.const.gridStatus = {}
  93.   nrg.const.gridStatus.standby = { name = "Standing by", action = nrg.gridManageStandby }
  94.   nrg.const.gridStatus.activate = { name = "Activating", action = nrg.gridManageActivate }
  95.   nrg.const.gridStatus.spinUp = { name = "Spinning Up", action = nrg.gridManageSpinUp }
  96.   nrg.const.gridStatus.charging = { name = "Charging", action = nrg.gridManageCharging }
  97.   nrg.const.gridStatus.spinDown = { name = "Spinning Down", action = nrg.gridManageSpinDown }
  98.   nrg.const.gridStatus.deactivate = { name = "Deactivating", action = nrg.gridManageDeactivate }
  99.  
  100.   nrg.const.coilOnThreshold = 1775
  101.   nrg.const.coilOffThreshold = 1750
  102.   nrg.const.coilSpinUpThreshhold = 1800
  103.   nrg.const.BRreactor = "BigReactors-Reactor"
  104.   nrg.const.BRturbine = "BigReactors-Turbine"
  105.   nrg.const.MKcube = "mekanism_machine"
  106.  
  107. end
  108.  
  109. function nrg.configInit(options)
  110.  
  111.   nrg.config = {}
  112.   nrg.status = {}
  113.  
  114.   if options == nil then options = {} end
  115.  
  116.   nrg.config.enabled = true
  117.   nrg.config.CC = (_CC_VERSION ~= nil)
  118.   nrg.status.gridValid = false
  119.  
  120.   nrg.config.spinUpPercent = options.spinUpPercent or 5
  121.   nrg.config.spinDownPercent = options.spinDownPercent or 95
  122.   nrg.status.current = nrg.const.gridStatus.standby
  123.   nrg.status.lastStatus = nil
  124.  
  125. end
  126.  
  127.  
  128.  
  129. function nrg.bridgingInit()
  130.  
  131.   nrg.fn = {}
  132.  
  133.   if nrg.config.CC then
  134.    
  135.     nrg.fn.findReactors = function() return {peripheral.find(nrg.const.BRreactor)} end
  136.     nrg.fn.findTurbines = function() return {peripheral.find(nrg.const.BRturbine)} end
  137.     nrg.fn.findStorage = function() return {peripheral.find(nrg.const.MKcube)} end
  138.     nrg.fn.isEnergyCube = function(e) return (e.getMaxEnergy ~= nil and e.getMaxEnergy() > 0) end
  139.     nrg.fn.getReactorActive = function() return nrg.reactor.getActive() end
  140.     nrg.fn.setReactorActive = function(a) nrg.reactor.setActive(a) end
  141.   else
  142.    
  143.     nrg.fn.testData = {}
  144.     nrg.fn.testData.reactorActive = false
  145.    
  146.     nrg.fn.findReactors = function() return {} end
  147.     nrg.fn.findTurbines = function() return {} end
  148.     nrg.fn.findStorage = function() return {} end
  149.     nrg.fn.isEnergyCube = function(e) return (e.getMaxEnergy ~=nil and e.getMaxEnergy() > 0 ) end
  150.     nrg.fn.getReactorActive = function() return nrg.fn.testData.reactorActive end
  151.     nrg.fn.setReactorActive = function(a) nrg.fn.testData.reactorActive = a end
  152.    
  153.   end
  154.  
  155. end
  156.  
  157. -- Finds power equipment hooked up to the network
  158. function nrg.gridInit()
  159.  
  160.   nrg.energyStorageMax = 0
  161.   nrg.energyStorageCurrent = 0
  162.  
  163.   nrg.reactor = nil
  164.   nrg.reactors = gridInitReactorsFind()
  165.   nrg.turbines = gridInitTurbinesFind()
  166.   nrg.storage = gridInitStorageFind(nrg.turbines)
  167.  
  168.   nrg.config.gridValid = ((#nrg.reactors == 1) and (#nrg.turbines > 0))
  169.  
  170.   if nrg.config.gridValid then
  171.     nrg.reactor = nrg.reactors[1]
  172.     nrg.gridUpdate()
  173.   end
  174.  
  175.   print("Grid summary:")
  176.   print("Reactors: "..#nrg.reactors)
  177.   print("Turbines: "..#nrg.turbines)
  178.   print("Storage Devices: "..#nrg.storage)
  179.   print("Storage Capacity: "..math.floor(nrg.energyStorageMax/1000).." krf")
  180.   print("Current Energy: "..math.floor(nrg.energyStorageCurrent/1000).." krf")
  181.   print("Valid: "..tostring(nrg.config.gridValid))
  182.  
  183. end
  184.  
  185.  
  186. function gridInitReactorsFind()
  187.  
  188.   local reactors = nrg.fn.findReactors()
  189.    
  190.    
  191.   print("Reactors found: "..#reactors)
  192.  
  193.   return reactors
  194.  
  195. end
  196.  
  197. function gridInitTurbinesFind()
  198.  
  199.   local turbines = nrg.fn.findTurbines()
  200.  
  201.  
  202.   print("Turbines found: "..#turbines)
  203.  
  204.   return turbines
  205.  
  206. end
  207.  
  208. function gridInitStorageFind(turbines)
  209.  
  210.   local storage = nrg.fn.findStorage()
  211.  
  212.  
  213.   print("Additional storage found: "..#storage)
  214.  
  215.   for k,turbine in pairs(turbines) do
  216.     -- BR devices don't have a getMaxEnergy function, so we add one, based on a constant
  217.     if turbine.getMaxEnergy == nil then
  218.       turbine.getMaxEnergy = function() return nrg.const.BRmaxEnergyStorage end
  219.     end
  220.     if turbine.getEnergy == nil then
  221.       turbine.getEnergy = turbine.getEnergyStored
  222.     end
  223.    
  224.     storage[#storage+1]=turbine
  225.   end
  226.  
  227.   nrg.energyStorageMax = 0
  228.  
  229.   for k,storageDevice in pairs(storage) do
  230.  
  231.     nrg.energyStorageMax = nrg.energyStorageMax + storageDevice.getMaxEnergy()
  232.  
  233.   end
  234.  
  235.   return storage
  236.  
  237. end
  238.  
  239. -- Loads data about the current state of the power grid
  240. function nrg.gridUpdate()
  241.  
  242.   local energyStored = 0
  243.  
  244.   for k, storageDevice in pairs(nrg.storage) do
  245.  
  246.     energyStored = energyStored + storageDevice.getEnergy()
  247.    
  248.   end
  249.  
  250.   nrg.energyStorageCurrent = energyStored
  251.  
  252.   nrg.energyPercentCapacity = math.floor(nrg.energyStorageCurrent / nrg.energyStorageMax * 100)
  253.  
  254.   nrg.reactorActive = nrg.fn.getReactorActive()
  255.  
  256. end
  257.  
  258. function nrg.gridManage()
  259.  
  260.   nrg.gridUpdate()
  261.  
  262.   if nrg.status.current ~= nrg.status.lastStatus then
  263.  
  264.     print(nrg.status.current.name)
  265.  
  266.   end
  267.      
  268.   nrg.status.current.action()
  269.  
  270. end
  271.  
  272.  
  273.  
  274. function nrg.reactorShouldBeOff()
  275.  
  276.   if nrg.reactor.getConnected() and nrg.reactor.getActive() then
  277.  
  278.     nrg.reactor.setActive(false)
  279.  
  280.   end
  281.  
  282. end
  283.  
  284. function nrg.reactorShouldBeOn()
  285.  
  286.   if nrg.reactor.getConnected() and not nrg.reactor.getActive() then
  287.    
  288.     nrg.reactor.setActive(true)
  289.    
  290.   end
  291.  
  292. end
  293.  
  294. function nrg.turbineCoilsShouldBeOff()
  295.      
  296.   for k, turbine in pairs(nrg.turbines) do
  297.  
  298.     if turbine.getConnected() and turbine.getActive() then
  299.    
  300.       turbine.setActive(false)
  301.    
  302.     end
  303.    
  304.   end
  305.  
  306. end
  307.  
  308. function nrg.turbinesShouldBeRPM(rpmTarget, makeActive)
  309.  
  310.   if makeActive == nil then makeActive = false end
  311.  
  312.   local allAtTarget = true
  313.  
  314.   for k, turbine in pairs(nrg.turbines) do
  315.  
  316.     if turbine.getConnected() and turbine.getRotorSpeed() >= rpmTarget then
  317.  
  318.       if makeActive then
  319.      
  320.         turbine.setInductorEngaged(true)
  321.      
  322.       end
  323.  
  324.     else
  325.      
  326.       allAtTarget = false
  327.      
  328.       if makeActive then
  329.           turbine.setInductorEngaged(false)
  330.       end
  331.      
  332.     end
  333.  
  334.   end
  335.  
  336.   return allAtTarget
  337.  
  338. end
  339.  
  340.  
  341.  
  342. -- Test code here
  343.  
  344. nrg.constInit()
  345. nrg.configInit()
  346. nrg.bridgingInit()
  347. nrg.gridInit()
  348.  
  349. if nrg.config.gridValid then
  350.   print("Valid grid.")
  351.   print("Managing grid...")
  352.  
  353.   while true do
  354.     nrg.gridManage()
  355.     os.sleep(2)
  356.   end
  357.  
  358. else
  359.  
  360.   print("Grid is not valid. Needs 1 reactor and at least 1 turbine.")
  361.  
  362. end
  363.  
  364.  
  365. return nrg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement