Module:UnitAbility: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
Created page with "-- 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 sp..."
 
No edit summary
 
Line 27: Line 27:
     local lang = frame.args.lang
     local lang = frame.args.lang


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


     local actualDescription = normalized:match("^([^\n]*)")
     local parts = mw.text.split(normalized, '\n')
 
     local morale = parts[2] or ''
     local necromancy = NECROMANCY[type] or 0
     local luck = parts[3] or ''
     local necromancy_hint = normalized:match("([^\n]*)$")
     local necromancy_hint = parts[4] or ''
 
    local ranges = mw.ustring.gsub(normalized, actualDescription, '')
     ranges = mw.ustring.gsub(ranges, necromancy_hint, '')
    ranges = mw.ustring.gsub(ranges, ":", ":")
    ranges = trim(ranges)
 
    local morale, luck = ranges:match("^([^\n]*)\n?(.*)$")


     local morale_hint = trim(morale:match("^([^:]*)") or "")
     local morale_hint = trim(morale:match("^([^:]*)") or "")
Line 48: Line 44:
     local luck_range = trim(luck:match("([^:]*)$") or "")
     local luck_range = trim(luck:match("([^:]*)$") or "")
     luck_range = mw.ustring.gsub(luck_range, "[.。]$", "")
     luck_range = mw.ustring.gsub(luck_range, "[.。]$", "")
    local necromancy = NECROMANCY[type] or 0


     return frame:preprocess(mw.ustring.format([[
     return frame:preprocess(mw.ustring.format([[

Latest revision as of 05:06, 1 August 2026

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