Модуль:Chemistry Lookup

Материал из DarkStation Wiki

Для документации этого модуля может быть создана страница Модуль:Chemistry Lookup/doc

local p = {}

-- Other functions omitted for brevity

function p.buildreaction(frame, react)
    local data = p.react[react]
    local dest = "Chemistry"
    local args = {}
    local i = 0
    
    -- Processing reactants
    for k, v in pairs(data.reactants) do
        i = i + 1
        local dest = "Chemistry"
        if p.chem[k] and p.groupDirection[p.chem[k].group] ~= nil then
            dest = p.groupDirection[p.chem[k].group]
        end
        args["component-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v.amount, dest = dest }}
    end
    
    -- Processing products
    i = 0
    for k, v in pairs(data.products) do
        i = i + 1
        local dest = "Chemistry"
        if p.chem[k] and p.groupDirection[p.chem[k].group] ~= nil then
            dest = p.groupDirection[p.chem[k].group]
        end
        args["result-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v, dest = dest }}
    end
    
    -- Processing effects
    if data.effects ~= nil then
        args.effects = p.geneffectlist(data.effects, frame, 1)
    end

    return frame:expandTemplate{ title = "Chem Box Recipe", args = args }
end

-- Function to generate effect list
function p.geneffectlist(effects, frame, rate)
    local out = ""
    for l, w in pairs(effects) do
        if w.id == "HealthChange" then
            out = out .. ":" .. p.genhealthchange(w, rate, frame) .. "\n"
        elseif w.id == "AdjustReagent" then
            out = out .. ":" .. p.genadjustreagent(w, rate, frame) .. "\n"
        elseif w.id == "FlammableReaction" then
            out = out .. ":" .. p.genflammablereaction(w, frame) .. "\n"
        elseif w.id == "AdjustTemperature" then
            out = out .. ":" .. p.genadjusttemperature(w, frame) .. "\n"
        elseif w.id == "GenericStatusEffect" then
            out = out .. ":" .. p.gengenericstatuseffect(w, frame) .. "\n"
        elseif w.id == "ExplosionReactionEffect" then
            out = out .. ":" .. p.genexplosionreactioneffect(w, frame) .. "\n"
        elseif w.id == "FoamAreaReactionEffect" then
            out = out .. ":" .. p.genfoamareareactioneffect(w, frame) .. "\n"
        elseif w.id == "SmokeAreaReactionEffect" then
            out = out .. ":" .. p.gensmokeareareactioneffect(w, frame) .. "\n"
        elseif w.id == "ModifyBleedAmount" then
            out = out .. ":" .. p.genmodifybleedamount(w, frame) .. "\n"
        end
    end
    return out
end

-- Function to handle SmokeAreaReactionEffect
function p.gensmokeareareactioneffect(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "SmokeAreaReactionEffect", args = { when = conds, prob = r.probability }}
end

-- Function to handle FoamAreaReactionEffect
function p.genfoamareareactioneffect(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "FoamAreaReactionEffect", args = { when = conds, prob = r.probability }}
end

-- Function to handle ExplosionReactionEffect
function p.genexplosionreactioneffect(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "ExplosionReactionEffect", args = { when = conds, prob = r.probability }}
end

-- Function to handle GenericStatusEffect
function p.gengenericstatuseffect(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "GenericStatusEffect", args = { key = r.Key, type = r.Type, time = r.Time, refresh = r.Refresh, when = conds, prob = r.probability }}
end

-- Function to handle AdjustTemperature
function p.genadjusttemperature(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "AdjustTemperature", args = { amount = r.Amount, when = conds, prob = r.probability }}
end

-- Function to handle FlammableReaction
function p.genflammablereaction(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds, prob = r.probability }}
end

-- Function to handle AdjustReagent
function p.genadjustreagent(r, rate, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "AdjustReagent", args = { amount = r.Amount, reagent = r.Reagent, when = conds, prob = r.probability }}
end

-- Function to handle ModifyBleedAmount
function p.genmodifybleedamount(r, frame)
    local conds = ""
    if r.conditions ~= nil then 
        conds = p.genconds(r.conditions, frame)
    end
    return frame:expandTemplate{ title = "ModifyBleedAmount", args = { amount = r.Amount, when = conds, prob = r.probability }}
end

-- Function to handle HealthChange
function p.genhealthchange(h, rate, frame)
    local healst = {}
    local dealst = {}
    local r = 1.0 / rate
    
    -- Handling damage types
    if h.damage.types ~= nil then
        for k, v in pairs(h.damage.types) do
            if v < 0 then
                healst[k] = v * r
            else
                dealst[k] = v * r
            end
        end
    end
    
    -- Handling damage groups
    if h.damage.groups ~= nil then
        for k, v in pairs(h.damage.groups) do
            if v < 0 then
                healst[k] = v * r
            else
                dealst[k] = v * r
            end
        end
    end
    
    local heals = hchangelist(healst, frame)
    local deals = hchangelist(dealst, frame)
    local conds = nil
    if h.conditions ~= nil then 
        conds = p.genconds(h.conditions, frame)
    end
    return frame:expandTemplate{ title = "HealthChange", args = { heals = heals, deals = deals, when = conds, prob = h.probability }}
end

-- Helper function for generating health change list
function hchangelist(l, frame)
    local out = ""
    local len = tablelength(l)
    local i = 0
    for k, v in pairs(l) do
        i = i + 1
        if len == i and i ~= 1 then
            out = out .. ", and "
        elseif i ~= 1 then
            out = out .. ", "
        end
        out = out .. hchange(k, v, frame)
    end
    return out
end

-- Helper function for health change
function hchange(ty, amnt, frame)
    return frame:expandTemplate{ title = "HealthModifier", args = { adj = amnt, kind = ty } }
end

-- Function to generate conditions
function p.genconds(conds, frame)
    local out = ""
    local len = tablelength(conds)
    local i = 0
    for k, v in pairs(conds) do
        i = i + 1
        if len == i and i ~= 1 then
            out = out .. ", and "
        elseif i ~= 1 then
            out = out .. ", "
        end
        out = out .. p.gencond(v, frame)
    end
    return out
end

-- Function to generate individual condition
function p.gencond(c, frame)
    if c.id == "TotalDamage" then
        return frame:expandTemplate{ title = "TotalDamage", args = { min = c.Min, max = c.Max } }
    elseif c.id == "ReagentThreshold" then
        return frame:expandTemplate{ title = "ReagentThreshold", args = { min = c.Min, max = c.Max, reagent = c.Reagent } }
    elseif c.id == "OrganType" then
        return frame:expandTemplate{ title = "OrganType", args = { shouldhave = c.ShouldHave, type = c.Type } }
    elseif c.id == "Temperature" then
        return frame:expandTemplate{ title = "Temperature", args = {  min = c.Min, max = c.Max } }
    end
    return ""
end

-- Function to get the length of a table
function tablelength(T)
    local count = 0
    for _ in pairs(T) do count = count + 1 end
    return count
end

return p