Module:Icons: Difference between revisions

From Melvor Idle
(Extension override for TotH)
m (Fix extension override handling for skill reqs)
 
(102 intermediate revisions by 6 users not shown)
Line 1: Line 1:
local Shared = require("Module:Shared")
local Shared = require("Module:Shared")
 
local Num = require('Module:Number')
local Overrides = require('Module:Icons/Overrides')
-- Locally index some functions for performance
-- Locally index some functions for performance
local sgsub, fixPagename, formatnum, tostring, type = string.gsub, Shared.fixPagename, Shared.formatnum, tostring, type
local sgsub, fixPagename, formatnum, tostring, type = string.gsub, Shared.fixPagename, Num.formatnum, tostring, type


local p = {}
local p = {}
--Extension overrides for items that have non-png images
--Extension overrides for items that have non-png images
--Name level overrides take precedence over type level overrides
--Name level overrides take precedence over type level overrides
local extOverrides = {
local extOverrides = Overrides.extOverrides()
["type"] = {
["skill"] = "svg",
["spellType"] = "svg",
["spell"] = "svg",
["curse"] = "svg", -- Some exceptions included by name below
["aurora"] = "svg",
["combatArea"] = "svg",
["dungeon"] = "svg",
["prayer"] = "svg",
["upgrade"] = "svg", -- Some exceptions included by name below
["tree"] = "svg",
["rock"] = "svg", -- Some exceptions included by name below
["thieving"] = "svg",
["constellation"] = "svg",
["building"] = "svg",
["resource"] = "svg",
["township"] = "svg"
},
["name"] = {
-- General
["Combat"] = "svg",
["Guides"] = "svg",
["Mastery"] = "svg",
["Coins"] = "svg",
["Slayer Coins"] = "svg",
["Raid Coins"] = "svg",
["TotH"] = "svg",
["Shop"] = "svg",
["Easter"] = "svg",
["Melvor Logo"] = "svg",
["Question"] = "svg",
["Timer"] = "svg",
["Hardcore"] = "svg",
["Chaos"] = "svg",
["Internal Suffering Speedrun"] = "svg",
["Hardcore Adventure Speedrun"] = "svg",
["Mastery Tokens"] = "gif",
["Prat, the Serpent of Fire"] = "svg",
-- Items
["Amulet of Fishing"] = "svg",
["Lemon"] = "jpg",
["Lemons"] = "jpg",
["Lemonade"] = "jpg",
["Ancient Crossbow"] = "svg",
["Air Shard"] = "svg",
["Water Shard"] = "svg",
["Earth Shard"] = "svg",
["Fire Shard"] = "svg",
["Confetti Crossbow"] = "svg",
["Cloudburst Staff"] = "gif",
["Shockwave"] = "svg",
["Event Clue 1"] = "svg",
["Event Clue 2"] = "svg",
["Event Clue 3"] = "svg",
["Event Clue 4"] = "svg",
-- Pets
["Cris"] = "svg",
["Cool Rock"] = "svg",
["Puff, the Baby Dragon"] = "svg",
["Bruce"] = "svg",
["Lil Ron"] = "svg",
["Leonardo"] = "svg",
["Golden Golbin"] = "svg",
["Ty"] = "svg",
["Ripper the Reindeer"] = "svg",
["Chick"] = "svg",
["Zarrah"] = "svg",
["Chio"] = "svg",
["Bouncing Bob"] = "svg",
["Rosey"] = "svg",
["Ayyden"] = "svg",
["Arctic Yeti"] = "svg",
["Mac"] = "svg",
["Jerry the Giraffe"] = "svg",
["Preston the Platypus"] = "svg",
["Quill"] = "svg",
["Gunter"] = "svg",
["Salem"] = "svg",
["Peri"] = "svg",
["Otto"] = "svg",
["Jelly Jim"] = "svg",
["Harley"] = "svg",
["Singe"] = "svg",
["Aquarias"] = "svg",
["Norman"] = "svg",
["Erran"] = "svg",
["Ren"] = "svg",
["Pablo"] = "svg",
["Sam"] = "svg",
["Tim the Wolf"] = "svg",
["Mark"] = "svg",
["Astro"] = "svg",
-- Curses, included to override the type level override above
["Madness"] = "png",
["Torment"] = "png",
["Despair"] = "png",
-- Alt. Magic
["Rags to Riches III"] = "png",
-- Upgrades, included to override the type level override above
["Golbin Crate"] = "png",
["Corundum Axe"] = "png",
["Augite Axe"] = "png",
["Meteorite Axe"] = "png",
["Divine Axe"] = "png",
["Spruce Cooking Fire"] = "png",
["Elderwood Cooking Fire"] = "png",
["Carrion Cooking Fire"] = "png",
["Iridium Pot"] = "png",
["Palladium Pot"] = "png",
["Divine Pot"] = "png",
["Corundum Furnace"] = "png",
["Augite Furnace"] = "png",
["Divine Furnace"] = "png",
["Sign of the Stars"] = "png",
["Summoner's Altar"] = "png",
["Golden Shard"] = "png",
["Labyrinth Solution"] = "png",
["Linden Boat"] = "png",
["Golden Compass"] = "png",
-- Rocks (Mining), included to override the type level override above
["Rune Essence"] = "png",
["Pure Essence"] = "png",
["Onyx"] = "png",
["Oricha"] = "png",
["Cerulean"] = "png",
-- Marks (Summoning), included to override the type level override above
["Eagle"] = "png",
["Owl"] = "png",
["Beaver"] = "png",
["Fox"] = "png",
["Lightning Spirit"] = "png",
["Siren"] = "png",
["Spider"] = "png",
["Spectre"] = "png"
}
}
 
