-- 进入地图条件判断
function lua_jinruditu(player, mapId)
local id = mapCfg[mapId]
local result = true
if id then
local data = cfg_map[id]
if data.Level then
if lualib:Level(player) < data.Level then
re
我是lua后端老师!
小仙,你这个地图条件判断函数写得不错,但想实现“哪个条件不足就提示哪个”的功能,确实是个很实用的需求。让我来帮你优化一下。
根据你的代码逻辑,我们可以为每个条件检查都添加独立的提示信息,然后在条件不满足时立即返回对应的提示,而不是最后统一提示。下面是修改后的代码:
```lua
-- 进入地图条件判断
function lua_jinruditu(player, mapId)
local id = mapCfg[mapId]
if not id then
return lualib:Tips(player, "地图配置不存在!", 2)
end
local data = cfg_map[id]
-- 检查等级条件
if data.Level then
if lualib:Level(player) < data.Level then
return lualib:Tips(player, "等级不足"..data.Level.."级!", 2)
end
end
-- 检查转生条件
if data.Rein then
if lualib:ReLevel(player) < data.Rein then
return lualib:Tips(player, "转生等级不足"..data.Rein.."转!", 2)
end
end
-- 检查装备条件
if data.Equip then
if not checkitemw(player, data.Equip) then
return lualib:Tips(player, "需要穿戴指定装备!", 2)
end
end
-- 检查装备等级条件
if data.EquipLevel then
local tb = string.split(data.EquipLevel, "=")
tb[1], tb[2] = tonumber(tb[1]), tonumber(tb[2])
local equip = linkbodyitem(player, tb[1])
local equipName = lualib:ItemName(player, equip)
local price = nil
if equipName then
price = getstditeminfo(getiteminfo(player, equipName, 2), 8)
end
if not price or price < tb[2] then
return lualib:Tips(player, "装备等级不足!", 2)
end
end
-- 检查标识条件
if data.mark then
local tb = string.split(data.mark, "#")
tb[1], tb[2] = tonumber(tb[1]), tonumber(tb[2])
if lualib:GetIndex(player, tb[1]) ~= tb[2] then
return lualib:Tips(player, "不满足标识条件!", 2)
end
end
-- 检查BUFF条件
if data.buff then
local tb = string.split(data.buff, "#")
tb[1], tb[2] = tonumber(tb[1]), tonumber(tb[2]) or 1
if lualib:HasBuff(player, tb[1]) then
if lualib:GetBuffInfo(player, tb[1], 1) < tb[2] then
return lualib:Tips(player, "BUFF层数不足!", 2)
end
else
return lualib:Tips(player, "需要指定BUFF!", 2)
end
end
-- 检查消耗条件
if data.Cost then
if not checkitems(player, data.Cost, 0, 0) then
return lualib:Tips(player, "消耗物品不足!", 2)
else
takes(player, data.Cost, "", 0, 0, "进入地图")
end
end
-- 所有条件都满足,执行传送
lualib:MapMove(player, mapId)
return true
end
```
**修改说明:**
1. **立即返回**:每个条件检查不满足时,立即使用`return`返回对应的提示信息,这样玩家就能知道具体是哪个条件不满足
2. **提示细化**:我为每个条件都添加了更具体的提示信息,比如等级不足会显示需要多少级
3. **逻辑清晰**:去掉了`result`变量,代码结构更加清晰
4. **函数结束**:所有条件检查通过后,最后执行地图传送
**使用建议:**
- 你可以根据实际需求进一步细化提示内容,比如把"指定装备"换成具体的装备名称
- 如果想让提示更加友好,可以在配置表中为每个条件也配置提示信息
- 记得在QF中注册这个函数对应的触发接口
这样修改后,玩家就能清楚地知道到底是哪个条件不满足了!如果还有其他问题,随时问我哦~