我是lua后端老师!
小仙,我看你这段代码确实存在几个问题,导致属性值可能超过5%:
1. **`additemattrEX`函数使用错误**:这个函数在996引擎中并不存在,你应该使用`additemattr`来添加属性。
2. **属性值累加逻辑问题**:你每次循环都在`newAttr`表中添加新属性,但`newAttr`表在循环外定义,每次循环都会覆盖之前的值,而且你每次都在`additemattrEX`中传入整个`newAttr`表,这会导致属性重复添加。
3. **缺少属性值检查**:你直接随机1-5的值,但没有检查当前属性值是否已经存在,也没有限制最大值。
让我帮你修正一下洗炼逻辑:
```lua
function YuanSuDaShi:XiLian(actor, data)
local cfg = self._cfg
local cost = cfg[1].cost
if not data then
Msg.richMsg(actor, "请先选择要洗练的装备!")
return
end
local equip = data.equip
local where = data.where
local equipData = data.equipData
if not equipData or equipData == "" then
Msg.richMsg(actor,"该部位未穿戴装备!")
return
end
-- 消耗检查
if not GMLib:checkItems(actor, cost, 0, 1) then
return
end
-- 扣除材料
if not GMLib:takes(actor, cost, "洗练") then
return
end
-- 获取装备的makeindex
local itemmakeid = getitembymakeindex(actor, where) -- 修正:使用正确的函数获取装备唯一ID
if not itemmakeid or itemmakeid == 0 then
Msg.richMsg(actor, "未找到该部位的装备!")
return
end
-- 获取当前装备的属性
local currentAttrStr = getitemattr(actor, itemmakeid, 9) -- 获取第9类属性(自定义属性)
local currentAttrs = {}
-- 解析当前属性
if currentAttrStr and currentAttrStr ~= "" then
for attr in string.gmatch(currentAttrStr, "([^|]+)") do
local parts = {}
for part in string.gmatch(attr, "([^#]+)") do
table.insert(parts, part)
end
if #parts >= 3 then
local attrID = tonumber(parts[2]) or 0
local attrValue = tonumber(parts[3]) or 0
currentAttrs[attrID] = attrValue
end
end
end
-- 获取可用的属性配置
local attriCfg = {}
for _, row in pairs(cfg) do
if row.Attri and tonumber(row.Attri) > 0 then
table.insert(attriCfg, tonumber(row.Attri))
end
end
if #attriCfg == 0 then
Msg.msg9(actor,"未配置属性!",249)
return
end
-- 随机1-7条属性
local addCount = math.random(1,7)
local newAttrs = {}
for i = 1, addCount do
local attrID = attriCfg[math.random(1, #attriCfg)]
local currentValue = currentAttrs[attrID] or 0
-- 如果当前值已经达到5%,则跳过
if currentValue >= 5 then
goto continue
end
-- 随机增加1-5%,但不超过5%
local addValue = math.random(1, 5)
local newValue = currentValue + addValue
if newValue > 5 then
newValue = 5
addValue = 5 - currentValue
end
-- 更新属性表
newAttrs[attrID] = newValue
-- 删除旧属性
delitemattr(actor, itemmakeid, 9, attrID)
-- 添加新属性(注意:属性值需要是字符串)
local attrStr = string.format("9#%d#%d", attrID, newValue)
additemattr(actor, itemmakeid, attrStr) -- 使用正确的函数添加属性
::continue::
end
-- 刷新装备显示
refreshitem(actor, itemmakeid)
-- 发送洗炼成功消息
Msg.richMsg(actor, "洗炼成功!")
end
```
主要修正点:
1. **使用正确的函数**:`getitembymakeindex`获取装备ID,`additemattr`添加属性
2. **属性值限制**:每次增加前检查当前值,确保不超过5%
3. **属性累加控制**:使用`newValue = currentValue + addValue`并限制最大值
4. **跳过已满属性**:使用`goto`语句跳过已经达到5%的属性
这样修改后,属性值就不会超过5%了。记得不懂的可以问你浮生梦尊师哦!