--Some overrides only apply for a specific type
--Some overrides only apply for a specific type
local typeExtOverrides = {
local typeExtOverrides = Overrides.typeExtOverrides()
-- Example: ["item"] = { ["Chicken"] = "png" }
["item"] = {
-- These familiars also exist as type mark, where the image is of PNG format
["Golbin Thief"] = "svg",
["Occultist"] = "svg",
["Wolf"] = "svg",
["Ent"] = "svg",
["Mole"] = "svg",
["Octopus"] = "svg",
["Minotaur"] = "svg",
["Centaur"] = "svg",
["Witch"] = "svg",
["Pig"] = "svg",
["Crow"] = "svg",
["Leprechaun"] = "svg",
["Cyclops"] = "svg",
["Yak"] = "svg",
["Unicorn"] = "svg",
["Dragon"] = "svg",
["Monkey"] = "svg",
["Salamander"] = "svg",
["Bear"] = "svg",
["Devil"] = "svg"
},
["township"] = {
-- Seasons, which are PNG unlike most other Township images
["Spring"] = "png",
["Summer"] = "png",
["Fall"] = "png",
["Winter"] = "png",
["Nightfall"] = "png",
["Solar Eclipse"] = "png",
}
}
 
--When calling for an icon of type Key, instead use type Value
--When calling for an icon of type Key, instead use type Value
local typeOverrides = {
local typeOverrides = Overrides.typeOverrides()
['combat'] = 'combatArea',
['slayer'] = 'combatArea',
}
--When calling for an icon for Key, replace with image Value, and optionally override type also
--When calling for an icon for Key, replace with image Value, and optionally override type also
local imgOverrides = {
local imgOverrides = Overrides.imgOverrides()
['Alt Magic'] = 'Magic',
['Alt. Magic'] = 'Magic',
['Alternative Magic'] = 'Magic',
['Melee'] = {'Attack', 'skill'},
['Spider (lv. 51)'] = 'Spider',
['Spider (lv. 52)'] = 'Brown Spider',
['Bane, Instrument of Fear'] = 'Bane',
['Mastery Token Astrology'] = 'Mastery Token (Astrology)',
['Event Token - Holiday 2021'] = 'Christmas Present (Yellow)',
["I Can't See Helmet"] = {'Golbin', 'monster'},
-- Shop purchases
['Basic Resupply'] = {'Lobster', 'item'},
['Standard Resupply'] = {'Crab', 'item'},
['Generous Resupply'] = {'Shark', 'item'},
['Plentiful Resupply'] = {'Magma Fish', 'item'},
['Bountiful Resupply'] = {'Static Jellyfish', 'item'},
['Cooking Upgrade 1'] = {'Cooking', 'skill'},
['Cooking Upgrade 2'] = {'Cooking', 'skill'},
['Loot Container Stacking'] = {'Amulet of Looting', 'item'},
['Access to Throne of the Herald'] = {'Golden Key', 'item'},
["Max Skillcape"] = 'Maximum Skillcape',
-- Alt.Magic spells using item icons
['Item Alchemy'] = {'Item Alchemy III', 'spell'},
['Superheat'] = {'Superheat IV', 'spell'},
["Holy Invocation I"] = {'Small Urn', 'item'},
["Holy Invocation II"] = {'Medium Urn', 'item'},
["Nullification"] = {'Rune Essence', 'item'},
["Transmutation"] = {'Oricha', 'item'},
["Purification"] = {'Pure Essence', 'item'},
["Fragmentation"] = {'Summoning Shard (Black)', 'item'},
["Holy Invocation III"] = {'Large Urn', 'item'},
["Embellish"] = {'Lava Fish (Perfect)', 'item'},
-- Overrides that allow generic potion pages to be linked to easily
['Melee Accuracy Potion'] = {'Melee Accuracy Potion I', 'item'},
['Melee Evasion Potion'] = {'Melee Evasion Potion I', 'item'},
['Ranged Assistance Potion'] = {'Ranged Assistance Potion I', 'item'},
['Hinder Potion'] = {'Hinder Potion I', 'item'},
['Magic Assistance Potion'] = {'Magic Assistance Potion I', 'item'},
['Regeneration Potion'] = {'Regeneration Potion I', 'item'},
['Famished Potion'] = {'Famished Potion I', 'item'},
['Ranged Strength Potion'] = {'Ranged Strength Potion I', 'item'},
['Lucky Herb Potion'] = {'Lucky Herb Potion I', 'item'},
['Divine Potion'] = {'Divine Potion I', 'item'},
['Melee Strength Potion'] = {'Melee Strength Potion I', 'item'},
['Magic Damage Potion'] = {'Magic Damage Potion I', 'item'},
['Lethal Toxins Potion'] = {'Lethal Toxins Potion I', 'item'},
['Diamond Luck Potion'] = {'Diamond Luck Potion I', 'item'},
['Damage Reduction Potion'] = {'Damage Reduction Potion I', 'item'},
['Area Control Potion'] = {'Area Control Potion I', 'item'},
['Adaptive Defence Potion'] = {'Adaptive Defence Potion I', 'item'},
['Slayer Bounty Potion'] = {'Slayer Bounty Potion I', 'item'},
['Holy Bulwark Potion'] = {'Holy Bulwark Potion I', 'item'},
['Adaptive Accuracy Potion'] = {'Adaptive Accuracy Potion I', 'item'},
['Reaper Potion'] = {'Reaper Potion I', 'item'},
['Penetration Potion'] = {'Penetration Potion I', 'item'},
['Critical Strike Potion'] = {'Critical Strike Potion I', 'item'},
['Bird Nest Potion'] = {'Bird Nest Potion I', 'item'},
['Controlled Heat Potion'] = {'Controlled Heat Potion I', 'item'},
['Generous Cook Potion'] = {'Generous Cook Potion I', 'item'},
['Seeing Gold Potion'] = {'Seeing Gold Potion I', 'item'},
['Fishermans Potion'] = {'Fishermans Potion I', 'item'},
['Skilled Fletching Potion'] = {'Skilled Fletching Potion I', 'item'},
['Gentle Hands Potion'] = {'Gentle Hands Potion I', 'item'},
['Secret Stardust Potion'] = {'Secret Stardust Potion I', 'item'},
['Crafting Potion'] = {'Crafting Potion I', 'item'},
['Perfect Swing Potion'] = {'Perfect Swing Potion I', 'item'},
['Necromancer Potion'] = {'Necromancer Potion I', 'item'},
['Performance Enhancing Potion'] = {'Performance Enhancing Potion I', 'item'},
['Elemental Potion'] = {'Elemental Potion I', 'item'},
['Herblore Potion'] = {'Herblore Potion I', 'item'},
['Generous Harvest Potion'] = {'Generous Harvest Potion I', 'item'},
['Alchemic Practice Potion'] = {'Alchemic Practice Potion I', 'item'},
['Gem Detector Potion'] = {'Gem Detector Potion I', 'item'},
['Multicooker Potion'] = {'Multicooker Potion I', 'item'},
['Star Seeker Potion'] = {'Star Seeker Potion I', 'item'},
['Traps Potion'] = {'Traps Potion I', 'item'},
['Blacksmith Potion'] = {'Blacksmith Potion I', 'item'},
['Enkindled Yields Potion'] = {'Enkindled Yields Potion I', 'item'},
['Alt Magic Potion'] = {'Alt Magic Potion I', 'item'},
['Alt. Magic Potion'] = {'Alt Magic Potion I', 'item'},
-- Township resources
['GP'] = {'Coins', ''},
['Food'] = {'Raw Beef', 'item'},
['Ore'] = {'Iron', 'rock'},
['Bar'] = {'Iron Bar', 'item'},
['Herbs'] = {'Garum Herb', 'item'},
['Clothing'] = {'Leather Body', 'item'},
-- Township generic statue building
['Statue of Worship'] = 'Statue of Nothing',
-- Easter egg stuff
["Lemonade (Empty)"] = "Lemonade",
["Lemonade (Very empty)"] = "Lemonade",
["Lemonade (Still very empty)"] = "Lemonade",
["Lemonade (Not as empty as before)"] = "Lemonade",
["Lemonade (Not much)"] = "Lemonade",
["Lemonade (Has a bit now)"] = "Lemonade",
["Lemonade (A little bit more now)"] = "Lemonade",
["Lemonade (Half full)"] = "Lemonade",
["Lemonade (Just over half full)"] = "Lemonade",
["Lemonade (Maybe this is half full?)"] = "Lemonade",
["Lemonade (Nope this is half full now)"] = "Lemonade",
["Lemonade (Wow this is slow)"] = "Lemonade",
["Lemonade (Just fill it up already)"] = "Lemonade",
["Lemonade (Still not full)"] = "Lemonade",
["Lemonade (Again, still not full)"] = "Lemonade",
["Lemonade (Less than before because you drank some)"] = "Lemonade",
["Lemonade (Back to where we were before)"] = "Lemonade",
["Lemonade (Almost full)"] = "Lemonade",
["Lemonade (Still almost full)"] = "Lemonade",
["Lemonade (How full is it supposed to be?)"] = "Lemonade",
["Lemonade (Wait this might be half full now)"] = "Lemonade",
["Lemonade (Haha just joking, hurry up)"] = "Lemonade",
["Lemonade (Okay this looks pretty full now)"] = "Lemonade",
["Lemonade (Now?)"] = "Lemonade",
["Lemonade (What about now?)"] = "Lemonade",
["Lemonade (YAY ITS FINALLY FULL!)"] = "Lemonade",
["Lemonade (Oh... still not full)"] = "Lemonade",
["Lemonade (Wait for it)"] = "Lemonade",
["Lemonade (Wait for it!)"] = "Lemonade",
["Lemonade (Full)"] = "Lemonade"
}
--When calling for an icon for Key + Type, replace with image Value, and optionally override type also
--When calling for an icon for Key + Type, replace with image Value, and optionally override type also
local typeImgOverrides = {
local typeImgOverrides = Overrides.typeImgOverrides()
['resource'] = {
['Rune Essence'] = {'Rune Essence', 'item'},
['Leather'] = {'Leather', 'item'},
}
}
--When calling for an icon for Key, the link goes to Value
--When calling for an icon for Key, the link goes to Value
local linkOverrides = {
local linkOverrides = Overrides.linkOverrides()
['Alt Magic'] = 'Alternative Magic',
['Alt. Magic'] = 'Alternative Magic',
['Spider2'] = 'Brown Spider'
}
--When calling for an icon for Key + Type, the link goes to Value
--When calling for an icon for Key + Type, the link goes to Value
local typeLinkOverrides = {
local typeLinkOverrides = Overrides.typeLinkOverrides()
['resource'] = {
-- If no other text override was specified for the given link, use these
-- Township resources
local txtOverrides = Overrides.txtOverrides()
['Food'] = '',
-- Ambiguous overrides section
['Wood'] = '',
local ambiguousOverrides = Overrides.ambiguousOverrides()
['Planks'] = '',
-- For ambiguous overrides, determines the type text to be used in the page link
['Stone'] = '',
local ambiguousTypeMap = Overrides.ambiguousTypeMap()
['Bar'] = '',
local currencyFormatDefn = Overrides.currencyFormatDefn()
['Ore'] = '',
['Coal'] = '',
['Rune Essence'] = '',
['Herbs'] = '',
['Potions'] = '',
['Leather'] = '',
['Clothing'] = ''
},
['township'] = {
['Workers'] = '',
['Statistics'] = '',
['Town'] = ''
},
['biome'] = {
['Grasslands'] = 'Biomes',
['Forest'] = 'Biomes',
['Mountains'] = 'Biomes',
['Water'] = 'Biomes',
['Swamp'] = 'Biomes',
['Valley'] = 'Biomes',
['Arid Plains'] = 'Biomes',
['Jungle'] = 'Biomes',
['Desert'] = 'Biomes',
['Snowlands'] = 'Biomes'
}
}


