Module:Weapon: Difference between revisions

From Zombie Panic! Official Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:
local item = args[1] or args.name or "Unknown"
local item = args[1] or args.name or "Unknown"
local size = args[2] or args.size or 30
local size = args[2] or args.size or 30
local size_wide = args.wide or size
local count = args.count or args.x or nil
local count = args.count or args.x or nil
local text = args.text or item
local text = args.text or item
Line 27: Line 28:
link = 'Weapon/' .. link
link = 'Weapon/' .. link
local file = "[[File:Weapon " .. item .. suffix .. ".png|" .. size .. "x" .. size .. "px|alt=" .. item .. "|link=" .. link .. "]]"
local file = "[[File:Weapon " .. item .. suffix .. ".png|" .. size_wide .. "x" .. size .. "px|alt=" .. item .. "|link=" .. link .. "]]"


local icon = nil
local icon = nil
Line 49: Line 50:
:css({
:css({
display  = "inline-block",
display  = "inline-block",
width    = size .. "px",
width    = size_wide .. "px",
height  = size .. "px",
height  = size .. "px",
position = "relative"
position = "relative"

Latest revision as of 23:14, 22 August 2025

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

local p = {}
local lib = require('Module:Feature')

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        parentFirst = true,
        wrapper = {'Template:Weapon'}
    })
    return p._main(args)
end

function p._main(args)
	local item = args[1] or args.name or "Unknown"
	local size = args[2] or args.size or 30
	local size_wide = args.wide or size
	local count = args.count or args.x or nil
	local text = args.text or item
	local link = args.link or lib.ternary(item == "Unknown", "", item)
	local blueprint = tostring(args.blueprint) == "1"
	local notext = tostring(args.notext) == "1"
	local white = tostring(args.white) == "1"
	local suffix = " Icon"
	if (white) then suffix = suffix .. " White" end
	
	item = lib.sanitizeFileName(item)
	
	-- Make sure the link is always Weapon/ at the start
	link = 'Weapon/' .. link
	
	local file = "[[File:Weapon " .. item .. suffix .. ".png|" .. size_wide .. "x" .. size .. "px|alt=" .. item .. "|link=" .. link .. "]]"

	local icon = nil
	local corner_size = math.floor(tonumber(size) / 2.5)
	local offset = 0
	if (corner_size > 21) then
		offset = 0
	elseif (corner_size > 8) then
		offset = math.floor((corner_size - 22) / 2)
	else
		offset = -6
	end

	if (lib.isEmpty(item)) then
		return ""
	end

	local result = mw.html.create():tag('span'):addClass('item weapon')
	local item_image = result:tag("span")
		:addClass("item-image")
		:css({
			display  = "inline-block",
			width    = size_wide .. "px",
			height   = size .. "px",
			position = "relative"
		})
		:wikitext(file)

	if (icon ~= nil) then
		item_image:tag("span")
			:css({
				position = "absolute",
				top      = offset .. "px",
				left     = "0px",
				width    = corner_size .. "px",
				height   = corner_size .. "px"
			})
			:wikitext(icon)
	end
	
	if (newline) then
		result:addClass('newline')
		result:tag('br'):addClass('hidden')
	end
	
	local item_text = result:tag('span'):addClass('item-text')
	if (not notext) then
		if (lib.isEmpty(link)) then
			item_text:wikitext(' ' .. text)
		else
			item_text:wikitext((newline and '' or ' ') .. '[[' .. link .. '|' .. text .. ']]')
		end
	end

	if (count) then
		item_text:wikitext((notext and '' or ' ') .. '×' .. count)
	end
	
	if (note) then
		item_text:tag('small'):wikitext(((notext and not count) and '' or ' '),'(', note, ')')
	end
	if (ref) then
		item_text:wikitext(ref)
	end
	
	return tostring(result);
end

return p