Simplify recipes comparison

This commit is contained in:
Jean-Patrick Guerrero 2021-11-04 00:53:14 +01:00
parent f779492aed
commit 22bfbf1f56
2 changed files with 18 additions and 55 deletions

View File

@ -157,60 +157,23 @@ local function array_diff(t1, t2)
return diff
end
local function table_eq(T1, T2)
local avoid_loops = {}
local function rcp_eq(rcp, rcp2)
if rcp.type ~= rcp2.type then return end
if rcp.width ~= rcp2.width then return end
if #rcp.items ~= #rcp2.items then return end
if rcp.output ~= rcp2.output then return end
local function recurse(t1, t2)
if type(t1) ~= type(t2) then return end
if not is_table(t1) then
return t1 == t2
for i, item in pairs(rcp.items) do
if item ~= rcp2.items[i] then return end
end
if avoid_loops[t1] then
return avoid_loops[t1] == t2
for i, item in pairs(rcp2.items) do
if item ~= rcp.items[i] then return end
end
avoid_loops[t1] = t2
local t2k, t2kv = {}, {}
for k in pairs(t2) do
if is_table(k) then
table.insert(t2kv, k)
end
t2k[k] = true
end
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if type(k1) == "table" then
local ok
for i = 1, #t2kv do
local tk = t2kv[i]
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
table.remove(t2kv, i)
t2k[tk] = nil
ok = true
break
end
end
if not ok then return end
else
if v2 == nil then return end
t2k[k1] = nil
if not recurse(v1, v2) then return end
end
end
if next(t2k) then return end
return true
end
return recurse(T1, T2)
end
local function is_group(item)
return sub(item, 1, 6) == "group:"
end
@ -536,9 +499,9 @@ local _ = {
remove = table.remove,
indexof = table.indexof,
is_table = is_table,
table_eq = table_eq,
table_merge = table_merge,
table_replace = table_replace,
rcp_eq = rcp_eq,
array_diff = array_diff,
-- Math

View File

@ -5,8 +5,8 @@ local maxn, copy, insert, sort, match = i3.get("maxn", "copy", "insert", "sort",
local is_group, extract_groups, item_has_groups, groups_to_items =
i3.get("is_group", "extract_groups", "item_has_groups", "groups_to_items")
local true_str, is_table, show_item, table_merge, table_replace, table_eq =
i3.get("true_str", "is_table", "show_item", "table_merge", "table_replace", "table_eq")
local true_str, is_table, show_item, table_merge, table_replace, rcp_eq =
i3.get("true_str", "is_table", "show_item", "table_merge", "table_replace", "rcp_eq")
local function get_burntime(item)
return core.get_craft_result{method = "fuel", items = {item}}.time
@ -231,7 +231,7 @@ local function resolve_aliases(hash)
local rcp_new = copy(i3.recipes_cache[newname][j])
rcp_new.output = oldname
if table_eq(rcp_old, rcp_new) then
if rcp_eq(rcp_old, rcp_new) then
similar = true
break
end
@ -272,7 +272,7 @@ local function init_recipes()
resolve_aliases(_select)
sort(i3.init_items)
if i3.http and type(i3.export_url) == "string" then
if i3.http and true_str(i3.export_url) then
local post_data = {
recipes = i3.recipes_cache,
usages = i3.usages_cache,