--If no other text override was specified for the given link, use these
-- Defines icon types for which a section or anchor on a static page should be linked to, rather
local txtOverrides = {
-- than the usual page linking
['Spider2'] = 'Brown Spider'
local sectionLinks = Overrides.sectionLinks()
}
--Ambiguous overrides section
local ambiguousOverrides = {
['Golbin'] = true,
['Chick'] = true,
['Chicken'] = true,
['Cyclops'] = true,
['Wizard'] = true,
['Spider'] = true,
['Necromancer'] = true,
['Dark Knight'] = true,
['Vampire'] = true,
['Phantom'] = true,
['Spectre'] = true,
['Siren'] = true,
['Lightning Spirit'] = true,
}
--When calling for an icon for Key, add a css class
local imgClassOverrides = {
-- Was previously used for Township stat icons prior to v1.1.2
--['Population'] = 'township-colortoggle'
}


function p.Icon(frame)
function p.Icon(frame)
Line 394: Line 43:
local nolink = args.nolink ~= nil and args.nolink ~= ''
local nolink = args.nolink ~= nil and args.nolink ~= ''
local noicon = args.noicon ~= nil and args.noicon ~= ''
local noicon = args.noicon ~= nil and args.noicon ~= ''
local nospan = args.nospan ~= nil and args.nospan
local menu = args.menu ~= nil and args.menu ~= ''
local menu = args.menu ~= nil and args.menu ~= ''
local imgSize = args.size ~= nil and args.size or 25
local imgSize = args.size ~= nil and args.size or 25
Line 400: Line 50:
local class = args.class ~= nil and args.class ~= '' and args.class or ''
local class = args.class ~= nil and args.class ~= '' and args.class or ''
local altText = args.alt ~= nil and args.alt or (notext and (text or link)) or ''
local altText = args.alt ~= nil and args.alt or (notext and (text or link)) or ''
local nospan = args.nospan ~= nil and args.nospan
local linkSection = args.section
local expIcon = args.expicon ~= nil and args.expicon or ''
local expIcon = args.expicon ~= nil and args.expicon or ''


