缺氧 Wiki

缺氧 wiki 编辑团队提示:注册账号并登录后浏览体验更佳 —— 官方免广告首页除外  •  启用小工具换源显示图片  •  通过参数设置定制化您的浏览体验

缺氧 Wiki 正在进行分叉讨论,欢迎参与!

如果你想帮助这个由玩家志愿编辑的 wiki 站点,欢迎加群 851803695 与其他编辑者一起参与建设!同时也欢迎加入缺氧 QQ 频道参与讨论~

了解更多

缺氧 Wiki
Advertisement
-- 小动物信息框
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string

local utils = require("Module:Utils")
local po = require([[Module:Po]]).po
local i18ncr = require([[Module:I18n]]).loadMessages([[Module:I18n/Creatures]])
local infobox = require([[Module:信息框/小动物]])
local getArgs = require("Dev:Arguments").getArgs
local crDataPath = [[Module:Data/Critters]]
local crData = mw.loadData(crDataPath)
local k0 = -273.15
local spSuf = "Species"
local spSufLen = #spSuf
local arrow = '➤'
local variant_exempt = {
    FreshWater = "FRESH_WATER"
}

local function familyCode(id)
    return fstr("STRINGS.CREATURES.FAMILY_PLURAL.%s", string.upper(id))
end

