Module:Sprite

-- local p = {}

local loader = require('Module:Loader') local getArgs = require('Module:Arguments').getArgs local string, yesno = loader.require('String', 'Yesno') local _1, _2, _3, _4, _5, _6 = loader.loadData('Mob/Sprites', 'NPC/Sprites', 'Location/Sprites', 'Mob/Aliases', 'NPC/Aliases', 'Location/Aliases')

local spritesList = { mob = _1, npc = _2, location = _3, } local aliasesList = { mob = _4, npc = _5, location = _6, }

function p.spriteGenerator(mode, key, alt, showName, noErr, noImagePadding, noLink) local sprites, aliases = spritesList[mode], aliasesList[mode] local spr, img, exist local untouchedKey = key showName = yesno(showName, true) showImage = yesno(showImage, true) noErr = yesno(noErr, false) noImagePadding = yesno(noImagePadding, false) noLink = yesno(noLink, 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] or sprites.dungeon_mobs[key:lower] or sprites.dungeon_bosses[key:lower] or sprites.dungeon_boss_mobs[key:lower] or sprites.dungeon_watcher_mobs[key:lower] end, npc = function return sprites[key:lower] end, location = function return sprites[key:lower] end, }	local function _getImage(img_, ext_, key_, nip) local imgexist = mw.title.existsWithoutWanted(('File:%s Sprite.%s'):format(img_, ext_ or 'png')) return imgexist and string.makeImage({ img_, ' Sprite' }, { ext=ext_, size=16, link=key_ }) or ( nip and 				or string.makeImage('Blank Icon', { ext='png', size=16, link= })			) 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 },	}	local function specialHandler(n) local handle_ = specialHandlers[n][mode] return handle_ and handle_ or nil 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 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{				_getImage(img, ext, key, noImagePadding),				-- 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{ _getImage(img, ext, key, noImagePadding), showName and (' ' .. (noLink and (alt or key) or 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'] local noImgPad = args['noimgpad'] local noLink = args['nolink'] or args['nl'] return p._mobSprite(mob, alt, showName, noErr, noImgPad, noLink) 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'] local noErr = args['noerr'] or args['noe'] local alt = args['alt'] local noImgPad = args['noimgpad'] local noLink = args['nolink'] or args ['nl'] return p._npcSprite(npc, alt, showName, noErr, noImgPad, noLink) 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'] local noErr = args['noerr'] or args['noe'] local alt = args['alt'] local noImgPad = args['noimgpad'] local noLink = args['nolink'] or args ['nl'] return p._locationSprite(location, alt, showName, noErr, noImgPad, noLink) end

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

--Finish Module return p