Module:植物信息框

来自缺氧 Wiki
跳转到导航 跳转到搜索

用于模板:Template:植物信息框。 视图模块:Module:信息框/植物。 数据模块:Module:Data/Plants



-- 植物信息框
local p = {}
local fstr = mw.ustring.format

local utils = require("Module:Utils")
local k0 = utils.K0
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 plDataPath = [[Module:Data/Plants]]
-- local plDataPath = [[Module:Sandbox/Data/Plants]]
local plData = mw.loadData(plDataPath)
local seedTypes = {
    "* 不可通过收获额外获取种子\n* 可拔除后移栽",
    "* 收获即被拔除,且不可移栽",
    "* 收获作物时<abbr title = 基础几率为10%>有几率</abbr>额外掉落种子\n* 可拔除后移栽",
    "",
    "* 收获变异作物不再额外掉落种子\n* 可拔除后移栽",
    "* 收获作物时掉落固定数量的种子\n* 可拔除后移栽",
    GasGrass = "* {{游戏本体图标}}本体游戏中,不可通过收获额外获取种子\n" ..
    "* {{眼冒金星图标}} DLC 中,收获作物时<abbr title = 基础几率为10%>有几率</abbr>额外掉落种子\n" ..
    "* 可拔除后移栽"
}
local exception = {
    WOOD_TREE = "ForestTree",
    WOOD_TREE_BRANCH = "ForestTreeBranch",
    BASICFABRICMATERIALPLANT = "BasicFabricPlant",
    SPICE_VINE = "SpiceVine",
    COLDBREATHERCLUSTER = "ColdBreather",
    BEAN_PLANT = "BeanPlant",
    BLUE_GRASS = "BlueGrass"
}
local deprecatedContent = ""

