Module:Infobox Monster

From RuneScape Classic Wiki
Jump to: navigation, search

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

--------------------------
-- Module for [[Template:Infobox Monster]]
------------------------
local p = {}

local onmain = require('Module:Mainonly').on_main
local commas = require('Module:Addcommas')._add
local infobox = require('Module:Infobox')

function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'name', func = 'name'},
		{ name = 'image', func = 'image' },
		{ name = 'release', func = 'release' },
		{ name = 'members', func = 'has_content' },
		{ name = 'examine', func = 'has_content' },

		{ name = 'combat', func = { name = combat_arg, params = { 'hits', 'attack', 'strength', 'defense' } } },
		{ name = 'maxhit', func = 'has_content' },
		{ name = 'hitsxp', func = { name = hitsxp_arg, params = { 'hits', 'attack', 'strength', 'defense' } } },
		{ name = 'combatxp', func = { name = combatxp_arg, params = { 'hitsxp' } } },
		{ name = 'fatigue', func = { name = fatigue_arg, params = { 'hitsxp' } } },
		
		{ name = 'aggression', func = 'has_content' },
		{ name = 'poisonous', func = 'has_content' },
		{ name = 'poisonable', func = 'has_content' },
		{ name = 'weakness', func = 'has_content' },

		{ name = 'hits', func = 'numeric_arg' },
		{ name = 'attack', func = 'numeric_arg' },
		{ name = 'strength', func = 'numeric_arg' },
		{ name = 'defense', func = 'numeric_arg' },

		{ name = 'id', func = 'has_content' },
		{ name = 'version', func = 'has_content' },
		{ name = 'id_smw', func = { name = idsmw, params = { 'id' }, flag = 'p' } },
	}

	ret:defineLinks({ hide = true })

	ret:useSMWSubobject{
		members = 'Is members only',
		id_smw = 'NPC ID',
		combat = 'Combat level',
	}
	
	ret:useSMWOne{
		combat = 'All Combat level',
	}

	ret:customButtonPlacement(true)
	ret:create()
	ret:cleanParams()

	ret:addButtonsCaption()

	ret:defineName('Infobox Monster')
	ret:addClass('infobox-monster')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '20' }
	}

	:pad(20)
	:addRow{
		{ tag = 'argd', content = 'image', class='infobox-image infobox-full-width-content', colspan = '20' }
	}
	:pad(20)

	:addRow{
		{ tag = 'th', content = 'Released', colspan = '7' },
		{ tag = 'argd', content = 'release', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Members', colspan = '7' },
		{ tag = 'argd', content = 'members', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Examine]]', colspan= '7' },
		{ tag = 'argd', content = 'examine', colspan = '13' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Combat values', colspan = '20', class = 'infobox-subheader' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = '[[Combat level]]', colspan = '7' },
		{ tag = 'argd', content = 'combat', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Max hit', colspan = '7' },
		{ tag = 'argd', content = 'maxhit', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Hits XP', colspan = '7' },
		{ tag = 'argd', content = 'hitsxp', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Combat XP', colspan = '7' },
		{ tag = 'argd', content = 'combatxp', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Fatigue', colspan = '7' },
		{ tag = 'argd', content = 'fatigue', colspan = '13' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Combat info', colspan = '20', class = 'infobox-subheader' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Aggression', colspan = '7' },
		{ tag = 'argd', content = 'aggression', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Poisonous', colspan = '7' },
		{ tag = 'argd', content = 'poisonous', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Poisonable', colspan = '7' },
		{ tag = 'argd', content = 'poisonable', colspan = '13' }
	}
	if ret:paramDefined('weakness') then
		ret:addRow{
			{ tag = 'th', content = 'Weakness', colspan = '7' },
			{ tag = 'argd', content = 'weakness', colspan = '13' }
		}
	end

	ret:pad(20)
	:addRow{
		{ tag = 'th', content = 'Combat stats', colspan = '20', class = 'infobox-subheader' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Hits', colspan = '5', class = 'infobox-nested' },
		{ tag = 'th', content = 'Attack', colspan = '5', class = 'infobox-nested' },
		{ tag = 'th', content = 'Strength', colspan = '5', class = 'infobox-nested' },
		{ tag = 'th', content = 'Defense', colspan = '5', class = 'infobox-nested' },
	}
	:addRow{
		{ tag = 'argd', content = 'hits', colspan = '5', class = 'infobox-nested' },
		{ tag = 'argd', content = 'attack', colspan = '5', class = 'infobox-nested' },
		{ tag = 'argd', content = 'strength', colspan = '5', class = 'infobox-nested' },
		{ tag = 'argd', content = 'defense', colspan = '5', class = 'infobox-nested' },
	}
	:pad(20)

	:addRow{
		{ tag = 'th', content = 'Advanced data', class = 'infobox-subheader', colspan = '20' },
		meta = {addClass = 'advanced-data'}
	}
	:pad(20, 'advanced-data')
	:addRow{
		{ tag = 'th', content = 'Monster ID', colspan = '7' },
		{ tag = 'argd', content = 'id',  colspan = '13' },
		meta = {addClass = 'advanced-data'}
	}
	ret:pad(20, 'advanced-data')

	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

function numeric_arg(arg)
	if not infobox.isDefined(arg) or not tonumber(arg) then
		return nil
	end
	return commas(arg)
end

function numeric_arg_raw(arg)
	if not infobox.isDefined(arg) then
		return nil
	end
	return tonumber(arg)
end

-- Calculate combat level based on stats
-- floor[(hits+att+str+def)/4]
function combat_arg(hits, attack, strength, defense)
	if not infobox.isDefined(hits) or 
		not infobox.isDefined(attack) or 
		not infobox.isDefined(strength) or 
		not infobox.isDefined(defense) then
		return nil
	end
	return math.floor((hits + attack + strength + defense) / 4)
end

-- Calculate hits xp based on stats
-- where total xp is floor[(hp+2*att+2*str+2*def)/7]*2+20
function hitsxp_arg(hits, attack, strength, defense)
	if not infobox.isDefined(hits) or 
		not infobox.isDefined(attack) or 
		not infobox.isDefined(strength) or 
		not infobox.isDefined(defense) then
		return nil
	end
	local total_xp = math.floor((hits + 2*attack + 2*strength + 2*defense) / 7) * 2 + 20
	return total_xp * 0.25
end

-- Calculate combat xp, which is 3*hitsxp
function combatxp_arg(hitsxp)
	if not infobox.isDefined(hitsxp) then
		return nil
	end
	return hitsxp * 3
end

-- Calculate fatigue percentage based on hitsxp
-- floor[hitsxp*16/3]/100
function fatigue_arg(hitsxp)
	if not infobox.isDefined(hitsxp) then
		return nil
	end
	local fatigue = math.floor((hitsxp * 16) / 3) / 100
	return fatigue..'%'
end

function idsmw(id)
	if infobox.isDefined(id) then
		local r = string.gsub(id, ',', '&&SPLITPOINT&&')
		return r
	end
	return nil
end

function addcategories(args, catargs)
	local ret = { 'Monsters' }

 	-- Add the associated category if the parameter has content
	local defined_args = {
		aka = 'Pages with AKA',
		aspeed = 'Pages with aspeed'
	}
	for n, v in pairs(defined_args) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret, v)
		end
	end

 	-- Add the associated category if the parameter doesn't have content
 	local notdefined_args = {
 		image = 'Needs image',
 		members = 'Needs members status',
 		release = 'Needs release date',
 		examine = 'Needs examine added',
 		combat = 'Needs combat level',
 		id = 'Needs ID',
 	}
	for n, v in pairs(notdefined_args) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end

	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if (v ~= '') then
			ret[i] = string.format('[[Category:%s]]', v)
		end
	end

	return table.concat(ret, '')
end

return p