break loops and cache revealed results for faster runtime

This commit is contained in:
Alexander Weber 2022-02-22 06:56:43 +01:00
parent ddd14839b8
commit 307540484b

View File

@ -1,5 +1,11 @@
local reg_items = minetest.registered_items local reg_items = minetest.registered_items
local revealed_cache = {}
minetest.register_on_leaveplayer(function(player)
revealed_cache[player:get_player_name()] = nil
end)
local function item_has_groups(item_groups, groups) local function item_has_groups(item_groups, groups)
for i = 1, #groups do for i = 1, #groups do
local group = groups[i] local group = groups[i]
@ -11,30 +17,43 @@ local function item_has_groups(item_groups, groups)
end end
local function revealed_show_recipe(recipe, playername) local function revealed_show_recipe(recipe, playername)
if not revealed_cache[playername] then
revealed_cache[playername] = {}
end
if revealed_cache[playername][recipe] then
return true
end
local revealed = doc.data.players[playername].stored_data.revealed local revealed = doc.data.players[playername].stored_data.revealed
for _, item in pairs(recipe.items) do for _, item in pairs(recipe.items) do
local is_revealed_item local is_revealed_item = false
if item:sub(1,6) == "group:" then if item:sub(1,6) == "group:" then
local groups = item:sub(7):split(",") local groups = item:sub(7):split(",")
for cid, items in pairs(revealed) do for cid, items in pairs(revealed) do
for revealed_item in pairs(items) do for revealed_item in pairs(items) do
if reg_items[revealed_item] and item_has_groups(reg_items[revealed_item].groups, groups) then if reg_items[revealed_item] and item_has_groups(reg_items[revealed_item].groups, groups) then
is_revealed_item = true is_revealed_item = true
break
end end
end end
if is_revealed_item then
break
end
end end
else else
for cid, items in pairs(revealed) do for cid, items in pairs(revealed) do
if items[item] then if items[item] then
is_revealed_item = true is_revealed_item = true
break
end end
end end
end end
if not is_revealed_item then if not is_revealed_item then
return return false
end end
end end
revealed_cache[playername][recipe] = true
return true return true
end end