Compare commits

...

2 Commits

2 changed files with 107 additions and 149 deletions

View File

@ -11,7 +11,6 @@ local usages_cache = {}
local fuel_cache = {} local fuel_cache = {}
local progressive_mode = M.settings:get_bool("mcl_craftguide_progressive_mode", true) local progressive_mode = M.settings:get_bool("mcl_craftguide_progressive_mode", true)
local sfinv_only = false
local colorize = M.colorize local colorize = M.colorize
local reg_items = M.registered_items local reg_items = M.registered_items
@ -24,11 +23,11 @@ local ESC = M.formspec_escape
local S = M.get_translator("mcl_craftguide") local S = M.get_translator("mcl_craftguide")
local maxn, sort, concat, insert, copy = local maxn, sort, concat, insert, copy =
table.maxn, table.sort, table.concat, table.insert, table.maxn, table.sort, table.concat, table.insert,
table.copy table.copy
local fmt, find, gmatch, match, sub, split, lower = local fmt, find, gmatch, match, sub, split, lower =
string.format, string.find, string.gmatch, string.match, string.format, string.find, string.gmatch, string.match,
string.sub, string.split, string.lower string.sub, string.split, string.lower
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
@ -42,38 +41,38 @@ local GRID_LIMIT = 5
local POLL_FREQ = 0.25 local POLL_FREQ = 0.25
local FMT = { local FMT = {
box = "box[%f,%f;%f,%f;%s]", box = "box[%f,%f;%f,%f;%s]",
label = "label[%f,%f;%s]", label = "label[%f,%f;%s]",
image = "image[%f,%f;%f,%f;%s]", image = "image[%f,%f;%f,%f;%s]",
button = "button[%f,%f;%f,%f;%s;%s]", button = "button[%f,%f;%f,%f;%s;%s]",
tooltip = "tooltip[%s;%s]", tooltip = "tooltip[%s;%s]",
item_image = "item_image[%f,%f;%f,%f;%s]", item_image = "item_image[%f,%f;%f,%f;%s]",
image_button = "image_button[%f,%f;%f,%f;%s;%s;%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]", item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]",
} }
local group_stereotypes = { local group_stereotypes = {
wood = "mcl_core:wood", wood = "mcl_core:wood",
stone = "mcl_core:stone", stone = "mcl_core:stone",
sand = "mcl_core:sand", sand = "mcl_core:sand",
wool = "mcl_wool:white", wool = "mcl_wool:white",
carpet = "mcl_wool:white_carpet", carpet = "mcl_wool:white_carpet",
dye = "mcl_dye:red", dye = "mcl_dye:red",
water_bucket = "mcl_buckets:bucket_water", water_bucket = "mcl_buckets:bucket_water",
flower = "mcl_flowers:dandelion", flower = "mcl_flowers:dandelion",
mushroom = "mcl_mushrooms:mushroom_brown", mushroom = "mcl_mushrooms:mushroom_brown",
wood_slab = "mcl_stairs:slab_wood", wood_slab = "mcl_stairs:slab_wood",
wood_stairs = "mcl_stairs:stairs_wood", wood_stairs = "mcl_stairs:stairs_wood",
coal = "mcl_core:coal_lump", coal = "mcl_core:coal_lump",
shulker_box = "mcl_chests:violet_shulker_box", shulker_box = "mcl_chests:violet_shulker_box",
quartz_block = "mcl_nether:quartz_block", quartz_block = "mcl_nether:quartz_block",
banner = "mcl_banners:banner_item_white", banner = "mcl_banners:banner_item_white",
mesecon_conductor_craftable = "mesecons:wire_00000000_off", mesecon_conductor_craftable = "mesecons:wire_00000000_off",
purpur_block = "mcl_end:purpur_block", purpur_block = "mcl_end:purpur_block",
normal_sandstone = "mcl_core:sandstone", normal_sandstone = "mcl_core:sandstone",
red_sandstone = "mcl_core:redsandstone", red_sandstone = "mcl_core:redsandstone",
compass = mcl_compass.stereotype, compass = mcl_compass.stereotype,
clock = mcl_clock.sterotype, clock = mcl_clock.sterotype,
} }
local group_names = { local group_names = {
@ -192,7 +191,7 @@ function mcl_craftguide.set_recipe_filter(name, f)
assert(name, func .. "filter name missing") assert(name, func .. "filter name missing")
assert(f and type(f) == "function", func .. "filter function missing") assert(f and type(f) == "function", func .. "filter function missing")
recipe_filters = {[name] = f} recipe_filters = { [name] = f }
end end
function mcl_craftguide.get_recipe_filters() function mcl_craftguide.get_recipe_filters()
@ -289,23 +288,23 @@ local function get_item_usages(item)
local usages, c = {}, 0 local usages, c = {}, 0
for _, recipes in pairs(recipes_cache) do for _, recipes in pairs(recipes_cache) do
for i = 1, #recipes do for i = 1, #recipes do
local recipe = recipes[i] local recipe = recipes[i]
if item_in_recipe(item, recipe) then if item_in_recipe(item, recipe) then
c = c + 1
usages[c] = recipe
else
recipe = groups_item_in_recipe(item, recipe)
if recipe then
c = c + 1 c = c + 1
usages[c] = recipe usages[c] = recipe
else
recipe = groups_item_in_recipe(item, recipe)
if recipe then
c = c + 1
usages[c] = recipe
end
end end
end end
end end
end
if fuel_cache[item] then if fuel_cache[item] then
usages[#usages + 1] = {type = "fuel", width = 1, items = {item}} usages[#usages + 1] = { type = "fuel", width = 1, items = { item } }
end end
return usages return usages
@ -320,7 +319,7 @@ local function get_filtered_items(player)
local usages = usages_cache[item] local usages = usages_cache[item]
if recipes and #apply_recipe_filters(recipes, player) > 0 or if recipes and #apply_recipe_filters(recipes, player) > 0 or
usages and #apply_recipe_filters(usages, player) > 0 then usages and #apply_recipe_filters(usages, player) > 0 then
c = c + 1 c = c + 1
items[c] = item items[c] = item
end end
@ -373,7 +372,7 @@ local function get_recipes(item, data, player)
end end
local function get_burntime(item) local function get_burntime(item)
return get_result({method = "fuel", width = 1, items = {item}}).time return get_result({ method = "fuel", width = 1, items = { item } }).time
end end
local function cache_fuel(item) local function cache_fuel(item)
@ -430,13 +429,13 @@ local function get_tooltip(item, groups, cooktime, burntime)
tooltip = groupstr tooltip = groupstr
else else
local groupstr, c = {}, 0 local grouptable, c = {}, 0
for i = 1, #groups do for i = 1, #groups do
c = c + 1 c = c + 1
groupstr[c] = colorize(gcol, groups[i]) grouptable[c] = colorize(gcol, groups[i])
end end
groupstr = concat(groupstr, ", ") groupstr = concat(grouptable, ", ")
tooltip = S("Any item belonging to the groups: @1", groupstr) tooltip = S("Any item belonging to the groups: @1", groupstr)
end end
else else
@ -475,19 +474,13 @@ local function get_recipe_fs(data, iY)
end end
local rows = ceil(maxn(recipe.items) / width) local rows = ceil(maxn(recipe.items) / width)
local rightest, btn_size, s_btn_size = 0, 1.1 local rightest, btn_size, s_btn_size = 0, 1.1, nil
local btn_lab = data.show_usages and local btn_lab = data.show_usages and
ESC(S("Usage @1 of @2", data.rnum, #data.recipes)) or ESC(S("Usage @1 of @2", data.rnum, #data.recipes)) or
ESC(S("Recipe @1 of @2", data.rnum, #data.recipes)) ESC(S("Recipe @1 of @2", data.rnum, #data.recipes))
fs[#fs + 1] = fmt(FMT.button, fs[#fs + 1] = fmt(FMT.button, data.iX - 2.6, iY + 3.3, 2.2, 1, "alternate", btn_lab)
sfinv_only and 5.8 or data.iX - 2.6,
sfinv_only and 7.9 or iY + 3.3,
2.2,
1,
"alternate",
btn_lab)
if width > GRID_LIMIT or rows > GRID_LIMIT then if width > GRID_LIMIT or rows > GRID_LIMIT then
fs[#fs + 1] = fmt(FMT.label, fs[#fs + 1] = fmt(FMT.label,
@ -499,8 +492,7 @@ local function get_recipe_fs(data, iY)
end end
for i, item in pairs(recipe.items) do for i, item in pairs(recipe.items) do
local X = ceil((i - 1) % width + xoffset - width) - local X = ceil((i - 1) % width + xoffset - width) - 0.2
(sfinv_only and 0 or 0.2)
local Y = ceil(i / width + (iY + 2) - min(2, rows)) local Y = ceil(i / width + (iY + 2) - min(2, rows))
if width > 3 or rows > 3 then if width > 3 or rows > 3 then
@ -527,7 +519,7 @@ local function get_recipe_fs(data, iY)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, X,
Y + (sfinv_only and 0.7 or 0.2), Y + 0.2,
btn_size, btn_size,
btn_size, btn_size,
item, item,
@ -545,7 +537,7 @@ local function get_recipe_fs(data, iY)
if custom_recipe or shapeless or recipe.type == "cooking" then if custom_recipe or shapeless or recipe.type == "cooking" then
local icon = custom_recipe and custom_recipe.icon or local icon = custom_recipe and custom_recipe.icon or
shapeless and "shapeless" or "furnace" shapeless and "shapeless" or "furnace"
if recipe.type == "cooking" then if recipe.type == "cooking" then
icon = "craftguide_furnace.png" icon = "craftguide_furnace.png"
@ -555,17 +547,17 @@ local function get_recipe_fs(data, iY)
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
rightest + 1.2, rightest + 1.2,
sfinv_only and 6.2 or iY + 1.7, iY + 1.7,
0.5, 0.5,
0.5, 0.5,
icon) icon)
local tooltip = custom_recipe and custom_recipe.description or local tooltip = custom_recipe and custom_recipe.description or
shapeless and S("Shapeless") or S("Cooking") shapeless and S("Shapeless") or S("Cooking")
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]", fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
rightest + 1.2, rightest + 1.2,
sfinv_only and 6.2 or iY + 1.7, iY + 1.7,
0.5, 0.5,
0.5, 0.5,
ESC(tooltip)) ESC(tooltip))
@ -576,7 +568,7 @@ local function get_recipe_fs(data, iY)
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
arrow_X, arrow_X,
sfinv_only and 6.85 or iY + 2.35, iY + 2.35,
0.9, 0.9,
0.7, 0.7,
"craftguide_arrow.png") "craftguide_arrow.png")
@ -584,7 +576,7 @@ local function get_recipe_fs(data, iY)
if recipe.type == "fuel" then if recipe.type == "fuel" then
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
output_X, output_X,
sfinv_only and 6.68 or iY + 2.18, iY + 2.18,
1.1, 1.1,
1.1, 1.1,
"mcl_craftguide_fuel.png") "mcl_craftguide_fuel.png")
@ -594,7 +586,7 @@ local function get_recipe_fs(data, iY)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, output_X,
sfinv_only and 6.7 or iY + 2.2, iY + 2.2,
1.1, 1.1,
1.1, 1.1,
recipe.output, recipe.output,
@ -606,14 +598,14 @@ local function get_recipe_fs(data, iY)
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
output_X + 1, output_X + 1,
sfinv_only and 6.83 or iY + 2.33, iY + 2.33,
0.6, 0.6,
0.4, 0.4,
"craftguide_arrow.png") "craftguide_arrow.png")
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
output_X + 1.6, output_X + 1.6,
sfinv_only and 6.68 or iY + 2.18, iY + 2.18,
0.6, 0.6,
0.6, 0.6,
"mcl_craftguide_fuel.png") "mcl_craftguide_fuel.png")
@ -625,29 +617,27 @@ end
local function make_formspec(name) local function make_formspec(name)
local data = player_data[name] local data = player_data[name]
local iY = sfinv_only and 4 or data.iX - 5 local iY = data.iX - 5
local ipp = data.iX * iY local ipp = data.iX * iY
data.pagemax = max(1, ceil(#data.items / ipp)) data.pagemax = max(1, ceil(#data.items / ipp))
local fs = {} local fs = {}
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] = "background9[1,1;1,1;mcl_base_textures_background9.png;true;7]" fs[#fs + 1] = "background9[1,1;1,1;mcl_base_textures_background9.png;true;7]"
fs[#fs + 1] = fmt([[ tooltip[size_inc;%s] fs[#fs + 1] = fmt([[ tooltip[size_inc;%s]
tooltip[size_dec;%s] ]], tooltip[size_dec;%s] ]],
ESC(S("Increase window size")), ESC(S("Increase window size")),
ESC(S("Decrease window size"))) ESC(S("Decrease window size")))
fs[#fs + 1] = fmt([[ 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_zoomin_icon.png;size_inc;]
image_button[%f,0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;] ]], image_button[%f,0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;] ]],
data.iX * 0.47, data.iX * 0.47,
data.iX * 0.47 + 0.6) data.iX * 0.47 + 0.6)
end
fs[#fs + 1] = [[ fs[#fs + 1] = [[
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;] image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
@ -665,15 +655,15 @@ local function make_formspec(name)
ESC(S("Next page"))) ESC(S("Next page")))
fs[#fs + 1] = fmt("label[%f,%f;%s]", fs[#fs + 1] = fmt("label[%f,%f;%s]",
sfinv_only and 6.3 or data.iX - 2.2, data.iX - 2.2,
0.22, 0.22,
ESC(colorize("#383838", fmt("%s / %u", data.pagenum, data.pagemax)))) ESC(colorize("#383838", fmt("%s / %u", data.pagenum, data.pagemax))))
fs[#fs + 1] = fmt([[ 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_prev_icon.png;prev;]
image_button[%f,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;] ]],
sfinv_only and 5.5 or data.iX - 3.1, data.iX - 3.1,
sfinv_only and 7.3 or (data.iX - 1.2) - (data.iX >= 11 and 0.08 or 0)) (data.iX - 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)) fs[#fs + 1] = fmt("field[0.3,0.32;2.5,1;filter;;%s]", ESC(data.filter))
@ -700,7 +690,7 @@ local function make_formspec(name)
local Y = (i % ipp - X) / data.iX + 1 local Y = (i % ipp - X) / data.iX + 1
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]",
X - (sfinv_only and 0 or (X * 0.05)), X - (X * 0.05),
Y, Y,
1.1, 1.1,
1.1, 1.1,
@ -727,11 +717,7 @@ local function make_formspec(name)
end end
local function show_fs(player, name) local function show_fs(player, name)
if sfinv_only then show_formspec(name, "mcl_craftguide", make_formspec(name))
sfinv.set_player_inventory_formspec(player)
else
show_formspec(name, "mcl_craftguide", make_formspec(name))
end
end end
mcl_craftguide.add_search_filter("groups", function(item, groups) mcl_craftguide.add_search_filter("groups", function(item, groups)
@ -741,7 +727,7 @@ mcl_craftguide.add_search_filter("groups", function(item, groups)
for i = 1, #groups do for i = 1, #groups do
local group = groups[i] local group = groups[i]
if not itemdef.groups[group] then if not itemdef.groups[group] then
has_groups = nil has_groups = false
break break
end end
end end
@ -772,9 +758,9 @@ local function search(data)
end end
for i = 1, #data.items_raw do for i = 1, #data.items_raw do
local item = data.items_raw[i] local item = data.items_raw[i]
local def = reg_items[item] local def = reg_items[item]
local desc = lower(def.description) local desc = lower(def.description)
local search_in = item .. desc local search_in = item .. desc
local to_add local to_add
@ -833,10 +819,10 @@ end
local function init_data(name) local function init_data(name)
player_data[name] = { player_data[name] = {
filter = "", filter = "",
pagenum = 1, pagenum = 1,
iX = sfinv_only and 8 or DEFAULT_SIZE, iX = DEFAULT_SIZE,
items = init_items, items = init_items,
items_raw = init_items, items_raw = init_items,
} }
end end
@ -863,8 +849,8 @@ local function get_init_items()
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
local is_fuel = cache_fuel(name) local is_fuel = cache_fuel(name)
if not (def.groups.not_in_craft_guide == 1) and if not (def.groups.not_in_craft_guide == 1) and
def.description and def.description ~= "" and def.description and def.description ~= "" and
(cache_recipes(name) or is_fuel) then (cache_recipes(name) or is_fuel) then
c = c + 1 c = c + 1
init_items[c] = name init_items[c] = name
end end
@ -898,7 +884,7 @@ local function on_receive_fields(player, fields)
show_fs(player, name) show_fs(player, name)
elseif (fields.key_enter_field == "filter" or fields.search) and elseif (fields.key_enter_field == "filter" or fields.search) and
fields.filter ~= "" then fields.filter ~= "" then
local fltr = lower(fields.filter) local fltr = lower(fields.filter)
if data.filter == fltr then if data.filter == fltr then
return return
@ -925,7 +911,7 @@ local function on_receive_fields(player, fields)
show_fs(player, name) show_fs(player, name)
elseif (fields.size_inc and data.iX < MAX_LIMIT) or elseif (fields.size_inc and data.iX < MAX_LIMIT) or
(fields.size_dec and data.iX > MIN_LIMIT) then (fields.size_dec and data.iX > MIN_LIMIT) then
data.pagenum = 1 data.pagenum = 1
data.iX = data.iX + (fields.size_inc and 1 or -1) data.iX = data.iX + (fields.size_inc and 1 or -1)
show_fs(player, name) show_fs(player, name)
@ -965,54 +951,26 @@ end
M.register_on_mods_loaded(get_init_items) M.register_on_mods_loaded(get_init_items)
-- TODO: Remove sfinv support M.register_on_player_receive_fields(function(player, formname, fields)
if sfinv_only then if formname == "mcl_craftguide" then
sfinv.register_page("craftguide:craftguide", { on_receive_fields(player, fields)
title = "Craft Guide", elseif fields.__mcl_craftguide then
mcl_craftguide.show(player:get_player_name())
end
end)
get = function(self, player, context) --[[local function on_use(user)
local name = player:get_player_name() local name = user:get_player_name()
local formspec = make_formspec(name)
return sfinv.make_formspec(player, context, formspec) if next(recipe_filters) then
end, local data = player_data[name]
data.items_raw = get_filtered_items(user)
search(data)
end
on_enter = function(self, player, context) show_formspec(name, "mcl_craftguide", make_formspec(name))
if next(recipe_filters) then end]]
local name = player:get_player_name()
local data = player_data[name]
data.items_raw = get_filtered_items(player)
search(data)
end
end,
on_player_receive_fields = function(self, player, context, fields)
on_receive_fields(player, fields)
end,
})
else
M.register_on_player_receive_fields(function(player, formname, fields)
if formname == "mcl_craftguide" then
on_receive_fields(player, fields)
elseif fields.__mcl_craftguide then
mcl_craftguide.show(player:get_player_name())
end
end)
--[[local function on_use(user)
local name = user:get_player_name()
if next(recipe_filters) then
local data = player_data[name]
data.items_raw = get_filtered_items(user)
search(data)
end
show_formspec(name, "mcl_craftguide", make_formspec(name))
end]]
end
if progressive_mode then if progressive_mode then
local function item_in_inv(item, inv_items) local function item_in_inv(item, inv_items)
@ -1073,9 +1031,9 @@ if progressive_mode then
local function poll_new_items() local function poll_new_items()
local players = M.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()
local data = player_data[name] local data = player_data[name]
local inv_items = get_inv_items(player) local inv_items = get_inv_items(player)
local diff = table_diff(inv_items, data.inv_items) local diff = table_diff(inv_items, data.inv_items)

View File

@ -2,4 +2,4 @@ name = mcl_craftguide
author = kilbith author = kilbith
description = The most comprehensive Crafting Guide on Minetest. description = The most comprehensive Crafting Guide on Minetest.
depends = mcl_core, mcl_compass, mcl_clock, doc, mcl_colors depends = mcl_core, mcl_compass, mcl_clock, doc, mcl_colors
optional_depends = sfinv, sfinv_buttons optional_depends =