craftguide/init.lua

1153 lines
25 KiB
Lua
Raw Normal View History

craftguide = {}
2018-12-16 23:20:54 +01:00
2019-02-17 15:04:01 +01:00
local M = minetest
2019-01-08 20:28:53 +01:00
local player_data = {}
2019-02-17 15:04:01 +01:00
-- Caches
local init_items = {}
local searches = {}
2019-01-22 01:44:36 +01:00
local recipes_cache = {}
2019-02-17 15:04:01 +01:00
local usages_cache = {}
local fuel_cache = {}
2018-11-11 22:24:48 +01:00
2019-02-17 15:04:01 +01:00
local progressive_mode = M.settings:get_bool("craftguide_progressive_mode")
local sfinv_only = M.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
2018-11-11 22:24:48 +01:00
2019-02-17 15:04:01 +01:00
local colorize = M.colorize
local reg_items = M.registered_items
local get_result = M.get_craft_result
local show_formspec = M.show_formspec
local get_player_by_name = M.get_player_by_name
local serialize, deserialize = M.serialize, M.deserialize
2019-02-17 15:04:01 +01:00
local ESC = M.formspec_escape
local S = M.get_translator("craftguide")
local maxn, sort, concat, insert = table.maxn, table.sort, table.concat, table.insert
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
local fmt, find, match, sub, split =
string.format, string.find, string.match, string.sub, string.split
local pairs, next, unpack = pairs, next, unpack
local vec_add, vec_mul = vector.add, vector.multiply
2016-12-17 13:15:02 +01:00
2018-04-17 13:30:51 +02:00
local DEFAULT_SIZE = 10
2018-11-18 20:06:45 +01:00
local MIN_LIMIT, MAX_LIMIT = 10, 12
2018-04-17 13:30:51 +02:00
DEFAULT_SIZE = min(MAX_LIMIT, max(MIN_LIMIT, DEFAULT_SIZE))
2018-04-27 22:18:18 +02:00
local GRID_LIMIT = 5
2019-02-17 15:04:01 +01:00
local POLL_FREQ = 0.25
2018-04-27 22:18:18 +02:00
local FMT = {
box = "box[%f,%f;%f,%f;%s]",
label = "label[%f,%f;%s]",
image = "image[%f,%f;%f,%f;%s]",
button = "button[%f,%f;%f,%f;%s;%s]",
tooltip = "tooltip[%s;%s]",
item_image = "item_image[%f,%f;%f,%f;%s]",
image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]",
item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]",
}
2019-01-22 01:59:54 +01:00
local group_stereotypes = {
wool = "wool:white",
dye = "dye:white",
water_bucket = "bucket:bucket_water",
vessel = "vessels:glass_bottle",
coal = "default:coal_lump",
flower = "flowers:dandelion_yellow",
mesecon_conductor_craftable = "mesecons:wire_00000000_off",
}
2019-02-13 01:30:34 +01:00
local item_lists = {
"main",
"craft",
"craftpreview",
}
local function table_merge(t, t2)
2019-02-10 23:07:28 +01:00
t, t2 = t or {}, t2 or {}
local c = #t
2019-02-10 23:07:28 +01:00
for i = 1, #t2 do
c = c + 1
t[c] = t2[i]
end
return t
end
local function table_diff(t, t2)
2019-02-13 14:04:13 +01:00
local hash = {}
for i = 1, #t do
local v = t[i]
2019-02-13 14:04:13 +01:00
hash[v] = true
end
for i = 1, #t2 do
local v = t2[i]
2019-02-13 14:04:13 +01:00
hash[v] = nil
end
local ret, c = {}, 0
for i = 1, #t do
local v = t[i]
2019-02-13 14:04:13 +01:00
if hash[v] then
c = c + 1
ret[c] = v
end
end
return ret
end
2018-12-30 21:32:36 +01:00
local function __func()
return debug.getinfo(2, "n").name
end
local custom_crafts, craft_types = {}, {}
2018-12-16 23:20:54 +01:00
function craftguide.register_craft_type(name, def)
2018-12-30 21:32:36 +01:00
local func = "craftguide." .. __func() .. "(): "
assert(name, func .. "'name' field missing")
assert(def.description, func .. "'description' field missing")
assert(def.icon, func .. "'icon' field missing")
craft_types[name] = def
2018-12-16 23:20:54 +01:00
end
function craftguide.register_craft(def)
2018-12-30 21:32:36 +01:00
local func = "craftguide." .. __func() .. "(): "
assert(def.type, func .. "'type' field missing")
2018-12-30 21:32:36 +01:00
assert(def.width, func .. "'width' field missing")
2019-01-02 18:57:35 +01:00
assert(def.output, func .. "'output' field missing")
assert(def.items, func .. "'items' field missing")
2018-12-30 21:32:36 +01:00
custom_crafts[#custom_crafts + 1] = def
2018-12-16 23:20:54 +01:00
end
local recipe_filters = {}
function craftguide.add_recipe_filter(name, func)
recipe_filters[name] = func
end
function craftguide.remove_recipe_filter(name)
recipe_filters[name] = nil
end
function craftguide.set_recipe_filter(name, func)
recipe_filters = {[name] = func}
end
function craftguide.get_recipe_filters()
return recipe_filters
end
local function apply_recipe_filters(recipes, player)
for _, filter in pairs(recipe_filters) do
recipes = filter(recipes, player)
end
return recipes
end
local formspec_elements = {}
function craftguide.add_formspec_element(name, def)
local func = "craftguide." .. __func() .. "(): "
assert(def.element, func .. "'element' field not defined")
assert(def.type, func .. "'type' field not defined")
assert(FMT[def.type], func .. "'" .. def.type .. "' type not supported by the API")
formspec_elements[name] = {
type = def.type,
element = def.element,
action = def.action,
}
end
function craftguide.remove_formspec_element(name)
formspec_elements[name] = nil
end
function craftguide.get_formspec_elements()
return formspec_elements
end
2019-02-10 23:07:28 +01:00
local function item_has_groups(item_groups, groups)
for i = 1, #groups do
local group = groups[i]
if not item_groups[group] then
return
end
end
return true
end
local function extract_groups(str)
return split(sub(str, 7), ",")
2019-02-10 23:07:28 +01:00
end
local function item_in_recipe(item, recipe)
local item_groups = reg_items[item].groups
for _, recipe_item in pairs(recipe.items) do
if recipe_item == item then
return true
2019-02-17 15:04:01 +01:00
elseif sub(recipe_item, 1, 6) == "group:" then
2019-02-10 23:07:28 +01:00
local groups = extract_groups(recipe_item)
if item_has_groups(item_groups, groups) then
return true
end
end
end
end
local function get_item_usages(item)
local usages, c = {}, 0
for _, recipes in pairs(recipes_cache) do
for i = 1, #recipes do
local recipe = recipes[i]
if item_in_recipe(item, recipe) then
c = c + 1
usages[c] = recipe
end
end
end
if fuel_cache[item] then
usages[#usages + 1] = {type = "fuel", width = 1, items = {item}}
end
return usages
end
local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items
if sub(item, 1, 6) == "group:" then
local groups = extract_groups(item)
for i = 1, inv_items_size do
local inv_item = reg_items[inv_items[i]]
if inv_item then
local item_groups = inv_item.groups
if item_has_groups(item_groups, groups) then
return true
end
end
end
else
for i = 1, inv_items_size do
if inv_items[i] == item then
return true
end
end
end
end
local function get_filtered_items(player)
local name = player:get_player_name()
local data = player_data[name]
2019-02-07 16:08:50 +01:00
local items, c = {}, 0
for i = 1, #init_items do
local item = init_items[i]
local recipes = recipes_cache[item]
local usages = usages_cache[item]
2019-02-13 00:50:26 +01:00
if recipes and #apply_recipe_filters(recipes, player) > 0 or
2019-02-21 01:44:09 +01:00
(usages and (progressive_mode and item_in_inv(item, data.inv_items)) and
#apply_recipe_filters(usages_cache[item], player) > 0) then
2019-02-13 00:50:26 +01:00
c = c + 1
items[c] = item
end
end
return items
end
2019-01-22 01:44:36 +01:00
local function cache_recipes(output)
2019-02-17 15:04:01 +01:00
local recipes = M.get_all_craft_recipes(output) or {}
2019-02-07 16:08:50 +01:00
local c = 0
2019-02-05 14:45:57 +01:00
for i = 1, #custom_crafts do
local custom_craft = custom_crafts[i]
if match(custom_craft.output, "%S*") == output then
2019-02-07 16:08:50 +01:00
c = c + 1
recipes[c] = custom_craft
end
end
2019-01-22 01:44:36 +01:00
if #recipes > 0 then
recipes_cache[output] = recipes
return true
end
end
local function get_recipes(item, data, player)
local recipes = recipes_cache[item]
local usages = usages_cache[item]
if recipes then
recipes = apply_recipe_filters(recipes, player)
end
local no_recipes = not recipes or #recipes == 0
if no_recipes and not usages then
return
elseif usages and no_recipes then
data.show_usages = true
end
if data.show_usages then
2019-02-17 15:04:01 +01:00
recipes = apply_recipe_filters(usages_cache[item], player)
if #recipes == 0 then
return
end
end
return recipes
end
2019-01-22 01:44:36 +01:00
local function get_burntime(item)
return get_result({method = "fuel", width = 1, items = {item}}).time
2018-12-16 23:20:54 +01:00
end
2019-01-22 01:44:36 +01:00
local function cache_fuel(item)
local burntime = get_burntime(item)
if burntime > 0 then
fuel_cache[item] = burntime
return true
end
2018-12-16 23:20:54 +01:00
end
local function groups_to_item(groups)
if #groups == 1 then
local group = groups[1]
2019-02-17 15:04:01 +01:00
local def_gr = "default:" .. group
if group_stereotypes[group] then
return group_stereotypes[group]
2019-02-17 15:04:01 +01:00
elseif reg_items[def_gr] then
return def_gr
end
end
for name, def in pairs(reg_items) do
if item_has_groups(def.groups, groups) then
return name
2016-02-21 00:42:52 +01:00
end
end
return ""
2016-02-21 00:42:52 +01:00
end
2019-01-22 01:44:36 +01:00
local function get_tooltip(item, groups, cooktime, burntime)
local tooltip
2017-03-21 14:26:58 +01:00
if groups then
2019-02-17 15:04:01 +01:00
local groupstr, c = {}, 0
2018-03-08 17:06:53 +01:00
for i = 1, #groups do
2019-02-17 15:04:01 +01:00
c = c + 1
groupstr[c] = colorize("yellow", groups[i])
end
2018-04-17 13:30:51 +02:00
2019-01-22 01:44:36 +01:00
groupstr = concat(groupstr, ", ")
tooltip = S("Any item belonging to the group(s): @1", groupstr)
2019-01-22 01:44:36 +01:00
else
tooltip = reg_items[item].description
end
2017-03-21 14:26:58 +01:00
2019-01-22 01:44:36 +01:00
if cooktime then
2019-02-11 00:22:00 +01:00
tooltip = tooltip .. "\n" ..
S("Cooking time: @1", colorize("yellow", cooktime))
end
2017-03-21 14:26:58 +01:00
2019-01-22 01:44:36 +01:00
if burntime then
2019-02-11 00:22:00 +01:00
tooltip = tooltip .. "\n" ..
S("Burning time: @1", colorize("yellow", burntime))
2016-12-10 17:17:05 +01:00
end
return fmt(FMT.tooltip, item, ESC(tooltip))
end
2019-01-22 01:44:36 +01:00
local function get_recipe_fs(data, iY)
2018-12-30 21:17:21 +01:00
local fs = {}
2019-01-22 01:44:36 +01:00
local recipe = data.recipes[data.rnum]
local width = recipe.width
local xoffset = data.iX / 2.15
local cooktime, shapeless
2018-12-30 21:17:21 +01:00
2019-01-22 01:44:36 +01:00
if recipe.type == "cooking" then
cooktime, width = width, 1
2018-11-17 17:48:47 +01:00
elseif width == 0 then
2019-01-22 01:44:36 +01:00
shapeless = true
width = min(3, #recipe.items)
2018-03-08 17:06:53 +01:00
end
2019-01-22 01:44:36 +01:00
local rows = ceil(maxn(recipe.items) / width)
2019-01-09 21:11:06 +01:00
local rightest, btn_size, s_btn_size = 0, 1.1
2016-12-10 23:15:02 +01:00
if width > GRID_LIMIT or rows > GRID_LIMIT then
fs[#fs + 1] = fmt(FMT.label,
2019-01-22 01:44:36 +01:00
(data.iX / 2) - 2,
2019-01-02 18:57:35 +01:00
iY + 2.2,
2019-02-17 15:04:01 +01:00
ESC(S("Recipe is too big to be displayed (@1x@2)", width, rows)))
2018-12-19 18:36:09 +01:00
return concat(fs)
end
2016-12-17 18:51:04 +01:00
2019-01-22 01:44:36 +01:00
for i, item in pairs(recipe.items) do
local X = ceil((i - 1) % width + xoffset - width) -
(sfinv_only and 0 or 0.2)
local Y = ceil(i / width + (iY + 2) - min(2, rows))
2018-11-18 20:06:45 +01:00
if width > 3 or rows > 3 then
2019-01-09 21:11:06 +01:00
btn_size = width > 3 and 3 / width or 3 / rows
s_btn_size = btn_size
X = btn_size * (i % width) + xoffset - 2.65
Y = btn_size * floor((i - 1) / width) + (iY + 3) - min(2, rows)
2016-12-17 18:51:04 +01:00
end
2018-11-17 17:48:47 +01:00
if X > rightest then
rightest = X
end
local groups
2019-02-17 15:04:01 +01:00
if sub(item, 1, 6) == "group:" then
groups = extract_groups(item)
item = groups_to_item(groups)
end
local label = groups and "\nG" or ""
fs[#fs + 1] = fmt(FMT.item_image_button,
X,
Y + (sfinv_only and 0.7 or 0.2),
2019-01-09 21:11:06 +01:00
btn_size,
btn_size,
item,
match(item, "%S*"),
2019-02-17 15:04:01 +01:00
ESC(label))
2019-01-22 01:44:36 +01:00
local burntime = fuel_cache[item]
if groups or cooktime or burntime then
fs[#fs + 1] = get_tooltip(item, groups, cooktime, burntime)
end
2016-12-10 23:15:02 +01:00
end
2018-03-08 17:06:53 +01:00
local custom_recipe = craft_types[recipe.type]
2019-01-22 01:44:36 +01:00
if custom_recipe or shapeless or recipe.type == "cooking" then
local icon = custom_recipe and custom_recipe.icon or
2019-02-05 14:45:57 +01:00
shapeless and "shapeless" or "furnace"
2019-01-22 01:44:36 +01:00
if not custom_recipe then
2019-02-17 15:04:01 +01:00
icon = fmt("craftguide_%s.png^[resize:16x16", icon)
2019-01-22 01:44:36 +01:00
end
2018-12-16 23:20:54 +01:00
fs[#fs + 1] = fmt(FMT.image,
2019-01-02 18:57:35 +01:00
rightest + 1.2,
iY + (sfinv_only and 2.2 or 1.7),
0.5,
0.5,
2019-01-22 01:44:36 +01:00
icon)
local tooltip = custom_recipe and custom_recipe.description or
shapeless and S("Shapeless") or S("Cooking")
2018-12-19 18:36:09 +01:00
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
2019-01-02 18:57:35 +01:00
rightest + 1.2,
iY + (sfinv_only and 2.2 or 1.7),
0.5,
0.5,
2019-02-17 15:04:01 +01:00
ESC(tooltip))
2018-11-18 20:06:45 +01:00
end
2019-01-09 21:11:06 +01:00
local arrow_X = rightest + (s_btn_size or 1.1)
2018-11-18 20:52:00 +01:00
local output_X = arrow_X + 0.9
fs[#fs + 1] = fmt(FMT.image,
2019-01-02 18:57:35 +01:00
arrow_X,
iY + (sfinv_only and 2.85 or 2.35),
0.9,
0.7,
"craftguide_arrow.png")
2018-11-17 17:48:47 +01:00
2019-01-22 01:44:36 +01:00
if recipe.type == "fuel" then
fs[#fs + 1] = fmt(FMT.image,
2019-01-02 18:57:35 +01:00
output_X,
iY + (sfinv_only and 2.68 or 2.18),
1.1,
1.1,
"craftguide_fire.png")
else
local output_name = match(recipe.output, "%S+")
2019-01-22 01:44:36 +01:00
local burntime = fuel_cache[output_name]
fs[#fs + 1] = fmt(FMT.item_image_button,
2019-01-02 18:57:35 +01:00
output_X,
iY + (sfinv_only and 2.7 or 2.2),
2019-01-09 21:11:06 +01:00
1.1,
1.1,
2019-01-22 01:44:36 +01:00
recipe.output,
ESC(output_name),
"")
2019-01-22 01:44:36 +01:00
if burntime then
fs[#fs + 1] = get_tooltip(output_name, nil, nil, burntime)
fs[#fs + 1] = fmt(FMT.image,
2019-01-22 01:44:36 +01:00
output_X + 1,
iY + (sfinv_only and 2.83 or 2.33),
0.6,
0.4,
"craftguide_arrow.png")
fs[#fs + 1] = fmt(FMT.image,
2019-01-22 01:44:36 +01:00
output_X + 1.6,
iY + (sfinv_only and 2.68 or 2.18),
0.6,
0.6,
"craftguide_fire.png")
end
end
2018-11-17 17:48:47 +01:00
2019-01-22 01:44:36 +01:00
fs[#fs + 1] = fmt("button[%f,%f;%f,%f;%s;%s %u %s %u]",
data.iX - (sfinv_only and 2.2 or 2.6),
iY + (sfinv_only and 3.9 or 3.3),
2.2,
1,
"alternate",
2019-02-17 15:04:01 +01:00
data.show_usages and ESC(S("Usage")) or ESC(S("Recipe")),
2019-01-22 01:44:36 +01:00
data.rnum,
2019-02-17 15:04:01 +01:00
ESC(S("of")),
2019-01-22 01:44:36 +01:00
#data.recipes)
2018-11-17 17:48:47 +01:00
2018-12-19 18:36:09 +01:00
return concat(fs)
2016-12-10 23:15:02 +01:00
end
local function make_formspec(name)
local data = player_data[name]
2018-11-11 22:24:48 +01:00
local iY = sfinv_only and 4 or data.iX - 5
local ipp = data.iX * iY
2016-12-24 21:17:32 +01:00
2016-12-17 23:03:46 +01:00
data.pagemax = max(1, ceil(#data.items / ipp))
2016-12-17 21:16:10 +01:00
2018-12-19 18:36:09 +01:00
local fs = {}
2019-02-05 14:45:57 +01:00
2018-11-11 22:24:48 +01:00
if not sfinv_only then
2019-02-05 14:45:57 +01:00
fs[#fs + 1] = fmt("size[%f,%f;]", data.iX - 0.35, iY + 4)
2019-02-17 15:04:01 +01:00
2019-02-05 14:45:57 +01:00
fs[#fs + 1] = [[
no_prepend[]
background[1,1;1,1;craftguide_bg.png;true]
]]
2019-02-17 15:04:01 +01:00
fs[#fs + 1] = fmt([[ tooltip[size_inc;%s]
tooltip[size_dec;%s] ]],
ESC(S("Increase window size")),
ESC(S("Decrease window size")))
2019-02-05 14:45:57 +01:00
2019-02-17 15:04:01 +01:00
fs[#fs + 1] = fmt([[
image_button[%f,0.12;0.8,0.8;craftguide_zoomin_icon.png;size_inc;]
image_button[%f,0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;] ]],
data.iX * 0.47,
data.iX * 0.47 + 0.6)
2018-11-11 22:24:48 +01:00
end
fs[#fs + 1] = [[
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false]
]]
2019-02-17 15:04:01 +01:00
fs[#fs + 1] = fmt([[ tooltip[search;%s]
tooltip[clear;%s]
tooltip[prev;%s]
tooltip[next;%s] ]],
ESC(S("Search")),
ESC(S("Reset")),
ESC(S("Previous page")),
ESC(S("Next page")))
fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
data.iX - (sfinv_only and 1.7 or 2.2),
0.22,
colorize("yellow", data.pagenum),
data.pagemax)
fs[#fs + 1] = fmt([[
image_button[%f,0.12;0.8,0.8;craftguide_prev_icon.png;prev;]
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] = fmt("field[0.3,0.32;2.5,1;filter;;%s]", ESC(data.filter))
2016-02-21 00:42:52 +01:00
2019-01-22 01:44:36 +01:00
if #data.items == 0 then
local no_item = S("No item to show")
local pos = (data.iX / 2) - 1
if next(recipe_filters) and #init_items > 0 and data.filter == "" then
no_item = S("Collect items to reveal more recipes")
pos = pos - 1
end
fs[#fs + 1] = fmt(FMT.label, pos, 2, ESC(no_item))
end
local first_item = (data.pagenum - 1) * ipp
for i = first_item, first_item + ipp - 1 do
local item = data.items[i + 1]
if not item then
2018-12-30 21:17:21 +01:00
break
end
local X = i % data.iX
local Y = (i % ipp - X) / data.iX + 1
2019-01-15 14:38:15 +01:00
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]",
2019-01-02 18:57:35 +01:00
X - (sfinv_only and 0 or (X * 0.05)),
Y,
2019-01-09 21:11:06 +01:00
1.1,
1.1,
item,
item)
2016-02-21 00:42:52 +01:00
end
2019-01-22 01:44:36 +01:00
if data.recipes and #data.recipes > 0 then
fs[#fs + 1] = get_recipe_fs(data, iY)
2016-02-21 00:42:52 +01:00
end
2016-03-17 01:10:34 +01:00
for elem_name, def in pairs(formspec_elements) do
local element = def.element(data)
if element then
if find(def.type, "button") then
insert(element, #element, elem_name)
end
fs[#fs + 1] = fmt(FMT[def.type], unpack(element))
end
end
2019-01-22 01:44:36 +01:00
return concat(fs)
2016-02-21 00:42:52 +01:00
end
local show_fs = function(player, name)
2018-12-17 01:02:19 +01:00
if sfinv_only then
2019-01-22 01:44:36 +01:00
sfinv.set_player_inventory_formspec(player)
2018-12-17 01:02:19 +01:00
else
show_formspec(name, "craftguide", make_formspec(name))
2018-12-17 01:02:19 +01:00
end
end
local function search(data)
local filter = data.filter
2019-02-05 14:45:57 +01:00
2019-01-08 20:28:53 +01:00
if searches[filter] then
data.items = searches[filter]
return
end
2019-01-11 01:54:17 +01:00
local filtered_list, c = {}, 0
2016-12-17 17:39:22 +01:00
for i = 1, #data.items_raw do
local item = data.items_raw[i]
2019-02-07 16:08:50 +01:00
local desc = reg_items[item].description:lower()
2019-02-17 15:04:01 +01:00
if find(item .. desc, filter, 1, true) then
c = c + 1
filtered_list[c] = item
2016-12-17 17:39:22 +01:00
end
end
2016-12-24 21:17:32 +01:00
if not next(recipe_filters) then
-- Cache the results only if searched 2 times
2019-01-08 20:28:53 +01:00
if searches[filter] == nil then
searches[filter] = false
else
2019-01-08 20:28:53 +01:00
searches[filter] = filtered_list
end
2016-12-24 21:17:32 +01:00
end
2016-12-17 21:16:10 +01:00
data.items = filtered_list
2016-02-21 00:42:52 +01:00
end
local function get_inv_items(player)
2019-01-22 01:44:36 +01:00
local inv = player:get_inventory()
2019-02-13 01:30:34 +01:00
local stacks = {}
for i = 1, #item_lists do
local l = inv:get_list(item_lists[i])
stacks = table_merge(stacks, l)
2019-02-13 01:30:34 +01:00
end
2019-02-07 16:08:50 +01:00
local inv_items, c = {}, 0
2019-01-22 01:44:36 +01:00
for i = 1, #stacks do
local stack = stacks[i]
if not stack:is_empty() then
2019-01-22 01:44:36 +01:00
local name = stack:get_name()
if reg_items[name] then
2019-02-07 16:08:50 +01:00
c = c + 1
inv_items[c] = name
2019-01-22 01:44:36 +01:00
end
end
end
return inv_items
end
2019-02-07 04:59:38 +01:00
local function init_data(name)
player_data[name] = {
filter = "",
pagenum = 1,
iX = sfinv_only and 8 or DEFAULT_SIZE,
2019-01-31 01:10:09 +01:00
items = init_items,
items_raw = init_items,
}
end
local function reset_data(data)
2019-01-22 01:44:36 +01:00
data.filter = ""
data.pagenum = 1
2019-02-06 01:51:09 +01:00
data.rnum = 1
2019-01-22 01:44:36 +01:00
data.query_item = nil
data.show_usages = nil
data.recipes = nil
data.items = data.items_raw
end
2019-02-17 15:04:01 +01:00
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 c = 0
for name, def in pairs(reg_items) do
2019-01-22 01:44:36 +01:00
local is_fuel = cache_fuel(name)
if not (def.groups.not_in_craft_guide == 1 or
def.groups.not_in_creative_inventory == 1) and
2019-01-22 01:44:36 +01:00
def.description and def.description ~= "" and
(cache_recipes(name) or is_fuel) then
c = c + 1
init_items[c] = name
end
end
sort(init_items)
2019-02-17 15:04:01 +01:00
cache_usages()
end
2019-01-22 01:44:36 +01:00
local function on_receive_fields(player, fields)
local name = player:get_player_name()
local data = player_data[name]
2016-02-21 00:42:52 +01:00
for elem_name, def in pairs(formspec_elements) do
if fields[elem_name] and def.action then
return def.action(player, data)
end
end
2016-02-21 00:42:52 +01:00
if fields.clear then
2019-01-08 20:28:53 +01:00
reset_data(data)
show_fs(player, name)
2017-03-21 14:26:58 +01:00
2016-02-21 00:42:52 +01:00
elseif fields.alternate then
2019-01-22 01:44:36 +01:00
if #data.recipes == 1 then
2018-12-30 21:00:04 +01:00
return
end
2019-01-22 01:44:36 +01:00
local num_next = data.rnum + 1
data.rnum = data.recipes[num_next] and num_next or 1
show_fs(player, name)
2017-03-21 14:26:58 +01:00
2016-12-24 03:27:41 +01:00
elseif (fields.key_enter_field == "filter" or fields.search) and
fields.filter ~= "" then
2019-01-11 01:54:17 +01:00
local fltr = fields.filter:lower()
if data.filter == fltr then
2019-01-11 01:54:17 +01:00
return
end
data.filter = fltr
data.pagenum = 1
search(data)
show_fs(player, name)
2017-03-21 14:26:58 +01:00
2016-02-21 00:42:52 +01:00
elseif fields.prev or fields.next then
2019-01-22 01:44:36 +01:00
if data.pagemax == 1 then
return
end
data.pagenum = data.pagenum - (fields.prev and 1 or -1)
2019-02-06 01:51:09 +01:00
2016-12-17 23:03:46 +01:00
if data.pagenum > data.pagemax then
data.pagenum = 1
elseif data.pagenum == 0 then
2016-12-17 23:03:46 +01:00
data.pagenum = data.pagemax
end
show_fs(player, name)
2017-03-21 14:26:58 +01:00
elseif (fields.size_inc and data.iX < MAX_LIMIT) or
(fields.size_dec and data.iX > MIN_LIMIT) then
data.pagenum = 1
2019-01-22 01:44:36 +01:00
data.iX = data.iX + (fields.size_inc and 1 or -1)
show_fs(player, name)
2017-03-21 14:26:58 +01:00
2019-01-22 01:44:36 +01:00
else
local item
for field in pairs(fields) do
2019-02-17 15:04:01 +01:00
if find(field, ":") then
2019-01-22 01:44:36 +01:00
item = field
break
2019-01-15 14:38:15 +01:00
end
2019-01-22 01:44:36 +01:00
end
2019-01-15 14:38:15 +01:00
2019-01-22 01:44:36 +01:00
if not item then
return
2019-02-17 15:04:01 +01:00
elseif sub(item, -4) == "_inv" then
item = sub(item, 1, -5)
2019-01-22 01:44:36 +01:00
end
2019-01-15 14:38:15 +01:00
2019-01-22 01:44:36 +01:00
if item ~= data.query_item then
data.show_usages = nil
else
data.show_usages = not data.show_usages
end
2019-02-13 14:04:13 +01:00
local recipes = get_recipes(item, data, player)
if not recipes then
return
end
2019-01-22 01:44:36 +01:00
data.query_item = item
2019-02-13 14:04:13 +01:00
data.recipes = recipes
2019-02-06 01:51:09 +01:00
data.rnum = 1
show_fs(player, name)
2016-02-21 00:42:52 +01:00
end
2018-11-11 22:24:48 +01:00
end
2016-02-21 00:42:52 +01:00
2019-02-17 15:04:01 +01:00
M.register_on_mods_loaded(get_init_items)
2019-02-17 15:04:01 +01:00
M.register_on_joinplayer(function(player)
local name = player:get_player_name()
2019-02-07 04:59:38 +01:00
init_data(name)
end)
2019-02-17 15:04:01 +01:00
M.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_data[name] = nil
end)
2018-11-11 22:24:48 +01:00
if sfinv_only then
sfinv.register_page("craftguide:craftguide", {
title = S("Craft Guide"),
2019-01-02 18:57:35 +01:00
2018-11-11 22:24:48 +01:00
get = function(self, player, context)
local name = player:get_player_name()
local formspec = make_formspec(name)
2019-01-22 01:44:36 +01:00
return sfinv.make_formspec(player, context, formspec)
2018-11-11 22:24:48 +01:00
end,
2019-01-02 18:57:35 +01:00
2018-11-11 22:24:48 +01:00
on_enter = function(self, player, context)
if next(recipe_filters) then
2019-02-07 04:59:38 +01:00
local name = player:get_player_name()
local data = player_data[name]
data.items_raw = get_filtered_items(player)
search(data)
2018-11-11 22:24:48 +01:00
end
end,
2019-01-02 18:57:35 +01:00
2018-11-11 22:24:48 +01:00
on_player_receive_fields = function(self, player, context, fields)
2019-01-22 01:44:36 +01:00
on_receive_fields(player, fields)
2018-11-11 22:24:48 +01:00
end,
})
else
2019-02-17 15:04:01 +01:00
M.register_on_player_receive_fields(function(player, formname, fields)
2019-01-22 01:44:36 +01:00
if formname == "craftguide" then
on_receive_fields(player, fields)
end
end)
2018-11-11 22:24:48 +01:00
2019-01-22 01:44:36 +01:00
local function on_use(user)
local name = user:get_player_name()
2017-01-30 05:24:03 +01:00
if next(recipe_filters) then
2019-02-07 04:59:38 +01:00
local data = player_data[name]
data.items_raw = get_filtered_items(user)
search(data)
2018-11-11 22:24:48 +01:00
end
show_formspec(name, "craftguide", make_formspec(name))
2016-02-21 00:42:52 +01:00
end
2019-02-17 15:04:01 +01:00
M.register_craftitem("craftguide:book", {
2018-11-11 22:24:48 +01:00
description = S("Crafting Guide"),
inventory_image = "craftguide_book.png",
wield_image = "craftguide_book.png",
stack_max = 1,
groups = {book = 1},
on_use = function(itemstack, user)
2019-01-22 01:44:36 +01:00
on_use(user)
2018-11-11 22:24:48 +01:00
end
})
2016-02-21 00:42:52 +01:00
2019-02-17 15:04:01 +01:00
M.register_node("craftguide:sign", {
2018-11-11 22:24:48 +01:00
description = S("Crafting Guide Sign"),
drawtype = "nodebox",
tiles = {"craftguide_sign.png"},
inventory_image = "craftguide_sign_inv.png",
wield_image = "craftguide_sign_inv.png",
paramtype = "light",
paramtype2 = "wallmounted",
sunlight_propagates = true,
2019-01-18 15:56:52 +01:00
groups = {oddly_breakable_by_hand = 1, flammable = 3},
2018-11-11 22:24:48 +01:00
node_box = {
type = "wallmounted",
wall_top = {-0.4375, 0.4375, -0.3125, 0.4375, 0.5, 0.3125},
wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125},
wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375}
},
2019-01-02 18:57:35 +01:00
2018-11-11 22:24:48 +01:00
on_construct = function(pos)
2019-02-17 15:04:01 +01:00
local meta = M.get_meta(pos)
2018-11-11 22:24:48 +01:00
meta:set_string("infotext", S("Crafting Guide Sign"))
end,
2019-01-02 18:57:35 +01:00
2018-11-11 22:24:48 +01:00
on_rightclick = function(pos, node, user, itemstack)
2019-01-22 01:44:36 +01:00
on_use(user)
2018-11-11 22:24:48 +01:00
end
})
2016-12-13 17:30:43 +01:00
2019-02-17 15:04:01 +01:00
M.register_craft({
2018-11-11 22:24:48 +01:00
output = "craftguide:book",
2019-02-07 04:59:38 +01:00
recipe = {
{"default:book"}
}
2018-11-11 22:24:48 +01:00
})
2017-01-30 05:24:03 +01:00
2019-02-17 15:04:01 +01:00
M.register_craft({
2018-11-11 22:24:48 +01:00
type = "fuel",
recipe = "craftguide:book",
burntime = 3
})
2019-02-17 15:04:01 +01:00
M.register_craft({
2018-11-11 22:24:48 +01:00
output = "craftguide:sign",
2019-02-07 04:59:38 +01:00
recipe = {
{"default:sign_wall_wood"}
}
2018-11-11 22:24:48 +01:00
})
2019-02-17 15:04:01 +01:00
M.register_craft({
2018-11-11 22:24:48 +01:00
type = "fuel",
recipe = "craftguide:sign",
burntime = 10
})
2017-01-30 05:24:03 +01:00
2018-12-17 01:02:19 +01:00
if rawget(_G, "sfinv_buttons") then
sfinv_buttons.register_button("craftguide", {
title = S("Crafting Guide"),
tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"),
2019-01-02 18:57:35 +01:00
image = "craftguide_book.png",
2018-12-17 01:02:19 +01:00
action = function(player)
2019-01-22 01:44:36 +01:00
on_use(player)
2018-12-17 01:02:19 +01:00
end,
})
end
2017-03-19 21:12:49 +01:00
end
if progressive_mode then
local function recipe_in_inv(recipe, inv_items)
for _, item in pairs(recipe.items) do
if not item_in_inv(item, inv_items) then
return
end
end
return true
end
local function progressive_filter(recipes, player)
local name = player:get_player_name()
local data = player_data[name]
if #data.inv_items == 0 then
return {}
end
2019-02-07 16:08:50 +01:00
local filtered, c = {}, 0
for i = 1, #recipes do
local recipe = recipes[i]
if recipe_in_inv(recipe, data.inv_items) then
2019-02-07 16:08:50 +01:00
c = c + 1
filtered[c] = recipe
end
end
return filtered
end
2019-02-13 14:32:36 +01:00
-- Workaround. Need an engine call to detect when the contents
-- of the player inventory changed, instead.
2019-02-16 01:52:01 +01:00
local function poll_new_items()
2019-02-17 15:04:01 +01:00
local players = M.get_connected_players()
for i = 1, #players do
local player = players[i]
local name = player:get_player_name()
local data = player_data[name]
local inv_items = get_inv_items(player)
local diff = table_diff(inv_items, data.inv_items)
if #diff > 0 then
data.inv_items = table_merge(diff, data.inv_items)
end
end
2019-02-16 01:52:01 +01:00
2019-02-17 15:04:01 +01:00
M.after(POLL_FREQ, poll_new_items)
2019-02-16 01:52:01 +01:00
end
poll_new_items()
craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
2019-02-17 15:04:01 +01:00
M.register_on_joinplayer(function(player)
local meta = player:get_meta()
local name = player:get_player_name()
local data = player_data[name]
data.inv_items = deserialize(meta:get_string("inv_items")) or {}
end)
local function save_meta(player)
local meta = player:get_meta()
local name = player:get_player_name()
local data = player_data[name]
meta:set_string("inv_items", serialize(data.inv_items))
end
2019-02-17 15:04:01 +01:00
M.register_on_leaveplayer(save_meta)
2019-02-17 15:04:01 +01:00
M.register_on_shutdown(function()
local players = M.get_connected_players()
for i = 1, #players do
local player = players[i]
save_meta(player)
end
end)
end
2019-02-06 01:51:09 +01:00
2019-02-17 15:04:01 +01:00
M.register_chatcommand("craft", {
description = S("Show recipe(s) of the pointed node"),
func = function(name)
local player = get_player_by_name(name)
local ppos = player:get_pos()
local dir = player:get_look_dir()
local eye_h = {x = ppos.x, y = ppos.y + 1.625, z = ppos.z}
local node_name
for i = 1, 10 do
local look_at = vec_add(eye_h, vec_mul(dir, i))
2019-02-17 15:04:01 +01:00
local node = M.get_node(look_at)
if node.name ~= "air" then
node_name = node.name
break
2019-02-06 01:51:09 +01:00
end
end
2019-02-06 01:51:09 +01:00
2019-02-11 00:22:00 +01:00
local red = colorize("red", "[craftguide] ")
2019-02-10 20:37:24 +01:00
if not node_name then
2019-02-10 20:37:24 +01:00
return false, red .. S("No node pointed")
end
local data = player_data[name]
reset_data(data)
local recipes = recipes_cache[node_name]
local usages = usages_cache[node_name]
if recipes then
recipes = apply_recipe_filters(recipes, player)
end
if not recipes or #recipes == 0 then
2019-02-11 00:22:00 +01:00
local ylw = colorize("yellow", node_name)
2019-02-10 20:37:24 +01:00
local msg = red .. "%s: " .. ylw
if usages then
2019-02-17 15:04:01 +01:00
recipes = usages_cache[node_name]
if #recipes > 0 then
data.show_usages = true
2019-02-06 01:51:09 +01:00
end
2019-02-10 20:37:24 +01:00
elseif recipes_cache[node_name] then
return false, fmt(msg, S("You don't know a recipe for this node"))
else
2019-02-10 23:07:28 +01:00
return false, fmt(msg, S("No recipe for this node"))
2019-02-06 01:51:09 +01:00
end
end
2019-02-06 01:51:09 +01:00
data.query_item = node_name
data.recipes = recipes
2019-02-06 01:51:09 +01:00
return true, show_fs(player, name)
end,
})
function craftguide.show(name, item, show_usages)
local func = "craftguide." .. __func() .. "(): "
assert(name, func .. "player name missing")
local data = player_data[name]
local player = get_player_by_name(name)
local query_item = data.query_item
reset_data(data)
item = reg_items[item] and item or query_item
data.query_item = item
data.show_usages = show_usages
data.recipes = get_recipes(item, data, player)
show_fs(player, name)
end
2018-04-27 22:18:18 +02:00
--[[ Custom recipes (>3x3) test code
2019-02-17 15:04:01 +01:00
M.register_craftitem(":secretstuff:custom_recipe_test", {
2018-04-27 22:18:18 +02:00
description = "Custom Recipe Test",
})
local cr = {}
for x = 1, 6 do
cr[x] = {}
for i = 1, 10 - x do
cr[x][i] = {}
for j = 1, 10 - x do
cr[x][i][j] = "group:sand"
2018-04-27 22:18:18 +02:00
end
end
2019-02-17 15:04:01 +01:00
M.register_craft({
output = "secretstuff:custom_recipe_test",
2018-04-27 22:18:18 +02:00
recipe = cr[x]
})
end
]]