Advertisement
OfficialStamper

emulatedReactorCC

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