我是lua后端老师!
小仙,看了你的洗练脚本,我发现了几个问题。让我帮你分析一下:
## 主要问题分析:
### 1. **锁定逻辑问题**
在你的`refined`函数中,虽然你检查了`lockStatus`,但是在处理属性时,`data.oldAttributes`在第一次洗练时是空的,所以即使复选框锁定,也会因为`data.oldAttributes[3]`为nil而重新生成属性。
### 2. **数据初始化问题**
从日志看,这是第53次洗练,但`data.oldAttributes`是空的,说明之前洗练的属性数据没有正确保存到全局表中。
### 3. **代码逻辑修正**
需要确保在每次洗练后,属性数据都正确保存到`data.oldAttributes`中,这样下次洗练时才能正确读取锁定属性。
## 修正建议:
```lua
-- 在refined函数开始处,确保获取到正确的洗练数据
function npc:refined(actor, param3, str)
local player_ID = getbaseinfo(actor, 2)
local itemObj = linkbodyitem(actor, param3)
local itemOnlyID = getiteminfo(actor, itemObj, 1)
release_print("====== 开始洗练 ======")
release_print("玩家ID: "..player_ID..", 装备位置: "..param3..", 装备唯一ID: "..itemOnlyID)
-- 从装备变量中读取洗练次数
local p3 = param3 + 1
local oldTimes = getitemparam(actor, -2, p3, itemObj)
local oldTimes = tonumber(oldTimes) or 0
-- 获取或创建洗练数据
local data = npc:getRefineData(actor, param3)
if not data then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>★★★ 获取装备数据失败!★★★</font>","Type":9}')
return
end
-- 如果装备变量中有洗练次数,同步到data中
if oldTimes > 0 and data.refineCount == 0 then
data.refineCount = oldTimes
end
-- 解析锁定状态
local lockStatus = json2tbl(str) or {}
data.lockCount = 0
data.lockedAttrs = {}
for i = 1, 3 do
if lockStatus[i] == true then
data.lockedAttrs[i] = {
locked = true,
index = i
}
data.lockCount = data.lockCount + 1
else
data.lockedAttrs[i] = {
locked = false,
index = i
}
end
end
-- 确保oldAttributes有值(从上次洗练结果中获取)
if not data.oldAttributes or #data.oldAttributes == 0 then
-- 如果是第一次洗练,初始化空属性
data.oldAttributes = {}
for i = 1, 3 do
data.oldAttributes[i] = nil
end
end
-- 计算费用
local baseIncrementCost = 100 + (data.refineCount * 100)
local lockExtraCost = 0
if data.lockCount == 1 then
lockExtraCost = 100 + (data.refineCount * 100)
elseif data.lockCount == 2 then
lockExtraCost = 300 + (data.refineCount * 100)
end
local totalCost = baseIncrementCost + lockExtraCost
-- 检查货币
local hasMoney = getbindmoney(actor, "元宝")
if hasMoney >= totalCost then
consumebindmoney(actor, "元宝", totalCost, "洗练扣除")
-- 更新洗练次数
local newTimes = oldTimes + 1
data.refineCount = newTimes
setitemparam(actor, -2, p3, newTimes, itemObj)
-- 清除旧属性
if data.oldAttrStr and data.oldAttrStr ~= "" then
setaddnewabil(actor, -2, "-", data.oldAttrStr, itemObj)
end
-- 生成新属性(根据锁定状态)
local newAttributes = {}
local attrStrs = {}
for i = 1, 3 do
if lockStatus[i] == true and data.oldAttributes[i] then
-- 锁定状态,使用旧属性
newAttributes[i] = data.oldAttributes[i]
-- 根据属性类型生成属性字符串
if newAttributes[i].type == 1 then
table.insert(attrStrs, "3#1#"..newAttributes[i].hp_value)
elseif newAttributes[i].type == 2 then
table.insert(attrStrs, "3#3#"..newAttributes[i].att_min.."|3#4#"..newAttributes[i].att_max)
elseif newAttributes[i].type == 3 then
table.insert(attrStrs, "3#9#"..newAttributes[i].ac_min.."|3#10#"..newAttributes[i].ac_max)
end
release_print("属性"..i.."被锁定,使用旧值")
else
-- 未锁定,生成新属性
if i == 1 then
-- 血量属性
local hp_value = math.random(100, 500)
local hp_color = "#FFFFFF"
if hp_value >= 100 and hp_value <= 199 then
hp_color = "#FFFFFF"
elseif hp_value >= 200 and hp_value <= 299 then
hp_color = "#00FF00"
elseif hp_value >= 300 and hp_value <= 500 then
hp_color = "#FF0000"
end
newAttributes[i] = {
type = 1,
name = "血量",
hp_value = hp_value,
color = hp_color,
index = i
}
table.insert(attrStrs, "3#1#"..hp_value)
release_print("血量未锁定,生成新值: "..hp_value)
elseif i == 2 then
-- 攻击属性
local min_attack = math.random(0, 10)
local max_attack = math.random(5, 20)
newAttributes[i] = {
type = 2,
name = "攻击力",
att_min = min_attack,
att_max = max_attack,
index = i
}
table.insert(attrStrs, "3#3#"..min_attack.."|3#4#"..max_attack)
release_print("攻击未锁定,生成新值: "..min_attack.."-"..max_attack)
elseif i == 3 then
-- 防御属性
local min_defense = math.random(1, 10)
local max_defense = math.random(5, 20)
newAttributes[i] = {
type = 3,
name = "防御力",
ac_min = min_defense,
ac_max = max_defense,
index = i
}
table.insert(attrStrs, "3#9#"..min_defense.."|3#10#"..max_defense)
release_print("防御未锁定,生成新值: "..min_defense.."-"..max_defense)
end
end
end
-- 保存新属性到data中
data.oldAttributes = newAttributes
-- 组合属性字符串
data.oldAttrStr = table.concat(attrStrs, "|")
-- 添加新属性到装备
for _, attrStr in ipairs(attrStrs) do
setaddnewabil(actor, -2, "+", attrStr, itemObj)
end
refreshitem(actor, itemObj)
-- 保存到全局表
if not npc.RefineData[player_ID] then
npc.RefineData[player_ID] = {}
end
npc.RefineData[player_ID][itemOnlyID] = data
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>★★★ 洗练成功!★★★</font>","Type":9}')
-- 发送洗练数据到前端
local refinedData = tbl2json(npc.RefineData[player_ID][itemOnlyID])
sendluamsg(actor, 1013, 13, 2, 0, refinedData)
else
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>★★★ 你兜里也妹有米er啊!★★★</font>","Type":9}')
return
end
release_print("====== 洗练结束 ======")
end
```
## 关键修改点:
1. **确保`data.oldAttributes`有值**:在洗练前检查并初始化
2. **正确判断锁定状态**:只有当`lockStatus[i] == true`且`data.oldAttributes[i]`存在时才使用旧属性
3. **属性生成逻辑分离**:将每个属性的生成逻辑分开处理
4. **数据持久化**:确保每次洗练后属性数据都保存到全局表
这样修改后,锁定功能应该能正常工作了。如果还有问题,记得检查QF触发文件,确保洗练相关的接口都正确配置了。