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: Difference between revisions

From Stationeers Community Wiki
RA2lover (talk | contribs)
No edit summary
RA2lover (talk | contribs)
mNo edit summary
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:
local Gas = require("Module:Gas")
local Gas = require("Module:Gas")


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


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


     return table.concat(InputReagents, " + ").." = "..table.concat(OutputReagents, " + ")..FormattedCombustionEnergy(Fuel, Oxidizer)
    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
end


local function FormattedCombustionEnergy(...)
function p.FormattedCombustionEnergy(...)
     local Energy = p.GetCombustionEnergy(...)
     local Energy = p.GetCombustionEnergy(...)
     if Energy==0 then return "" end
     if Energy==0 then return "" end
Line 38: Line 50:
function p.GetCombustionEnergy(Fuel, Oxidizer)
function p.GetCombustionEnergy(Fuel, Oxidizer)
     if Fuel.Gas.IsHypergol then return Fuel.Gas.Enthalpy*Fuel.Amount end
     if Fuel.Gas.IsHypergol then return Fuel.Gas.Enthalpy*Fuel.Amount end
     if not Oxidizer then Oxidizer = {Gas = Gas.GetGas("Oxygen"), Amount = 1} end
     if not Oxidizer or not Oxidizer.Gas then Oxidizer = {Gas = Gas.GetGas("Oxygen"), Amount = 1} end
      
      
     local TotalEnthalpy = Fuel.Gas.Enthalpy * Fuel.Amount
     local TotalEnthalpy = Fuel.Gas.Enthalpy * Fuel.Amount * Oxidizer.Gas.EnthalpyMultiplier


     if Fuel.Gas.State == "Liquid" then --vaporize liquid fuels
     if Fuel.Gas.State == "Liquid" then --vaporize liquid fuels
Line 68: Line 80:
end
end


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


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


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


return p
return p

Latest revision as of 22:52, 16 March 2026

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)
    mw.log("Frame: ")
    mw.logObject(frame)
    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 and Oxidizer.Gas 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 or not Oxidizer.Gas 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
        local oxidizer, oxidizerAmount = ParseInput(frame.args.Oxidizer)
    end
    
    
    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(frame, {Gas = fuel, Amount = fuelAmount}, {Gas = oxidizer, Amount = oxidizerAmount}, outputs)
end

return p