Module:Spawn table

local p = {}

local i18n = { processArgsModule = 'Module:ProcessArgs', entityLinkTemplate = 'EntityLink', -- defaultTitle = 'Mob spawn list', mobColumn = 'Mob', chanceColumn = mw.getCurrentFrame:expandTemplate{title='Tooltip', args={'Spawn chance', 'Chance to spawn relative to other mobs in the category'}}, groupSizeColumn = 'Group size' }

local spawnData = {} -- contains mob spawn info

local groupArgs = { ['passive'] = 'Passive category', ['ambient'] = 'Ambient category', ['hostile'] = 'Hostile category', ['water'] = 'Water category' }

local hasNotes = false

-- parses input arguments into spawnData table local function parseInput(args) for argName in pairs(groupArgs) do		local groupArg = args[argName] if groupArg then mw.logObject(groupArg) local currentGroup = {} local totalWeight = 0 currentGroup.mobs = {} groupArg = groupArg .. '\n' -- allow last line to be matched like the rest -- parse input of group parameter for line in mw.ustring.gmatch(groupArg, '[^\r\n]+[\r\n]') do -- split on newline local parsedLine = {} for key, value in mw.ustring.gmatch(line, '([%a]+)%s*:%s*(.-)%s*[,\r\n]') do					if value ~= '' then parsedLine[key:lower] = value end end parsedLine.mob = mw.ustring.match(line, '^%s*(.-)%s*,') parsedLine.note = mw.ustring.match(line, 'note:%s*(.-)%s*[\r\n]') local currentMob = {} -- convert weight to number; becomes nil if conversion fails if parsedLine.weight then weightNum = tonumber(parsedLine.weight) -- if converted to number successfully if weightNum then currentMob.weight = weightNum totalWeight = totalWeight + weightNum end end if parsedLine.count then currentMob.count = parsedLine.count end if parsedLine.note then hasNotes = true currentMob.note = parsedLine.note end if parsedLine.mob then currentMob.mob = parsedLine.mob table.insert(currentGroup.mobs, currentMob) end end currentGroup.totalWeight = totalWeight spawnData[argName] = currentGroup end end mw.logObject(spawnData) end

-- creates table body wikitext from info in spawnData local function createTableBody(numberOfColumns) local outputTable = {} local frame = mw.getCurrentFrame local groupNumber = 1 for groupArg, groupTable in pairs(spawnData) do		local separator = '|-' local color = '#f2f2f2' table.insert(outputTable, separator) table.insert(outputTable, "| style=\"text-align:center; background-color:" .. color			.. ";\" colspan=\"" .. numberOfColumns .. "\" | " .. groupArgs[groupArg] .. "") for _, mobData in ipairs(groupTable.mobs) do			table.insert(outputTable, separator) local rowTable = {} local mobCell = '| style="background-color:' .. color .. ';" | ' .. frame:expandTemplate{title=i18n.entityLinkTemplate, args={mobData.mob}} if mobData.note then mobCell = mobCell .. frame:extensionTag{name='ref', content=mobData.note, args={group='note'}} end table.insert(rowTable, mobCell) table.insert(rowTable, '| style="text-align:center;" | ' .. mobData.weight .. '&frasl;' .. groupTable.totalWeight .. '') table.insert(rowTable, '| style="text-align:center;" | ' .. mobData.count) table.insert(outputTable, table.concat(rowTable, '\n')) end -- groupNumber = groupNumber + 1 end return table.concat(outputTable, '\n') end

-- function called from template function p.mobSpawnTable(frame) local args = frame if frame == mw.getCurrentFrame then args = require(i18n.processArgsModule).merge( true ) else frame = mw.getCurrentFrame end parseInput(args)

local outputTable = {}	-- list of strings to be concatenated into final output local columns = {i18n.mobColumn, i18n.chanceColumn, i18n.groupSizeColumn} local title = args.title -- create final output table.insert(outputTable, '{| class = "wikitable"') if title then table.insert(outputTable, '! colspan = ' .. #columns .. ' | ' .. title) end local columnsStr = '! '	for index, value in pairs(columns) do		if index ~= 1 then columnsStr = columnsStr .. ' !! '		end columnsStr = columnsStr .. value end table.insert(outputTable, columnsStr) table.insert(outputTable, createTableBody(#columns)) table.insert(outputTable, '|}') local outputWikitext = table.concat(outputTable, '\n|-\n')

if hasNotes then outputWikitext = outputWikitext .. '\n\n' .. frame:extensionTag{name='references', args={group='note'}} end

return outputWikitext end

return p