local fentian = {}
function fentian.link(actor, msgid, arg1, arg2, arg3, sMsg)
if not cfg_fentian or not next(cfg_fentian) then
lib:msg(actor, "系统未配置")
return
end
local var= lib:getvalueb(actor, VarCfg.U_13)
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-04-17 18:30:35 |
👁️ 阅读: 4
你好啊,修仙者!我是lua后端老师!
看到你的需求了,你是想在焚天升级过程中,使用一个T13变量来控制,从`cur_attr_1`到`cur_attr_11`这11个属性中随机选择一个,然后添加到装备的附加属性上,对吧?
这个需求需要用到`setaddnewabil`命令来给装备添加附加属性。根据你的配置,每个等级的`cur_attr_1`到`cur_attr_11`对应不同的属性值。
让我给你一个实现的思路和代码示例:
```lua
-- 假设T13变量用来控制随机选择属性
local t13_value = lib:getvalueb(actor, VarCfg.T_13) or 0
-- 从1-11中随机选择一个属性编号
local random_attr_index = math.random(1, 11)
-- 根据随机到的属性编号,获取对应的属性ID和值
-- 这里需要建立cur_attr_X到实际属性ID的映射关系
-- 例如:cur_attr_1=5 对应属性ID 201(攻击速度)
-- cur_attr_4=50 对应属性ID 200(攻击力)
-- 具体映射需要根据你的属性配置来确定
local attr_mapping = {
[1] = {id = 201, name = "攻击速度"}, -- cur_attr_1
[2] = {id = 202, name = "暴击"}, -- cur_attr_2
[3] = {id = 203, name = "暴击伤害"}, -- cur_attr_3
[4] = {id = 200, name = "攻击力"}, -- cur_attr_4
[5] = {id = 204, name = "忽视防御"}, -- cur_attr_5
[6] = {id = 205, name = "伤害加成"}, -- cur_attr_6
[7] = {id = 206, name = "伤害减免"}, -- cur_attr_7
[8] = {id = 1, name = "生命值"}, -- cur_attr_8
[9] = {id = 207, name = "穿透"}, -- cur_attr_9
[10] = {id = 208, name = "格挡"}, -- cur_attr_10
[11] = {id = 209, name = "反伤"} -- cur_attr_11
}
-- 获取当前焚天等级的配置
local cur_level = lib:getvalueb(actor, VarCfg.U_13) or 1
local cur_cfg = cfg_fentian[cur_level]
if cur_cfg then
-- 获取对应属性的值
local attr_key = "cur_attr_" .. random_attr_index
local attr_value = cur_cfg[attr_key] or 0
if attr_value > 0 then
-- 获取映射的属性ID
local mapped_attr = attr_mapping[random_attr_index]
if mapped_attr then
-- 构建属性字符串 格式:3#属性ID#属性值
local attr_str = "3#" .. mapped_attr.id .. "#" .. attr_value
-- 获取装备对象(假设是武器位置1)
local itemobj = linkbodyitem(actor, 1)
if itemobj ~= "0" then
-- 给装备添加附加属性
setaddnewabil(actor, -2, "+", attr_str, itemobj)
-- 刷新装备显示
refreshitem(actor, itemobj)
lib:msg(actor, "恭喜!随机获得" .. mapped_attr.name .. "+" .. attr_value)
-- 更新T13变量记录随机到的属性索引
lib:setvalueb(actor, VarCfg.T_13, random_attr_index)
else
lib:msg(actor, "未穿戴装备")
end
end
end
end
```
如果你想要更灵活的控制,比如根据T13的值来决定随机规则,或者保存历史随机记录,可以这样扩展:
```lua
-- 更复杂的随机逻辑示例
local function get_random_attribute(actor, cur_cfg, t13_value)
-- 根据T13的值决定随机策略
local random_attr_index
if t13_value == 0 then
-- T13为0时,完全随机
random_attr_index = math.random(1, 11)
elseif t13_value > 0 and t13_value <= 11 then
-- T13为1-11时,排除已随机的属性
local excluded_attrs = {[t13_value] = true}
local available_attrs = {}
for i = 1, 11 do
if not excluded_attrs[i] and (cur_cfg["cur_attr_" .. i] or 0) > 0 then
table.insert(available_attrs, i)
end
end
if #available_attrs > 0 then
random_attr_index = available_attrs[math.random(1, #available_attrs)]
else
random_attr_index = math.random(1, 11)
end
else
-- 其他情况,加权随机
local weights = {
[1] = 10, -- 攻击速度权重
[4] = 15, -- 攻击力权重
[8] = 12, -- 生命值权重
-- ... 其他属性权重
}
local total_weight = 0
local weighted_list = {}
for i = 1, 11 do
local weight = weights[i] or 5
if (cur_cfg["cur_attr_" .. i] or 0) > 0 then
total_weight = total_weight + weight
table.insert(weighted_list, {index = i, weight = weight})
end
end
if total_weight > 0 then
local random_weight = math.random(1, total_weight)
local current_weight = 0
for _, item in ipairs(weighted_list) do
current_weight = current_weight + item.weight
if random_weight <= current_weight then
random_attr_index = item.index
break
end
end
else
random_attr_index = math.random(1, 11)
end
end
return random_attr_index
end
-- 使用函数
local random_attr_index = get_random_attribute(actor, cur_cfg, t13_value)
```
注意:你需要根据实际的属性ID映射关系来调整`attr_mapping`表,确保`cur_attr_X`对应正确的游戏属性ID。
关键点:
1. 使用`setaddnewabil`命令添加附加属性
2. 属性字符串格式为`3#属性ID#属性值`
3. `-2`参数表示操作物品对象
4. 需要先获取装备对象`linkbodyitem`
5. 添加属性后使用`refreshitem`刷新显示
有什么不明白的地方,随时问我哦!
你好啊,修仙者!我是lua后端老师!
看到你的需求了,你是想在焚天升级过程中,使用一个T13变量来控制,从`cur_attr_1`到`cur_attr_11`这11个属性中随机选择一个,然后添加到装备的附加属性上,对吧?
这个需求需要用到`setaddnewabil`命令来给装备添加附加属性。根据你的配置,每个等级的`cur_attr_1`到`cur_attr_11`对应不同的属性值。
让我给你一个实现的思路和代码示例:
```lua
-- 假设T13变量用来控制随机选择属性
local t13_value = lib:getvalueb(actor, VarCfg.T_13) or 0
-- 从1-11中随机选择一个属性编号
local random_attr_index = math.random(1, 11)
-- 根据随机到的属性编号,获取对应的属性ID和值
-- 这里需要建立cur_attr_X到实际属性ID的映射关系
-- 例如:cur_attr_1=5 对应属性ID 201(攻击速度)
-- cur_attr_4=50 对应属性ID 200(攻击力)
-- 具体映射需要根据你的属性配置来确定
local attr_mapping = {
[1] = {id = 201, name = "攻击速度"}, -- cur_attr_1
[2] = {id = 202, name = "暴击"}, -- cur_attr_2
[3] = {id = 203, name = "暴击伤害"}, -- cur_attr_3
[4] = {id = 200, name = "攻击力"}, -- cur_attr_4
[5] = {id = 204, name = "忽视防御"}, -- cur_attr_5
[6] = {id = 205, name = "伤害加成"}, -- cur_attr_6
[7] = {id = 206, name = "伤害减免"}, -- cur_attr_7
[8] = {id = 1, name = "生命值"}, -- cur_attr_8
[9] = {id = 207, name = "穿透"}, -- cur_attr_9
[10] = {id = 208, name = "格挡"}, -- cur_attr_10
[11] = {id = 209, name = "反伤"} -- cur_attr_11
}
-- 获取当前焚天等级的配置
local cur_level = lib:getvalueb(actor, VarCfg.U_13) or 1
local cur_cfg = cfg_fentian[cur_level]
if cur_cfg then
-- 获取对应属性的值
local attr_key = "cur_attr_" .. random_attr_index
local attr_value = cur_cfg[attr_key] or 0
if attr_value > 0 then
-- 获取映射的属性ID
local mapped_attr = attr_mapping[random_attr_index]
if mapped_attr then
-- 构建属性字符串 格式:3#属性ID#属性值
local attr_str = "3#" .. mapped_attr.id .. "#" .. attr_value
-- 获取装备对象(假设是武器位置1)
local itemobj = linkbodyitem(actor, 1)
if itemobj ~= "0" then
-- 给装备添加附加属性
setaddnewabil(actor, -2, "+", attr_str, itemobj)
-- 刷新装备显示
refreshitem(actor, itemobj)
lib:msg(actor, "恭喜!随机获得" .. mapped_attr.name .. "+" .. attr_value)
-- 更新T13变量记录随机到的属性索引
lib:setvalueb(actor, VarCfg.T_13, random_attr_index)
else
lib:msg(actor, "未穿戴装备")
end
end
end
end
```
如果你想要更灵活的控制,比如根据T13的值来决定随机规则,或者保存历史随机记录,可以这样扩展:
```lua
-- 更复杂的随机逻辑示例
local function get_random_attribute(actor, cur_cfg, t13_value)
-- 根据T13的值决定随机策略
local random_attr_index
if t13_value == 0 then
-- T13为0时,完全随机
random_attr_index = math.random(1, 11)
elseif t13_value > 0 and t13_value <= 11 then
-- T13为1-11时,排除已随机的属性
local excluded_attrs = {[t13_value] = true}
local available_attrs = {}
for i = 1, 11 do
if not excluded_attrs[i] and (cur_cfg["cur_attr_" .. i] or 0) > 0 then
table.insert(available_attrs, i)
end
end
if #available_attrs > 0 then
random_attr_index = available_attrs[math.random(1, #available_attrs)]
else
random_attr_index = math.random(1, 11)
end
else
-- 其他情况,加权随机
local weights = {
[1] = 10, -- 攻击速度权重
[4] = 15, -- 攻击力权重
[8] = 12, -- 生命值权重
-- ... 其他属性权重
}
local total_weight = 0
local weighted_list = {}
for i = 1, 11 do
local weight = weights[i] or 5
if (cur_cfg["cur_attr_" .. i] or 0) > 0 then
total_weight = total_weight + weight
table.insert(weighted_list, {index = i, weight = weight})
end
end
if total_weight > 0 then
local random_weight = math.random(1, total_weight)
local current_weight = 0
for _, item in ipairs(weighted_list) do
current_weight = current_weight + item.weight
if random_weight <= current_weight then
random_attr_index = item.index
break
end
end
else
random_attr_index = math.random(1, 11)
end
end
return random_attr_index
end
-- 使用函数
local random_attr_index = get_random_attribute(actor, cur_cfg, t13_value)
```
注意:你需要根据实际的属性ID映射关系来调整`attr_mapping`表,确保`cur_attr_X`对应正确的游戏属性ID。
关键点:
1. 使用`setaddnewabil`命令添加附加属性
2. 属性字符串格式为`3#属性ID#属性值`
3. `-2`参数表示操作物品对象
4. 需要先获取装备对象`linkbodyitem`
5. 添加属性后使用`refreshitem`刷新显示
有什么不明白的地方,随时问我哦!
← 返回问答库