From f0f94017da422880e65e14ed63365514185e3939 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sat, 18 Jun 2022 19:47:23 +0200 Subject: [PATCH] Cache groups (testing needed) --- init.lua | 1 + src/caches.lua | 18 +++++++++++++++--- src/common.lua | 7 +++---- src/gui.lua | 10 +++++++--- src/progressive.lua | 4 +++- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/init.lua b/init.lua index 38404ad..be163f3 100644 --- a/init.lua +++ b/init.lua @@ -82,6 +82,7 @@ i3 = { tabs = {}, cubes = {}, + groups = {}, plants = {}, modules = {}, craft_types = {}, diff --git a/src/caches.lua b/src/caches.lua index 63726dd..edacf16 100644 --- a/src/caches.lua +++ b/src/caches.lua @@ -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) diff --git a/src/common.lua b/src/common.lua index c812238..4916dea 100644 --- a/src/common.lua +++ b/src/common.lua @@ -236,9 +236,7 @@ local function is_group(item) end local function extract_groups(str) - if sub(str, 1, 6) == "group:" then - return split(sub(str, 7), ",") - end + return split(sub(str, 7), ",") end local function item_has_groups(item_groups, groups) @@ -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 diff --git a/src/gui.lua b/src/gui.lua index a532012..dd00dc6 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -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 "" diff --git a/src/progressive.lua b/src/progressive.lua index 184bf20..e206107 100644 --- a/src/progressive.lua +++ b/src/progressive.lua @@ -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]]