Module:Item recipes

From Plan B Terraform Wiki
Jump to navigation Jump to search

Examples

{{#invoke:Item_recipes|main|Factory}}

How to make it

Assembly Plant Assembly Plant
Ingredients Products Duration in days Nb per year
Mechanical Parts Mechanical Parts Factory Factory 25 12

What it can make

Factory Factory
Ingredients Products Duration in days Nb per year
Iron Ore Iron Ore Steel Bar Steel Bar 10 30
Steel Bar Steel Bar Mechanical Parts Mechanical Parts 10 30
Sulfur Sulfur Concrete Concrete 10 30
Concrete Concrete + Steel Bar Steel Bar Reinforced Concrete Reinforced Concrete 10 30
Aluminum Ore Aluminum Ore Aluminum Bar Aluminum Bar 10 30
Carbon Carbon Polymer Bar Polymer Bar 10 30
Aluminum Bar Aluminum Bar + Polymer Bar Polymer Bar High-tech Parts High-tech Parts 10 30
Carbon Carbon + Nitrogen Nitrogen Compost Compost 10 30
Steel Bar Steel Bar + Nitrogen Nitrogen Tempered Steel Tempered Steel 10 30
Tempered Steel Tempered Steel + Polymer Bar Polymer Bar Composite Composite 10 30
{{#invoke:Item_recipes|main|Steel Bar}}

How to make it

Factory Factory
Ingredients Products Duration in days Nb per year
Iron Ore Iron Ore Steel Bar Steel Bar 10 30
Recycling Center Recycling Center
Ingredients Products Duration in days Nb per year
Metal Waste Metal Waste Steel Bar Steel Bar + Aluminum Bar Aluminum Bar 10 30

What to make from it

Factory Factory
Ingredients Products Duration in days Nb per year
Steel Bar Steel Bar Mechanical Parts Mechanical Parts 10 30
Concrete Concrete + Steel Bar Steel Bar Reinforced Concrete Reinforced Concrete 10 30
Steel Bar Steel Bar + Nitrogen Nitrogen Tempered Steel Tempered Steel 10 30
Assembly Plant Assembly Plant
Ingredients Products Duration in days Nb per year
Steel Bar Steel Bar Extractor Extractor 25 12
Steel Bar Steel Bar Depot Depot 25 12
Concrete Concrete + Steel Bar Steel Bar Rail Rail 25 12
Steel Bar Steel Bar + 3× Polymer Bar Polymer Bar Greenhouse Greenhouse 25 12
{{#invoke:Item_recipes|main|Supply Center}}

How to make it

Assembly Plant Assembly Plant
Ingredients Products Duration in days Nb per year
Concrete Concrete Supply Center Supply Center 25 12

What it can make

Supply Center Supply Center
Ingredients Products Duration in days Nb per year Population
Concrete Concrete - 5 → 23.2 60 → 12.9 1 → 9
Concrete Concrete + Oxygen Oxygen - 6.7 → 33.1 45 → 9.1 10 → 99
Reinforced Concrete Reinforced Concrete + Oxygen Oxygen - 15 → 74.9 20 → 4 100 → 999
Reinforced Concrete Reinforced Concrete + Oxygen Oxygen + High-tech Parts High-tech Parts - 37.5 → 187.5 8 → 1.6 1000 → 9999
Reinforced Concrete Reinforced Concrete + Oxygen Oxygen + High-tech Parts High-tech Parts + Food Food - 100 → 500 3 → 0.6 10000 → 99999
Composite Composite + Oxygen Oxygen + High-tech Parts High-tech Parts + Food Food - 200 → - 1.5 → - 100000 → ∞

local p={}

function HasAUse(name, recipe)
    for i=1,7,2 do
        if recipe.inputs[i] == name then return true end
    end
    return false
end

function IsMadeWith(name, recipe)
    for i=1,7,2 do
        if recipe.outputs[i] == name then return true end
    end
end

function Produces(name, recipe)
	return recipe.factory == name
end

function CreateTabHeader(factory, frame, is_city)
    s = '\n{| class="wikitable sortable"\n'
        .. '! colspan=5 | ' .. frame:expandTemplate{title = 'item', args = {factory}} .. '\n'
        -- .. '! colspan=2 | ' .. string.format("{item|%s}", factory) .. '\n'
        .. '|-\n'
        .. '! Ingredients !! Products !! Duration in days !! Nb per year '
    if is_city then s = s .. ' !! Population' end
    return s .. '\n'
end

function CreateTableLine(recipe, frame)
    str = '|-\n| '
    if #recipe.inputs == 0 then str = str .. '-' end
    for i=1,7,2
    do
        if recipe.inputs[i] ~= nil then
            if i ~= 1 then str = str .. ' + ' end
            if recipe.inputs[i + 1] > 1 then str = str .. string.format('%d× ', recipe.inputs[i + 1]) end
            str = str .. frame:expandTemplate{title = 'item', args = {recipe.inputs[i]}}
            --str = str .. string.format("{item|%s}", recipe.inputs[i])
        end
    end
    str = str .. ' || '
    if #recipe.outputs == 0 then str = str .. '-' end
    for i=1,7,2
    do
        if recipe.outputs[i] ~= nil then
            if i ~= 1 then str = str .. ' + ' end
            if recipe.outputs[i + 1] > 1 then str = str .. string.format('%d× ', recipe.outputs[i + 1]) end
            str = str .. frame:expandTemplate{title = 'item', args = {recipe.outputs[i]}}
            --str = str .. string.format("{item|%s}", recipe.outputs[i])
        end
    end
    str = str .. ' || '
    if string.find(recipe.frequency, '*') then
    	str = str .. '<span title="Duration is capped at 30 days" style="border-bottom:1px dotted">' .. recipe.duration .. '</span>'
    		 .. ' || <span title="Nb per year is capped at 10" style="border-bottom:1px dotted">' .. recipe.frequency:sub(0, #recipe.frequency - 1) .. '</span>'
    else str = str .. recipe.duration .. ' || ' .. recipe.frequency end
    if recipe.pop ~= nil then str = str .. ' || ' .. recipe.pop end
    return str .. '\n'
end

function CreateTableContent(frame, name, condition, recipes, title)
	local tab = {}
	local found = false
	local needExplanation = false
	for i, recipe in ipairs(recipes)
    do
        if condition(name, recipe)
        then
            found = true
            factory = recipe.factory
            if tab[factory] == nil then tab[factory] = CreateTabHeader(factory, frame, recipe.pop ~= nil) end
            tab[factory] = tab[factory] .. CreateTableLine(recipe, frame)
        end
    end
    finalStr = ''
    for i, factory in pairs(tab)
    do
        finalStr = finalStr .. factory .. '|}'
        --if string.find(factory, '*') ~= nil then finalStr = finalStr .. '<i style="font-size: 80%">* duration is capped at 30 days</i>' end
    end
    return finalStr, found
end

function p.main(frame, cfg)
	local recipes = require('Module:Item_recipes_data')
    local name = frame.args[1]
    
    usedTo_table, hasAUse = CreateTableContent(frame, name, HasAUse, recipes, 'using')
    madeWith_table, isMadeWith = CreateTableContent(frame, name, IsMadeWith, recipes, 'producing')
    produces_table, produce = CreateTableContent(frame, name, Produces, recipes, 'made by')
    
    str = ''
    if isMadeWith then str = str .. '\n==How to make it==\n' .. madeWith_table end
    if hasAUse then str = str .. '\n==What to make from it==\n' .. usedTo_table end
    if produce then str = str .. '\n==What it can make==\n' .. produces_table end
	return str:sub(2) -- removing first charachter which is a breakline
end

return p