Line 409: Line 59:
img = sgsub(img, '/Training', '')
img = sgsub(img, '/Training', '')
img = sgsub(img, '/Guide', '')
img = sgsub(img, '/Guide', '')
img = sgsub(img, '/Boosts', '')
-- Remove '#' from links to facilitate linking to things with hashes in
-- their names (e.g. item 'Birthday Cake Piece #1'). Links to sections
-- of a page should instead be performed using the 'section' parameter.
link = sgsub(link, '#', '')
link = sgsub(link, '#', '')
 
-- Special handling for links to Agility obstacles or pillars
-- Handle types for which links should be to a section of a static page
if iconType == 'agility' then
local sl = sectionLinks[iconType]
img = 'Agility'
if sl ~= nil and not sl.exclude[link] then
iconType = 'skill'
if text == nil or text == '' then
if text == nil or text == '' then
text = link
text = link
end
end
link = 'Agility#' .. string.gsub(link, ' ', '')
linkSection = string.gsub(link, ' ', '')
-- Link overwriting must occur after text & linkSection have been set
link = sl.link
if sl.img ~= nil then
img = sl.img[1]
if sl.img[2] ~= nil then
iconType = sl.img[2]
end
end
end
end


--MANUAL OVERRIDES
--MANUAL OVERRIDES
local origType = iconType
local ovrImg = imgOverrides[link]
local ovrImg = imgOverrides[link]
local ovrTypeImg = typeImgOverrides[iconType]
local ovrTypeImg = typeImgOverrides[iconType]
Line 446: Line 108:
end
end
end
end
local ovrClass = imgClassOverrides[link]
 
-- Type & extension overrides must be after adjustments have been made
-- Type & extension overrides must be after adjustments have been made
-- for any image overrides
-- for any image overrides
local ovrTypeExt = typeExtOverrides[iconType]
local ovrTypeExt, ovrTypeExtTable = nil, typeExtOverrides[iconType]
if ovrTypeExt ~= nil then
if ovrTypeExtTable ~= nil then
ovrTypeExt = ovrTypeExt[img]
ovrTypeExt = ovrTypeExtTable[img]
end
end
local ovrType = typeOverrides[iconType]
local ovrType = typeOverrides[iconType]
Line 477: Line 139:
iconType = ovrType
iconType = ovrType
end
end
if ovrClass ~= nil then
 
class = ovrClass
end
-- There are a couple specific double overrides to be included that don't fit in the above lists
-- There are a couple specific double overrides to be included that don't fit in the above lists
if ambiguousOverrides[link] then
 
-- If the link requires disambiguation or a section, the modify the link accordingly
-- while preserving the displayed text
local isAmbig, hasSection = ambiguousOverrides[link], linkSection ~= nil and linkSection ~= ''
if isAmbig or hasSection then
if not hasText then
if not hasText then
text = link
text = link
hasText = true
hasText = true
end
end
link = link..' ('..(iconType == 'mark' and 'item' or iconType)..')'
if isAmbig then
link = link .. ' (' .. (ambiguousTypeMap[origType] or origType) .. ')'
end
if hasSection then
link = link .. '#' .. linkSection
end
end
end


Line 518: Line 187:


if menu then
if menu then
return '{| class="articletable" style="display:inline-block;vertical-align:middle;"\r\n|-\r\n|' .. expIcon .. resultText .. '\r\n|}'
return '{| class="articletable img-text" style="vertical-align:middle;"\r\n|-\r\n|' .. expIcon .. resultText .. '\r\n|}'
elseif not noicon and not nospan then
elseif not noicon and not nospan then
return '<span style="display:inline-block">' .. expIcon .. resultText .. '</span>'
return '<span class="img-text">' .. expIcon .. resultText .. '</span>'
else
else
return resultText
return resultText
Line 526: Line 195:
end
end


