Compare commits

..

1 Commits

Author SHA1 Message Date
103624bc28 Fix item positioning for large recipes 2020-04-16 18:39:39 +02:00
6 changed files with 239 additions and 338 deletions

View File

@ -19,4 +19,4 @@ For developers, `craftguide` also has a [modding API](https://github.com/minetes
Love this mod? Donations are appreciated: https://www.paypal.me/jpg84240 Love this mod? Donations are appreciated: https://www.paypal.me/jpg84240
![Preview2](https://i.imgur.com/mLlVyca.png) ![Preview2](https://content.minetest.net/uploads/wAGr5rE3fI.png)

561
init.lua
View File

@ -7,6 +7,7 @@ local searches = {}
local recipes_cache = {} local recipes_cache = {}
local usages_cache = {} local usages_cache = {}
local fuel_cache = {} local fuel_cache = {}
local toolrepair local toolrepair
local progressive_mode = core.settings:get_bool "craftguide_progressive_mode" local progressive_mode = core.settings:get_bool "craftguide_progressive_mode"
@ -15,7 +16,6 @@ local autocache = core.settings:get_bool "craftguide_autocache"
local http = core.request_http_api() local http = core.request_http_api()
local storage = core.get_mod_storage() local storage = core.get_mod_storage()
local singleplayer = core.is_singleplayer()
local reg_items = core.registered_items local reg_items = core.registered_items
local reg_tools = core.registered_tools local reg_tools = core.registered_tools
@ -64,11 +64,9 @@ local vec_add, vec_mul = vector.add, vector.multiply
local FORMSPEC_MINIMAL_VERSION = 3 local FORMSPEC_MINIMAL_VERSION = 3
local ROWS = 9 local ROWS = 9
local LINES = sfinv_only and 5 or 10 local LINES = sfinv_only and 5 or 9
local IPP = ROWS * LINES local IPP = ROWS * LINES
local WH_LIMIT = 5 local WH_LIMIT = 8
local MAX_FAVS = 6
local ITEM_BTN_SIZE = 1.1
local XOFFSET = sfinv_only and 3.83 or 11.2 local XOFFSET = sfinv_only and 3.83 or 11.2
local YOFFSET = sfinv_only and 4.9 or 1 local YOFFSET = sfinv_only and 4.9 or 1
@ -113,20 +111,6 @@ local function get_fs_version(name)
return info and info.formspec_version or 1 return info and info.formspec_version or 1
end end
local function outdated(name)
local fs = fmt([[
size[6.6,1.3]
image[0,0;1,1;%s]
label[1,0;%s]
button_exit[2.8,0.8;1,1;;OK]
]],
PNG.book,
"Your Minetest client is outdated.\n" ..
"Get the latest version on minetest.net to use the Crafting Guide.")
return show_formspec(name, "craftguide", fs)
end
local function mul_elem(elem, n) local function mul_elem(elem, n)
local fstr, elems = "", {} local fstr, elems = "", {}
@ -713,10 +697,6 @@ local function is_fav(data)
return fav, i return fav, i
end end
local function check_newline(def)
return def and def.description and find(def.description, "\n")
end
local function get_desc(name) local function get_desc(name)
if sub(name, 1, 1) == "_" then if sub(name, 1, 1) == "_" then
name = sub(name, 2) name = sub(name, 2)
@ -780,44 +760,26 @@ local function get_tooltip(name, info)
end end
if info.rarity then if info.rarity then
local chance = (1 / max(1, info.rarity)) * 100 local chance = (1 / info.rarity) * 100
tooltip = add(S("@1 of chance to drop", clr("#ff0", chance .. "%"))) tooltip = add(S("@1 of chance to drop", clr("#ff0", chance .. "%")))
end end
if info.tools then
local several = #info.tools > 1
local names = several and "\n" or ""
if several then
for i = 1, #info.tools do
names = fmt("%s\t\t- %s\n",
names, clr("#ff0", get_desc(info.tools[i])))
end
tooltip = add(S("Only drop if using one of these tools: @1",
sub(names, 1, -2)))
else
tooltip = add(S("Only drop if using this tool: @1",
clr("#ff0", get_desc(info.tools[1]))))
end
end
return fmt("tooltip[%s;%s]", name, ESC(tooltip)) return fmt("tooltip[%s;%s]", name, ESC(tooltip))
end end
local function get_output_fs(data, fs, rcp, shapeless, right, btn_size, _btn_size, spacing) local function get_output_fs(data, fs, L)
local custom_recipe = craft_types[rcp.type] local custom_recipe = craft_types[L.recipe.type]
if custom_recipe or shapeless or rcp.type == "cooking" then if custom_recipe or L.shapeless or L.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" L.shapeless and "shapeless" or "furnace"
if not custom_recipe then if not custom_recipe then
icon = fmt("craftguide_%s.png^[resize:16x16", icon) icon = fmt("craftguide_%s.png^[resize:16x16", icon)
end end
local pos_x = right + btn_size + 0.1 local pos_x = L.rightest + L.btn_size + 0.1
local pos_y = YOFFSET + (sfinv_only and 1.55 or -0.45) + spacing local pos_y = YOFFSET + (sfinv_only and 0.25 or -0.45) + L.spacing
if sub(icon, 1, 18) == "craftguide_furnace" then if sub(icon, 1, 18) == "craftguide_furnace" then
fs[#fs + 1] = fmt(FMT.animated_image, fs[#fs + 1] = fmt(FMT.animated_image,
@ -827,43 +789,36 @@ local function get_output_fs(data, fs, rcp, shapeless, right, btn_size, _btn_siz
end end
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" L.shapeless and S"Shapeless" or S"Cooking"
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y, 0.5, 0.5, ESC(tooltip)) fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
end end
local arrow_X = right + (_btn_size or ITEM_BTN_SIZE) local arrow_X = L.rightest + (L._btn_size or 1.1)
local output_X = arrow_X + 0.9 local output_X = arrow_X + 0.9
local Y = YOFFSET + (sfinv_only and 2 or 0) + spacing local Y = YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing
fs[#fs + 1] = fmt(FMT.image, arrow_X, Y + 0.2, 0.9, 0.7, PNG.arrow) fs[#fs + 1] = fmt(FMT.image, arrow_X, Y + 0.2, 0.9, 0.7, PNG.arrow)
if rcp.type == "fuel" then if L.recipe.type == "fuel" then
fs[#fs + 1] = fmt(FMT.animated_image, output_X, Y, fs[#fs + 1] = fmt(FMT.animated_image, output_X, Y, 1.1, 1.1, PNG.fire_anim, 8, 180)
ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180)
else else
local item = rcp.output local item = L.recipe.output
item = clean_name(item) item = clean_name(item)
local name = match(item, "%S*") local name = match(item, "%S*")
fs[#fs + 1] = fmt(FMT.image, output_X, Y, fs[#fs + 1] = fmt(FMT.image, output_X, Y, 1.1, 1.1, PNG.selected)
ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected)
local _name = sfinv_only and name or fmt("_%s", name) local _name = sfinv_only and name or fmt("_%s", name)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
output_X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, _name, "") output_X, Y, 1.1, 1.1, item, _name, "")
local def = reg_items[name]
local infos = { local infos = {
unknown = not def or nil, unknown = not reg_items[name] or nil,
burntime = fuel_cache[name], burntime = fuel_cache[name],
repair = repairable(name), repair = repairable(name),
rarity = rcp.rarity, rarity = L.rarity,
tools = rcp.tools,
newline = check_newline(def),
} }
if next(infos) then if next(infos) then
@ -872,11 +827,11 @@ local function get_output_fs(data, fs, rcp, shapeless, right, btn_size, _btn_siz
if infos.burntime then if infos.burntime then
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
output_X + 1, YOFFSET + (sfinv_only and 2 or 0.1) + spacing, output_X + 1, YOFFSET + (sfinv_only and 0.7 or 0.1) + L.spacing,
0.6, 0.4, PNG.arrow) 0.6, 0.4, PNG.arrow)
fs[#fs + 1] = fmt(FMT.animated_image, fs[#fs + 1] = fmt(FMT.animated_image,
output_X + 1.6, YOFFSET + (sfinv_only and 1.85 or 0) + spacing, output_X + 1.6, YOFFSET + (sfinv_only and 0.55 or 0) + L.spacing,
0.6, 0.6, PNG.fire_anim, 8, 180) 0.6, 0.6, PNG.fire_anim, 8, 180)
end end
end end
@ -884,7 +839,9 @@ end
local function get_grid_fs(data, fs, rcp, spacing) local function get_grid_fs(data, fs, rcp, spacing)
local width = rcp.width or 1 local width = rcp.width or 1
local right, btn_size, _btn_size = 0, ITEM_BTN_SIZE local replacements = rcp.replacements
local rarity = rcp.rarity
local rightest, btn_size, _btn_size = 0, 1.1
local cooktime, shapeless local cooktime, shapeless
if rcp.type == "cooking" then if rcp.type == "cooking" then
@ -921,15 +878,22 @@ local function get_grid_fs(data, fs, rcp, spacing)
local Y = ceil(i / width) + YOFFSET - min(2, rows) + spacing local Y = ceil(i / width) + YOFFSET - min(2, rows) + spacing
if large_recipe then if large_recipe then
btn_size = width > 3 and 3 / width or 3 / rows local xof = 1 - 4 / width
local yof = 1 - 4 / rows
local x_y = width > rows and xof or yof
btn_size = width > rows and
(3.5 + (xof * 2)) / width or (3.5 + (yof * 2)) / rows
_btn_size = btn_size _btn_size = btn_size
X = btn_size * ((i - 1) % width) + XOFFSET - 2.65 X = (btn_size * ((i - 1) % width) + XOFFSET -
Y = btn_size * floor((i - 1) / width) + spacing + (sfinv and 4 or 0) (sfinv_only and 2.83 or 0)) * (0.83 - (x_y / 5))
Y = (btn_size * floor((i - 1) / width) +
(sfinv_only and 5.81 or 3.92) + x_y) * (0.86 - (x_y / 5))
end end
if X > right then if X > rightest then
right = X rightest = X
end end
local groups local groups
@ -942,9 +906,9 @@ local function get_grid_fs(data, fs, rcp, spacing)
local label = groups and "\nG" or "" local label = groups and "\nG" or ""
local replace local replace
if rcp.replacements then if replacements then
for j = 1, #rcp.replacements do for j = 1, #replacements do
local replacement = rcp.replacements[j] local replacement = replacements[j]
if replacement[1] == name then if replacement[1] == name then
label = (label ~= "" and "\n" or "") .. label .. "\nR" label = (label ~= "" and "\n" or "") .. label .. "\nR"
replace = replacement[2] replace = replacement[2]
@ -952,7 +916,7 @@ local function get_grid_fs(data, fs, rcp, spacing)
end end
end end
Y = Y + (sfinv_only and 2 or 0) Y = Y + (sfinv_only and 0.7 or 0)
if not large_recipe then if not large_recipe then
fs[#fs + 1] = fmt(FMT.image, X, Y, btn_size, btn_size, PNG.selected) fs[#fs + 1] = fmt(FMT.image, X, Y, btn_size, btn_size, PNG.selected)
@ -961,15 +925,12 @@ local function get_grid_fs(data, fs, rcp, spacing)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y, btn_size, btn_size, item, item, label) X, Y, btn_size, btn_size, item, item, label)
local def = reg_items[name]
local infos = { local infos = {
unknown = not def or nil, unknown = not reg_items[name] or nil,
groups = groups, groups = groups,
burntime = fuel_cache[name], burntime = fuel_cache[name],
cooktime = cooktime, cooktime = cooktime,
replace = replace, replace = replace,
newline = check_newline(def),
} }
if next(infos) then if next(infos) then
@ -981,132 +942,70 @@ local function get_grid_fs(data, fs, rcp, spacing)
fs[#fs + 1] = "style_type[item_image_button;border=false]" fs[#fs + 1] = "style_type[item_image_button;border=false]"
end end
get_output_fs(data, fs, rcp, shapeless, right, btn_size, _btn_size, spacing) get_output_fs(data, fs, {
end recipe = rcp,
shapeless = shapeless,
local function get_rcp_lbl(data, fs, panel, spacing, rn, is_recipe) rightest = rightest,
local lbl btn_size = btn_size,
_btn_size = _btn_size,
if (not sfinv_only and is_recipe) or spacing = spacing,
(sfinv_only and not data.show_usages) then rarity = rarity,
lbl = ES("Recipe @1 of @2", data.rnum, rn) })
elseif not sfinv_only or (sfinv_only and data.show_usages) then
lbl = ES("Usage @1 of @2", data.unum, rn)
elseif sfinv_only then
lbl = data.show_usages and
ES("Usage @1 of @2", data.unum, rn) or
ES("Recipe @1 of @2", data.rnum, rn)
end
local _rn = tostring(rn)
local xu = tostring(data.unum) .. _rn
local xr = tostring(data.rnum) .. _rn
xu = max(-0.3, -((#xu - 3) * 0.05))
xr = max(-0.3, -((#xr - 3) * 0.05))
fs[#fs + 1] = fmt(FMT.label,
XOFFSET + (sfinv_only and 2.3 or 1.6) + (is_recipe and xr or xu),
YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl)
if rn > 1 then
local btn_suffix = is_recipe and "recipe" or "usage"
local prev_name = fmt("prev_%s", btn_suffix)
local next_name = fmt("next_%s", btn_suffix)
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing)
fs[#fs + 1] = fmt([[
style[%s;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[%s;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
]],
prev_name, PNG.prev, PNG.prev_hover, PNG.prev_hover,
next_name, PNG.next, PNG.next_hover, PNG.next_hover)
fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
x_arrow + (is_recipe and xr or xu), y_arrow,
PNG.prev, prev_name, "",
x_arrow + 1.8, y_arrow, PNG.next, next_name, "")
end
local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum]
get_grid_fs(data, fs, rcp, spacing)
end
local function get_title_fs(data, fs, spacing)
local desc = ESC(get_desc(data.query_item))
desc = #desc > 33 and fmt("%s...", sub(desc, 1, 30)) or desc
local t_desc = data.query_item
t_desc = #t_desc > 40 and fmt("%s...", sub(t_desc, 1, 37)) or t_desc
fs[#fs + 1] = fmt("hypertext[9.05,%f;5.85,1.2;item_title;%s]",
spacing - 0.1,
fmt("<item name=%s float=right width=64 height=64 rotate=yes>" ..
"<big><b>%s</b></big>\n<style color=#7bf font=mono>%s</style>",
data.query_item, desc, t_desc))
local fav = is_fav(data)
local nfavs = #data.favs
if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then
local fav_marked = fmt("craftguide_fav%s.png", fav and "_off" or "")
fs[#fs + 1] = fmt(
"style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
fmt("craftguide_fav%s.png", fav and "" or "_off"), fav_marked, fav_marked)
fs[#fs + 1] = fmt(FMT.image_button, 8.25, spacing + 0.15, 0.5, 0.45, "", "fav", "")
fs[#fs + 1] = fmt("tooltip[fav;%s]",
fav and ES"Unmark this item" or ES"Mark this item")
else
fs[#fs + 1] = fmt(
"style[fav_no;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
"craftguide_fav_off.png", PNG.nothing, PNG.nothing)
fs[#fs + 1] = fmt(FMT.image_button,
8.25, spacing + 0.15, 0.5, 0.45, "", "fav_no", "")
fs[#fs + 1] = fmt("tooltip[fav_no;%s]",
ES"Cannot mark this item. Limit of bookmarks reached.")
end
end end
local function get_panels(data, fs) local function get_panels(data, fs)
local title = {name = "title", height = 1.2} local start_y = sfinv_only and 0.33 or 0
local favs = {name = "favs", height = 1.91}
local recipes = {name = "recipes", rcp = data.recipes, height = 3.5}
local usages = {name = "usages", rcp = data.usages, height = 3.5}
local panels = {title, recipes, usages, favs}
if sfinv_only then local panels = {
panels = {data.show_usages and usages or recipes} {dat = data.usages or {}, height = 3.5},
{dat = data.recipes or {}, height = 3.5},
}
if not sfinv_only then
panels.favs = {height = 2.19}
else
panels = data.show_usages and {{dat = data.usages}} or {{dat = data.recipes}}
end end
for idx = 1, #panels do for k, v in pairs(panels) do
local panel, spacing = panels[idx], 0 start_y = start_y + 1
local spacing = (start_y - 1) * 3.6
if idx > 1 then if not sfinv_only then
for _idx = idx - 1, 1, -1 do fs[#fs + 1] = fmt("background9[8.1,%f;6.6,%f;%s;false;%d]",
spacing = spacing + panels[_idx].height + 0.1 -0.2 + spacing, v.height, PNG.bg_full, 10)
if k == 2 then
local fav = is_fav(data)
local nfavs = #data.favs
fs[#fs + 1] = fmt(
"style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
fmt("craftguide_fav%s.png", fav and "" or "_off"),
fmt("craftguide_fav%s.png", fav and "_off" or ""),
fmt("craftguide_fav%s.png", fav and "_off" or ""))
if nfavs < 6 or (nfavs >= 6 and fav) then
fs[#fs + 1] = fmt(FMT.image_button,
14, spacing, 0.5, 0.45, "", "fav", "")
end
fs[#fs + 1] = fmt("tooltip[fav;%s]",
fav and ES"Unmark this item" or ES"Mark this item")
end end
end end
local rn = panel.rcp and #panel.rcp local rn = v.dat and #v.dat or -1
local is_recipe = sfinv_only and not data.show_usages or panel.name == "recipes" local _rn = tostring(rn)
local recipe_or_usage = panel.name == "recipes" or panel.name == "usages" local xu = tostring(data.unum) .. _rn
local xr = tostring(data.rnum) .. _rn
xu = max(-0.3, -((#xu - 3) * 0.05))
xr = max(-0.3, -((#xr - 3) * 0.05))
if rn then local is_recipe = sfinv_only and not data.show_usages or k == 2
get_rcp_lbl(data, fs, panel, spacing, rn, is_recipe) local lbl = ""
end
if sfinv_only then return end if not sfinv_only and rn == 0 then
fs[#fs + 1] = fmt("background9[8.1,%f;6.6,%f;%s;false;%d]",
-0.2 + spacing, panel.height, PNG.bg_full, 10)
if recipe_or_usage and not rn then
local X = XOFFSET - 0.7 local X = XOFFSET - 0.7
local Y = YOFFSET - 0.4 + spacing local Y = YOFFSET - 0.4 + spacing
@ -1115,24 +1014,55 @@ local function get_panels(data, fs)
fs[#fs + 1] = fmt(FMT.tooltip, fs[#fs + 1] = fmt(FMT.tooltip,
X, Y, 2, 2, is_recipe and ES"No recipes" or ES"No usages") X, Y, 2, 2, is_recipe and ES"No recipes" or ES"No usages")
elseif panel.name == "title" then elseif (not sfinv_only and is_recipe) or
get_title_fs(data, fs, spacing) (sfinv_only and not data.show_usages) then
lbl = ES("Recipe @1 of @2", data.rnum, rn)
elseif panel.name == "favs" then elseif not sfinv_only or (sfinv_only and data.show_usages) then
fs[#fs + 1] = fmt(FMT.label, 8.3, spacing - 0.15, ES"Bookmarks") lbl = ES("Usage @1 of @2", data.unum, rn)
elseif sfinv_only then
lbl = data.show_usages and
ES("Usage @1 of @2", data.unum, rn) or
ES("Recipe @1 of @2", data.rnum, rn)
end
fs[#fs + 1] = fmt(FMT.label,
XOFFSET + (sfinv_only and 2.3 or 1.6) + (is_recipe and xr or xu),
YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl)
if rn > 1 then
local btn_suffix = is_recipe and "recipe" or "usage"
local prev_name = fmt("prev_%s", btn_suffix)
local next_name = fmt("next_%s", btn_suffix)
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing)
fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
x_arrow + (is_recipe and xr or xu), y_arrow,
PNG.prev, prev_name, "",
x_arrow + 1.8, y_arrow, PNG.next, next_name, "")
end
local rcp = v.dat and (is_recipe and v.dat[data.rnum] or v.dat[data.unum])
if rcp then
get_grid_fs(data, fs, rcp, spacing)
end
if k == "favs" and not sfinv_only then
fs[#fs + 1] = fmt(FMT.label, 8.3, spacing - 0.1, ES"Bookmarks")
for i = 1, #data.favs do for i = 1, #data.favs do
local item = data.favs[i] local item = data.favs[i]
local X = 7.85 + (i - 0.5) local X = 7.85 + (i - 0.5)
local Y = spacing + 0.4 local Y = spacing + 0.45
if data.query_item == item then if data.query_item == item then
fs[#fs + 1] = fmt(FMT.image, X, Y, fs[#fs + 1] = fmt(FMT.image, X, Y, 1.1, 1.1, PNG.selected)
ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected)
end end
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, "") X, Y, 1.1, 1.1, item, item, "")
end end
end end
end end
@ -1155,9 +1085,9 @@ local function make_fs(data)
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
style[filter;border=false] style[filter;border=false]
field[0.4,0.2;2.6,1;filter;;%s] field[0.4,0.2;2.5,1;filter;;%s]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
box[0,0;2.5,0.6;#bababa25] box[0,0;2.4,0.6;#bababa25]
]], ]],
ESC(data.filter)) ESC(data.filter))
@ -1168,7 +1098,6 @@ local function make_fs(data)
style[clear;fgimg=%s;fgimg_hovered=%s] style[clear;fgimg=%s;fgimg_hovered=%s]
style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s] style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[next_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s] style[next_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[pagenum;border=false]
]], ]],
PNG.selected, PNG.selected, PNG.selected, PNG.selected,
PNG.search, PNG.search_hover, PNG.search, PNG.search_hover,
@ -1176,27 +1105,17 @@ local function make_fs(data)
PNG.prev, PNG.prev_hover, PNG.prev_hover, PNG.prev, PNG.prev_hover, PNG.prev_hover,
PNG.next, PNG.next_hover, PNG.next_hover) PNG.next, PNG.next_hover, PNG.next_hover)
fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 2), fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 4),
2.6, -0.06, 0.85, 0.85, "", "search", "", sfinv_only and 2.6 or 2.54, -0.06, 0.85, 0.85, "", "search", "",
3.3, -0.06, 0.85, 0.85, "", "clear", "") sfinv_only and 3.3 or 3.25, -0.06, 0.85, 0.85, "", "clear", "",
sfinv_only and 5.45 or (9 * 6.83) / 11, -0.06, 0.85, 0.85, "", "prev_page", "",
if sfinv_only then sfinv_only and 7.2 or (9 * 8.75) / 11, -0.06, 0.85, 0.85, "", "next_page", "")
fs[#fs + 1] = "container[0.2,0]"
end
fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 2),
5.35, -0.06, 0.85, 0.85, "", "prev_page", "",
7.1, -0.06, 0.85, 0.85, "", "next_page", "")
data.pagemax = max(1, ceil(#data.items / IPP)) data.pagemax = max(1, ceil(#data.items / IPP))
fs[#fs + 1] = fmt(FMT.button, fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
5.97, -0.06, 1.36, 0.85, "pagenum", sfinv_only and 6.35 or (9 * 7.85) / 11,
fmt("%s / %u", clr("#ff0", data.pagenum), data.pagemax)) 0.06, clr("#ff0", data.pagenum), data.pagemax)
if sfinv_only then
fs[#fs + 1] = "container_end[]"
end
if #data.items == 0 then if #data.items == 0 then
local no_item = ES"No item to show" local no_item = ES"No item to show"
@ -1247,12 +1166,12 @@ end
craftguide.register_craft_type("digging", { craftguide.register_craft_type("digging", {
description = ES"Digging", description = ES"Digging",
icon = "craftguide_steelpick.png", icon = "default_tool_steelpick.png",
}) })
craftguide.register_craft_type("digging_chance", { craftguide.register_craft_type("digging_chance", {
description = ES"Digging (by chance)", description = ES"Digging Chance",
icon = "craftguide_mesepick.png", icon = "default_tool_mesepick.png",
}) })
local function search(data) local function search(data)
@ -1438,16 +1357,12 @@ local function handle_drops_table(name, drop)
if not dstack:is_empty() and dname ~= name then if not dstack:is_empty() and dname ~= name then
local dcount = dstack:get_count() local dcount = dstack:get_count()
if #di.items == 1 and max_start and if #di.items == 1 and di.rarity == 1 and max_start then
(not di.rarity or di.rarity <= 1) then
if not drop_sure[dname] then if not drop_sure[dname] then
drop_sure[dname] = {} drop_sure[dname] = 0
end end
drop_sure[dname] = { drop_sure[dname] = drop_sure[dname] + dcount
output = (drop_sure[dname].output or 0) + dcount,
tools = di.tools,
}
if max_items_left then if max_items_left then
max_items_left = max_items_left - 1 max_items_left = max_items_left - 1
@ -1462,46 +1377,46 @@ local function handle_drops_table(name, drop)
drop_maybe[dname] = {} drop_maybe[dname] = {}
end end
if not drop_maybe[dname].output then
drop_maybe[dname].output = 0
end
drop_maybe[dname] = { drop_maybe[dname] = {
output = (drop_maybe[dname].output or 0) + dcount, output = drop_maybe[dname].output + dcount,
rarity = di.rarity, rarity = di.rarity,
tools = di.tools,
} }
end end
end end
end end
end end
for item, data in pairs(drop_sure) do for item, count in pairs(drop_sure) do
craftguide.register_craft{ craftguide.register_craft{
type = "digging", type = "digging",
items = {name}, items = {name},
output = fmt("%s %u", item, data.output), output = fmt("%s %u", item, count),
tools = data.tools,
} }
end end
for item, data in pairs(drop_maybe) do for item, data in pairs(drop_maybe) do
craftguide.register_craft{ craftguide.register_craft{
type = "digging_chance", type = "digging_chance",
items = {name}, items = {name},
output = fmt("%s %u", item, data.output), output = fmt("%s %u", item, data.output),
rarity = data.rarity, rarity = data.rarity,
tools = data.tools,
} }
end end
end end
local function register_drops(name, drop) local function register_drops(name, drop)
if true_str(drop) then local dstack = ItemStack(drop)
local dstack = ItemStack(drop)
if not dstack:is_empty() and dstack:get_name() ~= name then if not dstack:is_empty() and dstack:get_name() ~= name then
craftguide.register_craft{ craftguide.register_craft{
type = "digging", type = "digging",
items = {name}, items = {name},
output = drop, output = drop,
} }
end
elseif is_table(drop) then elseif is_table(drop) then
handle_drops_table(name, drop) handle_drops_table(name, drop)
end end
@ -1561,12 +1476,10 @@ local function get_init_items()
recipes_cache = dslz(storage:get "recipes_cache") recipes_cache = dslz(storage:get "recipes_cache")
else else
print "[craftguide] Caching data (this may take a while)" print "[craftguide] Caching data (this may take a while)"
local _select, _preselect = {}, {} local hash = {}
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
if name ~= "" and show_item(def) then if show_item(def) then
register_drops(name, def.drop)
if not fuel_cache[name] then if not fuel_cache[name] then
cache_fuel(name) cache_fuel(name)
end end
@ -1576,19 +1489,16 @@ local function get_init_items()
end end
cache_usages(name) cache_usages(name)
register_drops(name, def.drop)
_preselect[name] = true if name ~= "" and recipes_cache[name] or usages_cache[name] then
init_items[#init_items + 1] = name
hash[name] = true
end
end end
end end
for name in pairs(_preselect) do handle_aliases(hash)
if recipes_cache[name] or usages_cache[name] then
init_items[#init_items + 1] = name
_select[name] = true
end
end
handle_aliases(_select)
sort(init_items) sort(init_items)
storage:set_string("init_items", slz(init_items)) storage:set_string("init_items", slz(init_items))
@ -1639,10 +1549,6 @@ on_mods_loaded(get_init_items)
on_joinplayer(function(player) on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
init_data(name) init_data(name)
if pdata[name].fs_version < FORMSPEC_MINIMAL_VERSION then
outdated(name)
end
end) end)
local function fields(player, _f) local function fields(player, _f)
@ -1673,7 +1579,7 @@ local function fields(player, _f)
data.pagenum = 1 data.pagenum = 1
search(data) search(data)
elseif _f.prev_page or _f.next_page or _f.pagenum then elseif _f.prev_page or _f.next_page then
if data.pagemax == 1 then return end if data.pagemax == 1 then return end
data.pagenum = data.pagenum - (_f.prev_page and 1 or -1) data.pagenum = data.pagenum - (_f.prev_page and 1 or -1)
@ -1687,7 +1593,7 @@ local function fields(player, _f)
local fav, i = is_fav(data) local fav, i = is_fav(data)
local total = #data.favs local total = #data.favs
if total < MAX_FAVS and not fav then if total < 6 and not fav then
data.favs[total + 1] = data.query_item data.favs[total + 1] = data.query_item
elseif fav then elseif fav then
remove(data.favs, i) remove(data.favs, i)
@ -1747,8 +1653,9 @@ if sfinv_only then
get = function(self, player, context) get = function(self, player, context)
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
local formspec = make_fs(data)
return sfinv.make_formspec(player, context, make_fs(data)) return sfinv.make_formspec(player, context, formspec)
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
@ -1777,7 +1684,17 @@ else
local data = pdata[name] local data = pdata[name]
if data.fs_version < FORMSPEC_MINIMAL_VERSION then if data.fs_version < FORMSPEC_MINIMAL_VERSION then
return outdated(name) local fs = fmt([[
size[6.6,1.3]
image[0,0;1,1;%s]
label[1,0;%s]
button_exit[2.8,0.8;1,1;;OK]
]],
PNG.nothing,
"Your Minetest client is outdated.\n" ..
"Get the latest version on minetest.net to use the Crafting Guide.")
return show_formspec(name, "craftguide", fs)
end end
if next(recipe_filters) then if next(recipe_filters) then
@ -1808,12 +1725,7 @@ else
paramtype = "light", paramtype = "light",
paramtype2 = "wallmounted", paramtype2 = "wallmounted",
sunlight_propagates = true, sunlight_propagates = true,
groups = { groups = {choppy = 1, attached_node = 1, oddly_breakable_by_hand = 1, flammable = 3},
choppy = 1,
attached_node = 1,
oddly_breakable_by_hand = 1,
flammable = 3,
},
node_box = { node_box = {
type = "wallmounted", type = "wallmounted",
wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5}, wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
@ -1858,7 +1770,7 @@ else
if rawget(_G, "sfinv_buttons") then if rawget(_G, "sfinv_buttons") then
sfinv_buttons.register_button("craftguide", { sfinv_buttons.register_button("craftguide", {
title = S"Crafting Guide", title = S"Crafting Guide",
tooltip = S"Shows a list of available crafting recipes", tooltip = S"Shows a list of available crafting recipes, cooking recipes and fuels",
image = PNG.book, image = PNG.book,
action = function(player) action = function(player)
on_use(player) on_use(player)
@ -1868,6 +1780,7 @@ else
end end
if progressive_mode then if progressive_mode then
local PLAYERS = {}
local POLL_FREQ = 0.25 local POLL_FREQ = 0.25
local HUD_TIMER_MAX = 1.5 local HUD_TIMER_MAX = 1.5
@ -1954,34 +1867,6 @@ if progressive_mode then
return inv_items return inv_items
end end
local function init_hud(player, data)
data.hud = {
bg = player:hud_add{
hud_elem_type = "image",
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
},
book = player:hud_add{
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1},
scale = {x = 4, y = 4},
text = PNG.book,
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
},
}
end
local function show_hud_success(player, data) local function show_hud_success(player, data)
-- It'd better to have an engine function `hud_move` to only need -- It'd better to have an engine function `hud_move` to only need
-- 2 calls for the notification's back and forth. -- 2 calls for the notification's back and forth.
@ -2029,9 +1914,8 @@ if progressive_mode then
-- Workaround. Need an engine call to detect when the contents of -- Workaround. Need an engine call to detect when the contents of
-- the player inventory changed, instead. -- the player inventory changed, instead.
local function poll_new_items() local function poll_new_items()
local players = get_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 = pdata[name] local data = pdata[name]
@ -2064,13 +1948,12 @@ if progressive_mode then
poll_new_items() poll_new_items()
globalstep(function() globalstep(function()
local players = get_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 = pdata[name] local data = pdata[name]
if data.show_hud ~= nil and singleplayer then if data.show_hud ~= nil then
show_hud_success(player, data) show_hud_success(player, data)
end end
end end
@ -2079,6 +1962,8 @@ if progressive_mode then
craftguide.add_recipe_filter("Default progressive filter", progressive_filter) craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
on_joinplayer(function(player) on_joinplayer(function(player)
PLAYERS = get_players()
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
@ -2086,9 +1971,31 @@ if progressive_mode then
data.inv_items = dslz(meta:get_string "inv_items") or {} data.inv_items = dslz(meta:get_string "inv_items") or {}
data.known_recipes = dslz(meta:get_string "known_recipes") or 0 data.known_recipes = dslz(meta:get_string "known_recipes") or 0
if singleplayer then data.hud = {
init_hud(player, data) bg = player:hud_add{
end hud_elem_type = "image",
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
},
book = player:hud_add{
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1},
scale = {x = 4, y = 4},
text = PNG.book,
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
},
}
end) end)
local to_save = {"inv_items", "known_recipes"} local to_save = {"inv_items", "known_recipes"}
@ -2104,12 +2011,14 @@ if progressive_mode then
end end
end end
on_leaveplayer(save_meta) on_leaveplayer(function(player)
PLAYERS = get_players()
save_meta(player)
end)
on_shutdown(function() on_shutdown(function()
local players = get_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)
end end
end) end)
@ -2137,11 +2046,13 @@ function craftguide.show(name, item, show_usages)
if not recipes and not usages then if not recipes and not usages then
if not recipes_cache[item] and not usages_cache[item] then if not recipes_cache[item] and not usages_cache[item] then
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%s: %s",
S"No recipe or usage for this item", get_desc(item))) S"No recipe or usage for this item",
get_desc(item)))
end end
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%s: %s",
S"You don't know a recipe or usage for this item", get_desc(item))) S"You don't know a recipe or usage for this item",
get_desc(item)))
end end
data.query_item = item data.query_item = item

