Module:Elderfield/Recipe

From Welcome to Elderfield Wiki
Jump to navigation Jump to search
-- Module:Elderfield/Recipe
--
-- Recipe tables, grouped by crafting station or by the item they produce or consume.

local u = require('Module:Elderfield/Utility')
local p = {}

-- Each part with its icon first, as the crafting screen shows it: the picture is what a
-- player knows an ingredient by. A part with no icon of its own is just its name.
local function refList(list)
	local out = {}
	for _, r in ipairs(list or {}) do
		local label = r.name or ''
		if r.amount and r.amount > 1 then label = label .. ' ×' .. r.amount end
		local named = r.title and u.link(r.title, label) or label
		local icon = u.icon(r.icon)
		out[#out + 1] = icon ~= '' and (icon .. ' ' .. named) or named
	end
	return table.concat(out, ', ')
end

--- Collects recipes matching a predicate.
local function collect(pred)
	local list = {}
	for _, rec in pairs(u.data('Recipes')) do
		if pred(rec) then list[#list + 1] = rec end
	end
	table.sort(list, function(a, b) return (a.name or '') < (b.name or '') end)
	return list
end

local function renderTable(list, showStation)
	local rows = {}
	for _, rec in ipairs(list) do
		local row = { rec.name, refList(rec.ingredients), refList(rec.products) }
		if showStation then table.insert(row, 2, rec.station or '') end
		row[#row + 1] = rec.level and tostring(rec.level) or ''
		row[#row + 1] = rec.unlock == 'known-from-start' and 'Known from the start'
			or (rec.unlock == 'no-known-unlock' and "''No known way to learn this''" or 'Taught')
		rows[#rows + 1] = row
	end
	local headers = { { label = 'Recipe' } }
	if showStation then headers[#headers + 1] = { label = 'Station' } end
	headers[#headers + 1] = { label = 'Ingredients' }
	headers[#headers + 1] = { label = 'Makes' }
	headers[#headers + 1] = { label = 'Level', align = 'right' }
	headers[#headers + 1] = { label = 'Unlock' }
	return u.table(headers, rows, 'elderfield-recipes')
end

--- {{#invoke:Elderfield/Recipe|station|name=Forge}}
function p.station(frame)
	local name = u.arg(frame, 'name')
	if not name then return u.missing('Station', '(no name given)') end
	return renderTable(collect(function(r) return r.station == name end), false)
end

--- {{#invoke:Elderfield/Recipe|makes|item=Furnace}}
function p.makes(frame)
	local item = u.arg(frame, 'item')
	return renderTable(collect(function(r)
		for _, prod in ipairs(r.products or {}) do
			if prod.name == item then return true end
		end
		return false
	end), true)
end

--- {{#invoke:Elderfield/Recipe|uses|item=Stone}}
function p.uses(frame)
	local item = u.arg(frame, 'item')
	return renderTable(collect(function(r)
		for _, ing in ipairs(r.ingredients or {}) do
			if ing.name == item then return true end
		end
		return false
	end), true)
end

--- {{#invoke:Elderfield/Recipe|unreachable}} -- recipes with no known unlock
function p.unreachable(frame)
	return renderTable(collect(function(r) return r.unlock == 'no-known-unlock' end), true)
end

return p