Module:Sprite

-- local p = {}

local loader = require('Module:Loader') local getArgs = require('Module:Arguments').getArgs local string, yesno = loader.require('String', 'Yesno')

local spritesList = { mob = loader.loadData('Module:Mob/Sprites'), npc = loader.loadData('Module:NPC/Sprites'), location = loader.loadData('Module:Location/Sprites'), } local aliasesList = { mob = loader.loadData('Module:Mob/Aliases'), npc = loader.loadData('Module:NPC/Aliases'), location = loader.loadData('Module:Location/Aliases'), }

function p.spriteGenerator(mode, key, alt, showName, noErr, showImage) local sprites, aliases = spritesList[mode], aliasesList[mode] local spr, img, alt, exist showName = yesno(showName, true) showImage = yesno(showImage, true) noErr = yesno(noErr, false) local getSprite = { mob = function return sprites.mobs[key:lower] or sprites.sea_creatures[key:lower] or sprites.slayer_mobs[key:lower] or sprites.passive_mobs[key:lower] end, npc = function return sprites[key:lower] end, location = function return sprites[key:lower] end, }	local specialHandlers = { -- 1		{			location = function if key:lower:match('[Cc]atacombs?') then return { action = 'return', value = table.concat{, key, }, }				else return end end }	}	local function specialHandler(n) local handle_ = specialHandlers[n][mode] if handle_ then return handle_ or { action = 'none', }		else return { action = 'none', }		end end --check for errors assertTrue(key, 'No sprite '..string.ucfirst(mode)..' provided', 2) --leave only text local pttn = '[^%a%s;%-% \'"%.]'	if key:find(pttn) then key = key:gsub(pttn, ) end	--alternate text	img, exist = key, false	if not alt and key:find(';') then 		local str_storage = mw.text.split(key, ";") 		key, alt = str_storage[1], str_storage[2]		img = key	end	--go through sprites and apply correct image	key = aliases[key:lower] or key	local handle = specialHandler(1, mode)	if handle.action == 'return' then return handle.value end	alt = alt or key	spr = getSprite[mode]	if spr then		exist = true		if type(spr) == 'table' then			img, ext = spr[1], sprites.foramt or 'png'		else			img, ext = spr, 'png'		end	elseif noErr then		return table.concat{			showImage and string.makeImage({ img, ' Sprite' }, { ext=ext, size=16, link=key }) or ,			showName and ' '..string.makeLink(key, alt) or '',		}	else		error('Invalid '..string.ucfirst(mode)..' name: "'..key..'"', 2)	end	return table.concat{ showImage and string.makeImage({ img, ' Sprite' }, { ext=ext, size=16, link=key }) or '', showName and ' '..string.makeLink(key, alt) or '', exist and  or , } end

-- -- Template:MobSprite -- -- Creates a link to a mob with an option for sprites and images -- function p.mobSprite(frame) local args = getArgs(frame) local mob = args[1] local showName = args[2] or args["showName"] or args["show_name"] or args["sn"] local alt = args['alt'] local noErr = args['noerr'] or args['noe'] return p.spriteGenerator('mob', mob, nil, showName, noErr) end

-- -- Template:NPCSprite -- -- Creates a link to an NPC with an option for sprites and images -- function p.npcSprite(frame) local args = getArgs(frame) local npc = args[1] local showName = args[2] or args["showName"] or args["show_name"] or args["sn"], true local noErr = args['noerr'] or args['noe'], false local alt = args['alt'] return p.spriteGenerator('npc', npc, nil, showName, noErr) end

-- -- Template:LocationSprite -- -- Creates a link to an NPC with an option for sprites and images -- function p.locationSprite(frame) local args = getArgs(frame) local location = args[1] local showName = args[2] or args["showName"] or args["show_name"] or args["sn"], true local noErr = args['noerr'] or args['noe'], false local alt = args['alt']

return p.spriteGenerator('location', location, nil, showName, noErr) end

--Finish Module return p