Module:UnitAbility

From Heroes of Might and Magic: Olden Era Official Wiki
Revision as of 05:06, 1 August 2026 by Aurelain (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

-- This module is intended as a supporting muscle for the UnitAbility template.
local p = {}

local NECROMANCY = {
    demon = 1,
    dragon = 1,
    living = 1,
    magic_creature = 1,
}

-- Displays a variable
------------------------------------------------------------------------------------------------------------------------
local function dump(target)
    return '<pre>' .. mw.dumpObject(target) .. '</pre>'
end

-- Helper to trim standard and full-width Japanese spaces (U+3000)
------------------------------------------------------------------------------------------------------------------------
local function trim(s)
    return mw.text.trim(s or "", "%s ")
end

------------------------------------------------------------------------------------------------------------------------
function p.buildStats(frame)
    local description = frame.args.description
    local type = frame.args.type
    local lang = frame.args.lang

    local normalized = description:gsub("<[bB][rR]%s*/?>", '\n')
    normalized = trim(normalized)
    normalized = mw.ustring.gsub(normalized, '\n+', '\n')
    normalized = mw.ustring.gsub(normalized, ":", ":")

    local parts = mw.text.split(normalized, '\n')
    local morale = parts[2] or ''
    local luck = parts[3] or ''
    local necromancy_hint = parts[4] or ''

    local morale_hint = trim(morale:match("^([^:]*)") or "")
    local morale_range = trim(morale:match("([^:]*)$") or "")
    morale_range = mw.ustring.gsub(morale_range, "[.。]$", "")

    local luck_hint = trim(luck:match("^([^:]*)") or "")
    local luck_range = trim(luck:match("([^:]*)$") or "")
    luck_range = mw.ustring.gsub(luck_range, "[.。]$", "")

    local necromancy = NECROMANCY[type] or 0

    return frame:preprocess(mw.ustring.format([[
        {{UnitAbility/stats
        | lang = %s
        | morale_hint = %s
        | morale_range = %s
        | luck_hint = %s
        | luck_range = %s
        | necromancy_hint = %s
        | necromancy = %s
        }}
    ]], lang, morale_hint, morale_range, luck_hint, luck_range, necromancy_hint, necromancy))
end

------------------------------------------------------------------------------------------------------------------------
function p.extractDescription(frame)
    local description = frame.args.description
    local normalized = description:gsub("<[bB][rR]%s*/?>", "\n")
    local actualDescription = normalized:match("^([^\n]*)") or ''
    return actualDescription
end

return p