| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
- EquipRandom = {}
- EquipRandom.Config = {
- -- 必须添加的属性ID列表
- FORCE_ATTR_IDS = {305012, -- 幸运一击伤害加成
- 900 -- 幸运(灵魂宝石成功率)
- },
- -- 属性值配置(可以根据需要调整)
- ATTR_VALUES = {
- [305012] = 500, -- 幸运一击伤害加成 +5%
- [900] = 2500 -- 幸运(灵魂宝石成功率)+25%
- },
- -- 是否启用强制属性添加
- ENABLED = true,
- -- 调试模式
- DEBUG_MODE = true,
- -- 装备类型过滤(只对装备类型为2的物品生效)
- EQUIP_TYPE = 2,
- -- 排除的物品ID列表(不需要添加属性的装备)
- EXCLUDE_ITEMS = {
- -- 可以在这里添加不需要添加属性的装备ID
- }
- }
- function EquipRandom.init(actor)
- end
- -- 检查装备是否已有指定属性
- -- @itemData 装备的属性
- -- @attrId 属性ID
- function EquipRandom.hasAttribute(itemData, attrId)
- -- 检查基础属性
- if itemData.basicattr then
- for _, attr in pairs(itemData.basicattr) do
- if tonumber(attr.attrid) == tonumber(attrId) then
- return true, attr.value
- end
- end
- end
- -- 检查词条属性
- if itemData.entries then
- for _, entry in pairs(itemData.entries) do
- if tonumber(entry.attrid) == tonumber(attrId) then
- return true, entry.value
- end
- end
- end
- -- 检查幸运属性
- if itemData.luckyattr then
- for _, attr in pairs(itemData.luckyattr) do
- if tonumber(attr.attrid) == tonumber(attrId) then
- return true, attr.value
- end
- end
- end
- -- 检查魔法属性
- if itemData.magicattr then
- for _, attr in pairs(itemData.magicattr) do
- if tonumber(attr.attrid) == tonumber(attrId) then
- return true, attr.value
- end
- end
- end
- return false, 0
- end
- -- 检测装备是否为卓越装备
- function EquipRandom.isExcellentEquip(itemData)
- if not itemData then
- return false, 0, "无装备数据"
- end
- -- 检查是否有词条属性
- if not itemData.entries or table.isNullOrEmpty(itemData.entries) then
- return false, 0, "无词条属性"
- end
- -- 计算词条数量
- local entryCount = 0
- for _ in pairs(itemData.entries) do
- entryCount = entryCount + 1
- end
- -- 判断是否为卓越装备(通常2个或以上词条为卓越)
- local isExcellent = entryCount >= 1
- -- 卓越等级
- local grade = ""
- if entryCount >= 4 then
- grade = "卓越4星"
- elseif entryCount >= 3 then
- grade = "卓越3星"
- elseif entryCount >= 2 then
- grade = "卓越2星"
- elseif entryCount >= 1 then
- grade = "卓越1星"
- else
- grade = "普通装备"
- end
- return isExcellent, entryCount, grade
- end
- -- 检测装备是否已有幸运属性
- function EquipRandom.hasLuckyAttr(itemQuip)
- -- 检查是否已经添加过强制属性
- for _, attrId in ipairs(EquipRandom.Config.FORCE_ATTR_IDS) do
- local hasAttr, attrValue = EquipRandom.hasAttribute(itemQuip, attrId)
- if hasAttr then
- return true
- end
- end
- return false
- end
- -- 直接给装备添加词条属性的方法
- -- @param actor 玩家对象
- -- @param itemId 装备ID
- -- @param entryId 词条ID(例如:攻击力、防御力等)
- -- @param entryValue 词条数值
- function EquipRandom.AddEntryToEquip(actor, itemId, entryId, entryValue)
- -- 检查装备是否存在
- local equipInfo = getequipinfo(actor, itemId, 1)
- if not equipInfo or not equipInfo.id then
- info(actor, "装备不存在!")
- return
- end
- info(string.format("AddEntryToEquip 添加词条 %s %s %s 值:%s", actor, itemId, entryId, entryValue))
- -- 检查词条ID和数值是否合法
- if not entryId or not entryValue then
- info(actor, "词条ID或数值无效!")
- return
- end
- -- 获取当前装备的词条列表
- local currentEntries = API.GetItemData(actor, itemId, "entries") or {}
- -- 检查词条是否已存在
- local entryExists = false
- for i, entry in ipairs(currentEntries) do
- if entry.id == entryId then
- -- 更新已有词条的数值
- currentEntries[i].value = entryValue
- entryExists = true
- break
- end
- end
- -- 使用API添加词条
- local newEntry = {
- attrid = entryId,
- grade = 0,
- value = entryValue,
- entryid = 1
- }
- -- 如果词条不存在,则添加新词条
- if not entryExists then
- table.insert(currentEntries, newEntry)
- end
- info('更新后的词条列表', currentEntries)
- -- 保存更新后的词条列表
- API.SetItemData(actor, itemId, "entries", currentEntries)
- -- 刷新装备属性(如果需要)
- Equip_QiangHua.sendAttr(actor, itemId)
- -- 推送红点通知
- sendluamsg(actor, LuaNetMsg.Equip_DuanZao_MsgID, {})
- -- 提示玩家操作成功
- info(actor, "装备词条已更新!")
- end
- -- 获取幸运的几率和属性
- function EquipRandom.getLuckvalue(actor, itemCfg, itemQuip, itemId)
- -- 检测装备是否已有幸运属性
- if EquipRandom.hasLuckyAttr(itemQuip) then
- info("getLuckvalue 装备已有幸运属性,跳过添加")
- return
- end
- local luckrandom = itemCfg.luckrandom
- if luckrandom == nil or luckrandom == "" then
- info("cfg_equip_entryLib getLuckvalue 装备没有幸运属性配置")
- return nil
- end
- local luckrandomSplit = string.split(luckrandom, "#")
- local randomNum = tonumber(luckrandomSplit[1]) or 0
- local randomValue = math.random(1, 10000)
- info(string.format("getLuckvalue 幸运属性配置 几率:%d", randomNum))
- if randomValue > randomNum then
- info("getLuckvalue 未触发幸运属性", randomValue, randomNum)
- return
- end
- EquipRandom.setLuck2(actor, itemQuip)
- -- local addedAttrs = {}
- -- local entryCount = 0
- -- for _, attrId in ipairs(EquipRandom.Config.FORCE_ATTR_IDS) do
- -- -- 修正:从配置中获取属性值,如果没有则使用默认值
- -- local attrValue = EquipRandom.Config.ATTR_VALUES[attrId]
- -- if attrValue then
- -- entryCount = entryCount + 1
- -- -- 修正:如果需要从 luckrandomSplit 中获取值,应该使用正确的索引
- -- -- luckrandomSplit[1] 是几率,luckrandomSplit[2] 是第一个属性值,luckrandomSplit[3] 是第二个属性值
- -- if #luckrandomSplit > entryCount then
- -- local configValue = tonumber(luckrandomSplit[entryCount + 1])
- -- if configValue then
- -- attrValue = configValue
- -- end
- -- end
- -- info(string.format("getLuckvalue 幸运属性配置 值:%d", attrValue))
- -- -- 使用API添加词条
- -- local newEntry = {
- -- attrid = attrId,
- -- grade = 0,
- -- value = attrValue,
- -- entryid = entryCount
- -- }
- -- addequipentry(actor, itemId, newEntry)
- -- table.insert(addedAttrs, attrId .. "=" .. attrValue)
- -- if EquipRandom.Config.DEBUG_MODE then
- -- info("EquipRandom: 为装备 " .. itemId .. " 添加词条: " .. attrId .. "=" .. attrValue)
- -- end
- -- end
- -- end
- -- info("getLuckvalue 添加幸运属性完成", itemQuip)
- -- jprint(itemQuip)
- end
- function EquipRandom.setLuck(actor, itemQuip, itemCfg)
- -- 检测装备是否已有幸运属性
- if EquipRandom.hasLuckyAttr(itemQuip) then
- return
- end
- local luckrandom = itemCfg.luckrandom
- if luckrandom == nil or luckrandom == "" then
- info("cfg_equip_entryLib getLuckvalue 装备没有幸运属性配置")
- return nil
- end
- local luckrandomSplit = string.split(luckrandom, "#")
- local randomNum = tonumber(luckrandomSplit[1]) or 0
- local addedAttrs = {}
- local entryCount = 0
- for _, attrId in ipairs(EquipRandom.Config.FORCE_ATTR_IDS) do
- -- 修正:从配置中获取属性值,如果没有则使用默认值
- local attrValue = EquipRandom.Config.ATTR_VALUES[attrId]
- if attrValue then
- entryCount = entryCount + 1
- -- 修正:如果需要从 luckrandomSplit 中获取值,应该使用正确的索引
- -- luckrandomSplit[1] 是几率,luckrandomSplit[2] 是第一个属性值,luckrandomSplit[3] 是第二个属性值
- if #luckrandomSplit > entryCount then
- local configValue = tonumber(luckrandomSplit[entryCount + 1])
- if configValue then
- attrValue = configValue
- end
- end
- -- 使用API添加词条
- local newEntry = {
- attrid = attrId,
- grade = 0,
- value = attrValue,
- entryid = entryCount
- }
- addequipentry(actor, itemQuip.id, newEntry)
- table.insert(addedAttrs, attrId .. "=" .. attrValue)
- end
- end
- end
- function EquipRandom.isAddLuck(luckrandom)
- if luckrandom == nil or luckrandom == "" then
- return false
- end
- local luckrandomSplit = string.split(luckrandom, "#")
- local randomNum = tonumber(luckrandomSplit[1]) or 0
- local randomValue = math.random(1, 10000)
- if randomValue > randomNum then
- return false
- end
- return true
- end
- -- 直接增加装备强化等级的方法
- -- @param actor 玩家对象
- -- @param itemId 装备ID
- -- @param times 需要增加的强化次数
- function EquipRandom.DirectIncreaseStrengthLevel(actor, itemId, times)
- -- 获取当前强化等级
- local currentLevel = API.GetItemData(actor, itemId, "strengthlv") or 0
- -- 检查装备是否存在
- local equipInfo = getequipinfo(actor, itemId, 1)
- if not equipInfo or not equipInfo.id then
- info(actor, "装备不存在!", itemId)
- return
- end
- -- 检查强化次数是否合法
- if times <= 0 then
- info(actor, "强化次数必须大于0!")
- return
- end
- -- 计算新的强化等级
- local newLevel = currentLevel + times
- -- 检查强化等级是否超过上限(假设最高强化等级为20)
- local maxLevel = 20
- if newLevel > maxLevel then
- info(actor, "强化等级已超过上限(" .. maxLevel .. "级)!")
- newLevel = maxLevel
- end
- -- 直接设置新的强化等级
- API.SetItemData(actor, itemId, "strengthlv", newLevel)
- -- -- 刷新装备属性
- -- Equip_QiangHua.sendAttr(actor, itemId)
- -- 重新计算强化后的穿戴需求
- -- 刷新流光效果
- API.refreshLiuGuang(actor, itemId)
- -- 通知客户端更新(这是关键!)
- local cfgId = equipInfo.cfgid
- Equip_QiangHua.RefreshLevel(actor, itemId, cfgId)
- -- -- 推送红点通知
- -- info(actor, LuaNetMsg.Equip_DuanZao_MsgID, {})
- -- -- 提示玩家强化成功
- -- info(actor, "装备强化等级已增加至 " .. newLevel .. " 级!")
- end
- -- 处理强化等级随机
- function EquipRandom.processStrengthLevelRandom(actor, itemId, strengthConfig)
- local configs = string.split(strengthConfig, "|")
- -- info('处理强化等级随机', configs)
- for _, config in ipairs(configs) do
- local parts = string.split(config, "#")
- if #parts >= 2 then
- local level = tonumber(parts[1]) or 0
- local chance = tonumber(parts[2]) or 0
- local randomValue = math.random(1, 10000)
- if randomValue <= chance then
- -- info("触发强化等级随机", "等级:", level, "几率:", chance, "随机值:", randomValue)
- EquipRandom.DirectIncreaseStrengthLevel(actor, itemId, level)
- break -- 只触发一次
- end
- end
- end
- end
- function EquipRandom.getStrengthLevelRandom(strengthConfig)
- if strengthConfig == nil or strengthConfig == "" then
- return 0
- end
- local configs = string.split(strengthConfig, "|")
- for _, config in ipairs(configs) do
- local parts = string.split(config, "#")
- if #parts >= 2 then
- local level = tonumber(parts[1]) or 0
- local chance = tonumber(parts[2]) or 0
- local randomValue = math.random(1, 10000)
- if randomValue <= chance then
- return level
- end
- end
- end
- return 0
- end
- -- 处理追加等级随机
- function EquipRandom.processAppendLevelRandom(actor, itemId, appendConfig)
- local configs = string.split(appendConfig, "|")
- info('处理追加等级随机', configs)
- for _, config in ipairs(configs) do
- local parts = string.split(config, "#")
- if #parts >= 2 then
- local level = tonumber(parts[1]) or 0
- local chance = tonumber(parts[2]) or 0
- local randomValue = math.random(1, 10000)
- if randomValue <= chance then
- info("触发追加等级随机", "等级:", level, "几率:", chance, "随机值:", randomValue)
- EquipRandom.DirectIncreaseAppendLevel(actor, itemId, level)
- break -- 只触发一次
- end
- end
- end
- end
- function EquipRandom.getAppendLevelRandom(appendConfig)
- if appendConfig == nil or appendConfig == "" then
- return 0
- end
- local configs = string.split(appendConfig, "|")
- for _, config in ipairs(configs) do
- local parts = string.split(config, "#")
- if #parts >= 2 then
- local level = tonumber(parts[1]) or 0
- local chance = tonumber(parts[2]) or 0
- local randomValue = math.random(1, 10000)
- if randomValue <= chance then
- return level
- end
- end
- end
- return 0
- end
- -- 直接增加装备追加等级的方法
- -- @param actor 玩家对象
- -- @param itemId 装备ID
- -- @param appendTimes 需要增加的追加次数
- function EquipRandom.DirectIncreaseAppendLevel(actor, itemId, appendTimes)
- -- 获取当前追加等级
- local currentAppendLevel = API.GetItemData(actor, itemId, "appendlv") or 0
- -- 检查装备是否存在
- local equipInfo = getequipinfo(actor, itemId, 1)
- if not equipInfo or not equipInfo.id then
- info(actor, "装备不存在!")
- return
- end
- -- 检查追加次数是否合法
- if appendTimes <= 0 then
- info(actor, "追加次数必须大于0!")
- return
- end
- -- 计算新的追加等级
- local newAppendLevel = currentAppendLevel + appendTimes
- -- 检查追加等级是否超过上限(假设最高追加等级为10)
- local maxAppendLevel = 10
- if newAppendLevel > maxAppendLevel then
- info(actor, "追加等级已超过上限(" .. maxAppendLevel .. "级)!")
- newAppendLevel = maxAppendLevel
- end
- -- 直接设置新的追加等级
- API.SetItemData(actor, itemId, "appendlv", newAppendLevel)
- -- -- 刷新装备属性(如果需要)
- -- Equip_QiangHua.sendAttr(actor, itemId)
- -- -- 推送红点通知
- -- sendluamsg(actor, LuaNetMsg.Equip_DuanZao_MsgID, {})
- -- -- 提示玩家追加成功
- -- info(actor, "装备追加等级已增加至 " .. newAppendLevel .. " 级!")
- end
- function try(func, catch)
- local ok, err = pcall(func)
- if not ok and catch then
- catch(err)
- end
- end
- function EquipRandom.resetEquipSkill(actor, itemQuip)
- if itemQuip.skill ~= nil and table.count(itemQuip.skill) > 0 then
- return
- end
- if itemQuip.luaextdata ~= nil and itemQuip.luaextdata.skillinfo ~= nil then
- for i, v in ipairs(itemQuip.luaextdata.skillinfo) do
- setequipskillbyid(actor, itemQuip.id, v.skillid, v.skilllevel)
- end
- end
- end
- function EquipRandom.newItmToBag(actor, itemId, itemCfgId)
- local type = ConfigDataManager.getTableValue("cfg_item", "type", "id", itemCfgId)
- if tonumber(type) ~= ItemType.EQUIP then
- return
- end
- local itemQuip = getequipinfo(actor, itemId, 1)
- if not itemQuip then
- return
- end
- -- 重新生成技能
- EquipRandom.resetEquipSkill(actor, itemQuip)
- -- local strengthlv = API.GetItemData(actor, itemId, "strengthlv") or 0
- if itemQuip.luaextdata ~= nil and itemQuip.luaextdata.strengthlv ~= nil and itemQuip.luaextdata.strengthlv > 0 then
- API.refreshLiuGuang(actor, itemId)
- end
-
- local isNew = API.GetItemData(actor, itemId, "isaddattr") or 0
- if isNew ~= nil and isNew == 1 then
- return
- end
- API.SetItemData(actor, itemId, "isaddattr", 1)
- -- -- 获取装备配置信息
- local itemCfg = ConfigDataManager.getTable("cfg_equip_entryLib", "id", itemCfgId)
- if itemCfg == nil then
- return
- end
- if #itemCfg > 0 then
- itemCfg = itemCfg[1]
- end
- -- 处理强化等级随机
- if itemCfg.strengthlevelrandom and itemCfg.strengthlevelrandom ~= "" and
- (itemQuip.luaextdata == nil or itemQuip.luaextdata.strengthlv == nil) then
- EquipRandom.processStrengthLevelRandom(actor, itemId, itemCfg.strengthlevelrandom)
- end
- -- 处理追加等级随机
- if itemCfg.appendlevelrandom and itemCfg.appendlevelrandom ~= "" and
- (itemQuip.luaextdata == nil or itemQuip.luaextdata.appendlv == nil) then
- EquipRandom.processAppendLevelRandom(actor, itemId, itemCfg.appendlevelrandom)
- end
- -- 处理随机属性问题
- if (itemQuip.luaextdata ~= nil and itemQuip.luaextdata.entryaddnum ~= nil and itemQuip.luaextdata.entryaddnum ~= 0) then
- local cfgEquip = ConfigDataManager.getById("cfg_equip_entryLib", itemQuip.cfgid)
- if cfgEquip.att1 ~= nil and cfgEquip.att1 ~= "" then
- local cfgEquipAtts = ConfigDataManager.getTable("cfg_equip_att")
- local tmpIndex = 0
- local tmpentrys = API.TableDeepcopy(itemQuip.entries)
- local entrys = {}
- for i, v in ipairs(tmpentrys) do
- table.insert(entrys, {
- value = v.value,
- entryid = tostring(v.entryid),
- attrid = tostring(v.attrid)
- })
- end
- local count = 200
- for i = 1, count do
- if tmpIndex >= itemQuip.luaextdata.entryaddnum then
- break
- end
- local addEntry = EquipRefined.equipEntryRandomAttrOne(itemQuip, cfgEquip, cfgEquipAtts)
- if addEntry ~= nil then
- local hasAttr, v = table.findByCondi(entrys, function(a)
- return a.attrid == addEntry.attrid;
- end)
- if hasAttr == nil then
- tmpIndex = tmpIndex + 1
- table.insert(entrys, addEntry)
- end
- end
- end
- local result = resetequipentry(actor, itemQuip.id, entrys)
- if not result then
- info("随机生成词条失败")
- end
- API.SetItemData(actor, itemId, "entryaddnum", 0)
- end
- end
- if itemQuip.luaextdata ~= nil and itemQuip.luaextdata.addLuck ~= nil then
- if itemQuip.luaextdata.addLuck == 1 then
- EquipRandom.setLuck2(actor, itemQuip)
- end
- else
- EquipRandom.getLuckvalue(actor, itemCfg, itemQuip, itemId)
- end
- API.SetItemData(actor, itemId, "form", itemQuip.fromnewest)
- end
- function EquipRandom.setLuck2(actor, itemQuip)
- -- 检测装备是否已有幸运属性
- if EquipRandom.hasLuckyAttr(itemQuip) then
- return
- end
- local tmpentrys = API.TableDeepcopy(itemQuip.entries)
- local entrys = {}
- for i, v in ipairs(tmpentrys) do
- table.insert(entrys, {
- value = v.value,
- entryid = tostring(v.entryid),
- attrid = tostring(v.attrid)
- })
- end
- local entryCount = table.count(entrys)
- for _, attrId in ipairs(EquipRandom.Config.FORCE_ATTR_IDS) do
- -- 修正:从配置中获取属性值,如果没有则使用默认值
- local attrValue = EquipRandom.Config.ATTR_VALUES[attrId]
- if attrValue then
- entryCount = entryCount + 1
- -- 使用API添加词条
- local newEntry = {
- attrid = tostring(attrId),
- grade = 0,
- value = attrValue,
- entryid = tostring(entryCount)
- }
- table.insert(entrys, newEntry)
- end
- end
- local result = resetequipentry(actor, itemQuip.id, entrys)
- if not result then
- info("随机生成词条失败")
- end
- end
|