Allow relative level argument in /level command

This commit is contained in:
Wuzzy 2024-12-24 14:15:58 +01:00
parent 77859b12bb
commit af17b3a1d6

View File

@ -404,15 +404,48 @@ minetest.register_chatcommand("level", {
if lzr_gamestate.is_loading() then if lzr_gamestate.is_loading() then
return false, S("Cant start a level while loading!") return false, S("Cant start a level while loading!")
end end
local pack, level = string.match(param, "([a-zA-Z0-9_]+) ([0-9]+)") -- Parse arguments. level argument may be relative (tilde notation)
if not pack then local pack, level = string.match(param, "([a-zA-Z0-9_]+) (~?-?[0-9]*)")
pack = "__core"
level = tonumber(param) local current_data = lzr_levels.get_current_level_data()
local current_pack
if current_data then
current_pack = current_data.name
end
-- Select level pack. Defaults to the current level pack
-- if playing one or __core if playing no level pack
-- (singleton levels don't count)
if not pack then
if current_pack then
pack = current_pack
else
pack = "__core"
end
level = string.match(param, "(~?-?[0-9]*)")
end end
level = tonumber(level)
if not level then if not level then
return false return false
end end
-- Handle relative level argument (e.g. "~1" to move to the next level)
local is_relative_level = string.sub(level, 1, 1) == "~"
if is_relative_level then
local current_level = lzr_levels.get_current_level()
-- Relative level argument is only allowed if playing in a level
-- and the chosen level pack is the same as the current one.
if not current_level then
return false, S("Level argument cant be relative when not playing in a level.")
end
if current_pack ~= pack then
return false, S("Level argument cant be relative when choosing a different level pack.")
end
level = minetest.parse_relative_number(level, current_level)
else
level = tonumber(level)
end
if not level then
return false
end
local level_data = lzr_levels.get_level_pack(pack) local level_data = lzr_levels.get_level_pack(pack)
if not level_data then if not level_data then
return false, S("Level pack “@1” doesnt exist.", pack) return false, S("Level pack “@1” doesnt exist.", pack)