FANDOM


-- <pre>
-- ( ͡° ͜ʖ ͡°)
local p = {}
 
-- "imports"
local paramtest = require('Module:Paramtest')
local yesno = require('Module:Yesno')
local commas = require('Module:Addcommas')._add
local infobox = require('Module:Infobox')
 
--[[
 -- Mapping
--]]
local attack_styles = {
	melee = { image = 'Attack-icon', link = 'Melee' },
	ranged = { image = 'Ranged-icon', link = 'Ranged' },
	magic = { image = 'Magic-icon', link = 'Magic' },
	dragonfire = { image = 'Dragonfire icon', link = 'Dragonfire' },
	na = { image = 'Zero weakness icon', link = '' },
	typeless = { image = 'Zero weakness icon', link = '' }
}
 
local styles_map = {
	melee = 'melee',
	magic = 'magic',
	mage = 'magic',
	range = 'ranged',
	ranged = 'ranged',
	ranging = 'ranged',
	dragonfire = 'dragonfire',
	['dragon fire'] = 'dragonfire',
	dragonbreath = 'dragonfire',
	['dragon breath'] = 'dragonfire',
	['n/a'] = 'na',
	na = 'na',
	none = 'na'
}
 
-- load weaknesses instead of remapping
local weaknesses = mw.loadData('Module:Weakness clickpic/data')
 
-- Main function called with invokes
function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)
	local all_args = { 'name', 'immune_to_poison', 'immune_to_deflect',
		'immune_to_stun', 'immune_to_drain', 'style', 'weakness', 'abilities', 'level', 'lifepoints', 'image' }
 
	-- Quickly try to replace blanks with <param1> just in case
	for _, v in ipairs(all_args) do
		if paramtest.is_empty(args[v]) then
			local _v = args[v..'1']
			if paramtest.has_content(_v) then
				args[v] = _v
			end
		end
	end
 
	local yn_args = {
		'immune_to_poison', 'immune_to_deflect',
		'immune_to_stun', 'immune_to_drain'
	}
	local num_args = {
		'level' }
	for _, v in ipairs(yn_args) do
		ret:defineParams{ { name = v, func = boolargs } }
	end
	for _, v in ipairs(num_args) do
		ret:defineParams{ { name = v, func = { name = numberargs, params = { v, v }, flag = { 'p', 'r' } } }}
	end
	ret:defineParams{
		{ name = 'style', func = stylearg },
		{ name = 'lifepoints', func = numargcommas },
		{ name = 'weakness', func = weaknessarg },
		{ name = 'abilities', func = abilarg },
	}
 
	ret:defineParams {
		{ name = 'name', func = namelink },
		{ name = 'image',
			func = function(arg) return paramtest.has_content(arg) and arg or '<i>No image</i>' end },
	}
	ret:noSwitch()
	ret:create()
	ret:cleanParams()
	ret:addClass('infobox-monster')
	ret:defineName('Infobox Monster')
	-- PARAMETER: image
	ret:addRow{
		{ tag = 'argd', content = 'image', class='infobox-image', colspan = '4' } }
 
	-- COMBAT INFO
	-- PARAMETER: level | lifepoints
		:addRow{ { tag = 'th', content = '[[Combat level|Level]]', css = { width = '50%' }, colspan = '2' },
				{ tag = 'th', content = '[[Life points|LP]]', css = { width = '50%' }, colspan = '2' } }
		:addRow{ { tag = 'argd', content = 'level', colspan = '2' },
				{ tag = 'argd', content = 'lifepoints', colspan = '2' } }
	-- PARAMETER: style | speed
		:addRow{ { tag = 'th', content = 'Style', colspan = '2' },
				{ tag = 'argd', content = 'style', colspan = '2' } }
	-- PARAMETER: abilities
	-- only add if they exist
	local abilities = ret:param('abilities','f')
	local _abilities = false
	if abilities.d and abilities.d ~= 'None' then
		_abilities = true
	end
	if abilities.switches then
		for _, v in ipairs(abilities.switches) do
			if v ~= _nil then
				_abilities = true
				break
		   end
		end
	end
	if _abilities then
		ret:addRow{ { tag = 'th', content = 'Abilities used', class = 'offensive-subheader', colspan = '4' } }
			:addRow{ { tag = 'argd', content = 'abilities', colspan = '4' } }
 
	end
	-- DEFENSIVE STATS
	-- PARAMETER: weakness
	ret:addRow{ { tag = 'th', content = 'Weakness', colspan = '2' },
				{ tag = 'argd', content = 'weakness', colspan = '2' } }
 
	-- PARAMETER: immune_to_poison | immune_to_deflect | immune_to_stun | immune_to_drain
		:addRow{ { tag = 'th', content = 'Immunities', class = 'defensive-subheader', colspan = '4' } }
		:addRow{ { tag = 'th', content = '[[File:Immune to poison.png|link=]]', title = 'Immune to poison' },
				{ tag = 'th', content = '[[File:Immune to deflect.png|link=]]', title = 'Immune to deflect' },
				{ tag = 'th', content = '[[File:Immune to stun.png|link=]]', title = 'Immune to stun' },
				{ tag = 'th', content = '[[File:Immune to drain.png|link=]]', title = 'Immune to stat drain' } }
		:addRow{ { tag = 'argd', content = 'immune_to_poison' },
				{ tag = 'argd', content = 'immune_to_deflect' },
				{ tag = 'argd', content = 'immune_to_stun' },
				{ tag = 'argd', content = 'immune_to_drain' } }
	-- Bottom link
		:addRow{ { tag = 'argd', content = 'name', colspan = '4' } }
	ret:finish()
	return ret:tostring()