-- Wrapper for p.Icon(), uses the same parameters but forces noicon = true
-- Returns a skill icon based on the realm.
function p.Link(frame)
function p._SkillRealmIcon(skill, realmID, size)
local realm = realmID or 'melvorD:Melvor'
local link = fixPagename(skill)
 
local img = mw.html.create('span')
if realm == 'melvorItA:Abyssal' then
img:addClass('abyss-icon')
end
size = size or 25
img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
:wikitext('|'):wikitext(size):wikitext('x'):wikitext(size):wikitext('px')
:wikitext('|link='):wikitext(link):wikitext(']]')
return tostring(img)
end
 
function p.SkillRealmIcon(frame)
local args = frame.args ~= nil and frame.args or frame
local args = frame.args ~= nil and frame.args or frame
args.noicon = true
local skill = args[1]
return p.Icon({['args'] = args})
local realmID = args.realm
local size = tonumber(args.size)
return p._SkillRealmIcon(skill, realmID, size)
end
end


function p._SkillReq(skill, level, showText)
function p._SkillReq(skill, level, showText, realmID)
local realm = realmID or 'melvorD:Melvor'
local link = fixPagename(skill)
local altText = (showText and '') or skill
local altText = (showText and '') or skill
local result = p.Icon({skill, type='skill', notext=(showText and '' or true), nospan=true, alt=altText})
if level == nil then  
if level == nil then level = 'Unknown [[Category:Pages with script errors]]' end
level = 'Unknown [[Category:Pages with script errors]]'  
result = result.." Level "..level
end


result = '<span style="display:inline-block">'..result..'</span>'
-- Create skill image.
return result
local img = mw.html.create()
end
 
if realm == 'melvorItA:Abyssal' then
function p.SkillReq(frame)
img = img:tag('span')
local args = frame.args ~= nil and frame.args or frame
:addClass('abyss-icon')
local skill = args[1]
end
local level = tonumber(args[2])
local showText = args.showText ~= nil and args.showText ~= '' and args.showText ~= 'false'
img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
return p._SkillReq(skill, level, showText)
:wikitext('|25x25px|link='):wikitext(link)
:wikitext('|alt='):wikitext(altText)
:wikitext(']]')
-- Create text and add image.
local text = mw.html.create('span')
:addClass('img-text')
:node(img)
if showText == true then
text:wikitext(' [[' .. link .. ']]')
end
--28/06/2024. Commented this out to try out red icons for abyssal levels
-- as opposed to the text below. Remove entire section if still commented out after
-- a week or two.
--if realm == 'melvorItA:Abyssal' then
-- text:wikitext(" Abyssal Level ")
--else
text:wikitext(" Level ")
--end
text:wikitext(level)
return tostring(text)
end
end


Line 563: Line 276:
local altTextMastery = (showText and '') or '&nbsp;Mastery'
local altTextMastery = (showText and '') or '&nbsp;Mastery'


local result = 'Level '..level..' '
    local result = mw.html.create('span')
result = result..p.Icon({linkname, img=iconname, type='item', notext=true, nospan=true, alt=altTextItem})..p.Icon({'Mastery', notext=true, nospan=true, alt=altTextMastery})
    result
if showText then result = result..'[['..linkname..']] [[Mastery]]' end
        :addClass('img-text')
result = '<span style="display:inline-block">'..result..'</span>'
        :wikitext('Level ' .. level .. ' ')
return result
        :node(p.Icon({linkname, img=iconname, type='item', notext=true, nospan=true, alt=altTextItem}))
        :node('[[File:Mastery.svg|25x25px|alt=' .. altTextMastery .. ']]')
 
    if showText then
        result
            :wikitext('[[' .. linkname .. ']] ')
            :wikitext('[[Mastery]]')
    end
 
    return tostring(result)
end
end


function p.MasteryReq(frame)
function p._Currency(currencyID, amount, maxAmount)
local args = frame.args ~= nil and frame.args or frame
local currencyDefn = currencyFormatDefn[currencyID]
local itemName = args[1]
if currencyDefn == nil then
local level = tonumber(args[2])
error('Invalid currency ID: ' .. currencyID, 2)
local showText = args.showText ~= nil and args.showText ~= '' and args.showText ~= 'false'
end
return p._MasteryReq(itemName, level, showText)
 
end
local fileName, link, altText = currencyDefn.img, currencyDefn.name, currencyDefn.abbr


function p._Currency(fileName, link, altText, amt, maxAmt)
local function numColour(amount)
local amtText = nil
if tonumber(amount) < 0 then
if tonumber(amt) ~= nil then
return mw.html.create('span')
amtText = formatnum(amt)
:css('color', 'red')
if maxAmt ~= nil and maxAmt >= amt then
:wikitext(formatnum(amount))
amtText = amtText .. ' - ' .. formatnum(maxAmt)
:done()
else
return formatnum(amount)
end
end
end
end
local html = mw.html.create('span')
:addClass('img-text')


local fileText = ''
-- Currency amounts
if tonumber(amount) ~= nil then
local text = mw.html.create('span')
:css('margin-right', '0.2em')
:node(numColour(amount))
if (tonumber(maxAmount) ~= nil and maxAmount > amount) then
text:wikitext(' - ' .. numColour(maxAmount))
end
html:node(text:done())
end
-- Currency icon
if fileName ~= nil then
if fileName ~= nil then
fileText = '[[File:' .. fileName .. '|25px'
html:wikitext('[[File:'):wikitext(fileName):wikitext('|25px')
if link ~= nil then
if link ~= nil then
fileText = fileText .. '|link=' .. link
html:wikitext('|link='):wikitext(link)
end
end
fileText = fileText .. '|alt=' .. ((altText == nil and '') or altText) .. ']]'
html:wikitext('|alt=')
:wikitext(((altText == nil and '') or altText))
:wikitext(']]')
end
html:done()
if ((amount ~= nil and tonumber(amount) == nil)
or (maxAmount ~= nil and tonumber(maxAmount) == nil)) then
html:wikitext('[[Category:Pages with non-numeric currency arguments]]')
end
end
return tostring(html)
end


return '<span style="display:inline-block">' .. fileText .. (amtText ~= nil and '&nbsp;' .. amtText or '') .. '</span>'
-- p.Currency() is called by various currency templates:
-- [[Template:GP]], [[Template:SC]], [[Template:RC]]
function p.Currency(frame)
local args = frame.args ~= nil and frame.args or frame
local currID, minVal, maxVal, _
currID = args[1]
if args[2] ~= nil and args[2] ~= '' then
minVal, _ = string.gsub(args[2], ',', '')
end
if args[3] ~= nil and args[3] ~= '' then
maxVal, _ = string.gsub(args[3], ',', '')
end
return p._Currency((currID or 'nil'), minVal, maxVal)
end
end


function p.GP(amt, maxamt)
--Adding a shortcut function for this
return p._Currency('Coins.svg', 'Coins', 'GP', amt, maxamt)
function p.TotH()
return '<span class="img-text">[[File:TotH.svg|25x25px|link=Throne of the Herald Expansion]]</span>'
end
end


function p.SC(amt, maxamt)
function p.AoD()
return p._Currency('Slayer Coins.svg', 'Currency#Slayer Coins', 'SC', amt, maxamt)
return '<span class="img-text">[[File:AoD.png|25x25px|link=Atlas of Discovery Expansion]]</span>'
end
end


function p.RC(amt, maxamt)
function p.ItA()
return p._Currency('Raid_Coins.svg', 'Currency#Raid Coins', 'RC', amt, maxamt)
return '<span class="img-text">[[File:ItA.png|25x25px|link=Into the Abyss Expansion]]</span>'
end
end


--Adding a shortcut function for this
function p.Melvor()
function p.TotH()
return '<span class="img-text">[[File:Melvor_Logo.svg|25x25px|link=Full Version]]</span>'
return p.Icon({'Throne of the Herald Expansion', notext=true, img='TotH', alt=''})
end
 
-- Function to retrieve the DLC id used for sorting columns that contain a DLC icon.
-- Passthrough from shared so that caller doesn't have to include Shared Module
function p.getExpansionID(id)
return Shared.getLocalID(id)
end
end


Line 620: Line 388:
if ns == 'melvorTotH' then
if ns == 'melvorTotH' then
return p.TotH()..' '
return p.TotH()..' '
elseif ns == 'melvorAoD' then
return p.AoD()..' '
elseif ns == 'melvorItA' then
return p.ItA()..' '
else
else
return ''
return ''
end
end
end
-- Special function that returns a Melvor idle icon
-- Specifically used for a "DLC" column on tables.
-- Avoids every item from receiving the "base game icon"
function p.getDLCColumnIcon(id)
local icon = p.getExpansionIcon(id)
if icon == '' then
icon = p.Melvor() .. ' '
end
return icon
end
end


return p
return p

Latest revision as of 20:57, 26 August 2024

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

local Shared = require("Module:Shared")
local Num = require('Module:Number')
local Overrides = require('Module:Icons/Overrides')
-- Locally index some functions for performance
local sgsub, fixPagename, formatnum, tostring, type = string.gsub, Shared.fixPagename, Num.formatnum, tostring, type

local p = {}

--Extension overrides for items that have non-png images
--Name level overrides take precedence over type level overrides
local extOverrides = Overrides.extOverrides()
--Some overrides only apply for a specific type
local typeExtOverrides = Overrides.typeExtOverrides()
--When calling for an icon of type Key, instead use type Value
local typeOverrides = Overrides.typeOverrides()
--When calling for an icon for Key, replace with image Value, and optionally override type also
local imgOverrides = Overrides.imgOverrides()
--When calling for an icon for Key + Type, replace with image Value, and optionally override type also
local typeImgOverrides = Overrides.typeImgOverrides()
--When calling for an icon for Key, the link goes to Value
local linkOverrides = Overrides.linkOverrides()
--When calling for an icon for Key + Type, the link goes to Value
local typeLinkOverrides = Overrides.typeLinkOverrides()
-- If no other text override was specified for the given link, use these
local txtOverrides = Overrides.txtOverrides()
-- Ambiguous overrides section
local ambiguousOverrides = Overrides.ambiguousOverrides()
-- For ambiguous overrides, determines the type text to be used in the page link
local ambiguousTypeMap = Overrides.ambiguousTypeMap()
local currencyFormatDefn = Overrides.currencyFormatDefn()

-- Defines icon types for which a section or anchor on a static page should be linked to, rather
-- than the usual page linking
local sectionLinks = Overrides.sectionLinks()

