Cleanup and optimizations

This commit is contained in:
Jean-Patrick Guerrero 2019-02-17 15:04:01 +01:00
parent 97e6eceb75
commit bfe99092db

194
init.lua

@ -3,39 +3,43 @@ craftguide = {
craft_types = {}, craft_types = {},
} }
local mt = minetest local M = minetest
local player_data = {} local player_data = {}
-- Caches
local init_items = {} local init_items = {}
local recipes_cache = {}
local fuel_cache = {}
local searches = {} local searches = {}
local recipes_cache = {}
local usages_cache = {}
local fuel_cache = {}
local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode") local progressive_mode = M.settings:get_bool("craftguide_progressive_mode")
local sfinv_only = mt.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv") local sfinv_only = M.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
local colorize = mt.colorize local colorize = M.colorize
local fs_esc = mt.formspec_escape local reg_items = M.registered_items
local reg_items = mt.registered_items local get_result = M.get_craft_result
local get_result = mt.get_craft_result local show_formspec = M.show_formspec
local show_formspec = mt.show_formspec local get_player_by_name = M.get_player_by_name
local get_player_by_name = mt.get_player_by_name local serialize, deserialize = M.serialize, M.deserialize
local serialize, deserialize = mt.serialize, mt.deserialize
local S = mt.get_translator("craftguide") local ESC = M.formspec_escape
local S = M.get_translator("craftguide")
-- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage: -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
-- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1 -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
local maxn, sort, concat = table.maxn, table.sort, table.concat local maxn, sort, concat = table.maxn, table.sort, table.concat
local vector_add, vector_mul = vector.add, vector.multiply local vector_add, vector_mul = vector.add, vector.multiply
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
local fmt, pairs, next = string.format, pairs, next local fmt, find, sub, gmatch = string.format, string.find, string.sub, string.gmatch
local pairs, next = pairs, next
local DEFAULT_SIZE = 10 local DEFAULT_SIZE = 10
local MIN_LIMIT, MAX_LIMIT = 10, 12 local MIN_LIMIT, MAX_LIMIT = 10, 12
DEFAULT_SIZE = min(MAX_LIMIT, max(MIN_LIMIT, DEFAULT_SIZE)) DEFAULT_SIZE = min(MAX_LIMIT, max(MIN_LIMIT, DEFAULT_SIZE))
local GRID_LIMIT = 5 local GRID_LIMIT = 5
local POLL_FREQ = 0.5 local POLL_FREQ = 0.25
local FMT_label = "label[%f,%f;%s]" local FMT_label = "label[%f,%f;%s]"
local FMT_image = "image[%f,%f;%f,%f;%s]" local FMT_image = "image[%f,%f;%f,%f;%s]"
@ -157,8 +161,18 @@ local function item_has_groups(item_groups, groups)
return true return true
end end
local function split(str)
local t, c = {}, 0
for s in gmatch(str, "([^,]+)") do
c = c + 1
t[c] = s
end
return t
end
local function extract_groups(str) local function extract_groups(str)
return str:sub(7):split(",") return split(sub(str, 7))
end end
local function item_in_recipe(item, recipe) local function item_in_recipe(item, recipe)
@ -166,7 +180,7 @@ local function item_in_recipe(item, recipe)
for _, recipe_item in pairs(recipe.items) do for _, recipe_item in pairs(recipe.items) do
if recipe_item == item then if recipe_item == item then
return true return true
elseif recipe_item:sub(1,6) == "group:" then elseif sub(recipe_item, 1, 6) == "group:" then
local groups = extract_groups(recipe_item) local groups = extract_groups(recipe_item)
if item_has_groups(item_groups, groups) then if item_has_groups(item_groups, groups) then
return true return true
@ -204,7 +218,7 @@ local function get_filtered_items(player)
local fuel = fuel_cache[item] local fuel = fuel_cache[item]
if recipes and #apply_recipe_filters(recipes, player) > 0 or if recipes and #apply_recipe_filters(recipes, player) > 0 or
fuel and #apply_recipe_filters(get_item_usages(item), player) > 0 then fuel and #apply_recipe_filters(usages_cache[item], player) > 0 then
c = c + 1 c = c + 1
items[c] = item items[c] = item
end end
@ -214,7 +228,7 @@ local function get_filtered_items(player)
end end
local function cache_recipes(output) local function cache_recipes(output)
local recipes = mt.get_all_craft_recipes(output) or {} local recipes = M.get_all_craft_recipes(output) or {}
local c = 0 local c = 0
for i = 1, #craftguide.custom_crafts do for i = 1, #craftguide.custom_crafts do
@ -247,7 +261,7 @@ local function get_recipes(item, data, player)
end end
if data.show_usages then if data.show_usages then
recipes = apply_recipe_filters(get_item_usages(item), player) recipes = apply_recipe_filters(usages_cache[item], player)
if #recipes == 0 then if #recipes == 0 then
return return
end end
@ -271,10 +285,12 @@ end
local function groups_to_item(groups) local function groups_to_item(groups)
if #groups == 1 then if #groups == 1 then
local group = groups[1] local group = groups[1]
local def_gr = "default:" .. group
if group_stereotypes[group] then if group_stereotypes[group] then
return group_stereotypes[group] return group_stereotypes[group]
elseif reg_items["default:" .. group] then elseif reg_items[def_gr] then
return "default:" .. group return def_gr
end end
end end
@ -291,9 +307,11 @@ local function get_tooltip(item, groups, cooktime, burntime)
local tooltip local tooltip
if groups then if groups then
local groupstr = {} local groupstr, c = {}, 0
for i = 1, #groups do for i = 1, #groups do
groupstr[#groupstr + 1] = colorize("yellow", groups[i]) c = c + 1
groupstr[c] = colorize("yellow", groups[i])
end end
groupstr = concat(groupstr, ", ") groupstr = concat(groupstr, ", ")
@ -312,7 +330,7 @@ local function get_tooltip(item, groups, cooktime, burntime)
S("Burning time: @1", colorize("yellow", burntime)) S("Burning time: @1", colorize("yellow", burntime))
end end
return "tooltip[" .. item .. ";" .. fs_esc(tooltip) .. "]" return fmt("tooltip[%s;%s]", item, ESC(tooltip))
end end
local function get_recipe_fs(data, iY) local function get_recipe_fs(data, iY)
@ -336,7 +354,7 @@ local function get_recipe_fs(data, iY)
fs[#fs + 1] = fmt(FMT_label, fs[#fs + 1] = fmt(FMT_label,
(data.iX / 2) - 2, (data.iX / 2) - 2,
iY + 2.2, iY + 2.2,
fs_esc(S("Recipe is too big to be displayed (@1x@2)", width, rows))) ESC(S("Recipe is too big to be displayed (@1x@2)", width, rows)))
return concat(fs) return concat(fs)
end end
@ -358,7 +376,7 @@ local function get_recipe_fs(data, iY)
end end
local groups local groups
if item:sub(1,6) == "group:" then if sub(item, 1, 6) == "group:" then
groups = extract_groups(item) groups = extract_groups(item)
item = groups_to_item(groups) item = groups_to_item(groups)
end end
@ -372,7 +390,7 @@ local function get_recipe_fs(data, iY)
btn_size, btn_size,
item, item,
item:match("%S*"), item:match("%S*"),
fs_esc(label)) ESC(label))
local burntime = fuel_cache[item] local burntime = fuel_cache[item]
@ -388,7 +406,7 @@ local function get_recipe_fs(data, iY)
shapeless and "shapeless" or "furnace" shapeless and "shapeless" or "furnace"
if not custom_recipe then if not custom_recipe then
icon = "craftguide_" .. icon .. ".png^[resize:16x16" icon = fmt("craftguide_%s.png^[resize:16x16", icon)
end end
fs[#fs + 1] = fmt(FMT_image, fs[#fs + 1] = fmt(FMT_image,
@ -406,7 +424,7 @@ local function get_recipe_fs(data, iY)
iY + (sfinv_only and 2.2 or 1.7), iY + (sfinv_only and 2.2 or 1.7),
0.5, 0.5,
0.5, 0.5,
fs_esc(tooltip)) ESC(tooltip))
end end
local arrow_X = rightest + (s_btn_size or 1.1) local arrow_X = rightest + (s_btn_size or 1.1)
@ -436,7 +454,7 @@ local function get_recipe_fs(data, iY)
1.1, 1.1,
1.1, 1.1,
recipe.output, recipe.output,
fs_esc(output_name)) ESC(output_name))
if burntime then if burntime then
fs[#fs + 1] = get_tooltip(output_name, nil, nil, burntime) fs[#fs + 1] = get_tooltip(output_name, nil, nil, burntime)
@ -463,9 +481,9 @@ local function get_recipe_fs(data, iY)
2.2, 2.2,
1, 1,
"alternate", "alternate",
data.show_usages and fs_esc(S("Usage")) or fs_esc(S("Recipe")), data.show_usages and ESC(S("Usage")) or ESC(S("Recipe")),
data.rnum, data.rnum,
fs_esc(S("of")), ESC(S("of")),
#data.recipes) #data.recipes)
return concat(fs) return concat(fs)
@ -482,18 +500,22 @@ local function make_formspec(name)
if not sfinv_only then if not sfinv_only then
fs[#fs + 1] = fmt("size[%f,%f;]", data.iX - 0.35, iY + 4) fs[#fs + 1] = fmt("size[%f,%f;]", data.iX - 0.35, iY + 4)
fs[#fs + 1] = [[ fs[#fs + 1] = [[
no_prepend[] no_prepend[]
background[1,1;1,1;craftguide_bg.png;true] background[1,1;1,1;craftguide_bg.png;true]
]] ]]
fs[#fs + 1] = "tooltip[size_inc;" .. fs_esc(S("Increase window size")) .. "]" fs[#fs + 1] = fmt([[ tooltip[size_inc;%s]
fs[#fs + 1] = "tooltip[size_dec;" .. fs_esc(S("Decrease window size")) .. "]" tooltip[size_dec;%s] ]],
ESC(S("Increase window size")),
ESC(S("Decrease window size")))
fs[#fs + 1] = "image_button[" .. (data.iX * 0.47) .. fs[#fs + 1] = fmt([[
",0.12;0.8,0.8;craftguide_zoomin_icon.png;size_inc;]" image_button[%f,0.12;0.8,0.8;craftguide_zoomin_icon.png;size_inc;]
fs[#fs + 1] = "image_button[" .. ((data.iX * 0.47) + 0.6) .. image_button[%f,0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;] ]],
",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]" data.iX * 0.47,
data.iX * 0.47 + 0.6)
end end
fs[#fs + 1] = [[ fs[#fs + 1] = [[
@ -502,24 +524,28 @@ local function make_formspec(name)
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
]] ]]
fs[#fs + 1] = "tooltip[search;" .. fs_esc(S("Search")) .. "]" fs[#fs + 1] = fmt([[ tooltip[search;%s]
fs[#fs + 1] = "tooltip[clear;" .. fs_esc(S("Reset")) .. "]" tooltip[clear;%s]
fs[#fs + 1] = "tooltip[prev;" .. fs_esc(S("Previous page")) .. "]" tooltip[prev;%s]
fs[#fs + 1] = "tooltip[next;" .. fs_esc(S("Next page")) .. "]" tooltip[next;%s] ]],
ESC(S("Search")),
ESC(S("Reset")),
ESC(S("Previous page")),
ESC(S("Next page")))
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) .. fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]"
fs[#fs + 1] = fmt(FMT_label,
data.iX - (sfinv_only and 1.7 or 2.2), data.iX - (sfinv_only and 1.7 or 2.2),
0.22, 0.22,
colorize("yellow", data.pagenum) .. " / " .. data.pagemax) colorize("yellow", data.pagenum),
data.pagemax)
fs[#fs + 1] = "image_button[" .. (data.iX - fs[#fs + 1] = fmt([[
(sfinv_only and 0.7 or 1.2) - (data.iX >= 11 and 0.08 or 0)) .. image_button[%f,0.12;0.8,0.8;craftguide_prev_icon.png;prev;]
",0.12;0.8,0.8;craftguide_next_icon.png;next;]" image_button[%f,0.12;0.8,0.8;craftguide_next_icon.png;next;] ]],
data.iX - (sfinv_only and 2.6 or 3.1),
data.iX - (sfinv_only and 0.7 or 1.2) - (data.iX >= 11 and 0.08 or 0))
fs[#fs + 1] = "field[0.3,0.32;2.5,1;filter;;" .. fs_esc(data.filter) .. "]" fs[#fs + 1] = fmt("field[0.3,0.32;2.5,1;filter;;%s]", ESC(data.filter))
if #data.items == 0 then if #data.items == 0 then
local no_item = S("No item to show") local no_item = S("No item to show")
@ -530,7 +556,7 @@ local function make_formspec(name)
pos = pos - 1 pos = pos - 1
end end
fs[#fs + 1] = fmt(FMT_label, pos, 2, fs_esc(no_item)) fs[#fs + 1] = fmt(FMT_label, pos, 2, ESC(no_item))
end end
local first_item = (data.pagenum - 1) * ipp local first_item = (data.pagenum - 1) * ipp
@ -581,7 +607,7 @@ local function search(data)
local item = data.items_raw[i] local item = data.items_raw[i]
local desc = reg_items[item].description:lower() local desc = reg_items[item].description:lower()
if (item .. desc):find(filter, 1, true) then if find(item .. desc, filter, 1, true) then
c = c + 1 c = c + 1
filtered_list[c] = item filtered_list[c] = item
end end
@ -643,6 +669,13 @@ local function reset_data(data)
data.items = data.items_raw data.items = data.items_raw
end end
local function cache_usages()
for i = 1, #init_items do
local item = init_items[i]
usages_cache[item] = get_item_usages(item)
end
end
local function get_init_items() local function get_init_items()
local c = 0 local c = 0
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
@ -657,6 +690,7 @@ local function get_init_items()
end end
sort(init_items) sort(init_items)
cache_usages()
end end
local function on_receive_fields(player, fields) local function on_receive_fields(player, fields)
@ -712,7 +746,7 @@ local function on_receive_fields(player, fields)
else else
local item local item
for field in pairs(fields) do for field in pairs(fields) do
if field:find(":") then if find(field, ":") then
item = field item = field
break break
end end
@ -720,8 +754,8 @@ local function on_receive_fields(player, fields)
if not item then if not item then
return return
elseif item:sub(-4) == "_inv" then elseif sub(item, -4) == "_inv" then
item = item:sub(1,-5) item = sub(item, 1, -5)
end end
if item ~= data.query_item then if item ~= data.query_item then
@ -743,14 +777,14 @@ local function on_receive_fields(player, fields)
end end
end end
mt.register_on_mods_loaded(get_init_items) M.register_on_mods_loaded(get_init_items)
mt.register_on_joinplayer(function(player) M.register_on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
init_data(name) init_data(name)
end) end)
mt.register_on_leaveplayer(function(player) M.register_on_leaveplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
player_data[name] = nil player_data[name] = nil
end) end)
@ -781,7 +815,7 @@ if sfinv_only then
end, end,
}) })
else else
mt.register_on_player_receive_fields(function(player, formname, fields) M.register_on_player_receive_fields(function(player, formname, fields)
if formname == "craftguide" then if formname == "craftguide" then
on_receive_fields(player, fields) on_receive_fields(player, fields)
end end
@ -799,7 +833,7 @@ else
show_formspec(name, "craftguide", make_formspec(name)) show_formspec(name, "craftguide", make_formspec(name))
end end
mt.register_craftitem("craftguide:book", { M.register_craftitem("craftguide:book", {
description = S("Crafting Guide"), description = S("Crafting Guide"),
inventory_image = "craftguide_book.png", inventory_image = "craftguide_book.png",
wield_image = "craftguide_book.png", wield_image = "craftguide_book.png",
@ -810,7 +844,7 @@ else
end end
}) })
mt.register_node("craftguide:sign", { M.register_node("craftguide:sign", {
description = S("Crafting Guide Sign"), description = S("Crafting Guide Sign"),
drawtype = "nodebox", drawtype = "nodebox",
tiles = {"craftguide_sign.png"}, tiles = {"craftguide_sign.png"},
@ -828,7 +862,7 @@ else
}, },
on_construct = function(pos) on_construct = function(pos)
local meta = mt.get_meta(pos) local meta = M.get_meta(pos)
meta:set_string("infotext", S("Crafting Guide Sign")) meta:set_string("infotext", S("Crafting Guide Sign"))
end, end,
@ -837,27 +871,27 @@ else
end end
}) })
mt.register_craft({ M.register_craft({
output = "craftguide:book", output = "craftguide:book",
recipe = { recipe = {
{"default:book"} {"default:book"}
} }
}) })
mt.register_craft({ M.register_craft({
type = "fuel", type = "fuel",
recipe = "craftguide:book", recipe = "craftguide:book",
burntime = 3 burntime = 3
}) })
mt.register_craft({ M.register_craft({
output = "craftguide:sign", output = "craftguide:sign",
recipe = { recipe = {
{"default:sign_wall_wood"} {"default:sign_wall_wood"}
} }
}) })
mt.register_craft({ M.register_craft({
type = "fuel", type = "fuel",
recipe = "craftguide:sign", recipe = "craftguide:sign",
burntime = 10 burntime = 10
@ -879,7 +913,7 @@ if progressive_mode then
local function item_in_inv(item, inv_items) local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items local inv_items_size = #inv_items
if item:sub(1,6) == "group:" then if sub(item, 1, 6) == "group:" then
local groups = extract_groups(item) local groups = extract_groups(item)
for i = 1, inv_items_size do for i = 1, inv_items_size do
local item_groups = reg_items[inv_items[i]].groups local item_groups = reg_items[inv_items[i]].groups
@ -929,7 +963,7 @@ if progressive_mode then
-- Workaround. Need an engine call to detect when the contents -- Workaround. Need an engine call to detect when the contents
-- of the player inventory changed, instead. -- of the player inventory changed, instead.
local function poll_new_items() local function poll_new_items()
local players = mt.get_connected_players() local players = M.get_connected_players()
for i = 1, #players do for i = 1, #players do
local player = players[i] local player = players[i]
local name = player:get_player_name() local name = player:get_player_name()
@ -942,14 +976,14 @@ if progressive_mode then
end end
end end
mt.after(POLL_FREQ, poll_new_items) M.after(POLL_FREQ, poll_new_items)
end end
poll_new_items() poll_new_items()
craftguide.add_recipe_filter("Default progressive filter", progressive_filter) craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
mt.register_on_joinplayer(function(player) M.register_on_joinplayer(function(player)
local meta = player:get_meta() local meta = player:get_meta()
local name = player:get_player_name() local name = player:get_player_name()
local data = player_data[name] local data = player_data[name]
@ -965,10 +999,10 @@ if progressive_mode then
meta:set_string("inv_items", serialize(data.inv_items)) meta:set_string("inv_items", serialize(data.inv_items))
end end
mt.register_on_leaveplayer(save_meta) M.register_on_leaveplayer(save_meta)
mt.register_on_shutdown(function() M.register_on_shutdown(function()
local players = mt.get_connected_players() local players = M.get_connected_players()
for i = 1, #players do for i = 1, #players do
local player = players[i] local player = players[i]
save_meta(player) save_meta(player)
@ -976,7 +1010,7 @@ if progressive_mode then
end) end)
end end
mt.register_chatcommand("craft", { M.register_chatcommand("craft", {
description = S("Show recipe(s) of the pointed node"), description = S("Show recipe(s) of the pointed node"),
func = function(name) func = function(name)
local player = get_player_by_name(name) local player = get_player_by_name(name)
@ -987,7 +1021,7 @@ mt.register_chatcommand("craft", {
for i = 1, 10 do for i = 1, 10 do
local look_at = vector_add(eye_h, vector_mul(dir, i)) local look_at = vector_add(eye_h, vector_mul(dir, i))
local node = mt.get_node(look_at) local node = M.get_node(look_at)
if node.name ~= "air" then if node.name ~= "air" then
node_name = node.name node_name = node.name
@ -1016,7 +1050,7 @@ mt.register_chatcommand("craft", {
local msg = red .. "%s: " .. ylw local msg = red .. "%s: " .. ylw
if is_fuel then if is_fuel then
recipes = get_item_usages(node_name) recipes = usages_cache[node_name]
if #recipes > 0 then if #recipes > 0 then
data.show_usages = true data.show_usages = true
end end
@ -1055,7 +1089,7 @@ end
--[[ Custom recipes (>3x3) test code --[[ Custom recipes (>3x3) test code
mt.register_craftitem(":secretstuff:custom_recipe_test", { M.register_craftitem(":secretstuff:custom_recipe_test", {
description = "Custom Recipe Test", description = "Custom Recipe Test",
}) })
@ -1069,7 +1103,7 @@ for x = 1, 6 do
end end
end end
mt.register_craft({ M.register_craft({
output = "secretstuff:custom_recipe_test", output = "secretstuff:custom_recipe_test",
recipe = cr[x] recipe = cr[x]
}) })