local junguan = {}
function junguan.link(actor, msgid, arg1, arg2, arg3, sMsg)
-- 首先检查配置是否加载
if not cfg_junguan or not next(cfg_junguan) then
lib:msg(actor, "配置错误")
return
end
-- 获取玩家的焚天等级(假设存储在U52变量中)
local
我是lua后端老师!小仙,我来帮你完善这个军管系统代码,加入U53来记录领取状态。你的思路很对,用U53作为领取标记,这样玩家就不会重复领取奖励了。
```lua
local junguan = {}
function junguan.link(actor, msgid, arg1, arg2, arg3, sMsg)
-- 首先检查配置是否加载
if not cfg_junguan or not next(cfg_junguan) then
lib:msg(actor, "配置错误")
return
end
-- 获取玩家的焚天等级(假设存储在U52变量中)
local junguanLevel = lib:getvalueb(actor, "U52") or 0
release_print("当前焚天等级:", junguanLevel)
-- 获取领取状态标记(U53的每一位代表一个选项的领取状态)
local receivedMark = lib:getvalueb(actor, "U53") or 0
release_print("当前领取标记:", receivedMark)
-- 检查是否已经领取过该选项的奖励
local function hasReceived(option)
local bitValue = 1 << option -- 左移操作,为每个选项分配一个bit位
return (receivedMark & bitValue) ~= 0
end
-- 标记该选项为已领取
local function markAsReceived(option)
local bitValue = 1 << option
local newMark = receivedMark | bitValue
lib:setvalueb(actor, "U53", newMark)
release_print("更新领取标记:", newMark)
end
-- 使用 if...elseif 结构来处理不同的 arg2 选项
if arg2 == 0 then
-- 选项0:需要焚天等级 >= 30
if hasReceived(0) then
lib:msg(actor, "你已经领取过这个奖励了!")
return
end
if junguanLevel >= 30 then
lib:msg(actor, "领取成功!!!")
giveitem(actor, "魔·丸", 3) -- 给予物品
markAsReceived(0) -- 标记为已领取
else
lib:msg(actor, "你的镇压点数不足!!!")
end
elseif arg2 == 1 then
-- 选项1:需要焚天等级 >= 60
if hasReceived(1) then
lib:msg(actor, "你已经领取过这个奖励了!")
return
end
if junguanLevel >= 60 then
lib:msg(actor, "领取成功!!!")
giveitem(actor, "隐魔秘宝", 1) -- 给予物品
markAsReceived(1) -- 标记为已领取
else
lib:msg(actor, "你的镇压点数不足!!!")
end
elseif arg2 == 2 then
-- 选项2:需要焚天等级 >= 90,并授予称号
if hasReceived(2) then
lib:msg(actor, "你已经领取过这个奖励了!")
return
end
if junguanLevel >= 90 then
lib:msg(actor, "领取成功!!!")
confertitle(actor, "深渊先锋") -- 授予称号
markAsReceived(2) -- 标记为已领取
else
lib:msg(actor, "你的镇压点数不足!!!")
end
else
-- 处理意料之外的 arg2 值
lib:msg(actor, "无效的选项")
end
end
-- 添加一个重置领取状态的函数(可选,用于测试或重置)
function junguan.resetReceived(actor)
lib:setvalueb(actor, "U53", 0)
lib:msg(actor, "领取状态已重置")
release_print("重置领取标记")
end
-- 添加一个查看领取状态的函数(可选,用于调试)
function junguan.checkStatus(actor)
local receivedMark = lib:getvalueb(actor, "U53") or 0
local statusMsg = "当前领取状态:"
for i = 0, 2 do
local bitValue = 1 << i
local hasRecv = (receivedMark & bitValue) ~= 0
statusMsg = statusMsg .. string.format("\n选项%d: %s", i, hasRecv and "已领取" or "未领取")
end
lib:msg(actor, statusMsg)
release_print("领取状态检查:", receivedMark)
end
return junguan
```
这个代码的核心改进点:
1. **位运算记录状态**:用U53的二进制位来记录每个选项的领取状态
- 第0位(bit 0)代表选项0的领取状态
- 第1位(bit 1)代表选项1的领取状态
- 第2位(bit 2)代表选项2的领取状态
2. **领取前检查**:每次领取前先检查是否已经领取过,避免重复领取
3. **领取后标记**:领取成功后立即标记对应位为1
4. **辅助函数**:添加了重置和查看状态的函数,方便调试
小仙,这样设计的好处是只需要一个U53变量就能记录所有选项的领取状态,节省变量资源。如果以后增加更多选项,只需要扩展bit位就可以了。记得在QF中调用这些函数时要确保U52和U53变量已经初始化哦!