Advertisement
1ng0

DRC-v2.0

Mar 10th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.88 KB | None | 0 0
  1. --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--
  2. -- 1ng0                                     --
  3. -- Draconic Reactor Control v2.0 Script     --
  4. -- http://                                  --
  5. --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--
  6. --
  7. --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--
  8. -- The Field input value should be 30% of the power provied
  9. -- Capacitors are used to help guage when the power storage is full
  10. -- The inflow flux gate can also be modem networked together with the computer
  11. --
  12. -----------
  13.  
  14. -- ReS - Reactor Stabilizer
  15. -- "+" - Cryo-stabilized fluxduct
  16. -- CPU - Computer
  17. -- FxG - Flux Gate
  18. -- ___ - Air
  19. -- CAP - Basic Capacitor (The smaller the total value, the better)
  20.  
  21. -- ==Level 1==
  22. -- ReS +
  23. -- CPU FxG
  24. -- ___ +
  25.  
  26. -- ==Level 2==
  27. -- ___ CAP
  28. -- CAP CAP
  29. -- ___ CAP
  30.  
  31. wantedField=50000000                    -- Force the field value to this approximate value
  32.  
  33. FluxMODEMVALUE = 0
  34. ValueChange = 10000 -- Change on the Flux Gate over time (Not exact, temperature and saturation divide this)
  35. MinEnergySat = 1000000 -- Minimum energy saturation level
  36. WantedRFt = 1200000
  37. totalFuel = 10368   -- Fully upgraded reactor
  38.  
  39. CapacitorNum = 5                            -- 5 Capacitors on top of computer
  40. CapacitorVal = 1000000                  -- Power holding capacity of the capacitors
  41.  
  42.  
  43. FIRavg = {}     -- last FIR to go     [Flux Input Rate]
  44. FIRLen = 20     -- length of averages
  45. FIRi = 0        -- location to do running average
  46. FIRVal = 200000 -- initial value for FIR
  47. FIRChg = false  -- has an up-down happened?
  48. FIRLow = 0
  49. FIRHigh = 0
  50. FIRLastDelta = 999999
  51.  
  52. for i=0, FIRLen-1 do
  53.     FIRavg[i] = FIRVal
  54. end
  55.  
  56. -----------
  57. function init()
  58.     inflowFluxGate = peripheral.wrap("bottom")
  59.     if inflowFluxGate ~= nil then
  60.         inflowFluxGate.open(1)
  61.         print("|# Input fluxgate:      "..inflowFluxGate.callRemote("flux_gate_"..FluxMODEMVALUE,"getSignalLowFlow"))
  62.     end
  63.  
  64.  
  65.    
  66.     maxFieldCharge = totalFuel * 96.45061728395062 * 100
  67.     maxEnergySaturation = (totalFuel * 96.45061728395062 * 1000)
  68.  
  69.     -- Capacitor on top
  70.     CapacitorMax = CapacitorVal*CapacitorNum    -- Power holding capacity of the capacitors
  71.  
  72.  
  73.  
  74.     capacitor_1 = peripheral.wrap("top")
  75.     tbl = peripheral.call("back","getReactorInfo")
  76.     FluxGateStatus = peripheral.call("right", "getSignalLowFlow")
  77.  
  78.     for k, v in pairs (tbl) do
  79.         --print(k,v)
  80.         if k == "temperature" then -- Weise bestimmte programmrelevante Parameter einzelnen Variablen zu
  81.             temperatur = v
  82.         end
  83.         if k == "energySaturation" then
  84.             energysat = v
  85.         end
  86.         if k == "fieldStrength" then
  87.             feldst = v
  88.         end
  89.         if k == "generationRate" then
  90.             RFpt = v
  91.         end
  92.         if k == "fuelConversion" then
  93.             RestKraftstoff = v
  94.         end
  95.         if k == "fuelConversionRate" then
  96.             Kraftstoffverbrauch = v
  97.         end
  98.     end
  99.     energieladung = energysat * 0.0000001
  100.     fieldstr = feldst * 0.000001
  101.     kRFpt = RFpt / 1000
  102.     fuel = RestKraftstoff / 10368
  103.     FuelConv = fuel * 100
  104.     --------------------------------------------------
  105.     print("|# Reactor temperature: "..math.ceil(temperatur))
  106.     print("|# Energy saturation %: "..math.ceil(energieladung))
  107.     print("|# Field strength %:    "..math.ceil(fieldstr))
  108.     print("|# Refuel level %:      "..math.ceil(FuelConv))
  109.     --print("|# ConvesionRate:     "..math.ceil(Kraftstoffverbrauch))
  110.     print("|# Energyproduction:    "..math.ceil(kRFpt).." kRF/t")
  111.     -------------------------------------------------
  112. end
  113.  
  114. function doFIRavg()
  115.     local val=0
  116.     for i=0, FIRLen-1 do
  117.         val = val+FIRavg[i]
  118.     end
  119.     return val/FIRLen
  120. end
  121.  
  122. function calcFieldDrain()
  123.         if FIRi >= FIRLen then
  124.             FIRi = 0
  125.         else
  126.             FIRi = FIRi+1
  127.         end
  128.        
  129.         FIRavg[FIRi]=feldst
  130.         local avg = doFIRavg()
  131.         --print(avg.." "..wantedField)
  132.         local difference = math.abs(avg-wantedField)
  133.        
  134.         --local valuedelta = math.pow(avg-wantedField, 2)/(300*avg)
  135.        
  136.         local height = 40000
  137.        
  138.         local Offset = 78540000
  139.         local valuedelta = math.sin((avg-(wantedField+Offset))/(50000000))*height+height
  140.         if(valuedelta < 0) then
  141.             valuedelta = 1
  142.             --print("Neg at: "..avg)
  143.         end
  144.        
  145.         --print("TTT: "..valuedelta)
  146.        
  147.        
  148.         if (avg > wantedField) then
  149.             if FIRVal-valuedelta < 10000 then
  150.                 FIRVal = 10000
  151.             else
  152.                 FIRVal = FIRVal-valuedelta
  153.             end
  154.             if(FIRChg) then
  155.                 FIRChg = false
  156.                 FIRHigh = FIRVal
  157.                
  158.                 -- Predict middle
  159.                 --FIRVal = (FIRHigh+FIRLow)/2
  160.             end
  161.         else
  162.             if (avg < wantedField) then
  163.                 if FIRVal+valuedelta > (900000-valuedelta) then
  164.                     FIRVal = 900000
  165.                 else
  166.                     FIRVal = FIRVal+valuedelta
  167.                 end
  168.             end
  169.             if(not FIRChg) then
  170.                 FIRChg = true
  171.                 FIRLow = FIRVal
  172.                
  173.                 -- Predict middle
  174.                 --FIRVal = (FIRHigh+FIRLow)/2
  175.             end
  176.            
  177.         end
  178.        
  179.         --print("Low: "..FIRLow)
  180.         --print("High: "..FIRHigh)
  181.         --print("Delta: "..math.abs(FIRHigh-FIRLow))
  182.        
  183.         fluxval = FIRVal
  184.         print("|#== FluxInputRate:     "..math.floor(fluxval).." kRF/t")
  185. end
  186.    
  187.  
  188. -- Set the shield input level
  189. function setLowLevel()
  190.     if inflowFluxGate ~= nil then
  191.         calcFieldDrain()
  192.         inflowFluxGate.callRemote("flux_gate_"..FluxMODEMVALUE,"setSignalLowFlow",fluxval)
  193.     end
  194. end
  195.  
  196.  
  197. function setLevel(level)
  198.     print("|#== FluxGate set to:   "..math.ceil(FluxGateStatus).." kRF/t")
  199.     peripheral.call("right", "setSignalLowFlow", FluxGateStatus)
  200.    
  201. end
  202.  
  203.  
  204. function limitLevel(FluxStatus)
  205.     -- Set the maximum value
  206.     if FluxGateStatus >= (WantedRFt*0.9-FluxGateStatus*0.3) then
  207.         FluxGateStatus = (WantedRFt*0.9-FluxGateStatus*0.3)
  208.     end
  209. end
  210.  
  211.  
  212. -- Start the reactor
  213. function startReactor()
  214.     peripheral.call("back", "chargeReactor")
  215.         print("|#==        Reactor charge !!        ==#|")
  216.     if feldst >= 50000000 then
  217.         peripheral.call("back", "activateReactor")
  218.             print("|#==        Reactor activate !       ==#|")
  219.     end
  220. end
  221.  
  222.  
  223. function testFieldStrength()
  224.     if feldst <= 15000000 then
  225.         peripheral.call("back", "stopReactor")
  226.         print("|#==     Reactor Emergency stop !    ==#|")
  227.         print("|#==     Field strength too low !     ==#|")
  228.     else
  229.         if feldst >= 20000000 then
  230.             peripheral.call("back", "activateReactor")
  231.             --print("|#==         Reactor active !        ==#|")
  232.         end
  233.     end
  234. end
  235.  
  236.  
  237. function calcNewValueChange()
  238.     local chg = ValueChange
  239.     local saturationchg = (energieladung/100)-0.3
  240.     local reactorTempx = 1-((temperatur-3000)/10000+0.2) -- reactor temperature accounting for +3000 heat
  241.    
  242.     print("|# Temperature Factor:  "..reactorTempx)
  243.     print("|# Saturation Factor:   "..saturationchg)
  244.     chg = chg*math.min(saturationchg,reactorTempx) -- Account for saturation and Temperature
  245.    
  246.     return chg
  247. end
  248.  
  249.  
  250. function testCapacitor()
  251.     -- Slow down the reactor to 1/8 of the requested power level
  252.     -- This is so that a slow-start is not required for later use
  253.     if capacitor_1 ~= nil then
  254.         local energyStored = capacitor_1.getEnergyStored()
  255.         if energyStored >= CapacitorMax then
  256.             FluxGateStatus = WantedRFt/8
  257.             setLevel(FluxGateStatus)
  258.         end
  259.     end
  260. end
  261.  
  262.  
  263. -- Run this program!
  264. function doRun()
  265.     if temperatur > 7777 then -- Temperatur check wenn mehr als 7777 Grad
  266.         FluxGateStatus = 200000
  267.     end
  268.  
  269.  
  270.     -- Set the energy level to be produced
  271.     if energysat > MinEnergySat then
  272.         limitLevel(FluxGateStatus)
  273.         FluxGateStatus = RFpt + calcNewValueChange() -- anpassen der Flux-Gate_Variablen an den neuen Wert
  274.         setLevel(FluxGateStatus)
  275.     end
  276.  
  277.  
  278.  
  279.  
  280.     -- Energy Saturation settings
  281.     --if energysat < MinEnergySat then
  282.     --  if FluxGateStatus <= 10000 then -- Minimalwert abregelung auf 0
  283.     --      FluxGateStatus = 10000
  284.     --  end
  285.     --  FluxGateStatus = FluxGateStatus - ValueChange -- anpassen der Flux-Gate_Variablen an den neuen Wert
  286.     --  setLevel(FluxGateStatus)
  287.     --end
  288.  
  289.  
  290.  
  291.     -- Stop the reactor when 90% of the fuel is gone
  292.     if FuelConv >= 90 then
  293.         peripheral.call("back", "stopReactor")
  294.  
  295.         print("|#==      Reactor refuel stop !      ==#|")
  296.         print("|#============#| S D R C |#============#|")
  297.         sleep(300)
  298.     end
  299.  
  300.     if temperatur <= 2000 then -- Starttest zum aufladen des Reaktors
  301.         startReactor()
  302.     else
  303.         testFieldStrength()
  304.     end
  305.  
  306.     setLowLevel(level)
  307.     testCapacitor()
  308. end
  309.  
  310. while true do
  311.     term.clear()
  312.     term.setCursorPos(1,1)
  313.     init()
  314.     doRun()
  315.     sleep(0.06)
  316. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement