Module:Mob

local loader = require('Module:Loader') local string, table, yesno, format, arguments, odds, itemMdl, rarityTier, sprite, notice, colorMdl = loader.lazy.require('String', 'Table', 'Yesno', 'Format', 'Arguments', 'Odds', 'Item', 'RarityTier', 'Sprite', 'Notice', 'Color') local getArgs = arguments.getArgs local statdata = loader.lazy.loadData('Statname/Data')

local p = {}

local function isInfoNeeded(str) str = str:lower local keys = table.Set{ 'notice._info_needed', 'in', 'moreinfoneeded', 'min', 'moreinfo', }

return keys[str] end

function p.mobSpawnsTableLocation(frame) local args = getArgs(frame) local list = { {			mob = args[1] or args.mob, spots = args[2] or args.spots, time = args[3] or args.time, max = args[4] or args.max, },	}	local i = 2 while args['mob'..i] do		list[i] = { mob = args['mob'..i], spots = args['spots'..i], time = args['time'..i], max = args['max'..i], }		i = i + 1 end return p._mobSpawnsTableLocation(list) end

function p._mobSpawnsTableLocation(list) local wikitable = mw.html.create('table') :addClass('wikitable') :tag('tr') :tag('th'):wikitext('Mob'):done :tag('th'):wikitext(format._makeAbbr('The number of spots in the location where this mob can spawn in.' ,'Spawn Spots')):done :tag('th'):wikitext(format._makeAbbr('The time, after which a new mob spawns in every spot.', 'Spawn Delay')):done :tag('th'):wikitext(format._makeAbbr('The maximum number of mobs that can exist in each spot at a time.', 'Max per spot')):done :done --if true then return tostring(wikitable) end for i, mob in ipairs(list) do		wikitable:tag('tr') :tag('td'):wikitext(sprite._mobSprite(mob.mob, nil, true)):done :tag('td'):wikitext(mob.spots):done :tag('td'):wikitext(mob.time):done :tag('td'):wikitext(mob.max):done :done end return tostring(wikitable) end

function p.mobSpawnsTableMob(frame) local args = getArgs(frame) local list = { {			location = args[1] or args.location or args.loc, isSprite = yesno(args.is_sprite, true), spots = args[2] or args.spots, time = args[3] or args.time, max = args[4] or args.max, },	}	local i = 2 while args['mob'..i] do		list[i] = { location = args['location'..i] or args['loc'..i], isSprite = yesno(args['is_sprite'..i], true), spots = args['spots'..i], time = args['time'..i], max = args['max'..i], }		i = i + 1 end return p._mobSpawnsTableMob(list) end

function p._mobSpawnsTableMob(list) local wikitable = mw.html.create('table') :addClass('wikitable') :tag('tr') :tag('th'):wikitext('Location'):done :tag('th'):wikitext(format._makeAbbr('The number of spots in the location where this mob can spawn in.' ,'Spawn Spots')):done :tag('th'):wikitext(format._makeAbbr('The time, after which a new mob spawns in every spot.', 'Spawn Delay')):done :tag('th'):wikitext(format._makeAbbr('The maximum number of mobs that can exist in each spot at a time.', 'Max per spot')):done :done --if true then return tostring(wikitable) end for i, location in ipairs(list) do		wikitable:tag('tr') :tag('td'):wikitext(location.isSprite and sprite._locationSprite(location.location, nil, true) or location.location):done :tag('td'):wikitext(location.spots):done :tag('td'):wikitext(location.time):done :tag('td'):wikitext(location.max):done :done end return tostring(wikitable) end

function p.mobDropsTable(frame) local args = getArgs(frame) local list = { magic_find = args.magic_find, pet_luck = args.pet_luck, coins = args.coins or args.c,		combat_xp = args.combat_xp or args.cxp, fishing_xp = args.fishing_xp, exp = args.exp, mithril_powder = args.mithril_powder, caption = args.caption, {			drop = args[1] or args.drop or args.d,			rarity = args[2] or args.rarity or args.r,			chance = args[3] or args.chance or args.ch, count = args[4] or args.count or args.co, odds = yesno(args.odds, false), },	}	local i = 2 while args['drop'..i] do		list[i] = { drop = args['drop'..i] or args['d'..i], rarity = args['rarity'..i] or args['r'..i], chance = args['chance'..i] or args['ch'..i], count = args['count'..i] or args['co'..i], odds = yesno(args['odds'..i], false), }		i = i + 1 end return mw.getCurrentFrame:preprocess(p._mobDropsTable(list)) end

function p._mobDropsTable(list) local function lootRarity(str) str = str:lower local keys = { ['common'] = 'common loot', ['c'] = 'common loot', ['common loot'] = 'common loot', ['c l'] = 'common loot', ['cl'] = 'common loot', ['uncommon'] = 'uncommon loot', ['u'] = 'uncommon loot', ['uncommon loot'] = 'uncommon loot', ['uc l'] = 'uncommon loot', ['ucl'] = 'uncommon loot', ['rare'] = 'rare loot', ['r'] = 'rare loot', ['rare loot'] = 'rare loot', ['r l'] = 'rare loot', ['rl'] = 'rare loot', ['legendary'] = 'legendary loot', ['leg'] = 'legendary loot', ['l'] = 'legendary loot', ['legendary loot'] = 'legendary loot', ['l l'] = 'legendary loot', ['ll'] = 'legendary loot', ['rngesus'] = 'rngesus loot', ['rng'] = 'rngesus loot', ['rngesus loot'] = 'rngesus loot', ['rng loot'] = 'rngesus loot', ['rng l'] = 'rngesus loot', ['rngl'] = 'rngesus loot', }		return keys[str] end local function dropChance(baseChance, i)		if not list.magic_find or not list.pet_luck then return baseChance end local chance = tonumber(tostring(baseChance:gsub("%%", ""))) if chance and chance >= 5 then return baseChance end if not chance then return baseChance end chance = chance * (1 + ((list.magic_find + (list[i].drop:find('[Pp][Ee][Tt]') and list.pet_luck or 0)) / 100)) return tostring(chance) end local wikitable = mw.html.create('table'):addClass('wikitable') :tag('caption'):wikitext(list.caption):done if list[1].drop then wikitable:tag('tr') :tag('th'):attr{ ['colspan']=4 }:wikitext('Item drops'):done :done :tag('tr') :tag('th'):wikitext('Drop'):done :tag('th'):wikitext(string.makeTitle('Count', 'Each count for an item not prefixed with a &quot;+&quot; is guaranteed to drop.\nEach count for an item prefixed with a &quot;+&quot; has a chance to be added to the total amount dropped.')):done :tag('th'):wikitext('Drop Rarity'):done :tag('th'):wikitext('Chance'):done :done --if true then return tostring(wikitable) end for i, drop in ipairs( list ) do			if drop.drop:lower == 'none' then wikitable:tag('tr') :tag('td'):wikitext(itemMdl._itemDisplay(drop.drop, true)):attr{ colspan=4 }:done :done break else local counts = mw.text.split(drop.count:gsub(',%s*', ','), ",") local chances = mw.text.split(drop.chance:gsub(',%s*', ','), ",") local rows = math.max(#counts, #chances) local row = wikitable:tag('tr') :tag('td'):attr{ ['rowspan'] = rows}:wikitext(isInfoNeeded(drop.drop) and notice._info_needed or itemMdl._itemDisplay(drop.drop, true)):done :tag('td'):wikitext(isInfoNeeded(counts[1]) and notice._info_needed or counts[1]):done :tag('td'):attr{ ['rowspan'] = rows}:wikitext(isInfoNeeded(drop.rarity) and notice._info_needed or (lootRarity(drop.rarity) and odds._odds(lootRarity(drop.rarity), true) or rarityTier._link(drop.rarity))):done :tag('td'):wikitext(isInfoNeeded(chances[1]) and notice._info_needed or (chances[1] and odds._odds(dropChance(chances[1], i), false, false, true) or colorMdl.makeColor('green', odds._odds(dropChance(chances[1], i), nil, nil, true)))):done :done for j = 2, rows, 1 do					local row = wikitable:tag('tr') :tag('td'):wikitext(isInfoNeeded(counts[j] or '0') and notice._info_needed or counts[j] or '0'):done :tag('td'):wikitext(isInfoNeeded(chances[j] or '0') and notice._info_needed or ((chances[j] or '0') and odds._odds(dropChance(chances[j], i) or '0', false, false, true) or colorMdl.makeColor('green', odds._odds(dropChance(chances[j], i) or '0', nil, nil, true)))):done :done end end end end if list.coins or list.combat_xp or list.fishing_xp or list.coins or list.mithril_powder then wikitable:tag('tr') :tag('th'):attr{ ['colspan']=4 }:wikitext('Non-item drops'):done :done wikitable:tag('tr') :tag('th'):attr{ ['colspan']=1 }:wikitext('Drop'):done :tag('th'):attr{ ['colspan']=3 }:wikitext('Count'):done :done end if list.coins then wikitable:tag('tr') :tag('td'):attr{ ['colspan']=1 }:wikitext(colorMdl.makeColor('gold', itemMdl._itemDisplay('Coins', true))):done :tag('td'):attr{ ['colspan']=3 }:wikitext(isInfoNeeded(list.coins) and notice._info_needed or string.wrapTag(colorMdl.makeColor('gold', list.coins), 'b')):done :done end if list.combat_xp then wikitable:tag('tr') :tag('td'):attr{ ['colspan']=1 }:wikitext(''):done :tag('td'):attr{ ['colspan']=3 }:wikitext(isInfoNeeded(list.combat_xp) and notice._info_needed or string.wrapTag(colorMdl.makeColor('blue', list.combat_xp), 'b')):done :done end if list.fishing_xp then wikitable:tag('tr') :tag('td'):attr{ ['colspan']=1 }:wikitext(''):done :tag('td'):attr{ ['colspan']=3 }:wikitext(isInfoNeeded(list.fishing_xp) and notice._info_needed or string.wrapTag(colorMdl.makeColor('blue', list.fishing_xp), 'b')):done :done end if list.exp then wikitable:tag('tr') :tag('td'):attr{ ['colspan']=1 }:wikitext(itemMdl._itemDisplay('Experience Orbs', true)):done :tag('td'):attr{ ['colspan']=3 }:wikitext(isInfoNeeded(list.exp) and notice._info_needed or string.wrapTag(colorMdl.makeColor('blue', list.exp), 'b')):done :done end if list.mithril_powder then wikitable:tag('tr') :tag('td'):attr{ ['colspan']=1 }:wikitext(colorMdl.makeColor('green', itemMdl._itemDisplay('Mithril Powder', true))):done :tag('td'):attr{ ['colspan']=3 }:wikitext(isInfoNeeded(list.mithril_powder) and notice._info_needed or string.wrapTag(colorMdl.makeColor('green', list.mithril_powder), 'b')):done :done end return tostring(wikitable) end

function p.mobVariants(frame) local args = getArgs(frame) local data = { mob = args.mob or args.name or args[1] or mw.title.getCurrentTitle.namespace == 0 and mw.title.getCurrentTitle.prefixedText, image = args.image, caption = args.caption, levels = {args.level}, spawn_locations = {args.spawn_location}, stats = {}, drops = { combat_xp = {args.combat_xp or args.cxp}, experience = {args.experience or args.exp}, coins = {args.coins or args.c}, },	}	for k, v in pairs(statdata) do		if args[v.name:lower:gsub(' ', '_')] or args[v.nameShort:lower:gsub(' ', '_')] or args[v.nameSuperShort:lower:gsub(' ', '_')] or args[v.shortcode] then data.stats[v.shortcode] = {color = v.color, aliases = {v.name:lower:gsub(' ', '_'), v.nameShort:lower:gsub(' ', '_'), v.nameSuperShort:lower:gsub(' ', '_'), v.shortcode}} data.stats[v.shortcode][1] = args[v.shortcode] end end for i = 2, table.length(args), 1 do		data.levels[i] = args['level'..i]		data.spawn_locations[i] = args['spawn_location'..i]

for k, v in pairs(data.stats) do			for _, alias in ipairs(v.aliases) do				if not data.stats[k][i] then data.stats[k][i] = args[alias..i]				end end end

data.drops.combat_xp[i] = args['combat_xp'..i] or args['cxp'..i]		data.drops.experience[i] = args['experience'..i] or args['exp'..i]		data.drops.coins[i] = args['coins'..i] or args['c'..i]	end -- mw.logObject(data) return mw.getCurrentFrame:preprocess(p._mobVariants(data)) end

p.mobInfo = p.mobVariants

function p._mobVariants(data) local ROWS = math.max(#data.levels, #data.spawn_locations, #data.drops.combat_xp, #data.drops.experience, #data.drops.coins)

local wikitable = mw.html.create('table'):addClass('wikitable centertext') wikitable:tag('caption'):wikitext(data.caption):done :tag('tr') :tag('th'):attr('rowspan', '2'):wikitext('Level'):done :tag('th'):attr('rowspan', '2'):wikitext('Spawn Location'):done :tag('th'):attr('colspan', table.length(data.stats)):wikitext('Stats'):done :tag('th'):attr('colspan', '3'):wikitext('Drops'):done :done

local row = wikitable:tag('tr') for k, v in pairs(data.stats) do				row:tag('th'):wikitext(''):done end row:tag('th'):wikitext(''):done :tag('th'):wikitext('Experience'):done :tag('th'):wikitext(''):done row:done

local row = wikitable:tag('tr') :tag('td'):wikitext(''):done :tag('td'):wikitext(data.spawn_locations[1]):done for k, v in pairs(data.stats) do			row:tag('td'):wikitext(colorMdl.makeColor(v.color, tonumber(v[i]) and string._formatNum(v[1] or '0') or v[1])):done end row:tag('td'):wikitext(colorMdl.makeColor('green', data.drops.combat_xp[1] or '0')):done :tag('td'):wikitext(colorMdl.makeColor('blue', data.drops.experience[1] or '0')):done :tag('td'):wikitext(colorMdl.makeColor('gold', data.drops.coins[1] or '0')):done row:done

for i = 2, ROWS, 1 do		local row = wikitable:tag('tr') :tag('td'):wikitext(''):done :tag('td'):wikitext(data.spawn_locations[i]):done for k, v in pairs(data.stats) do				row:tag('td'):wikitext(colorMdl.makeColor(v.color, tonumber(v[i]) and string._formatNum(v[i] or '0') or v[i])):done end row:tag('td'):wikitext(colorMdl.makeColor('green', data.drops.combat_xp[i] or '0')):done :tag('td'):wikitext(colorMdl.makeColor('blue', data.drops.experience[i] or '0')):done :tag('td'):wikitext(colorMdl.makeColor('gold', data.drops.coins[i] or '0')):done row:done end

return tostring(wikitable) end

return p