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) local sprites, aliases = spritesList[mode], aliasesList[mode] local spr, img, alt, exist local untouchedKey = key 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 function _getImage(img_, ext_, key_) local imgexist = mw.title.makeTitle(6, ('%s Sprite.%s'):format(img_, ext_ or 'png')).file.exists return imgexist and string.makeImage({ img_, ' Sprite' }, { ext=ext_, size=16, link=key_ }) or string.makeImage('Blank Icon', { ext='png', size=16, link='' }) end local function _getImageNoSpace(img_, ext_, key_) local imgexist = mw.title.makeTitle(6, ('%s Sprite.%s'):format(img_, ext_ or 'png')).file.exists return imgexist and string.makeImage({ img_, ' Sprite' }, { ext=ext_, size=16, link=key_ }) or '' end local specialHandlers = { -- 1: returns a value to be returned; else return nothing {			location = function if key:lower:match('[Cc]atacombs?') then return table.concat{, key, } else return end end },		-- 2: returns a function that adds the sprite image {			mob = function return _getImage end, npc = function return _getImage end, location = function return _getImageNoSpace end, }	}	local function specialHandler(n) local handle_ = specialHandlers[n][mode] return handle_ and handle_ or nil end local function _getImage(img_, ext_, key_) local imgexist = mw.title.makeTitle(6, ('%s Sprite.%s'):format(img_, ext_ or 'png')).file.exists return imgexist and string.makeImage({ img_, ' Sprite' }, { ext=ext_, size=16, link=key_ }) or string.makeImage('Blank Icon', { ext='png', size=16, link='' }) end --check for errors assertTrue(key, 'No sprite '..string.ucfirst(mode)..' provided', 2) --leave only text -- local pttn = '[^%a%s;%-% \'"%.,]' -- (whitelist pattern)	local pttn = '[%[%]|]' --(blacklist pattern)	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 and 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	else		if noErr then			return table.concat{				specialHandler(2, mode)(img, ext, key),				-- showName and ' '..string.makeLink(key, alt) or ,				showName and ' '..untouchedKey or '',			}		else			formattedError('Invalid %s name: %q', 2, string.ucfirst(mode), key) end end return table.concat{ specialHandler(2, mode)(img, ext, key), 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._mobSprite(mob, nil, showName, noErr) end

p._mobSprite = bind(p.spriteGenerator, 'mob')

-- -- 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._npcSprite(npc, nil, showName, noErr) end

p._npcSprite = bind(p.spriteGenerator, 'npc')

-- -- 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._locationSprite(location, nil, showName, noErr) end

p._locationSprite = bind(p.spriteGenerator, 'location')

--Finish Module return p