Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Combustion

From Stationeers Community Wiki
Revision as of 22:52, 15 March 2026 by RA2lover (talk | contribs) (Add optional ShowAutoignition parameter)

Documentation for this module may be created at Module:Combustion/doc

local GasData = mw.loadData("Module:Gas/data")
local Gas = require("Module:Gas")

local function GetRepresentation(gas, frame)
    mw.log("GetRepresentation called. gas:")
    mw.logObject(gas)
    return frame:expandTemplate({title = "GasIcon", args = {gas.Name} }) -- Embeds a Not-yet-implemented template.
end

local p={}
local function RenderCombustionTemplate(frame, Fuel, Oxidizer, Reagents)
    --TODO: Add actual formatting into a box or something.
    local InputReagents = {}
   
    local FuelRepresentation = GetRepresentation(Fuel.Gas, frame)
    table.insert (InputReagents, Fuel.Amount.."x "..FuelRepresentation)
    if Oxidizer then
        local OxidizerRepresentation = GetRepresentation(Oxidizer.Gas, frame)
        table.insert (InputReagents, Oxidizer.Amount.."x "..OxidizerRepresentation)
    end
   
    local OutputReagents = {}
    for n=1,#Reagents do
        local ReagentRepresentation = GetRepresentation(Reagents[n].Gas, frame)
        table.insert(OutputReagents, Reagents[n].Amount.."x "..ReagentRepresentation)
    end    

    local Autoignition=""
    if frame.args.ShowAutoignition then
        local T = 0

        if Fuel.Gas.IsHypergol then
             T = Fuel.Gas.AutoignitionTemperature
        else T = Fuel.Gas.AutoignitionTemperature + Oxidizer.Gas.AutoignitionOffset end

        Autoignition = " (Autoignites at "..frame:ExpandTemplate({title="Temperature", args={T}})..")"
    end
    return table.concat(InputReagents, " + ").." = "..table.concat(OutputReagents, " + ")..p.FormattedCombustionEnergy(Fuel, Oxidizer)..Autoignition
end

function p.FormattedCombustionEnergy(...)
    local Energy = p.GetCombustionEnergy(...)
    if Energy==0 then return "" end
    if Energy<0 then return " - ".. -Energy/1000 .." kJ" end
    if Energy>0 then return " + ".. Energy/1000 .." kJ" end
end

function p.GetCombustionEnergy(Fuel, Oxidizer)
    if Fuel.Gas.IsHypergol then return Fuel.Gas.Enthalpy*Fuel.Amount end
    if not Oxidizer then Oxidizer = {Gas = Gas.GetGas("Oxygen"), Amount = 1} end
    
    local TotalEnthalpy = Fuel.Gas.Enthalpy * Fuel.Amount * Oxidizer.Gas.EnthalpyMultiplier

    if Fuel.Gas.State == "Liquid" then --vaporize liquid fuels
        TotalEnthalpy = TotalEnthalpy - (Fuel.Gas.LatentHeat * Fuel.Amount)
    end

    if Oxidizer.Gas.State == "Liquid" then --vaporize liquid oxidizers
        TotalEnthalpy = TotalEnthalpy - (Oxidizer.Gas.LatentHeat * Oxidizer.Amount)
    end

    return TotalEnthalpy

end

local function ParseInput(str)
    mw.log("ParseInput called on "..str) 
    local gas, amount, remainder = string.match(str, "^[,;:-]?%s*([%a%s]+)%s*[,;:-]?%s*(%d+)(.*)$")
    --"-Liquid Sodium Chloride , 2; Hydrochloric Acid = 1" should result in "Liquid Sodium Chloride", 2, "; Hydrochloric Acid = 1"
    if not gas then return nil end
    mw.log("Gas parsed:")
    mw.logObject(gas)
    mw.log("Amount parsed:")
    mw.logObject(amount)
    mw.log("Remainder:")
    mw.logObject(remainder)
    return Gas.GetGas(gas), amount, remainder
end

function p.Template(frame)
    frame = frame:getParent(frame)

    if not frame.args.Fuel then error("Missing 'Fuel' parameter") end
    local fuel, fuelAmount = ParseInput(frame.args.Fuel)
    if not fuelAmount then error("Missing quantity for 'Fuel' parameter") end

    local oxidizer, oxidizerAmount
    if not fuel.IsHypergol then
        if not frame.args.Oxidizer then error("Missing 'Oxidizer' parameter") end
    end
    local oxidizer, oxidizerAmount = ParseInput(frame.args.Oxidizer)
    
    local outputs = {}
    local outputsCount = 0
    for k,v in pairs(frame.args) do
        if string.match(k, "^Output") then
            outputsCount = outputsCount + 1
            local output, outputAmount = ParseInput(v)
            if not outputAmount then error ("Missing quantity for '"..k.."' parameter") end
            outputs[outputsCount] = {Gas = output, Amount = outputAmount}
        end
    end
 
    return RenderCombustionTemplate(input, {Gas = fuel, Amount = fuelAmount}, {Gas = oxidizer, Amount = oxidizerAmount}, outputs)
end

return p