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 20:24, 14 March 2026 by RA2lover (talk | contribs) (Initial revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)
    return "{{GasIcon|"..gas.Name.."}}" -- Embeds a Not-yet-implemented template.
end

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

    return table.concat(InputReagents, " + ").." = "..table.concat(OutputReagents, " + ")..FormattedCombustionEnergy(Fuel, Oxidizer)
end

local function 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

    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)
    local gas, amount, remainder = string.match(str, "^[,;:-]?%s*(%D+)%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
    return Gas.GetGas(gas), amount, remainder
end

function p.Template(input)

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

    local oxidizer, oxidizerAmount
    if not fuel.IsHypergol then
        if not input.args.Oxidizer then error("Missing 'Oxidizer' parameter") end
    end
    local oxidizer, oxidizerAmount = ParseInput(input.args.Oxidizer)
    
    local outputs = {}
    local outputsCount = 0
    for k,v in pairs(input.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({Gas = fuel, Amount = fuelAmount}, {Gas = oxidizer, Amount = OxidizerAmount}, outputs)
end

return p