-- 进入地图消耗判断
-- 返回值: 1=材料不足 2=没有此地图配置 3=可以进入地图
-- 进入地图条件判断
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:Leve
我是lua后端老师!
小仙,你这个问题问得非常好!想要实现哪个条件不足就提示哪个,关键在于**在每个条件判断失败时立即返回具体的提示信息**,而不是等到最后统一提示。我来帮你优化一下这个函数:
```lua
-- 进入地图消耗判断
-- 返回值: 1=材料不足 2=没有此地图配置 3=可以进入地图
-- 进入地图条件判断
function lua_jinruditu(player, mapId)
local id = mapCfg[mapId]
-- 先检查地图配置是否存在
if not id then
lualib:Tips(player, "没有此地图配置!", 2) -- 地图不存在提示
return 2
end
local data = cfg_map[id]
-- 检查等级条件
if data.Level then
if lualib:Level(player) < data.Level then
lualib:Tips(player, "等级不足,需要"..data.Level.."级!", 2)
return 1
end
end
-- 检查转生等级
if data.Rein then
if lualib:ReLevel(player) < data.Rein then
lualib:Tips(player, "转生等级不足,需要"..data.Rein.."转!", 2)
return 1
end
end
-- 检查装备需求
if data.Equip then
if not checkitemw(player, data.Equip) then
lualib:Tips(player, "需要穿戴指定装备!", 2)
return 1
end
end
-- 检查装备等级
if data.EquipLevel then
local tb = string.split(data.EquipLevel, "=") -- tb[1]=装备位置 tb[2]=装备表Price字段
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
lualib:Tips(player, "装备等级不足!", 2)
return 1
end
end
-- 检查标识
if data.mark then
local tb = string.split(data.mark, "#") -- tb[1]=标识ID tb[2]=检测值
tb[1], tb[2] = tonumber(tb[1]), tonumber(tb[2])
if lualib:GetIndex(player, tb[1]) ~= tb[2] then
lualib:Tips(player, "未满足标识条件!", 2)
return 1
end
end
-- 检查BUFF
if data.buff then
local tb = string.split(data.buff, "#") -- tb[1]=BUFFID tb[2]=检测层数[默认1层]
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
lualib:Tips(player, "BUFF层数不足!", 2)
return 1
end
else
lualib:Tips(player, "需要指定BUFF!", 2)
return 1
end
end
-- 检查消耗材料(最后检查,因为要扣除)
if data.Cost then
if not checkitems(player, data.Cost, 0, 0) then
lualib:Tips(player, "材料不足!", 2)
return 1
else
takes(player, data.Cost, "", 0, 0, "进入地图")
end
end
-- 所有条件都满足,进入地图
lualib:MapMove(player, mapId)
callscriptex(player, "DSFUNCALL", "DINGSHICF_挂机执行", 500, 0, 0)
return 3 -- 可以进入地图
end
```
**优化要点:**
1. **立即返回原则**:每个条件判断失败后,立即用`lualib:Tips`提示具体原因并返回
2. **更详细的提示**:在等级、转生等条件中,可以显示具体需要的数值
3. **返回值明确**:保持原有的返回值约定(1=材料不足,2=没有此地图配置,3=可以进入)
4. **消耗材料最后处理**:因为要扣除物品,所以放在最后判断
**使用示例:**
```lua
-- 在NPC脚本中调用
local result = lua_jinruditu(actor, "3")
if result == 3 then
release_print("成功进入地图")
end
```
这样玩家就能清楚地知道到底是哪个条件不满足了,体验会好很多!如果还有不明白的地方,随时可以问我,或者请教浮生梦尊师哦!