asidjazz

draconic reactor control

Jun 20th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.01 KB | None | 0 0
  1. -- kevin olson (asidjazz/256.io/etc)
  2.  
  3. -- heavily modified alteration of EterniaLogic modified SDRC script
  4.  
  5. -----------
  6. -- The Field input value should be 30% of the power provied
  7. -- Capacitors are used to help guage when the power storage is full
  8. -- The inflow flux gate can also be modem networked together with the computer
  9. --
  10. -----------
  11.  
  12. -- ReS - Reactor Stabilizer
  13. -- "+" - Cryo-stabilized fluxduct
  14. -- CPU - Computer
  15. -- FxG - Flux Gate
  16. -- ___ - Air
  17. -- CAP - Basic Capacitor (The smaller the total value, the better)
  18.  
  19. -- ==Level 1==
  20. -- ReS +
  21. -- CPU FxG
  22. -- ___ +
  23.  
  24. -- ==Level 2==
  25. -- ___ CAP
  26. -- CAP CAP
  27. -- ___ CAP
  28.  
  29. wantedField=50000000                    -- Force the field value to this approximate value
  30.  
  31.  
  32. ValueChange = 10000 -- Change on the Flux Gate over time (Not exact, temperature and saturation divide this)
  33. MinEnergySat = 1000000 -- Minimum energy saturation level
  34. WantedRFt = 1200000
  35. totalFuel = 10368   -- Fully upgraded reactor
  36.  
  37. CapacitorNum = 5                            -- 5 Capacitors on top of computer
  38. CapacitorVal = 1000000                  -- Power holding capacity of the capacitors
  39.  
  40.  
  41. FIRavg = {}     -- last FIR to go     [Flux Input Rate]
  42. FIRLen = 20     -- length of averages
  43. FIRi = 0        -- location to do running average
  44. FIRVal = 200000 -- initial value for FIR
  45. FIRChg = false  -- has an up-down happened?
  46. FIRLow = 0
  47. FIRHigh = 0
  48. FIRLastDelta = 999999
  49. reactorState = "..."
  50.  
  51. for i=0, FIRLen-1 do
  52.     FIRavg[i] = FIRVal
  53. end
  54.  
  55. local mon
  56. local monX
  57. local monY
  58.  
  59. local inputfluxgate
  60.  
  61. function periphSearch(type)
  62.    local names = peripheral.getNames()
  63.    local i, name
  64.    for i, name in pairs(names) do
  65.       if peripheral.getType(name) == type then
  66.          return peripheral.wrap(name)
  67.       end
  68.    end
  69.    return null
  70. end
  71.  
  72. function periphSearchName(type)
  73.    local names = peripheral.getNames()
  74.    local i, name
  75.    for i, name in pairs(names) do
  76.       if peripheral.getType(name) == type then
  77.          return name
  78.       end
  79.    end
  80.    return null
  81. end
  82.  
  83. -------------------FORMATTING-------------------------------
  84.  
  85. -- 5000 becomes 5,000
  86. function format_int(number)
  87.  
  88.   local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  89.   -- reverse the int-string and append a comma to all blocks of 3 digits
  90.   int = int:reverse():gsub("(%d%d%d)", "%1,")
  91.  
  92.   -- reverse the int-string back remove an optional comma and put the
  93.   -- optional minus and fractional part back
  94.   return minus .. int:reverse():gsub("^,", "") .. fraction
  95. end
  96.  
  97. function clear()
  98.   term.clear()
  99.   term.setCursorPos(1,1)
  100.   mon.setBackgroundColor(colors.black)
  101.   mon.clear()
  102.   mon.setCursorPos(1,1)
  103. end
  104.  
  105. --display text text on monitor, "mon" peripheral
  106. function draw_text(x, y, text, text_color, bg_color)
  107.   mon.setBackgroundColor(bg_color)
  108.   mon.setTextColor(text_color)
  109.   mon.setCursorPos(x,y)
  110.   mon.write(text)
  111. end
  112.  
  113. function draw_text_right(offset, y, text, text_color, bg_color)
  114.     mon.setBackgroundColor(bg_color)
  115.     mon.setTextColor(text_color)
  116.     mon.setCursorPos(monX-string.len(text)-offset,y)
  117.     mon.write(text)
  118. end
  119.  
  120. --draw line on computer terminal
  121. function draw_line(x, y, length, color)
  122.     mon.setBackgroundColor(color)
  123.     mon.setCursorPos(x,y)
  124.     mon.write(string.rep(" ", length))
  125. end
  126.  
  127. --create progress bar
  128. --draws two overlapping lines
  129. --background line of bg_color
  130. --main line of bar_color as a percentage of minVal/maxVal
  131. function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  132.   draw_line(x, y, length, bg_color) --backgoround bar
  133.   local barSize = math.floor((minVal/maxVal) * length)
  134.   draw_line(x, y, barSize, bar_color) --progress so far
  135. end
  136.  
  137. mon = periphSearch("monitor")
  138. inputfluxgate = periphSearchName("flux_gate")
  139.  
  140. if mon == null then
  141.     error("No valid monitor was found")
  142. end
  143.  
  144. if inputfluxgate == null then
  145.     error("No valid flux gate was found")
  146. end
  147.  
  148. monX, monY = mon.getSize()
  149.  
  150. -----------
  151. function init()
  152.     inflowFluxGate = peripheral.wrap("bottom")
  153.     if inflowFluxGate ~= nil then
  154.         inflowFluxGate.open(1)
  155.         print("|# Reactor State:  ["..reactorState.."]")
  156.         draw_text(2, 2, "Reactor State", colors.white, colors.black)
  157.  
  158.         if reactorState == "online" then
  159.             draw_text_right(1, 2, string.upper(reactorState), colors.lime, colors.black)
  160.         elseif reactorState == "offline" then
  161.             draw_text_right(1, 2, string.upper(reactorState), colors.gray, colors.black)
  162.         else
  163.             draw_text_right(1, 2, string.upper(reactorState), colors.red, colors.black)
  164.         end
  165.  
  166.         local inputFlow = inflowFluxGate.callRemote(inputfluxgate,"getSignalLowFlow")
  167.        
  168.         print("|# Input fluxgate:      "..inputFlow)
  169.         draw_text(2, 7, "Input Gate", colors.white, colors.black)
  170.         draw_text_right(1, 7, format_int(inputFlow) .. " rf/t", colors.white, colors.black)
  171.     end
  172.  
  173.  
  174.    
  175.     maxFieldCharge = totalFuel * 96.45061728395062 * 100
  176.     maxEnergySaturation = (totalFuel * 96.45061728395062 * 1000)
  177.  
  178.     -- Capacitor on top
  179.     CapacitorMax = CapacitorVal*CapacitorNum    -- Power holding capacity of the capacitors
  180.  
  181.  
  182.  
  183.     capacitor_1 = peripheral.wrap("top")
  184.     tbl = peripheral.call("back","getReactorInfo")
  185.     FluxGateStatus = peripheral.call("right", "getSignalLowFlow")
  186.  
  187.     for k, v in pairs (tbl) do
  188.         --print(k,v)
  189.         if k == "temperature" then -- Weise bestimmte programmrelevante Parameter einzelnen Variablen zu
  190.             temperatur = v
  191.         end
  192.         if k == "energySaturation" then
  193.             energysat = v
  194.         end
  195.         if k == "fieldStrength" then
  196.             feldst = v
  197.         end
  198.         if k == "generationRate" then
  199.             RFpt = v
  200.         end
  201.         if k == "fuelConversion" then
  202.             RestKraftstoff = v
  203.         end
  204.         if k == "fuelConversionRate" then
  205.             Kraftstoffverbrauch = v
  206.         end
  207.         if k == "status" then
  208.             reactorState = v
  209.         end
  210.     end
  211.     energieladung = energysat * 0.0000001
  212.     fieldstr = feldst * 0.000001
  213.     kRFpt = RFpt / 1000
  214.     fuel = RestKraftstoff / 10368
  215.     FuelConv = fuel * 100
  216.     --------------------------------------------------
  217.     print("|# Reactor temperature: "..math.ceil(temperatur))
  218.     print("|# Energy saturation: "..math.ceil(energieladung).."%")
  219.     print("|# Field strength:    "..math.ceil(fieldstr).."%")
  220.     print("|# Refuel level:      "..math.ceil(FuelConv).."%")
  221.     --print("|# ConvesionRate:     "..math.ceil(Kraftstoffverbrauch))
  222.     print("|# Energyproduction:    "..math.ceil(kRFpt).." kRF/t")
  223.  
  224.     -- 2 = reactor state, 4 = production, 6 = temperature, 7 = input gate, 8 = output gate
  225.     -- 10/11 = saturation
  226.     -- 13/14 = field strength
  227.     -- 16/17 = refuel level
  228.  
  229.     local energySatPerc = math.ceil(energieladung)
  230.     local fieldStrPerc = math.ceil(fieldstr)
  231.     local fuelLevel = math.ceil(FuelConv)
  232.     local energyProd = math.ceil(kRFpt)
  233.  
  234.     draw_text(2, 4, "Production", colors.white, colors.black)
  235.     draw_text_right(1, 4, format_int(energyProd) .. "kRF/t", colors.blue, colors.black)
  236.  
  237.     draw_text(2, 6, "Temperature", colors.white, colors.black)
  238.     draw_text_right(1, 6, format_int(math.ceil(temperatur)) .. "C", colors.white, colors.black)
  239.  
  240.     draw_text(2, 10, "Energy Saturation", colors.white, colors.black)
  241.     draw_text_right(1, 10, format_int(energySatPerc) .. "%", colors.white, colors.black)
  242.  
  243.     if energySatPerc > 70 then
  244.         progress_bar(2, 11, monX-2, energySatPerc, 100, colors.red, colors.gray)
  245.     elseif energySatPerc < 70 and energySatPerc > 40 then
  246.         progress_bar(2, 11, monX-2, energySatPerc, 100, colors.orange, colors.gray)
  247.     else
  248.         progress_bar(2, 11, monX-2, energySatPerc, 100, colors.green, colors.gray)
  249.     end
  250.  
  251.     draw_text(2, 13, "Field Strength", colors.white, colors.black)
  252.     draw_text_right(1, 13, format_int(fieldStrPerc) .. "%", colors.white, colors.black)
  253.  
  254.     if fieldStrPerc > 70 then
  255.         progress_bar(2, 14, monX-2, fieldStrPerc, 100, colors.red, colors.gray)
  256.     elseif fieldStrPerc < 70 and fieldStrPerc > 40 then
  257.         progress_bar(2, 14, monX-2, fieldStrPerc, 100, colors.green, colors.gray)
  258.     else
  259.         progress_bar(2, 14, monX-2, fieldStrPerc, 100, colors.orange, colors.gray)
  260.     end
  261.  
  262.     draw_text(2, 16, "Refuel Level", colors.white, colors.black)
  263.     draw_text_right(1, 16, format_int(fuelLevel) .. "%", colors.white, colors.black)
  264.  
  265.     if fuelLevel > 70 then
  266.         progress_bar(2, 17, monX-2, fuelLevel, 100, colors.red, colors.gray)
  267.     elseif fuelLevel < 70 and fuelLevel > 40 then
  268.         progress_bar(2, 17, monX-2, fuelLevel, 100, colors.orange, colors.gray)
  269.     else
  270.         progress_bar(2, 17, monX-2, fuelLevel, 100, colors.green, colors.gray)
  271.     end
  272.  
  273.  
  274.  
  275.  
  276.     -------------------------------------------------
  277. end
  278.  
  279. function doFIRavg()
  280.     local val=0
  281.     for i=0, FIRLen-1 do
  282.         val = val+FIRavg[i]
  283.     end
  284.     return val/FIRLen
  285. end
  286.  
  287. function calcFieldDrain()
  288.         if FIRi >= FIRLen then
  289.             FIRi = 0
  290.         else
  291.             FIRi = FIRi+1
  292.         end
  293.        
  294.         FIRavg[FIRi]=feldst
  295.         local avg = doFIRavg()
  296.         --print(avg.." "..wantedField)
  297.         local difference = math.abs(avg-wantedField)
  298.        
  299.         --local valuedelta = math.pow(avg-wantedField, 2)/(300*avg)
  300.        
  301.         local height = 40000
  302.        
  303.         local Offset = 78540000
  304.         local valuedelta = math.sin((avg-(wantedField+Offset))/(50000000))*height+height
  305.         if(valuedelta < 0) then
  306.             valuedelta = 1
  307.             --print("Neg at: "..avg)
  308.         end
  309.        
  310.         --print("TTT: "..valuedelta)
  311.        
  312.        
  313.         if (avg > wantedField) then
  314.             if FIRVal-valuedelta < 10000 then
  315.                 FIRVal = 10000
  316.             else
  317.                 FIRVal = FIRVal-valuedelta
  318.             end
  319.             if(FIRChg) then
  320.                 FIRChg = false
  321.                 FIRHigh = FIRVal
  322.                
  323.                 -- Predict middle
  324.                 --FIRVal = (FIRHigh+FIRLow)/2
  325.             end
  326.         else
  327.             if (avg < wantedField) then
  328.                 if FIRVal+valuedelta > (900000-valuedelta) then
  329.                     FIRVal = 900000
  330.                 else
  331.                     FIRVal = FIRVal+valuedelta
  332.                 end
  333.             end
  334.             if(not FIRChg) then
  335.                 FIRChg = true
  336.                 FIRLow = FIRVal
  337.                
  338.                 -- Predict middle
  339.                 --FIRVal = (FIRHigh+FIRLow)/2
  340.             end
  341.            
  342.         end
  343.        
  344.         --print("Low: "..FIRLow)
  345.         --print("High: "..FIRHigh)
  346.         --print("Delta: "..math.abs(FIRHigh-FIRLow))
  347.        
  348.         fluxval = FIRVal
  349.         print("|#== FluxInputRate:     "..math.floor(fluxval).." kRF/t")       
  350. end
  351.    
  352.  
  353. -- Set the shield input level
  354. function setLowLevel()
  355.     if inflowFluxGate ~= nil then
  356.         calcFieldDrain()
  357.         inflowFluxGate.callRemote(inputfluxgate,"setSignalLowFlow",fluxval)
  358.     end
  359. end
  360.  
  361.  
  362. function setLevel(level)
  363.     print("|#== FluxGate set to:   "..math.ceil(FluxGateStatus).." kRF/t")
  364.     peripheral.call("right", "setSignalLowFlow", FluxGateStatus)
  365.     draw_text(2, 8, "Output Gate", colors.white, colors.black)
  366.     draw_text_right(1, 8, format_int(math.ceil(FluxGateStatus)) .. " rf/t", colors.white, colors.black)
  367.    
  368. end
  369.  
  370.  
  371. function limitLevel(FluxStatus)
  372.     -- Set the maximum value
  373.     if FluxGateStatus >= (WantedRFt*0.9-FluxGateStatus*0.3) then
  374.         FluxGateStatus = (WantedRFt*0.9-FluxGateStatus*0.3)
  375.     end
  376. end
  377.  
  378. -- Start the reactor
  379. function startReactor()
  380.     peripheral.call("back", "chargeReactor")
  381.         print("|#==        Reactor charge !!        ==#|")
  382.     if feldst >= 50000000 then
  383.         peripheral.call("back", "activateReactor")
  384.         print("|#==        Reactor activate !       ==#|")
  385.     end
  386. end
  387.  
  388. function testFieldStrength()
  389.     if feldst <= 15000000 then
  390.         peripheral.call("back", "stopReactor")
  391.         print("|#==     Reactor Emergency stop !    ==#|")
  392.         print("|#==     Field strength too low !     ==#|")
  393.     else
  394.         if feldst >= 20000000 then
  395.             peripheral.call("back", "activateReactor")
  396.             --print("|#==         Reactor active !        ==#|")
  397.         end
  398.     end
  399. end
  400.  
  401. function calcNewValueChange()
  402.     local chg = ValueChange
  403.     local saturationchg = (energieladung/100)-0.3
  404.     local reactorTempx = 1-((temperatur-3000)/10000+0.2) -- reactor temperature accounting for +3000 heat
  405.    
  406.     print("|# Temperature Factor:  "..reactorTempx)
  407.     print("|# Saturation Factor:   "..saturationchg)
  408.     chg = chg*math.min(saturationchg,reactorTempx) -- Account for saturation and Temperature
  409.    
  410.     return chg
  411. end
  412.  
  413.  
  414. function testCapacitor()
  415.     -- Slow down the reactor to 1/8 of the requested power level
  416.     -- This is so that a slow-start is not required for later use
  417.     if capacitor_1 ~= nil then
  418.         local energyStored = capacitor_1.getEnergyStored()
  419.         if energyStored >= CapacitorMax then
  420.             FluxGateStatus = WantedRFt/8
  421.             setLevel(FluxGateStatus)
  422.         end
  423.     end
  424. end
  425.  
  426.  
  427. -- Run this program!
  428. function doRun()
  429.     if temperatur > 7777 then -- Temperatur check wenn mehr als 7777 Grad
  430.         FluxGateStatus = 200000
  431.     end
  432.  
  433.  
  434.     -- Set the energy level to be produced
  435.     if energysat > MinEnergySat then
  436.         limitLevel(FluxGateStatus)
  437.         FluxGateStatus = RFpt + calcNewValueChange() -- anpassen der Flux-Gate_Variablen an den neuen Wert
  438.         setLevel(FluxGateStatus)
  439.     end
  440.  
  441.     -- Energy Saturation settings
  442.     --if energysat < MinEnergySat then
  443.     --  if FluxGateStatus <= 10000 then -- Minimalwert abregelung auf 0
  444.     --      FluxGateStatus = 10000
  445.     --  end
  446.     --  FluxGateStatus = FluxGateStatus - ValueChange -- anpassen der Flux-Gate_Variablen an den neuen Wert
  447.     --  setLevel(FluxGateStatus)
  448.     --end
  449.  
  450.  
  451.  
  452.     -- Stop the reactor when 90% of the fuel is gone
  453.     if FuelConv >= 90 then
  454.         peripheral.call("back", "stopReactor")
  455.  
  456.         print("|#==      Reactor refuel stop !      ==#|")
  457.         print("|#============#| S D R C |#============#|")
  458.         sleep(300)
  459.     end
  460.  
  461.     if temperatur <= 2000 then -- Starttest zum aufladen des Reaktors
  462.         startReactor()
  463.     else
  464.         testFieldStrength()
  465.     end
  466.  
  467.     setLowLevel(level)
  468.     testCapacitor()
  469. end
  470.  
  471. while true do
  472.     clear()
  473.     init()
  474.     doRun()
  475.     sleep(0.1)
  476. end
Advertisement
Add Comment
Please, Sign In to add comment