View File

@ -56,10 +56,5 @@ No node pointed=Aucun bloc visé
You don't know a recipe or usage for this item=Tu ne connais aucune recette pour ce bloc You don't know a recipe or usage for this item=Tu ne connais aucune recette pour ce bloc
No recipe or usage for this item=Aucune recette pour ce bloc No recipe or usage for this item=Aucune recette pour ce bloc
Digging=Destruction Digging=Destruction
Digging (by chance)=Destruction chanceuse Digging Chance=Destruction chanceuse
@1 of chance to drop=@1 de chance de tomber @1 of chance to drop=@1 de chance de tomber
Mark this item=Mettre en favori.
Unmark this item=Enlever des favoris.
Cannot mark this item. Limit of bookmarks reached.=Impossible de mettre cet item en favori. Limite des favoris atteinte.
Only drop if using one of these tools: @1=Tombe seulement si détruit avec un de ces outils: @1
Only drop if using this tool: @1=Tombe seulement si détruit avec cet outil: @1

View File

@ -56,10 +56,5 @@ No node pointed=
You don't know a recipe or usage for this item= You don't know a recipe or usage for this item=
No recipe or usage for this item= No recipe or usage for this item=
Digging= Digging=
Digging (by chance)= Digging Chance=
@1 of chance to drop= @1 of chance to drop=
Mark this item=
Unmark this item=
Cannot mark this item. Limit of bookmarks reached.=
Only drop if using one of these tools: @1=
Only drop if using this tool: @1=

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB