Module:Spawn table

local p = {}

local spawnData = {} -- contains mob spawn info local groupArgs = { ['passive'] = 'Passive mobs', ['ambient'] = 'Ambient mobs', ['hostile'] = 'Hostile mobs', ['water'] = 'Water mobs' }

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 = {} parsedLine.mob = mw.ustring.match(line, '^%s*(.-)%s*,') for key, value in mw.ustring.gmatch(line, ',%s*([%a]+)%s*:%s*(.-)%s*[,\r\n]') do					if value ~= '' then parsedLine[key:lower] = value end end 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.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 for groupArg, groupTable in pairs(spawnData) do table.insert(outputTable, "| style=\"text-align:center;\" colspan=\"" .. numberOfColumns .. "\" | " .. groupArgs[groupArg] .. "") for mobName, mobData in pairs(groupTable.mobs) do			local rowTable = {} table.insert(rowTable, '| ' .. frame:expandTemplate{title='EntityLink', args={mobName}}) table.insert(rowTable,  .. mobData.weight .. '&frasl;' .. groupTable.totalWeight .. ) table.insert(rowTable, mobData.count) table.insert(outputTable, table.concat(rowTable, ' || ')) end end return table.concat(outputTable, '\n|-\n') end

function p.mobSpawnTable(frame) local args = frame if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge( true ) else frame = mw.getCurrentFrame end

local outputTable = {}	-- list of strings to be concatenated into final output local columns = {'Mob', 'Weight', 'Group size'} local title = args.title or 'Mob spawn list' parseInput(args) -- create final output table.insert(outputTable, '{| class = "wikitable"') table.insert(outputTable, '! colspan = ' .. #columns .. ' | ' .. title) 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