local function crBaseCode(crData)
    local id = crData.id

    local exceptions = {
        Bee = "STRINGS.CREATURES.SPECIES.BEE",
        BeeHive = "STRINGS.BUILDINGS.PREFABS.BEEHIVE",
        ScoutRover = "STRINGS.ROBOTS.MODELS.SCOUT",
        SweepBot = "STRINGS.ROBOTS.MODELS.SWEEPBOT"
    }
    if exceptions[id] ~= nil then return exceptions[id] end

    local familyName = crData.family:sub(0, -spSufLen - 1)
    local prefix = "STRINGS.CREATURES.SPECIES."

    if id == familyName then return prefix .. id:upper() end

    local variName = id:sub(#familyName + 1)
    variName = variant_exempt[variName] or variName

    return prefix .. fstr("%s.VARIANT_%s", familyName:upper(), variName:upper())
end

function p._variant(variId) -- variId is somthing like DreckoPlastic
    local variData = crData[variId]
    local baseCode = crBaseCode(variData)
    local out = {}
    out["ID"] = fstr("<code>%s</code>",variId)
    out["名称"] = po(baseCode .. ".NAME")
    out["图片"] = fstr("%s.png", out["名称"])
    out["图片说明"] = po(baseCode .. ".DESC")
    out["寿命"] = variData.ageMax
    out["血量"] = variData.hp
    out["装饰值"] = variData.decor
    out["装饰半径"] = variData.decorRadius

    if variData.deathDropItem then
        local ddiName = utils.getEntry(variData.deathDropItem)
        out["死亡掉落"] = fstr("[[%s]] × %d", ddiName,
                                   variData.deathDropItemAmount)
    end
    out["舒适温度下限"] = variData.warningLowTemperature and
                                    utils.float2str(
                                        variData.warningLowTemperature + k0)
    out["舒适温度上限"] = variData.warningHighTemperature and
                                    utils.float2str(
                                        variData.warningHighTemperature + k0)
    out["存活温度下限"] = variData.lethalLowTemperature and
                                    utils.float2str(
                                        variData.lethalLowTemperature + k0)
    out["存活温度上限"] = variData.lethalHighTemperature and
                                    utils.float2str(
                                        variData.lethalHighTemperature + k0)
    out["需求空间"] = variData.spaceRequired
    out["掩埋致死"] = variData.entombVulnerable ~= nil and
                              tostring(variData.entombVulnerable) or nil
    out["淹没致死"] = variData.drownVulnerable ~= nil and
                              tostring(variData.drownVulnerable) or nil
    out["孵化期"] = variData.incubationCycles
    out["成熟期"] = variData.adultThreshold
    out["产卵期"] = variData.fertilityCycles

    -- out["产卵概率"]
    out["产卵概率"] = {}
    if variData.baseLayEggChances then
        local totalWeight = 0
        for _, egg in ipairs(variData.baseLayEggChances) do
            totalWeight = totalWeight + tonumber(egg.weight)
        end

        for _, egg in ipairs(variData.baseLayEggChances) do
            local offspringId = egg.name:sub(1, -4) -- remove the "egg" suffix
            local offspringCode = crBaseCode(crData[offspringId])
            local offspringEggName = po(offspringCode .. ".EGG_NAME")
            local offspringWeight = tonumber(egg.weight) / totalWeight

            local offspringEntry = fstr("%s: %.1f%%", offspringEggName,
                                        offspringWeight * 100)
            table.insert(out["产卵概率"], offspringEntry)
        end
    end
    out["产卵概率"] = #out["产卵概率"] ~= 0 and
                              table.concat(out["产卵概率"], "<br/>") or nil

    -- out["代谢"]
    if variData.diet then
        local kCalPerCycle = math.abs(variData.kCaloriesPerCycle or 0)
        local plantData = nil
        local function reprDiet(di)
            local intake, produce = nil, nil
            local intakePlantPeriod = nil
            local ratio = tonumber(di.conversionRatio) or 1

            local intakeName, intakeIsCate, intakeCode = utils.getEntry(
                                                             di.element)
            local kCalPerKg = di.caloriesPerKg / 1000
            local intakeMass = ""
            if not di.plant then
                intakeMass = fstr("%s 单位", kCalPerCycle / kCalPerKg)
                if utils.startswith(intakeCode, 'STRINGS.ELEMENTS.') or
                    utils.startswith(intakeCode, 'STRINGS.ITEMS.FOOD.') then
                    intakeMass = utils.kg2str(kCalPerCycle / kCalPerKg, 1)
                end
            else
                plantData = plantData or mw.loadData([[Module:Data/Plants]])
                intakePlantPeriod = plantData[di.element] and plantData[di.element].cropVal and plantData[di.element].cropVal.cropDuration
                if not intakePlantPeriod then
                    return "出错:找不到植物数据"
                end
                intakeMass = fstr("%.1f%%", 60000 * kCalPerCycle / kCalPerKg /
                                      intakePlantPeriod)
            end

            if di.element ~= "" then
                intake = intakeName and
                             utils.wikiLink(intakeName, {isCate = intakeIsCate}) or
                             di.element
            end
            local prodName, prodIsCate = utils.getEntry(di.producedElement)
            if di.producedElement ~= "" then
                produce = prodName and
                              utils.wikiLink(prodName, {isCate = prodIsCate}) or
                              di.producedElement
                local produceMass = utils.kg2str(
                                        kCalPerCycle / kCalPerKg * ratio, 1)
                produce = fstr("%s %s", produce, produceMass)
            end
            intake = intake or '无'
            produce = produce or '无'
            return fstr('<div>%s</div>' ..
                            '<div style="text-align: right">%s</div>' ..
                            '<div>%s</div>' .. '<div>%s</div>', intake,
                        intakeMass, arrow, produce)
        end
        out["代谢"] = table.concat(utils.map(variData.diet, reprDiet), "\n")
    end
    -- test by: mw.logObject(p._variant("Hatch"))
    -- test by: mw.logObject(p._variant("Squirrel"))
    return out
end

function p.variant(frame)
    local args = getArgs(frame)
    return p._variant(args[1] or args.variId)
end

-- test by: = p.main(require("Module:debug").frame({},{debug=1, "Hatch"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="好吃哈奇"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="发光虫"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="辐射蜂"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="树鼠"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="毛鳞壁虎"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="释气海牛"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="抛壳蟹"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "ScoutRover"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "SweepBot"}))
function p.main(frame)
    local args = getArgs(frame)
    local familyName = args[1]

    if familyName == nil then
        local msgctxt = i18ncr:msgRev({
            key = args.pagename,
            args = {prefix = "STRINGS.CREATURES.FAMILY_PLURAL."}
        } or "")
        if msgctxt == nil then
            return {
                ["名称"] = fstr(
                    "找不到小动物科 '%s',请使用参数1或检查 [[%s]]。",
                    args.pagename, crDataPath)
            }
        end
        familyName = msgctxt:match("[%u_]+$")
    end

    if familyName:upper():find("SCOUTROVER") == nil and
        familyName:upper():find("SWEEPBOT") == nil and
        familyName:upper():find("SPECIES$") == nil then
        familyName = familyName .. "SPECIES"
    end

    local varis = {}
    for k, v in pairs(crData) do
        if v.family:upper() == familyName:upper() then
            table.insert(varis, k)
        end
    end
    table.sort(varis)

    local varisData = {}
    for _, vari in ipairs(varis) do
        local curr = p._variant(vari)
        table.insert(varisData, {label = curr['名称'], data = curr})
    end
    if args.debug then mw.logObject(varisData, "varisData") end
    return infobox.main(po(familyCode(familyName)), varisData)
end

return p
Advertisement