Advertisement
Guest User

ReactorControl.lua

a guest
Feb 5th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. --Includes
  2. local component = require("component")
  3. local term = require("term")
  4.  
  5. --Capacitor Variables
  6. local capacitor = component.capacitor_bank
  7. local cap_store, cap_max, cap_avg
  8. local low_trigger = 0.2
  9.  
  10. --Reactor Variables
  11. local reactor = component.br_reactor
  12. local rea_steam, rea_fuel, rea_rea
  13.  
  14. --Turbine Variables
  15. local turbines = {}
  16. for temp, _ in pairs(component.list("br_turbine")) do
  17.    table.insert (turbines, component.proxy(temp))
  18. end
  19. local tur_high_trig = 1900
  20. local tur_target = 1840
  21. local tur_low_trig = 1600
  22.  
  23. --Init the turbines
  24. for t, turbine in pairs(turbines) do
  25.    turbine.setActive (true)
  26.    turbine.setInductorEngaged (false)
  27. end
  28.  
  29. --General Variables
  30. local gpu = component.gpu
  31. local w, h = gpu.getResolution ()
  32. local run = true
  33. local total_power
  34. local charging = true
  35.  
  36. function updateData ()
  37.    cap_store = capacitor.getEnergyStored ()
  38.    cap_max = capacitor.getMaxEnergyStored ()
  39.    cap_avg = capacitor.getAverageChangePerTick ()
  40.    rea_steam = reactor.getHotFluidProducedLastTick ()
  41.    rea_fuel = reactor.getFuelAmount ()
  42.    rea_fuel_max = reactor.getFuelAmountMax ()
  43.    rea_waste = reactor.getWasteAmount ()
  44.    rea_consum = reactor.getFuelConsumedLastTick ()
  45.    rea_reac = reactor.getFuelReactivity ()
  46.    total_power = 0;
  47.    for t, turbine in pairs(turbines) do
  48.       total_power = total_power + turbine.getEnergyProducedLastTick()
  49.    end
  50. end
  51.  
  52. function reactorControl ()
  53.    --Charge until full
  54.    if (reactor.getActive ()) then
  55.       if (cap_store == cap_max) then
  56.          charging = false
  57.       end
  58.    end
  59.  
  60.    --If power low start charging again
  61.    if (cap_store < (cap_max * low_trigger)) then
  62.       charging = true
  63.       reactor.setActive (true)
  64.    end
  65.  
  66.    --setActive
  67.    --setAllControlRodLevel
  68. end
  69.  
  70. function turbineControl ()
  71.    local highest_rpm = 0
  72.    local lowest_rpm = 10000
  73.    
  74.    for t, turbine in pairs(turbines) do
  75.       if (charging) then
  76.          --If we are charging, spin up the rotors to the target speed and engage coils
  77.          if ((turbine.getRotorSpeed() >= tur_target) and (turbine.getInductorEngaged (false))) then
  78.             turbine.setInductorEngaged (true)
  79.          elseif ((turbine.getRotorSpeed() < tur_low_trig) and (turbine.getInductorEngaged (true))) then
  80.             turbine.setInductorEngaged (false)
  81.             reactor.setActive (false)
  82.          end
  83.       else
  84.          --Otherwise get the highest and lowest rpm
  85.          if (turbine.getRotorSpeed () > highest_rpm) then
  86.             highest_rpm = turbine.getRotorSpeed ()
  87.          end
  88.          if (turbine.getRotorSpeed () < lowest_rpm) then
  89.             lowest_rpm = turbine.getRotorSpeed ()
  90.          end
  91.       end
  92.    end
  93.    
  94.    --If not charging control the reactor to keep the turbine spun up
  95.    if (charging == false) then
  96.       if (highest_rpm > tur_high_trig) then
  97.          reactor.setActive (true)
  98.       elseif (lowest_rpm < tur_low_trig) then
  99.          reactor.setActive (false)
  100.       end
  101.    end
  102.    
  103.    --setActive
  104.    --setInductorEngaged
  105. end
  106.  
  107. function renderGUI ()
  108.    --gpu.fill (1, 1, w, h, " ")
  109.    term.clear()
  110.    print ("Capacitor")
  111.    print ("---------")
  112.    print ("Stored: "  .. cap_store .. " / " .. cap_max)
  113.    print ("Power Change: " .. cap_avg)
  114.    print ("")
  115.    print ("Reactor")
  116.    print ("-------")
  117.    print ("Steam: " .. rea_steam .. " / 10000")
  118.    print ("Fuel Use: " .. rea_consum)
  119.    print ("Reactivity: " .. rea_reac)
  120.    print ("")
  121.    --for x,t in pairs(turbines) do
  122.    for t, turbine in pairs(turbines) do
  123.       --local turbine = component.proxy(x)
  124.       print ("Turbine " .. t)
  125.       print ("---------")
  126.       print ("Speed: " .. turbine.getRotorSpeed())
  127.       print ("Steam: " .. turbine.getFluidFlowRate())
  128.       print ("Power: " .. turbine.getEnergyProducedLastTick())
  129.       print ("Stored: " .. turbine.getEnergyStored())
  130.       print ("")
  131.    end
  132. end
  133.  
  134. while (run) do
  135.    updateData ()
  136.    renderGUI ()
  137.    os.sleep (1)
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement