Cache groups (testing needed)

This commit is contained in:
Jean-Patrick Guerrero 2022-06-18 19:47:23 +02:00
parent cf5f18e1c1
commit f0f94017da
5 changed files with 29 additions and 11 deletions

View File

@ -82,6 +82,7 @@ i3 = {
tabs = {},
cubes = {},
groups = {},
plants = {},
modules = {},
craft_types = {},

View File

@ -23,10 +23,22 @@ local function cache_fuel(item)
end
end
local function get_item_usages(item, recipe, added)
local groups = extract_groups(item)
local function cache_groups(groupname, groups)
i3.groups[groupname] = {}
i3.groups[groupname].groups = groups
i3.groups[groupname].items = groups_to_items(groups, true)
end
local function get_item_usages(item, recipe, added)
if is_group(item) then
local groupname = item:sub(7)
local group_cache = i3.groups[groupname]
local groups = group_cache and group_cache.groups or extract_groups(item)
if not group_cache then
cache_groups(groupname, groups)
end
if groups then
for name, def in pairs(reg_items) do
if not added[name] and valid_item(def) and item_has_groups(def.groups, groups) then
local usage = copy(recipe)

View File

@ -236,10 +236,8 @@ local function is_group(item)
end
local function extract_groups(str)
if sub(str, 1, 6) == "group:" then
return split(sub(str, 7), ",")
end
end
local function item_has_groups(item_groups, groups)
for i = 1, #groups do
@ -385,7 +383,8 @@ local function craft_stack(player, data, craft_rcp)
if is_group(name) then
items = {}
local groups = extract_groups(name)
local item_groups = groups_to_items(groups, true)
local groupname = name:sub(7)
local item_groups = i3.groups[groupname].items or groups_to_items(groups, true)
local remaining = count
for _, item in ipairs(item_groups) do

View File

@ -93,7 +93,9 @@ local function get_stack_max(inv, data, is_recipe, rcp)
local def = reg_items[item]
if def then
local groups = extract_groups(name)
local groupname = name:sub(7)
local group_cache = i3.groups[groupname]
local groups = group_cache and group_cache.groups or extract_groups(name)
if item_has_groups(def.groups, groups) then
counts_inv[name] = (counts_inv[name] or 0) + count
@ -994,8 +996,10 @@ local function get_grid_fs(fs, data, rcp, is_recipe)
local groups
if is_group(name) then
groups = extract_groups(name)
name = groups_to_items(groups)
local groupname = name:sub(7)
local group_cache = i3.groups[groupname]
groups = group_cache and group_cache.groups or extract_groups(name)
name = group_cache and group_cache.items[1] or groups_to_items(groups)
end
local label = groups and "\nG" or ""

View File

@ -32,7 +32,9 @@ local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items
if is_group(item) then
local groups = extract_groups(item)
local groupname = item:sub(7)
local group_cache = i3.groups[groupname]
local groups = group_cache and group_cache.groups or extract_groups(item)
for i = 1, inv_items_size do
local def = core.registered_items[inv_items[i]]