mooviies

DraconicReactorEmulator

May 16th, 2017
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.20 KB | None | 0 0
  1. local reactor = {}
  2.  
  3. --local event = require("event")
  4. local computer = require("computer")
  5.  
  6. -- Config
  7. local REACTOR_OUTPUT_MULTIPLIER = 1
  8. local REACTOR_FUEL_USAGE_MULTIPLIER = 1
  9.  
  10. -- States
  11. local STATE_COLD = "COLD"
  12. local STATE_WARMING_UP = "WARMING_UP"
  13. local STATE_RUNNING = "RUNNING"
  14. local STATE_STOPPING = "STOPPING"
  15. local STATE_COOLING = "COOLING"
  16. local STATE_BEYOND_HOPE = "BEYOND_HOPE"
  17. --
  18.  
  19. local MAX_TEMPERATURE = 10000
  20. local reactorState = STATE_COLD
  21. local startupInitialized = false
  22. local reactableFuel = 0
  23. local convertedFuel = 0
  24. local failSafeMode = false
  25. local temperature = 20
  26. local saturation = 0
  27. local maxSaturation = 0
  28. local shieldCharge = 0
  29. local maxShieldCharge = 0
  30. local inputRate = 0
  31. local outputRate = 0
  32. local generationRate = 0
  33. local tempDrainFactor = 0
  34. local fieldDrain = 0
  35. local fieldInputRate = 0
  36. local fuelUseRate = 0
  37. local energyStorage = 9880000000
  38.  
  39. local function initializeStartup ()
  40.   if not startupInitialized then
  41.     local totalFuel = reactableFuel + convertedFuel
  42.     maxShieldCharge = totalFuel * 96.45061728395062 * 100
  43.     maxSaturation = math.floor(totalFuel * 96.45061728395062 * 1000)
  44.    
  45.     if saturation > maxSaturation then saturation = maxSaturation end
  46.     if shieldCharge > maxShieldCharge then shieldCharge = maxShieldCharge end
  47.    
  48.     startupInitialized = true
  49.   end
  50. end
  51.  
  52. local function canCharge ()
  53.   if reactorState == STATE_BEYOND_HOPE then return false end
  54.   return ((reactorState == STATE_COLD) or reactorState == STATE_COOLING) and reactableFuel + convertedFuel >= 144
  55. end
  56.  
  57. local function canActivate ()
  58.   if(reactorState == STATE_BEYOND_HOPE) then return false end
  59.   return ((reactorState == STATE_WARMING_UP) or reactorState == STATE_STOPPING) and temperature >= 2000 and ((saturation >= maxSaturation / 2 and shieldCharge >= maxShieldCharge / 2) or reactorState == STATE_STOPPING)
  60. end
  61.  
  62. local function canStop ()
  63.   if(reactorState == STATE_BEYOND_HOPE) then return false end
  64.   return reactorState == STATE_RUNNING or reactorState == STATE_WARMING_UP
  65. end
  66.  
  67. local function shutdownReator ()
  68.   if canStop() then reactorState = STATE_STOPPING end
  69. end
  70.  
  71. local function updateOfflineState ()
  72.   if temperature > 20 then temperature = temperature - 0.5 end
  73.  
  74.   if shieldCharge > 0 then shieldCharge = shieldCharge - maxShieldCharge * 0.0005
  75.   elseif shieldCharge < 0 then shieldCharge = 0 end
  76.  
  77.   if saturation > 0 then saturation = saturation - maxSaturation * 0.000001
  78.   elseif saturation < 0 then saturation = 0 end
  79. end
  80.  
  81. local function updateOnlineState ()
  82.   local coreSat = saturation / maxSaturation
  83.   local negCSat = (1 - coreSat) * 99;
  84.   local temp50 = math.min((temperature / MAX_TEMPERATURE) * 50, 99)
  85.   local tFuel = convertedFuel + reactableFuel
  86.   local convLVL = ((convertedFuel / tFuel) * 1.3) - 0.3
  87.  
  88.   -- Temperature Calculation
  89.   local tempOffset = 444.7
  90.   local tempRiseExpo = negCSat^3 / (100 - negCSat) + tempOffset
  91.   local tempRiseResist = temp50^4 / (100 - temp50)
  92.   local riseAmount = (tempRiseExpo - (tempRiseResist * (1 - convLVL)) + convLVL * 1000) / 10000
  93.  
  94.   if reactorState == STATE_STOPPING and convLVL < 1 then
  95.     if temperature < 2001 then
  96.       reactorState = STATE_COOLING
  97.       startupInitialized = false
  98.       return
  99.     end
  100.     if saturation >= maxSaturation * 0.99 and reactableFuel > 0 then
  101.       temperature = temperature - (1 - convLVL)
  102.     else
  103.       temperature = temperature + riseAmount * 10
  104.     end
  105.   else
  106.     temperature = temperature + riseAmount * 10
  107.   end
  108.  
  109.   -- Energy Calculation
  110.   local baseMaxRFt = math.floor((maxSaturation / 1000) * REACTOR_OUTPUT_MULTIPLIER * 1.5)
  111.   local maxRFt = math.floor(baseMaxRFt * (1 + (convLVL * 2)))
  112.   generationRate = (1 - coreSat) * maxRFt
  113.   saturation = math.floor(saturation + generationRate)
  114.  
  115.   -- Shield Calculation
  116.   tempDrainFactor = 0
  117.   if temperature > 8000 then
  118.     tempDrainFactor = 1 + ((temperature - 8000)^2 * 0.0000025)
  119.   elseif temperature > 2000 then
  120.     tempDrainFactor = 1
  121.   elseif temperature > 1000 then
  122.     tempDrainFactor = (temperature - 1000) / 1000
  123.   else
  124.     tempDrainFactor = 0
  125.   end
  126.  
  127.   fieldDrain = math.floor(math.min(tempDrainFactor * math.max(0.01, (1 - coreSat)) * (baseMaxRFt / 10.923556), 2147483647))
  128.  
  129.   local fieldNegPercent = 1 - (shieldCharge / maxShieldCharge)
  130.   fieldInputRate = fieldDrain / fieldNegPercent
  131.   shieldCharge = shieldCharge - math.min(fieldDrain, shieldCharge)
  132.  
  133.   -- Fuel Calculation
  134.   fuelUseRate = tempDrainFactor * (1 - coreSat) * (0.001 * REACTOR_FUEL_USAGE_MULTIPLIER)
  135.   if reactableFuel > 0 then
  136.     convertedFuel = convertedFuel + fuelUseRate
  137.     reactableFuel = reactableFuel - fuelUseRate
  138.   end
  139.  
  140.   -- Explosion
  141.   if (shieldCharge <= 0) and (temperature > 2000) and (reactorState ~= STATE_BEYOND_HOPE) then
  142.     reactorState = STATE_BEYOND_HOPE
  143.   end
  144. end
  145.  
  146. local function updateCoreLogic ()
  147.   if reactorState == STATE_COLD then
  148.     updateOfflineState()
  149.   elseif reactorState == STATE_WARMING_UP then
  150.     initializeStartup()
  151.   elseif reactorState == STATE_RUNNING then
  152.     updateOnlineState()
  153.    
  154.     if failSafeMode and temperature < 2500 and (saturation / maxSaturation) >= 0.99 then
  155.       shutdownReator()
  156.     end
  157.   elseif reactorState == STATE_STOPPING then
  158.     updateOnlineState()
  159.     if temperature <= 2000 then
  160.       reactorState = STATE_COOLING
  161.     end
  162.   elseif reactorState == STATE_COOLING then
  163.     updateOfflineState()
  164.     if temperature <= 100 then
  165.       reactorState = STATE_COLD
  166.     end
  167.   end
  168. end
  169.  
  170. local function injectEnergy(rf)
  171.   local received = 0
  172.   if reactorState == STATE_WARMING_UP then
  173.     if not startupInitialized then
  174.       return 0
  175.     end
  176.    
  177.     if shieldCharge < (maxShieldCharge / 2) then
  178.       received = math.min(rf, math.floor(maxShieldCharge / 2) - math.floor(shieldCharge) + 1)
  179.       shieldCharge = shieldCharge + received
  180.       if shieldCharge > (maxShieldCharge / 2) then
  181.         shieldCharge = maxShieldCharge / 2
  182.       end
  183.     elseif saturation < (maxSaturation / 2) then
  184.       received = math.min(rf, (maxSaturation / 2) - saturation)
  185.       saturation = math.floor(saturation + received)
  186.     elseif temperature < 2000 then
  187.       received = rf
  188.       temperature = temperature + received / (1000 + (reactableFuel * 10))
  189.       if temperature > 2500 then
  190.         temperature = 2500
  191.       end
  192.     end
  193.   elseif reactorState == STATE_RUNNING or reactorState == STATE_STOPPING then
  194.     local tempFactor = 1
  195.     if temperature > 15000 then
  196.       tempFactor = 1 - math.min(1, (temperature - 15000) / 10000)
  197.     end
  198.    
  199.     shieldCharge = shieldCharge + math.min(rf * (1 - (shieldCharge / maxShieldCharge)), maxShieldCharge - shieldCharge) * tempFactor
  200.     if shieldCharge > maxShieldCharge then
  201.       shieldCharge = maxShieldCharge
  202.     end
  203.  
  204.     if reactorState == STATE_STOPPING then
  205.       energyStorage = energyStorage - rf
  206.     end
  207.    
  208.     return rf
  209.   end
  210.  
  211.   return received
  212. end
  213.  
  214. local previousTime = computer.uptime()
  215. local updateSpeed = 0.05
  216.  
  217. function reactor.update ()
  218.   local currentTime = computer.uptime()
  219.   local elapsed = currentTime - previousTime
  220.   if elapsed >= updateSpeed then
  221.     previousTime = currentTime
  222.     local nbTick = elapsed / updateSpeed
  223.     for i=1,math.floor(nbTick) do
  224.       updateCoreLogic()
  225.       if inputRate > 0 then injectEnergy(inputRate) end
  226.       if outputRate > 0 then saturation = math.floor(saturation - outputRate) end
  227.       if saturation < 0 then saturation = 0 end
  228.     end
  229.   end
  230. end
  231.  
  232. function reactor.setUpdateSpeed(speed)
  233.   updateSpeed = 0.05 / speed
  234. end
  235.  
  236. function reactor.chargeReactor ()
  237.   if canCharge() then reactorState = STATE_WARMING_UP end
  238. end
  239.  
  240. function reactor.activateReactor ()
  241.   if canActivate() then reactorState = STATE_RUNNING end
  242. end
  243.  
  244. function reactor.getReactorInfo ()
  245.     local info = {
  246.         energySaturation = saturation,
  247.         fieldStrength = math.floor(shieldCharge),
  248.         fuelConversion = math.floor(convertedFuel),
  249.         temperature = math.ceil(temperature),
  250.         maxEnergySaturation = maxSaturation,
  251.         fuelConversionRate = math.floor(fuelUseRate * 1000000),
  252.         fieldDrainRate = fieldDrain,
  253.         maxFuelConversion = reactableFuel + convertedFuel,
  254.         generationRate = math.floor(generationRate),
  255.         maxFieldStrength = math.floor(maxShieldCharge),
  256.         failsafe = failSafeMode
  257.     }
  258.    
  259.     if reactorState == STATE_RUNNING then info.status = "running"
  260.     elseif reactorState == STATE_WARMING_UP then info.status = "warming_up"
  261.     elseif reactorState == STATE_COLD then info.status = "cold"
  262.     elseif reactorState == STATE_COOLING then info.status = "cooling"
  263.     elseif reactorState == STATE_STOPPING then info.status = "stopping"
  264.     elseif reactorState == STATE_BEYOND_HOPE then info.status = "beyond_hope"
  265.     else info.status = "invalid"
  266.     end
  267.    
  268.     return info
  269. end
  270.  
  271. function reactor.setFailSafe (activate)
  272.   failSafeMode = activate
  273. end
  274.  
  275. function reactor.stopReactor ()
  276.   shutdownReator()
  277. end
  278.  
  279. reactor.fluxGates =
  280. {
  281.   input =
  282.   {
  283.     setOverrideEnabled = function () end,
  284.     getFlow = function () return inputRate end,
  285.     setFlowOverride = function (flow)
  286.       inputRate = flow
  287.     end
  288.   },
  289.   output =
  290.   {
  291.     setOverrideEnabled = function () end,
  292.     getFlow = function () return outputRate end,
  293.     setFlowOverride = function (flow)
  294.       outputRate = flow
  295.     end
  296.   }
  297. }
  298.  
  299. function reactor.setFuel(fuel, convertedRatio)
  300.   if convertedRatio > 1 then convertedRatio = 1 elseif convertedRatio < 0 then convertedRatio = 0 end
  301.  
  302.   if fuel > 10368 then
  303.     reactableFuel = 10368
  304.   elseif fuel < 0 then
  305.     reactableFuel = 0
  306.   else
  307.     reactableFuel = fuel
  308.   end
  309.  
  310.   convertedFuel = convertedRatio * reactableFuel
  311.   reactableFuel = reactableFuel - convertedFuel
  312. end
  313.  
  314. function reactor.setEnergyStorage(storage)
  315.   if storage < 0 then storage = 0 end
  316.   energyStorage = storage
  317. end
  318.  
  319. function reactor.getEnergyStorage()
  320.   return energyStorage
  321. end
  322.  
  323. function reactor.reset()
  324.   reactorState = STATE_COLD
  325.   startupInitialized = false
  326.   reactableFuel = 0
  327.   convertedFuel = 0
  328.   failSafeMode = false
  329.   temperature = 20
  330.   saturation = 0
  331.   maxSaturation = 0
  332.   shieldCharge = 0
  333.   maxShieldCharge = 0
  334.   inputRate = 0
  335.   outputRate = 0
  336. end
  337.  
  338. return reactor
Add Comment
Please, Sign In to add comment