全站通知:

模块:Category

来自星露谷物语维基
跳到导航 跳到搜索

本模块会根据具体物品输出游戏内对应的分类的色系和名称,用于模板:InfoboxCategory,具体使用说明见给定的模板。

[ 查看 | 编辑 | 历史 | 刷新 ]上述文档的内容来自模块:Category/doc
local Helper = require("Module:Helper")
local ID = require("Module:ID")
local Furniture = require("Module:Furniture")
local ObjectData = Helper.LazyLoad('Module:Object/data')
local WeaponsData = Helper.LazyLoad('Module:Weapons/data')

local p = {}

-- 工具函数
local function startsWith(str, prefix)
    if not str or not prefix then return false end
    return string.sub(str, 1, string.len(prefix)) == prefix
end

-- 提取通用的ID清理函数
local function stripPrefix(id)
    if not id then return "" end
    return string.gsub(id, "^%([^%)]*%)", "")
end

local categoryNames = {
    [-103] = "技能书", -- skillBook_Category
    [-102] = "书", -- Book_Category
    [-100] = "服装", -- category_clothes
    [-99] = "工具", -- Tool.cs.14307
    [-97] = "鞋类", -- Boots.cs.12501
    [-96] = "戒指", -- Ring.cs.1
    [-81] = "采集品", -- Object.cs.12869
    [-80] = "花", -- Object.cs.12866
    [-79] = "水果", -- Object.cs.12854
    [-75] = "蔬菜", -- Object.cs.12851
    [-74] = "种子", -- Object.cs.12855
    [-28] = "怪物战利品", -- Object.cs.12867
    [-27] = "工匠物品", -- Object.cs.12862
    [-26] = "工匠物品", -- Object.cs.12862
    [-25] = "菜品", -- Object.cs.12853
    [-24] = "装饰", -- Object.cs.12859 / Furniture_Decoration
    [-22] = "钓具", -- Object.cs.12858
    [-21] = "鱼饵", -- Object.cs.12857
    [-20] = "垃圾", -- Object.cs.12860
    [-19] = "化肥", -- Object.cs.12856
    [-18] = "动物制品", -- Object.cs.12864
    [-16] = "资源", -- Object.cs.12868
    [-15] = "资源", -- Object.cs.12868
    [-14] = "动物制品", -- Object.cs.12864
    [-12] = "矿物", -- Object.cs.12850
    [-8] = "制造品", -- Object.cs.12863
    [-7] = "菜品", -- Object.cs.12853
    [-6] = "动物制品", -- Object.cs.12864
    [-5] = "动物制品", -- Object.cs.12864
    [-4] = "鱼", -- Object.cs.12852
    [-2] = "矿物" -- Object.cs.12850
}
local categoryColors = {
    [-103] = "122,93,39",
    [-102] = "85,47,27",
    [-81] = "10,130,50",
    [-80] = "219,54,211",
    [-79] = "255,20,147", -- DeepPink
    [-75] = "0,128,0", -- Green
    [-74] = "165,42,42", -- Brown
    [-28] = "50,10,70",
    [-27] = "0,155,111",
    [-26] = "0,155,111",
    [-24] = "150,80,190",
    [-22] = "0,139,139", -- DarkCyan
    [-21] = "139,0,0", -- DarkRed
    [-20] = "105,105,105", -- DimGray
    [-19] = "112,128,144", -- SlateGray
    [-18] = "255,0,100",
    [-16] = "64,102,114",
    [-15] = "64,102,114",
    [-14] = "255,0,100",
    [-12] = "110,0,90",
    [-8] = "148,61,40",
    [-7] = "220,60,0",
    [-6] = "255,0,100",
    [-5] = "255,0,100",
    [-4] = "0,0,139", -- DarkBlue
    [-2] = "110,0,90"
}

-- 缓存家具数据
local FurnitureData

local function getFurnitures()
    if not FurnitureData then 
        FurnitureData = Furniture:parseData() 
    end
    return FurnitureData
end

-- 获取物品类别ID
local function getCategoryById(id)
    local cleanId = stripPrefix(id)
    local item = ObjectData[cleanId]
    return item and item.Category or nil
end

-- 获取物品类型
local function getTypeById(id)
    local cleanId = stripPrefix(id)
    local item = ObjectData[cleanId]
    return item and item.Type or nil
end

-- 获取家具信息
local function getFurniture(id)
    if not startsWith(id, "(F)") then return nil end
    
    local cleanId = stripPrefix(id)
    local furnitures = getFurnitures()
    return furnitures[cleanId]
end

-- 武器特殊加成配置
local weaponBonuses = {
    ["(W)2"] = 20.0,
    ["(W)3"] = 15.0
}

-- 武器类型名称映射
local weaponTypeNames = {
    [1] = '匕首', -- Tool.cs.14304
    [2] = '锤',   -- Tool.cs.14305
    [0] = '剑'    -- Tool.cs.14306 (默认)
}

