Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --OfficialStamper - leeched from https://repl.it/@mooviies/draconicreactor
- local reactor = {}
- --local event = require("event")
- --local computer = require("computer")
- local computer = {}
- computer.uptime = function() return os.clock() end
- -- Config
- local REACTOR_OUTPUT_MULTIPLIER = 1
- local REACTOR_FUEL_USAGE_MULTIPLIER = 1
- -- States
- local STATE_COLD = "COLD"
- local STATE_WARMING_UP = "WARMING_UP"
- local STATE_RUNNING = "RUNNING"
- local STATE_STOPPING = "STOPPING"
- local STATE_COOLING = "COOLING"
- local STATE_BEYOND_HOPE = "BEYOND_HOPE"
- --
- local MAX_TEMPERATURE = 10000
- local reactorState = STATE_COLD
- local startupInitialized = false
- local reactableFuel = 0
- local convertedFuel = 0
- local failSafeMode = false
- local temperature = 20
- local saturation = 0
- local maxSaturation = 0
- local shieldCharge = 0
- local maxShieldCharge = 0
- local inputRate = 0
- local outputRate = 0
- local generationRate = 0
- local tempDrainFactor = 0
- local fieldDrain = 0
- local fieldInputRate = 0
- local fuelUseRate = 0
- local energyStorage = 9880000000
- --Stamper added
- local riseAmount = 0 --To add to info
- local function initializeStartup ()
- if not startupInitialized then
- local totalFuel = reactableFuel + convertedFuel
- maxShieldCharge = totalFuel * 96.45061728395062 * 100
- maxSaturation = math.floor(totalFuel * 96.45061728395062 * 1000)
- if saturation > maxSaturation then saturation = maxSaturation end
- if shieldCharge > maxShieldCharge then shieldCharge = maxShieldCharge end
- startupInitialized = true
- end
- end
- local function canCharge ()
- if reactorState == STATE_BEYOND_HOPE then return false end
- return ((reactorState == STATE_COLD) or reactorState == STATE_COOLING) and reactableFuel + convertedFuel >= 144
- end
- local function canActivate ()
- if(reactorState == STATE_BEYOND_HOPE) then return false end
- return ((reactorState == STATE_WARMING_UP) or reactorState == STATE_STOPPING) and temperature >= 2000 and ((saturation >= maxSaturation / 2 and shieldCharge >= maxShieldCharge / 2) or reactorState == STATE_STOPPING)
- end
- local function canStop ()
- if(reactorState == STATE_BEYOND_HOPE) then return false end
- return reactorState == STATE_RUNNING or reactorState == STATE_WARMING_UP
- end
- local function shutdownReator ()
- if canStop() then reactorState = STATE_STOPPING end
- end
- local function updateOfflineState ()
- if temperature > 20 then temperature = temperature - 0.5 end
- if shieldCharge > 0 then shieldCharge = shieldCharge - maxShieldCharge * 0.0005
- elseif shieldCharge < 0 then shieldCharge = 0 end
- if saturation > 0 then saturation = saturation - maxSaturation * 0.000001
- elseif saturation < 0 then saturation = 0 end
- end
- local function updateOnlineState ()
- local coreSat = saturation / maxSaturation
- local negCSat = (1 - coreSat) * 99;
- local temp50 = math.min((temperature / MAX_TEMPERATURE) * 50, 99)
- local tFuel = convertedFuel + reactableFuel
- local convLVL = ((convertedFuel / tFuel) * 1.3) - 0.3
- -- Temperature Calculation
- local tempOffset = 444.7
- local tempRiseExpo = negCSat^3 / (100 - negCSat) + tempOffset
- local tempRiseResist = temp50^4 / (100 - temp50)
- -- local riseAmount = (tempRiseExpo - (tempRiseResist * (1 - convLVL)) + convLVL * 1000) / 10000
- riseAmount = (tempRiseExpo - (tempRiseResist * (1 - convLVL)) + convLVL * 1000) / 10000
- if reactorState == STATE_STOPPING and convLVL < 1 then
- if temperature < 2001 then
- reactorState = STATE_COOLING
- startupInitialized = false
- return
- end
- if saturation >= maxSaturation * 0.99 and reactableFuel > 0 then
- temperature = temperature - (1 - convLVL)
- else
- temperature = temperature + riseAmount * 10
- end
- else
- temperature = temperature + riseAmount * 10
- end
- -- Energy Calculation
- local baseMaxRFt = math.floor((maxSaturation / 1000) * REACTOR_OUTPUT_MULTIPLIER * 1.5)
- local maxRFt = math.floor(baseMaxRFt * (1 + (convLVL * 2)))
- generationRate = (1 - coreSat) * maxRFt
- saturation = math.floor(saturation + generationRate)
- -- Shield Calculation
- tempDrainFactor = 0
- if temperature > 8000 then
- tempDrainFactor = 1 + ((temperature - 8000)^2 * 0.0000025)
- elseif temperature > 2000 then
- tempDrainFactor = 1
- elseif temperature > 1000 then
- tempDrainFactor = (temperature - 1000) / 1000
- else
- tempDrainFactor = 0
- end
- fieldDrain = math.floor(math.min(tempDrainFactor * math.max(0.01, (1 - coreSat)) * (baseMaxRFt / 10.923556), 2147483647))
- local fieldNegPercent = 1 - (shieldCharge / maxShieldCharge)
- fieldInputRate = fieldDrain / fieldNegPercent
- shieldCharge = shieldCharge - math.min(fieldDrain, shieldCharge)
- -- Fuel Calculation
- fuelUseRate = tempDrainFactor * (1 - coreSat) * (0.001 * REACTOR_FUEL_USAGE_MULTIPLIER)
- if reactableFuel > 0 then
- convertedFuel = convertedFuel + fuelUseRate
- reactableFuel = reactableFuel - fuelUseRate
- end
- -- Explosion
- if (shieldCharge <= 0) and (temperature > 2000) and (reactorState ~= STATE_BEYOND_HOPE) then
- reactorState = STATE_BEYOND_HOPE
- end
- end
- local function updateCoreLogic ()
- if reactorState == STATE_COLD then
- updateOfflineState()
- elseif reactorState == STATE_WARMING_UP then
- initializeStartup()
- elseif reactorState == STATE_RUNNING then
- updateOnlineState()
- if failSafeMode and temperature < 2500 and (saturation / maxSaturation) >= 0.99 then
- shutdownReator()
- end
- elseif reactorState == STATE_STOPPING then
- updateOnlineState()
- if temperature <= 2000 then
- reactorState = STATE_COOLING
- end
- elseif reactorState == STATE_COOLING then
- updateOfflineState()
- if temperature <= 100 then
- reactorState = STATE_COLD
- end
- end
- end
- local function injectEnergy(rf)
- local received = 0
- if reactorState == STATE_WARMING_UP then
- if not startupInitialized then
- return 0
- end
- if shieldCharge < (maxShieldCharge / 2) then
- received = math.min(rf, math.floor(maxShieldCharge / 2) - math.floor(shieldCharge) + 1)
- shieldCharge = shieldCharge + received
- if shieldCharge > (maxShieldCharge / 2) then
- shieldCharge = maxShieldCharge / 2
- end
- elseif saturation < (maxSaturation / 2) then
- received = math.min(rf, (maxSaturation / 2) - saturation)
- saturation = math.floor(saturation + received)
- elseif temperature < 2000 then
- received = rf
- temperature = temperature + received / (1000 + (reactableFuel * 10))
- if temperature > 2500 then
- temperature = 2500
- end
- end
- elseif reactorState == STATE_RUNNING or reactorState == STATE_STOPPING then
- local tempFactor = 1
- if temperature > 15000 then
- tempFactor = 1 - math.min(1, (temperature - 15000) / 10000)
- end
- shieldCharge = shieldCharge + math.min(rf * (1 - (shieldCharge / maxShieldCharge)), maxShieldCharge - shieldCharge) * tempFactor
- if shieldCharge > maxShieldCharge then
- shieldCharge = maxShieldCharge
- end
- if reactorState == STATE_STOPPING then
- energyStorage = energyStorage - rf
- end
- return rf
- end
- return received
- end
- local previousTime = computer.uptime()
- local updateSpeed = 0.05
- function update ()
- local currentTime = computer.uptime()
- local elapsed = currentTime - previousTime
- if elapsed >= updateSpeed then
- previousTime = currentTime
- local nbTick = elapsed / updateSpeed
- for i=1,math.floor(nbTick) do
- updateCoreLogic()
- if inputRate > 0 then injectEnergy(inputRate) end
- if outputRate > 0 then saturation = math.floor(saturation - outputRate) end
- if saturation < 0 then saturation = 0 end
- end
- end
- end
- function setUpdateSpeed(speed)
- updateSpeed = 0.05 / speed
- end
- function chargeReactor ()
- if canCharge() then reactorState = STATE_WARMING_UP end
- end
- function activateReactor ()
- if canActivate() then reactorState = STATE_RUNNING end
- end
- function getReactorInfo ()
- local info = {
- energySaturation = saturation,
- fieldStrength = math.floor(shieldCharge),
- fuelConversion = convertedFuel,
- temperature = math.ceil(temperature),
- maxEnergySaturation = maxSaturation,
- fuelConversionRate = math.floor(fuelUseRate * 1000000),
- fieldDrainRate = fieldDrain,
- maxFuelConversion = reactableFuel + convertedFuel,
- generationRate = math.floor(generationRate),
- maxFieldStrength = math.floor(maxShieldCharge),
- failsafe = failSafeMode,
- riseAmount = riseAmount * 10 -- (Stamper) added
- }
- if reactorState == STATE_RUNNING then info.status = "running"
- elseif reactorState == STATE_WARMING_UP then info.status = "warming_up"
- elseif reactorState == STATE_COLD then info.status = "cold"
- elseif reactorState == STATE_COOLING then info.status = "cooling"
- elseif reactorState == STATE_STOPPING then info.status = "stopping"
- elseif reactorState == STATE_BEYOND_HOPE then info.status = "beyond_hope"
- else info.status = "invalid"
- end
- return info
- end
- function setFailSafe (activate)
- failSafeMode = activate
- end
- function stopReactor ()
- shutdownReator()
- end
- fluxGates =
- {
- input =
- {
- setOverrideEnabled = function () end,
- getFlow = function () return inputRate end,
- setFlowOverride = function (flow)
- inputRate = flow
- end
- },
- output =
- {
- setOverrideEnabled = function () end,
- getFlow = function () return outputRate end,
- setFlowOverride = function (flow)
- outputRate = flow
- end
- }
- }
- function setFuel(fuel, convertedRatio)
- if convertedRatio > 1 then convertedRatio = 1 elseif convertedRatio < 0 then convertedRatio = 0 end
- if fuel > 10368 then
- reactableFuel = 10368
- elseif fuel < 0 then
- reactableFuel = 0
- else
- reactableFuel = fuel
- end
- convertedFuel = convertedRatio * reactableFuel
- reactableFuel = reactableFuel - convertedFuel
- end
- function setEnergyStorage(storage)
- if storage < 0 then storage = 0 end
- energyStorage = storage
- end
- function getEnergyStorage()
- return energyStorage
- end
- function reset()
- reactorState = STATE_COLD
- startupInitialized = false
- reactableFuel = 0
- convertedFuel = 0
- failSafeMode = false
- temperature = 20
- saturation = 0
- maxSaturation = 0
- shieldCharge = 0
- maxShieldCharge = 0
- inputRate = 0
- outputRate = 0
- end
- return reactor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement