Jump to content
Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Documentation for this module may be created at Module:Monster/doc

-- The monster infobox of Template:Monster: name, rank, kind, and a table with one column per difficulty.
local p = {}

local DIFFS = { { 'h', 'Hatred' }, { 't', 'Terror' }, { 'd', 'Destruction' } }
local RANKS = {
	normal = 'Normal', elite = 'Elite', heroic = 'Heroic', veteran = 'Veteran', boss = 'Boss',
	superunique = 'Superunique', special = 'Special',
}
-- rows of the table: parameter suffix, label, one value per line
local ROWS = {
	{ 'level', 'Level' }, { 'life', 'Life' }, { 'defense', 'Defense' }, { 'tohit', 'Attack rating' },
	{ 'damage', 'Damage', true }, { 'resist', 'Resistances', true }, { 'immune', 'Immune to' },
	{ 'exp', 'Experience' }, { 'props', 'Special', true },
}

local function arg(a, name)
	local v = a[name]
	if v and mw.text.trim(v) ~= '' then
		return mw.text.trim(v)
	end
	return nil
end

local function lines(text)
	local out = {}
	for line in mw.text.gsplit(text or '', '\n', true) do
		line = mw.text.trim(line)
		if line ~= '' then
			table.insert(out, line)
		end
	end
	return out
end

function p.infobox(frame)
	local a = frame:getParent().args
	local rank = mw.ustring.lower(arg(a, 'rank') or 'normal')
	if not RANKS[rank] then
		rank = 'normal'
	end
	local box = mw.html.create('div'):addClass('d2-mon'):addClass('d2-mon-' .. rank)
	if arg(a, 'inline') then
		box:addClass('d2-mon-inline')
	end
	if arg(a, 'image') then
		box:tag('div'):addClass('d2-mon-image'):wikitext('[[File:' .. a.image .. '|link=|alt=]]')
	end
	box:tag('div'):addClass('d2-mon-name'):wikitext(arg(a, 'name') or mw.title.getCurrentTitle().text)
	local kind = {}
	for _, v in ipairs({ RANKS[rank], arg(a, 'category'), arg(a, 'type') }) do
		if v then
			table.insert(kind, v)
		end
	end
	box:tag('div'):addClass('d2-mon-kind'):wikitext(table.concat(kind, ' · '))
	if arg(a, 'class') then
		box:tag('div'):addClass('d2-mon-class'):wikitext(arg(a, 'class'))
	end

	local tbl = box:tag('table'):addClass('d2-mon-table')
	local head = tbl:tag('tr')
	head:tag('th')
	for _, d in ipairs(DIFFS) do
		head:tag('th'):addClass('d2-mon-' .. d[1]):wikitext(d[2])
	end
	for _, row in ipairs(ROWS) do
		local any = false
		for _, d in ipairs(DIFFS) do
			if arg(a, d[1] .. '_' .. row[1]) then
				any = true
			end
		end
		if any then
			local tr = tbl:tag('tr')
			tr:tag('th'):wikitext(row[2])
			for _, d in ipairs(DIFFS) do
				local v = arg(a, d[1] .. '_' .. row[1])
				local td = tr:tag('td')
				if not v then
					td:wikitext('–')
				elseif row[3] then
					for _, line in ipairs(lines(v)) do
						td:tag('div'):wikitext(line)
					end
				else
					td:wikitext(v)
				end
			end
		end
	end

	local function section(label, text, cls)
		if text then
			local div = box:tag('div'):addClass('d2-mon-section'):addClass(cls or '')
			div:tag('div'):addClass('d2-mon-label'):wikitext(label)
			div:tag('div'):wikitext(text)
		end
	end
	section('Skills', arg(a, 'skills'))
	section('Modifiers', arg(a, 'mods'))
	for _, d in ipairs(DIFFS) do
		section('Areas (' .. d[2] .. ')', arg(a, d[1] .. '_areas'), 'd2-mon-areas')
	end
	if arg(a, 'note') then
		box:tag('div'):addClass('d2-mon-note'):wikitext(a.note)
	end
	return frame:extensionTag('templatestyles', '', { src = 'Template:Monster/styles.css' }) .. tostring(box)
end

return p