Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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

local p = {}

local db = mw.loadData('Module:Mob/Aliases')

function p.main(frame)
    -- Get arguments from either the module call or the parent template
    local args = frame:getParent().args
    local mobName = args[1] or frame.args[1]

    if not mobName or mobName == "" then
        return "<strong class='error'>Error: Please specify a mob name.</strong>"
    end

    local mob = db[mobName]
    if not mob then
        return "<strong class='error'>Error: Mob '" .. mobName .. "' not found in database.</strong>"
    end

    -- Format the layers (avoiding the # operator which fails on mw.loadData)
    local layersHtml = {}
    if mob.layers then
        for _, layer in ipairs(mob.layers) do
            table.insert(layersHtml, "[[File:" .. layer .. ".png|16px|link=]]")
        end
    end

    -- Format the drops table (avoiding the # operator)
    local dropsHtml = ""
    if mob.drops then
        local hasDrops = false
        for _, drop in ipairs(mob.drops) do
            -- Only initialize the table header if there is at least one valid drop
            if not hasDrops then
                dropsHtml = '<table class="wikitable" style="width:100%; margin-top:5px;">\n<tr><th>Drop</th><th>Chance</th><th>Amount</th></tr>\n'
                hasDrops = true
            end
            
            local title = drop.lootingWorks and "Looting works" or "No looting"
            local amountStr = drop.lootingWorks and ("<b>" .. drop.amount .. "</b>") or drop.amount
            
            -- Expand the 'Slot' template dynamically before appending it to the string
            local slotItemHtml = frame:expandTemplate{ title = 'Slot', args = { drop.item } }
            
            dropsHtml = dropsHtml .. string.format(
                '<tr><td>%s</td><td>%s</td><td title="%s">%s</td></tr>\n', 
                slotItemHtml, drop.chance, title, amountStr
            )
        end
        if hasDrops then
            dropsHtml = dropsHtml .. "</table>"
        end
    end

    -- Construct the arguments to pass to the visual Template
    local targs = {
        name = mob.name,
        namecolor = mob.nameColor or "inherit",
        health = tostring(mob.health),
        category = mob.category,
        layers = table.concat(layersHtml, " "),
        drops = dropsHtml,
        image = args.image or args[2] or "" 
    }

    -- Pass the calculated data to the layout template
    return frame:expandTemplate{ title = 'Template:Mob/Layout', args = targs }
end

-- Get all mobs by a specific layer
function p.byLayer(frame)
    local args = frame:getParent().args
    local layerName = args[1] or frame.args[1]

    if not layerName or layerName == "" then
        return "<strong class='error'>Error: Please specify a layer name.</strong>"
    end

    local output = {}
    local mobNames = {}

    -- Iterate through the database and collect mobs belonging to the requested layer
    for name, mob in pairs(db) do
        if mob.layers then
            for _, layer in ipairs(mob.layers) do
                if layer == layerName then
                    table.insert(mobNames, name)
                    break
                end
            end
        end
    end

    -- Expand the standard 'Mob' template for every match
    for _, name in ipairs(mobNames) do
        table.insert(output, frame:expandTemplate{ title = 'Mob', args = { name } })
    end

    if #output == 0 then
        return "''No mobs found for " .. layerName .. ".''"
    end

    -- Wrap the output in a flex container so the floating mob cards wrap nicely and don't break page layouts
    return table.concat(output, "\n")
end

-- Get all naturally occurring mobs (has at least one layer)
function p.allNatural(frame)
    local output = {}
    local mobNames = {}

    -- Iterate through the database and collect mobs that have at least one layer
    for name, mob in pairs(db) do
        local hasLayer = false
        if mob.layers then
            -- Safely check for layer entries bypassing mw.loadData metatable restrictions
            for _, _ in pairs(mob.layers) do
                hasLayer = true
                break
            end
        end
        
        if hasLayer then
            table.insert(mobNames, name)
        end
    end


    -- Expand the standard 'Mob' template for every match
    for _, name in ipairs(mobNames) do
        table.insert(output, frame:expandTemplate{ title = 'Mob', args = { name } })
    end

    if #output == 0 then
        return "''No naturally spawning mobs found.''"
    end

    return table.concat(output, "\n")
end

return p