-- server.lua
local luosha_server = {}
-- 装备定义
local EQUIPMENTS = {
{id = 51433, name = "【禁忌ミ魂器】·罗刹骨", prob = 50, desc = "略有聪慧,但威力不俗。"},
{id = 51434, name = "【神级ミ魂器】·罗刹骨", prob = 30, desc = "锻造精良,蕴含修罗战意。"},
{id = 51435, name = "【至尊ミ魂器】·罗
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-26 18:35:25 |
👁️ 阅读: 14
我是Lua后端老师!小仙,让我看看你的代码问题所在。
从你的代码来看,问题出在随机数生成和概率计算的逻辑上。根据你的代码,我发现了几个可能的问题点:
1. **随机数生成范围问题**:`math.random(1, totalProb)` 这个范围是 1-100(因为总概率是50+30+15+5=100),这是正确的。
2. **概率累计逻辑问题**:你的累计概率计算逻辑是正确的。
3. **关键问题可能在这里**:`lib:getvalueb(actor, "U56")` 这个变量在提交材料时设置为1,但是在随机装备生成时没有重置为0,导致每次都会进入随机装备生成逻辑。
让我帮你修改一下代码,在成功获得装备后将投喂次数重置:
```lua
-- server.lua
local luosha_server = {}
-- 装备定义
local EQUIPMENTS = {
{id = 51433, name = "【禁忌ミ魂器】·罗刹骨", prob = 50, desc = "略有聪慧,但威力不俗。"},
{id = 51434, name = "【神级ミ魂器】·罗刹骨", prob = 30, desc = "锻造精良,蕴含修罗战意。"},
{id = 51435, name = "【至尊ミ魂器】·罗刹骨", prob = 15, desc = "光华内敛,乃不可多得的精品。"},
{id = 51436, name = "【八荒ミ魂器】·罗刹骨", prob = 5, desc = "巧夺天工,已生出一丝武神之力!"}
}
-- 计算总概率
local totalProb = 0
for _, equip in ipairs(EQUIPMENTS) do
totalProb = totalProb + equip.prob
end
-- 后端主处理函数
function luosha_server.link(actor, msgid, arg1, arg2, arg3, sMsg)
local rlv = lib:getvalueb(actor, "U56") or 0
release_print("当前(投喂次数):", rlv)
if arg2 == 0 then
if rlv >= 1 then
lib:msg(actor, "你已经提交过了")
return
end
local needItemCount = 1
local itemName1 = "修罗骸骨"
local itemName2 = "百炼黑钢"
local currentItemCount1 = getbagitemcount(actor, itemName1)
local currentItemCount2 = getbagitemcount(actor, itemName2)
if currentItemCount1 < needItemCount then
lib:msg(actor, "你还没有" .. itemName1, 1)
return
end
if currentItemCount2 < needItemCount then
lib:msg(actor, "你还没有" .. itemName2, 1)
return
end
-- 材料都足够,开始扣除
takeitem(actor, itemName1, needItemCount)
takeitem(actor, itemName2, needItemCount)
lib:msg(actor, "<font color='#00ff00'>材料齐了?一边待着去,看老娘给你露一手!......成!拿去吧,是块好料还是废铁,就看你的命了。</font>", 1)
-- 增加投喂次数
lib:setvalueb(actor, "U56", 1)
elseif arg2 == 1 then
if rlv < 1 then
lib:msg(actor, "你还没有提交相应物品,无法进行打造。", 1)
return
end
-- 重置随机数种子
math.randomseed(tostring(os.time()):reverse():sub(1, 7))
local randomNum = math.random(1, totalProb)
release_print("随机数:", randomNum, "总概率:", totalProb)
local selectedEquip = nil
local cumulativeProb = 0
for _, equip in ipairs(EQUIPMENTS) do
cumulativeProb = cumulativeProb + equip.prob
release_print("装备:", equip.name, "概率:", equip.prob, "累计概率:", cumulativeProb)
if randomNum <= cumulativeProb then
selectedEquip = equip
break
end
end
if selectedEquip then
-- 将随机装备给予玩家
giveitem(actor, selectedEquip.name, 1)
-- 发送消息给前端展示
local msg = {
action = "show_random_equip",
equipId = selectedEquip.id,
equipName = selectedEquip.name,
equipDesc = selectedEquip.desc
}
local jsonstr = tbl2json(msg)
sendluamsg(58003, 0, 0, 0, jsonstr)
lib:msg(actor, "恭喜你获得了:" .. selectedEquip.name, 1)
-- 重要:获得装备后重置投喂次数
lib:setvalueb(actor, "U56", 0)
else
lib:msg(actor, "随机生成装备失败,请重试。", 1)
end
end
end
return luosha_server
```
主要修改点:
1. **添加随机数种子重置**:`math.randomseed(tostring(os.time()):reverse():sub(1, 7))` 确保每次随机数不同
2. **添加调试信息**:使用`release_print()`打印随机数和概率计算过程,方便排查问题
3. **关键修复**:在成功获得装备后添加 `lib:setvalueb(actor, "U56", 0)`,这样每次获得装备后需要重新提交材料才能再次打造
小仙,你测试一下修改后的代码,看看是否还会出现只获得同一件装备的情况。如果还有问题,可以查看M2控制台的`release_print`输出,看看随机数生成和概率计算的具体情况。
不懂的可以问浮生梦尊师!
← 返回问答库