function p.Icon(frame)
	local args = frame.args ~= nil and frame.args or frame
	local link = args[1]
	local text = args[2]
	local iconType = args.type
	local ext = args.ext ~= nil and args.ext ~= '' and args.ext or 'png'
	local notext = args.notext ~= nil and args.notext ~= ''
	local nolink = args.nolink ~= nil and args.nolink ~= ''
	local noicon = args.noicon ~= nil and args.noicon ~= ''
	local nospan = args.nospan ~= nil and args.nospan
	local menu = args.menu ~= nil and args.menu ~= ''
	local imgSize = args.size ~= nil and args.size or 25
	local qty = args.qty
	local img = args.img ~= nil and args.img ~= '' and args.img or link
	local class = args.class ~= nil and args.class ~= '' and args.class or ''
	local altText = args.alt ~= nil and args.alt or (notext and (text or link)) or ''
	local linkSection = args.section
	local expIcon = args.expicon ~= nil and args.expicon or ''

	link = fixPagename(link)
	img = fixPagename(img)

	img = sgsub(img, '#', '')
	img = sgsub(img, '/Training', '')
	img = sgsub(img, '/Guide', '')
	img = sgsub(img, '/Boosts', '')
	-- Remove '#' from links to facilitate linking to things with hashes in
	-- their names (e.g. item 'Birthday Cake Piece #1'). Links to sections
	-- of a page should instead be performed using the 'section' parameter.
	link = sgsub(link, '#', '')

	-- Handle types for which links should be to a section of a static page
	local sl = sectionLinks[iconType]
	if sl ~= nil and not sl.exclude[link] then
		if text == nil or text == '' then
			text = link
		end
		linkSection = string.gsub(link, ' ', '')
		-- Link overwriting must occur after text & linkSection have been set
		link = sl.link
		if sl.img ~= nil then
			img = sl.img[1]
			if sl.img[2] ~= nil then
				iconType = sl.img[2]
			end
		end
	end

	--MANUAL OVERRIDES
	local origType = iconType
	local ovrImg = imgOverrides[link]
	local ovrTypeImg = typeImgOverrides[iconType]
	if ovrTypeImg ~= nil then
		ovrImg = ovrTypeImg ~= nil and ovrTypeImg[img] or ovrImg
	end
	local ovrTxt = txtOverrides[link]
	local ovrLink = linkOverrides[link]
	local ovrTypeLink = typeLinkOverrides[iconType]
	if ovrTypeLink ~= nil and ovrTypeLink[img] ~= nil then
		if ovrTypeLink[img] == '' then
			nolink = true
		else
			ovrLink = ovrTypeLink[img]
		end
	end
	local hasText = (text ~= nil and text ~= '')
	if ovrImg ~= nil and img == link then
		if type(ovrImg) == 'string' then
			img = ovrImg
		elseif type(ovrImg) == 'table' then
			img = ovrImg[1]
			if ovrImg[2] ~= nil then iconType = ovrImg[2] end
		end
	end

	-- Type & extension overrides must be after adjustments have been made
	-- for any image overrides
	local ovrTypeExt, ovrTypeExtTable = nil, typeExtOverrides[iconType]
	if ovrTypeExtTable ~= nil then
		ovrTypeExt = ovrTypeExtTable[img]
	end
	local ovrType = typeOverrides[iconType]
	local ovrExt = extOverrides.name[img]
	if ovrExt == nil then
		ovrExt = extOverrides.type[ovrType or iconType]
	end
	if not hasText and ovrTxt ~= nil then
		text = ovrTxt
		hasText = true
	end
	if ovrLink ~= nil then
		if not hasText then
			text = link
			hasText = true
		end
		link = ovrLink
	end
	if ovrTypeExt ~= nil then
		ext = ovrTypeExt
	elseif ovrExt ~= nil then
		ext = ovrExt
	end
	if ovrType ~= nil then
		iconType = ovrType
	end

	-- There are a couple specific double overrides to be included that don't fit in the above lists

	-- If the link requires disambiguation or a section, the modify the link accordingly
	-- while preserving the displayed text
	local isAmbig, hasSection = ambiguousOverrides[link], linkSection ~= nil and linkSection ~= ''
	if isAmbig or hasSection then
		if not hasText then
			text = link
			hasText = true
		end
		if isAmbig then
			link = link .. ' (' .. (ambiguousTypeMap[origType] or origType) .. ')'
		end
		if hasSection then
			link = link .. '#' .. linkSection
		end
	end

	local resultText = ''
	if not noicon then
		-- Image size
		local txtSize = tostring(imgSize)
		-- Extension and type if one is set
		local txtImg = (iconType ~= nil and iconType ~= '' and img .. '_(' .. iconType .. ')' or img) .. '.' .. ext
		-- Include a link unless no link -and- no text was requested
		local txtLink = '|link=' .. (nolink and '' or link)
		resultText = '[[File:' .. txtImg .. '|' .. txtSize .. 'x' .. txtSize .. 'px|alt=' .. altText .. txtLink .. ']]'
		if class ~= nil and class ~= '' then
			resultText = '<span class="'..class..'">'..resultText..'</span>'
		end
	end

	if not notext then
		if nolink then
			resultText = resultText .. (noicon and '' or ' ') .. (hasText and text or link)
		else
			resultText = resultText .. (noicon and '' or ' ') .. '[[' .. link .. (hasText and text ~= link and '|' .. text or '') .. ']]'
		end
	end

	-- Add Quantity to the front if requested
	--local resultQty = (qty ~= nil and qty ~= '' and formatnum(qty) .. ' ' or '')
	if qty ~= nil and qty ~= '' then
		resultText = formatnum(qty) .. ' ' .. resultText
	end

	if menu then
		return '{| class="articletable img-text" style="vertical-align:middle;"\r\n|-\r\n|' .. expIcon .. resultText .. '\r\n|}'
	elseif not noicon and not nospan then
		return '<span class="img-text">' .. expIcon .. resultText .. '</span>'
	else
		return resultText
	end
end

-- Returns a skill icon based on the realm.
function p._SkillRealmIcon(skill, realmID, size)
	local realm = realmID or 'melvorD:Melvor'
	local link = fixPagename(skill)

	local img = mw.html.create('span')
	if realm == 'melvorItA:Abyssal' then
		img:addClass('abyss-icon')
	end
	
	size = size or 25
	img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
		:wikitext('|'):wikitext(size):wikitext('x'):wikitext(size):wikitext('px')
		:wikitext('|link='):wikitext(link):wikitext(']]')
		
	return tostring(img)
end

function p.SkillRealmIcon(frame)
	local args = frame.args ~= nil and frame.args or frame
	local skill = args[1]
	local realmID = args.realm
	local size = tonumber(args.size)
	return p._SkillRealmIcon(skill, realmID, size)
