Module:Spawn table

local p = {}

local i18n = { processArgsModule = 'Module:ProcessArgs', entityLinkTemplate = 'EntityLink', -- defaultTitle = 'Mob spawn list', mobColumn = 'Mob', weightColumn = 'Weight', groupSizeColumn = 'Group size', notesColumn = 'Notes' }

local spawnData = {} -- contains mob spawn info

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

local hasNotesColumn = false

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.notes = mw.ustring.match(line, 'notes:%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.notes then hasNotesColumn = true currentMob.notes = parsedLine.notes end if parsedLine.mob then currentGroup.mobs[parsedLine.mob] = currentMob end end currentGroup.totalWeight = totalWeight spawnData[argName] = currentGroup end end mw.logObject(spawnData) end

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 mobName, mobData in pairs(groupTable.mobs) do			table.insert(outputTable, separator) local rowTable = {} table.insert(rowTable, '| style="background-color:' .. color .. ';" | ' .. frame:expandTemplate{title=i18n.entityLinkTemplate, args={mobName}}) table.insert(rowTable, '| style="text-align:center;" | ' .. mobData.weight .. '&frasl;' .. groupTable.totalWeight .. '') table.insert(rowTable, '| style="text-align:center;" | ' .. mobData.count) if hasNotesColumn then local notesCell = '|' if mobData.notes then notesCell = notesCell .. ' ' .. mobData.notes end table.insert(rowTable, ' ' .. notesCell) end table.insert(outputTable, table.concat(rowTable, '\n')) end -- groupNumber = groupNumber + 1 end return table.concat(outputTable, '\n') end

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.weightColumn, i18n.groupSizeColumn} if hasNotesColumn then table.insert(columns, i18n.notesColumn) end 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, '|}') return table.concat(outputTable, '\n|-\n') end

return p