end
 
function namelink(arg)
	arg = arg or ''
	return '<i>[['..arg..'|For full info, see here]]</i>'
end
 
-- For numerical args
function numberargs(arg,v)
	local arg_v = (arg or ''):find('%S') and string.gsub(arg,',','') or -1
	local arg_i
	if arg_v == -1 then
		arg_i = nil
	elseif string.lower(arg_v) == 'n/a' then
		arg_i = 'N/A'
	elseif string.lower(arg_v) == 'varies' then
		arg_i = 'Varies'
	else
		arg_i = tonumber(arg_v:gsub(',',''),10)
		if not arg_i then
			arg_i = badarg(v,'should be a single numerical value.')
		end
	end
	return arg_i
end
 
-- For numbers (adds commas)
function numargcommas(arg)
	local ret = numberargs(arg)
	if type(ret) == 'number' then
		return commas(ret)
	else
		return ret
	end
end
 
-- For true/false
function boolargs(arg)
	local arg_v = (arg or ''):find('%S') and arg or -1
	local arg_i
	if arg_v == -1 then
		arg_i = nil
	else
		arg_v = yesno(arg_v)
		if arg_v then
			arg_i = '[[File:Yes check.svg|20px|alt=Yes|link=]]'
		else
			arg_i = '[[File:X mark.svg|20px|alt=No|link=]]'
		end
	end
	return arg_i
end
 
-- style
function stylearg(arg)
	-- split by commas
	local atts = mw.text.split(string.lower(arg or ''),'%s*,%s*')
	local _atts = {}
	-- remake the list as a table, remove anything that's blank/doesn't exist
	for _, v in ipairs(atts) do
		local att_x = styles_map[v]
		if att_x then
			table.insert(_atts,attack_styles[att_x])
		end
	end
	local p_att
	if #_atts == 0 then
		p_att = nil
	else
		p_att = {}
		for _, v in ipairs(_atts) do
			table.insert(p_att,string.format('[[File:%s.png|25px|link=%s]]',v.image,v.link))
		end
		p_att = table.concat(p_att,' ')
	end
	return p_att
end
 
-- weakness
function weaknessarg(arg)
	-- split by commas
	local wk = mw.text.split(string.lower(arg or ''),'%s*,%s*')
	local _wk = {}
 
	-- remake the list as a table, remove anything that's blank/doesn't exist
	for _, v in ipairs(wk) do
		local wk_x = weaknesses[v]
		if wk_x then
			table.insert(_wk,wk_x)
		end
	end
	local p_wk
	if #_wk == 0 then
		p_wk = nil
	else
		p_wk = {}
		for _, v in ipairs(_wk) do
			table.insert(p_wk,string.format('[[File:%s|25px|link=%s]]',v.image,v.link))
		end
		p_wk = table.concat(p_wk,' ')
	end
	return p_wk
end
 
-- Abilities used
function abilarg(arg)
	arg = paramtest.default_to(arg,false)
	if not arg then
		arg = 'None'
	elseif arg:find('clickpic') then
		arg = mw.getCurrentFrame():preprocess(arg)
	else
		arg = 'None'
	end
	return arg
end
 
-- red ERR span with title hover for explanation
function badarg(argname, argmessage)
	return '<span '..
			'title="The parameter «'..argname..'» '..argmessage..'" '..
			'style="color:red; font-weight:bold; cursor:help; border-bottom:1px dotted red;">'..
			'ERR</span>'
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.