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

View File

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