end

function p._SkillReq(skill, level, showText, realmID)
	local realm = realmID or 'melvorD:Melvor'
	local link = fixPagename(skill)
	local altText = (showText and '') or skill
	if level == nil then 
		level = 'Unknown [[Category:Pages with script errors]]' 
	end

	-- Create skill image.
	local img = mw.html.create()
	
	if realm == 'melvorItA:Abyssal' then
		img = img:tag('span')
				 :addClass('abyss-icon')
	end
	
	img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
		:wikitext('|25x25px|link='):wikitext(link)
		:wikitext('|alt='):wikitext(altText)
		:wikitext(']]')
	
	-- Create text and add image.
	local text = mw.html.create('span')
		:addClass('img-text')
		:node(img)
	
	if showText == true then
		text:wikitext(' [[' .. link .. ']]')
	end
	
	--28/06/2024. Commented this out to try out red icons for abyssal levels
	-- as opposed to the text below. Remove entire section if still commented out after
	-- a week or two.
	--if realm == 'melvorItA:Abyssal' then
	--	text:wikitext(" Abyssal Level ")
	--else
	text:wikitext(" Level ")
	--end
	
	text:wikitext(level)
	return tostring(text)	
end

function p._MasteryReq(itemName, level, showText)
	local iconname = itemName
	local linkname = itemName
	--First, go with the lowest tier of potions if a potion is mentioned
	local s, e = string.find(itemName, 'Potion')
	if e ~= nil then
		linkname = string.sub(itemName, 1, e)
		iconname = linkname..' I'
	end
	local altTextItem = (showText and '') or linkname
	local altTextMastery = (showText and '') or '&nbsp;Mastery'

    local result = mw.html.create('span')
    result
        :addClass('img-text')
        :wikitext('Level ' .. level .. ' ')
        :node(p.Icon({linkname, img=iconname, type='item', notext=true, nospan=true, alt=altTextItem}))
        :node('[[File:Mastery.svg|25x25px|alt=' .. altTextMastery .. ']]')

    if showText then
        result
            :wikitext('[[' .. linkname .. ']] ')
            :wikitext('[[Mastery]]')
    end

    return tostring(result)
end

function p._Currency(currencyID, amount, maxAmount)
	local currencyDefn = currencyFormatDefn[currencyID]
	if currencyDefn == nil then
		error('Invalid currency ID: ' .. currencyID, 2)
	end

	local fileName, link, altText = currencyDefn.img, currencyDefn.name, currencyDefn.abbr

	local function numColour(amount)
		if tonumber(amount) < 0 then
			return mw.html.create('span')
				:css('color', 'red')
				:wikitext(formatnum(amount))
				:done()
		else
			return formatnum(amount)
		end
	end
	
	local html = mw.html.create('span')
		:addClass('img-text')

	-- Currency amounts
	if tonumber(amount) ~= nil then
		local text = mw.html.create('span')
			:css('margin-right', '0.2em')
			:node(numColour(amount))
		if (tonumber(maxAmount) ~= nil and maxAmount > amount) then
			text:wikitext(' - ' .. numColour(maxAmount))
		end
		html:node(text:done())
	end
	
	-- Currency icon
	if fileName ~= nil then
		html:wikitext('[[File:'):wikitext(fileName):wikitext('|25px')
		if link ~= nil then
			html:wikitext('|link='):wikitext(link)
		end
		html:wikitext('|alt=')
			:wikitext(((altText == nil and '') or altText))
			:wikitext(']]')
	end
	html:done()
	
	if ((amount ~= nil and tonumber(amount) == nil)
		or (maxAmount ~= nil and tonumber(maxAmount) == nil)) then
		html:wikitext('[[Category:Pages with non-numeric currency arguments]]')
	end
	
	return tostring(html)
end

-- p.Currency() is called by various currency templates:
--	[[Template:GP]], [[Template:SC]], [[Template:RC]]
function p.Currency(frame)
	local args = frame.args ~= nil and frame.args or frame
	local currID, minVal, maxVal, _
	currID = args[1]
	if args[2] ~= nil and args[2] ~= '' then
		minVal, _ = string.gsub(args[2], ',', '')
	end
	if args[3] ~= nil and args[3] ~= '' then
		maxVal, _ = string.gsub(args[3], ',', '')
	end
	
	return p._Currency((currID or 'nil'), minVal, maxVal)
end

--Adding a shortcut function for this
function p.TotH()
	return '<span class="img-text">[[File:TotH.svg|25x25px|link=Throne of the Herald Expansion]]</span>'
end

function p.AoD()
	return '<span class="img-text">[[File:AoD.png|25x25px|link=Atlas of Discovery Expansion]]</span>'
end

function p.ItA()
	return '<span class="img-text">[[File:ItA.png|25x25px|link=Into the Abyss Expansion]]</span>'
end

function p.Melvor()
	return '<span class="img-text">[[File:Melvor_Logo.svg|25x25px|link=Full Version]]</span>'
end

-- Function to retrieve the DLC id used for sorting columns that contain a DLC icon.
-- Passthrough from shared so that caller doesn't have to include Shared Module
function p.getExpansionID(id)
	return Shared.getLocalID(id)
end

function p.getExpansionIcon(id)
	local ns, _ = Shared.getLocalID(id)
	if ns == 'melvorTotH' then
		return p.TotH()..' '
	elseif ns == 'melvorAoD' then
		return p.AoD()..' '
	elseif ns == 'melvorItA' then
		return p.ItA()..' '
	else
		return ''
	end
end

-- Special function that returns a Melvor idle icon
-- Specifically used for a "DLC" column on tables.
-- Avoids every item from receiving the "base game icon"
function p.getDLCColumnIcon(id)
	local icon = p.getExpansionIcon(id)
	if icon == '' then
		icon = p.Melvor() .. ' '
	end
	return icon
end

return p