function p._main(plantData, plantCode)
    local out = {}
    out["ID"] = fstr("<code>%s</code>", plantData.id)
    out["名称"] = po(plantCode)
    out["图片"] = fstr("%s.png", out["名称"])
    out["图片说明"] = po(plantCode:sub(1, -6) .. ".DESC")
    if plantData.decorToggler ~= nil then
    	out["装饰值"] = plantData.decorToggler.posDecor
    	out["装饰半径"] = plantData.decorToggler.posRadius
    	out["枯萎装饰值"] = plantData.decorToggler.negDecor
    	out["枯萎装饰半径"] = plantData.decorToggler.negRadius
    elseif plantData.decorProvider ~= nil then
        out["装饰值"] = plantData.decorProvider.baseDecor
        out["装饰半径"] = plantData.decorProvider.baseRadius
    end
    if plantData.temperatureVulnerable ~= nil then
    	local temps = plantData.temperatureVulnerable
    	local tempWarningLow = temps.temperatureWarningLow
    	local tempWarningHigh = temps.temperatureWarningHigh
    	local tempLethalLow = temps.temperatureLethalLow < temps.temperatureWarningLow and temps.temperatureLethalLow or temps.temperatureWarningLow
    	local tempLethalHigh = temps.temperatureLethalHigh > temps.temperatureWarningHigh and temps.temperatureLethalHigh or temps.temperatureWarningHigh
        out["生长温度下限"] = utils.float2str(tempWarningLow + k0)
        out["生长温度上限"] = utils.float2str(tempWarningHigh + k0)
        out["存活温度下限"] = utils.float2str(tempLethalLow + k0)
        out["存活温度上限"] = utils.float2str(tempLethalHigh + k0)
    end
    if plantData.drowningMonitor ~= nil then
        out["淹没致死"] = plantData.drowningMonitor.canDrownToDeath and "✔" or "仅停止生长"
    else
        out["淹没致死"] = "✖"
    end
    if plantData.kBoxCollider2D ~= nil then
        out["宽度"] = plantData.kBoxCollider2D.x
        out["高度"] = plantData.kBoxCollider2D.y
    end

    out["方向"] = "向上生长"
    if plantData.tags then
	    for _, v in ipairs(plantData.tags) do
	        if (v == "Hanging") then
	            out["方向"] = "向下生长"
	        end
	        if (v == "DeprecatedContent") then
	        	deprecatedContent = "[[Category:未实装内容]]"
	        end
	    end
    end

    if plantData.pressureVulnerable ~= nil then
        if plantData.pressureVulnerable.pressure_sensitive then
            out["气压上限"] = plantData.pressureVulnerable.pressureWarning_High
            out["气压下限"] = plantData.pressureVulnerable.pressureWarning_Low
        end
        local safeAtmo = {}
        for _, v in ipairs(plantData.pressureVulnerable.safe_atmospheres) do
            local safeEl = fstr("{{物品|%s}}", utils.getEntry(v))
            table.insert(safeAtmo, safeEl)
        end
        out["环境"] = table.concat(safeAtmo, ' / ')
    end
    if plantData.illuminationVulnerable ~= nil then
        out["光照阈值"] = plantData.illuminationVulnerable.LightIntensityThreshold
        out["光照"] = plantData.illuminationVulnerable.prefersDarkness and "黑暗" or
                            fstr("最低 %s 勒克斯", out["光照阈值"])
    end
    if plantData.fertilization ~= nil then
        out["施肥"] = {}
        for _, fertilizer in ipairs(plantData.fertilization) do
            table.insert(out["施肥"], fstr("{{物品|%s}} %s/周期", utils.getEntry(fertilizer.tag.Name),
                utils.kg2str(fertilizer.massConsumptionRate * 600, 1)))
        end
        out["施肥"] = table.concat(out["施肥"], '\n')
    end
    if plantData.irrigation ~= nil then
        out["灌溉"] = {}
        for _, fertilizer in ipairs(plantData.irrigation) do
            table.insert(out["灌溉"], fstr("{{物品|%s}} %s/周期", utils.getEntry(fertilizer.tag.Name),
                utils.kg2str(fertilizer.massConsumptionRate * 600, 1)))
        end
        out["灌溉"] = table.concat(out["灌溉"], '\n')
    end
    out["效果"] = {}
    local quadrapleConsumeWhenReplanted = {-- 移栽后吸收效率升至4倍的植物
    	ColdBreather = true,
    	Oxyfern = true,
    	BlueGrass = true,
    }
    if plantData.elementConsumer then
    	local elementConsumer = plantData.elementConsumer
    	local elementConsumerDesc
    	if elementConsumer.consumptionRadius and elementConsumer.elementToConsume and elementConsumer.consumptionRate then
    		elementConsumerDesc = fstr("距离 %s 格内的{{物品|%s}}:%s/秒",
    			elementConsumer.consumptionRadius, utils.getEntry(elementConsumer.elementToConsume), utils.kg2str(elementConsumer.consumptionRate, 2))
    		if quadrapleConsumeWhenReplanted[plantData.id] then
    			elementConsumerDesc = elementConsumerDesc .. fstr("\n* 人工栽培时吸收速率变为 %s/秒", utils.kg2str(elementConsumer.consumptionRate * 4, 2))
    		end
    		if elementConsumer.isRequired then
    			out["吸收元素需求"] = elementConsumerDesc
    		else
    			table.insert(out["效果"], elementConsumerDesc)
    		end
			if plantData.elementConverters then
    			for _, elementConverter in pairs(plantData.elementConverters) do
    				-- 单种元素转化成单种元素,且转化元素为此前所吸收元素的情况
    				local sourceEl = elementConverter.consumedElements[1].Tag.Name
    				local multiplier = elementConverter.OutputMultiplier
    				local targetEl = elementConverter.outputElements[1].element
    				if sourceEl ~= elementConsumer.elementToConsume then
    					error()
    				end
    				local elementConverterdesc = fstr("将所吸收的{{物品|%s}} 以 1 :%s 的比率转换为{{物品|%s}}", 
    					utils.getEntry(sourceEl), multiplier, utils.getEntry(targetEl))
    				table.insert(out["效果"], elementConverterdesc)
    			end
			end
    	else
    		error("Invalid elementConsumer.")
    	end
    end
	out["效果"] = table.concat(out["效果"], '\n')
    if plantData.cropVal ~= nil then
    	local _tmp = ""
    	local cropCode = ""
        out["收获物"], _tmp, cropCode = utils.getEntry(plantData.cropVal.cropId)
        out["收获数量"] = plantData.cropVal.numProduced
        out["收获"] = fstr("{{物品|%s}} %s %s", out["收获物"], out["收获数量"], utils.startswith(cropCode, 'STRINGS.ELEMENTS.') and "千克" or "单位")
        if plantData.maturityMax then
        	out["生长时间"] = plantData.maturityMax
        	out["产出时间"] = plantData.cropVal.cropDuration
        else
        	out["生长时间"] = plantData.cropVal.cropDuration
        end
    end
    if plantData.seedInfo ~= nil then
        out["种子"] = fstr("{{物品|%s}}", utils.getEntry(plantData.seedInfo.seedId))
        out["种子说明"] = seedTypes[plantData.id] or seedTypes[plantData.seedInfo.productionType + 1]
    end
    if plantData.growing ~= nil and plantData.growing.shouldGrowOld then
        out["挂留时间"] = plantData.growing.maxAge
    end
    if plantData.wiltConditions ~= nil then
	    out["辐射下限"] = plantData.wiltConditions.minRad
	    out["辐射上限"] = plantData.wiltConditions.maxRad
    end
    if plantData.primaryElement ~= nil then
        out["元素"] = fstr("{{物品|%s}}", utils.getEntry(plantData.primaryElement.Name))
        out["质量"] = utils.kg2str(plantData.primaryElement.Mass)
        out["初始温度"] = utils.float2str(plantData.primaryElement.InternalTemperature + k0) .. " °C"
    end
    if plantData.id == "PrickleFlower" then out["初始温度"] = "15 °C" end -- 临时处理
    return out
