ctf_map_core: Move treasure registration into meta_helpers.lua (#545)

This commit is contained in:
ANAND 2019-12-28 15:04:26 +05:30 committed by Thomas--S
parent e5793d7c7f
commit 19d9b3efb8
2 changed files with 58 additions and 47 deletions

View file

@ -64,6 +64,63 @@ local function update_physics(player)
}) })
end end
---------------------------
-- TREASURE REGISTRATION --
---------------------------
function ctf_map.register_treasures(map)
if not ctf_treasure then
return
end
treasurer.treasures = {}
if map.treasures then
for _, item in pairs(map.treasures) do
item = item:split(",")
-- treasurer.register_treasure(name, rarity, preciousness, count)
if #item == 4 then
treasurer.register_treasure(item[1],
tonumber(item[2]),
tonumber(item[3]),
tonumber(item[4])
)
-- treasurer.register_treasure(name, rarity, preciousness, {min, max})
elseif #item == 5 then
treasurer.register_treasure(item[1],
tonumber(item[2]),
tonumber(item[3]),
{
tonumber(item[4]),
tonumber(item[5])
}
)
end
end
else
-- If treasure is a part of map's initial stuff, don't register it
local blacklist = ctf_map.map.initial_stuff or give_initial_stuff.get_stuff()
for _, def in pairs(ctf_treasure.get_default_treasures()) do
local is_valid = true
for _, b_item in pairs(blacklist) do
local b_stack = ItemStack(b_item)
local t_stack = ItemStack(def[1])
if b_stack:get_name() == t_stack:get_name() and
t_stack:get_count() == 1 then
is_valid = false
minetest.log("info",
"ctf_map: Omitting treasure - " .. def[1])
break
end
end
if is_valid then
minetest.log("info",
"ctf_map: Registering treasure - " .. def[1])
treasurer.register_treasure(def[1], def[2], def[3], def[4])
end
end
end
end
--------------- ---------------
-- CALLBACKS -- -- CALLBACKS --
--------------- ---------------

View file

@ -380,53 +380,7 @@ ctf_match.register_on_new_match(function()
-- Register per-map treasures, or the default set of treasures -- Register per-map treasures, or the default set of treasures
-- if treasures field hasn't been defined in map meta -- if treasures field hasn't been defined in map meta
if ctf_treasure then ctf_map.register_treasures(ctf_map.map)
treasurer.treasures = {}
if ctf_map.map.treasures then
for _, item in pairs(ctf_map.map.treasures) do
item = item:split(",")
-- treasurer.register_treasure(name, rarity, preciousness, count)
if #item == 4 then
treasurer.register_treasure(item[1],
tonumber(item[2]),
tonumber(item[3]),
tonumber(item[4]))
-- treasurer.register_treasure(name, rarity, preciousness, {min, max})
elseif #item == 5 then
treasurer.register_treasure(item[1],
tonumber(item[2]),
tonumber(item[3]),
{
tonumber(item[4]),
tonumber(item[5])
})
end
end
else
-- If treasure is a part of map's initial stuff, don't register it
local blacklist = ctf_map.map.initial_stuff or give_initial_stuff.get_stuff()
for _, def in pairs(ctf_treasure.get_default_treasures()) do
local is_valid = true
for _, b_item in pairs(blacklist) do
local b_stack = ItemStack(b_item)
local t_stack = ItemStack(def[1])
if b_stack:get_name() == t_stack:get_name() and
t_stack:get_count() == 1 then
is_valid = false
minetest.log("info",
"ctf_map: Omitting treasure - " .. def[1])
break
end
end
if is_valid then
minetest.log("info",
"ctf_map: Registering treasure - " .. def[1])
treasurer.register_treasure(def[1], def[2], def[3], def[4])
end
end
end
end
-- Place map -- Place map
place_map(ctf_map.map) place_map(ctf_map.map)