local function getWeaponLevel(weapon, qualifiedItemId)
    local weaponPoints = 0.0

    -- 计算基础伤害点数
    local avgDamage = (weapon.MaxDamage + weapon.MinDamage) / 2
    local speedBonus = math.max(0, weapon.Speed) + (weapon.Type == 1 and 15 or 0)
    weaponPoints = weaponPoints + math.floor(avgDamage * (1.0 + 0.03 * speedBonus))

    -- 计算精度、防御和暴击点数
    local precisionDefenseBonus = weapon.Precision / 2 + weapon.Defense
    local critBonus = (weapon.CritChance - 0.02) * 200.0 + (weapon.CritMultiplier - 3.0) * 6.0
    weaponPoints = weaponPoints + math.floor(precisionDefenseBonus + critBonus)

    -- 根据武器ID添加特殊加成
    local bonus = weaponBonuses[qualifiedItemId]
    if bonus then
        weaponPoints = weaponPoints + bonus
    end

    -- 额外防御加成
    weaponPoints = weaponPoints + weapon.Defense * 2

    -- 返回最终等级
    return math.floor(weaponPoints / 7.0 + 1.0)
end

-- 获取武器类别信息
local function getWeaponCategory(id)
    local qualifiedItemId = id
    local cleanId = stripPrefix(id)
    local weapon = WeaponsData[cleanId]
    
    if not weapon then return nil end
    
    if string.find(weapon.Name, "Scythe") then
        return '镰刀'
    end
    
    if string.find(weapon.Name, "Slingshot") then
        return '弹弓'
    end
    
    -- 获取武器类型名称
    local typeName = weaponTypeNames[weapon.Type] or weaponTypeNames[0]
    local level = getWeaponLevel(weapon, qualifiedItemId)
    
    -- return level .. ' 级' .. typeName
    return typeName
end

-- 获取类别颜色
function getCategoryColor(id)
    -- 家具类别
    if getFurniture(id) then return '100,25,90' end
    
    -- 饰品类别
    if startsWith(id, "(TR)") then return '96,81,255' end
    
    -- 工具类别
    if startsWith(id, "(T)") or startsWith(id, "(W)") then return '47,79,79' end -- DarkSlateGray
    
    -- 打造品
    if startsWith(id, "(BC)") then return '0,0,0' end
    
    -- 古物类别
    local itemType = getTypeById(id)
    if itemType == 'Arch' then return '110,0,90' end
    
    -- 其他类别
    local category = getCategoryById(id)
    return categoryColors[category] or "0,0,0" -- Black
end

-- =p.getCategoryColor{args={'阿比盖尔的蝴蝶结','(H)AbigailsBow'}}
function p.getCategoryColor(frame)
	local name = frame.args[1]
    local id = frame.args[2]
    local id2 = ID.id {args = { name }}
    local result = getCategoryColor(id)
    if id ~= id2 then
	    local result2 = getCategoryColor(id2)
	    if result == result2 then return result end
		if result == '0,0,0' then return result2 else return result end
    end
	return result
end

-- 家具放置限制类型
local furnitureRestrictions = {
    [1] = "户外家具", -- Furniture_Outdoors
    [2] = "装饰"      -- Furniture_Decoration
}

-- 服装类前缀
local clothingPrefixes = {"(H)", "(S)", "(P)"}

-- 检查是否为服装类物品
local function isClothing(id)
    for _, prefix in ipairs(clothingPrefixes) do
        if startsWith(id, prefix) then
            return true
        end
    end
    return false
end

-- 获取类别名称
function getCategoryName(id)
    -- 家具类别
    local furniture = getFurniture(id)
    if furniture then
        local restriction = furniture.placementRestriction
        return furnitureRestrictions[restriction] or "家具" -- Object.cs.12847
    end
    
    -- 服装类别
    if isClothing(id) then
        return '服装' -- -100
    end
    
    -- 其他特殊前缀类别
    if startsWith(id, "(TR)") then return '饰品' end    -- Trinket
    if startsWith(id, "(T)") then return '工具' end     -- -99
    if startsWith(id, "(B)") then return '鞋类' end     -- -97
    -- 打造品
    if startsWith(id, "(BC)") then return '' end
    
    -- 武器类别
    if startsWith(id, "(W)") then
        local weaponCategory = getWeaponCategory(id)
        return (weaponCategory ~= '镰刀' and weaponCategory ~= '弹弓' and weaponCategory) or '工具'
    end
    
    -- 根据类型判断
    local itemType = getTypeById(id)
    if itemType == 'Arch' then return '古物' end -- Object.cs.12849
    if itemType == 'Ring' then return '戒指' end -- -96
    
    -- 根据类别ID返回名称
    local category = getCategoryById(id)
    return categoryNames[category] or ""
end

-- =p.getCategoryName{args={'阿比盖尔的蝴蝶结','(H)AbigailsBow'}}
function p.getCategoryName(frame)
	local name = frame.args[1]
    local id = frame.args[2]
    local id2 = ID.id {args = { name }}
    local result = getCategoryName(id)
    if id ~= id2 then
	    local result2 = getCategoryName(id2)
	    if result == result2 then return result end
	    if result == '' or result == nil then return result2 else return result end
    end
	return result
end

-- 调试函数(可以删除或保留用于测试)
function p.debug()
    local id = "(F)MoldyCouch"
    local furniture = getFurniture(id)
    
    if furniture then
        local restriction = furniture.placementRestriction
        return furnitureRestrictions[restriction] or "家具"
    end
    
    return nil
end

return p