end

-- 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, "ForestTree"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "ForestTree", "ForestTreeBranch"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "ForestTree", "ForestTreeBranch", "SapTree"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "PrickleGrass", "EvilFlower"}))
function p.main(frame)
    local args = getArgs(frame)
    local infos = {}
    local plants = {}

    if args[1] ~= nil then
        for _, plantId in ipairs(args) do
            local _, _, plantCode = utils.getEntry(plantId)
            if plantCode == nil then
                return {
                    ["名称"] = fstr("找不到植物 '%s',请使用参数1或检查 [[%s]]。", plantId, plDataPath)
                }
            end
            table.insert(plants, {
                id = plantId,
                code = plantCode
            })
        end
    else
        local plantCode = i18ncr:msgRev({
            key = args.pagename,
            args = {
                prefix = "STRINGS.CREATURES.SPECIES."
            }
        } or "")
        if plantCode == nil then
            return {
                ["名称"] = fstr("找不到植物 '%s',请使用参数1或检查 [[%s]]。", args.pagename,
                    plDataPath)
            }
        end
        local plantId = plantCode:match("([%u_]+).NAME$")
        table.insert(plants, {
            id = plantId,
            code = plantCode
        })
    end
    for i, plant in ipairs(plants) do
        plant.id = exception[plant.id] ~= nil and exception[plant.id] or plant.id
        for k, v in pairs(plData) do
            if k:upper() == plant.id:upper() then
                local branchDef = v.plantBranchGrowerDef
                if branchDef ~= nil and branchDef.BRANCH_PREFAB_NAME ~= nil then
                    local branchId = branchDef.BRANCH_PREFAB_NAME
                    local branch = plData[branchId]
                    if branch ~= nil then
                        local pTrunk = p._main(v, plant.code)
                        table.insert(infos, {
                            label = pTrunk["名称"],
                            data = pTrunk
                        })
                        local _, _, branchCode = utils.getEntry(branchId)
                        if branchCode ~= nil then
                            local pBranch = p._main(branch, branchCode)
                            table.insert(infos, {
                                label = pTrunk["名称"].."枝杈",
                                data = pBranch
                            })
                        end
                    end
                else
                    local curr = p._main(v, plant.code)
                    table.insert(infos, {
                        label = curr["名称"],
                        data = curr
                    })
                end
            end
        end
    end

    if args.debug then
        mw.logObject(infos)
    end
    local infoboxTitle = #infos > 1 and args.pagename or infos[1].label
    local category = args.nocat and "" or "[[Category:植物]]" .. deprecatedContent
    return infobox.main(infoboxTitle, infos) .. category
end

return p